2023-04-10 10:51:36 +08:00
|
|
|
package chaoran.business.adapter;
|
|
|
|
|
|
|
|
|
|
|
2024-01-11 17:27:22 +08:00
|
|
|
import android.app.ActivityManager;
|
2023-04-10 10:51:36 +08:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
|
2024-01-11 17:27:22 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2023-04-10 10:51:36 +08:00
|
|
|
import chaoran.business.activity.ResultListener;
|
|
|
|
|
import chaoran.business.service.ScanServiceEDA50P;
|
|
|
|
|
import chaoran.business.service.ScanServiceZEBRA;
|
|
|
|
|
import chaoran.business.strategy.Strategy;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class HoneywellAdapter implements Adapter {
|
|
|
|
|
private Context context;
|
|
|
|
|
private Strategy strategy;
|
|
|
|
|
private ResultListener resultListener;
|
|
|
|
|
private Intent intent = null;
|
|
|
|
|
|
|
|
|
|
public HoneywellAdapter(Context context, ResultListener resultListener) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.resultListener = resultListener;
|
|
|
|
|
strategy = new Receiver();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void start() {
|
|
|
|
|
strategy.executeStrategy(resultListener);
|
|
|
|
|
openContinueScan();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 17:27:22 +08:00
|
|
|
@Override
|
|
|
|
|
public void stop2() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 10:51:36 +08:00
|
|
|
@Override
|
|
|
|
|
public void stop() {
|
2023-04-21 15:03:30 +08:00
|
|
|
new ScanServiceEDA50P().onDestroy();
|
2023-04-10 10:51:36 +08:00
|
|
|
strategy.exclusiveStrategy();
|
2024-01-11 17:27:22 +08:00
|
|
|
this.stopContinueScan();
|
2023-04-10 10:51:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Receiver extends BroadcastReceiver implements Strategy {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
resultListener.result(intent.getStringExtra("data"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void executeStrategy(ResultListener resultListener) {
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
|
filter.addAction("com.honeywell.scan.broadcast");
|
|
|
|
|
context.registerReceiver(this, filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exclusiveStrategy() {
|
|
|
|
|
context.unregisterReceiver(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-10 15:37:18 +08:00
|
|
|
|
2024-01-11 17:27:22 +08:00
|
|
|
public void stopContinueScan() {
|
|
|
|
|
Class clazz = null;
|
|
|
|
|
if("eda50p".equals(Build.MODEL.toLowerCase())){ // 扫描正常
|
|
|
|
|
intent = new Intent(context, ScanServiceEDA50P.class);
|
|
|
|
|
clazz = ScanServiceEDA50P.class;
|
|
|
|
|
}else if("eda51".equals(Build.MODEL.toLowerCase())){ // 扫描正常
|
|
|
|
|
intent = new Intent(context, ScanServiceEDA50P.class);
|
|
|
|
|
clazz = ScanServiceEDA50P.class;
|
|
|
|
|
}
|
|
|
|
|
else if("tc26".equals(Build.MODEL.toLowerCase())){
|
|
|
|
|
intent = new Intent(context, ScanServiceZEBRA.class);
|
|
|
|
|
clazz = ScanServiceZEBRA.class;
|
|
|
|
|
}
|
|
|
|
|
if (clazz != null && isServiceRunning(clazz)) {
|
|
|
|
|
context.stopService(intent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isServiceRunning(Class<?> serviceClass) {
|
|
|
|
|
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
|
|
List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(Integer.MAX_VALUE);
|
|
|
|
|
|
|
|
|
|
for (ActivityManager.RunningServiceInfo service : runningServices) {
|
|
|
|
|
if (serviceClass.getName().equals(service.service.getClassName())) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 10:51:36 +08:00
|
|
|
public void openContinueScan(){
|
2023-04-10 16:17:49 +08:00
|
|
|
|
2026-03-18 11:07:58 +08:00
|
|
|
if("eda50p".equals(Build.MODEL.toLowerCase()) || "eda51".equals(Build.MODEL.toLowerCase()) || "eda52".equals(Build.MODEL.toLowerCase())){ // 扫描正常
|
2023-04-10 10:51:36 +08:00
|
|
|
intent = new Intent(context, ScanServiceEDA50P.class);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
context.getApplicationContext().startForegroundService(intent);
|
|
|
|
|
}else {
|
|
|
|
|
context.getApplicationContext().startService(intent);
|
|
|
|
|
}
|
2023-04-10 15:37:18 +08:00
|
|
|
}
|
|
|
|
|
else if("tc26".equals(Build.MODEL.toLowerCase())){
|
2023-04-10 10:51:36 +08:00
|
|
|
intent = new Intent(context, ScanServiceZEBRA.class);
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
context.getApplicationContext().startForegroundService(intent);
|
|
|
|
|
}else {
|
|
|
|
|
context.getApplicationContext().startService(intent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|