247 lines
9.5 KiB
Java
247 lines
9.5 KiB
Java
package cn.com.magnity.magnitycx.sdk;
|
|
|
|
import android.app.PendingIntent;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.hardware.usb.UsbDevice;
|
|
import android.hardware.usb.UsbManager;
|
|
import android.telephony.TelephonyManager;
|
|
import cn.com.magnity.magnitycx.FragmentMainCenter;
|
|
import cn.com.magnity.magnitycx.MagApplication;
|
|
import cn.com.magnity.magnitycx.log.Logging;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class UsbConnection extends Observable<DelegateDeviceConnected> {
|
|
private static final String ACTION_USB_PERMISSION = "cn.com.magnity.magnitycx.USB_PERMISSION";
|
|
private static UsbConnection instance_ = new UsbConnection();
|
|
private FragmentMainCenter fragmentMainCenter_;
|
|
private PhoneReceiver phoneReceiver_;
|
|
private UsbCommunication usbComm_;
|
|
private UsbConnectionReceiver usbConnectionReceiver_;
|
|
private UsbPermissionReceiver usbPermissionReceiver_;
|
|
private Context context_ = MagApplication.getInstance();
|
|
private UsbManager usbManager_ = (UsbManager) this.context_.getSystemService("usb");
|
|
|
|
public static UsbConnection getInstance() {
|
|
return instance_;
|
|
}
|
|
|
|
private UsbConnection() {
|
|
}
|
|
|
|
public void setCallbackReceiver(FragmentMainCenter fragment) {
|
|
this.fragmentMainCenter_ = fragment;
|
|
if (this.usbComm_ != null) {
|
|
this.usbComm_.setCallbackReceiver(fragment);
|
|
}
|
|
}
|
|
|
|
public boolean init() {
|
|
if (this.phoneReceiver_ == null) {
|
|
registerPhoneReceiver();
|
|
}
|
|
if (this.usbConnectionReceiver_ == null) {
|
|
registerConnectionReceiver();
|
|
}
|
|
UsbDevice device = findDevice(33596, 1);
|
|
if (device != null) {
|
|
requestDevicePermission(device);
|
|
}
|
|
return device != null;
|
|
}
|
|
|
|
public void deinit(boolean notifyUi) {
|
|
if (this.usbConnectionReceiver_ != null) {
|
|
this.context_.unregisterReceiver(this.usbConnectionReceiver_);
|
|
this.usbConnectionReceiver_ = null;
|
|
}
|
|
if (this.usbPermissionReceiver_ != null) {
|
|
this.context_.unregisterReceiver(this.usbPermissionReceiver_);
|
|
this.usbPermissionReceiver_ = null;
|
|
}
|
|
disconnect(notifyUi);
|
|
}
|
|
|
|
public boolean isConnected() {
|
|
return this.usbComm_ != null;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public boolean connect(UsbDevice device) {
|
|
if (this.usbComm_ != null) {
|
|
return true;
|
|
}
|
|
Logging.info("Try to connect to device");
|
|
try {
|
|
this.usbComm_ = new UsbCommunication(this, this.usbManager_, device, this.fragmentMainCenter_);
|
|
int ret = this.usbComm_.connect();
|
|
if (ret == -1) {
|
|
Logging.error("Fail to connect to device, do clean work now");
|
|
this.usbComm_.disconnect();
|
|
this.usbComm_ = null;
|
|
return false;
|
|
}
|
|
if (ret == 0) {
|
|
return false;
|
|
}
|
|
notifyObervers(1);
|
|
return true;
|
|
} catch (Exception ex) {
|
|
Logging.warn("Exception when connecting to device (" + ex.getMessage() + ")");
|
|
if (this.usbComm_ != null) {
|
|
this.usbComm_.disconnect();
|
|
}
|
|
this.usbComm_ = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void notifyObervers(int status) {
|
|
synchronized (this.observers_) {
|
|
if (status == 1) {
|
|
for (int i = this.observers_.size() - 1; i != -1; i--) {
|
|
((DelegateDeviceConnected) this.observers_.get(i)).onDeviceConnected(this.usbComm_);
|
|
}
|
|
} else if (status == 2) {
|
|
for (int i2 = this.observers_.size() - 1; i2 != -1; i2--) {
|
|
}
|
|
} else if (status == 0) {
|
|
for (int i3 = this.observers_.size() - 1; i3 != -1; i3--) {
|
|
((DelegateDeviceConnected) this.observers_.get(i3)).onDeviceDisconnected();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void disconnect(boolean notifyUi) {
|
|
Logging.info("Try to disconnect to device");
|
|
if (this.usbComm_ != null) {
|
|
this.usbComm_.disconnect();
|
|
this.usbComm_ = null;
|
|
}
|
|
if (notifyUi) {
|
|
notifyObervers(0);
|
|
}
|
|
}
|
|
|
|
private UsbDevice findDevice(int vendorId, int productId) {
|
|
for (UsbDevice device : this.usbManager_.getDeviceList().values()) {
|
|
if (device.getVendorId() == vendorId && device.getProductId() == productId) {
|
|
return device;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void registerConnectionReceiver() {
|
|
this.usbConnectionReceiver_ = new UsbConnectionReceiver();
|
|
IntentFilter localFilter = new IntentFilter();
|
|
localFilter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
|
|
localFilter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
|
|
this.context_.registerReceiver(this.usbConnectionReceiver_, localFilter);
|
|
}
|
|
|
|
private void registerPhoneReceiver() {
|
|
this.phoneReceiver_ = new PhoneReceiver();
|
|
IntentFilter localFilter = new IntentFilter();
|
|
localFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
|
|
localFilter.addAction("android.intent.action.PHONE_STATE");
|
|
this.context_.registerReceiver(this.phoneReceiver_, localFilter);
|
|
}
|
|
|
|
private void requestDevicePermission(UsbDevice device) {
|
|
if (this.usbManager_.hasPermission(device)) {
|
|
Logging.trace("Already has permission");
|
|
connect(device);
|
|
return;
|
|
}
|
|
if (this.usbPermissionReceiver_ == null) {
|
|
this.usbPermissionReceiver_ = new UsbPermissionReceiver();
|
|
IntentFilter localFilter = new IntentFilter(ACTION_USB_PERMISSION);
|
|
this.context_.registerReceiver(this.usbPermissionReceiver_, localFilter);
|
|
}
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context_, 0, new Intent(ACTION_USB_PERMISSION), 0);
|
|
this.usbManager_.requestPermission(device, pendingIntent);
|
|
Logging.trace("Request permission");
|
|
}
|
|
|
|
private final class UsbConnectionReceiver extends BroadcastReceiver {
|
|
private UsbConnectionReceiver() {
|
|
}
|
|
|
|
@Override // android.content.BroadcastReceiver
|
|
public void onReceive(Context context, Intent intent) {
|
|
switch (intent.getAction()) {
|
|
case "android.hardware.usb.action.USB_DEVICE_ATTACHED":
|
|
Logging.trace("Broadcast for usb attached received");
|
|
UsbConnection.this.disconnect(false);
|
|
UsbConnection.this.init();
|
|
break;
|
|
case "android.hardware.usb.action.USB_DEVICE_DETACHED":
|
|
Logging.trace("Broadcast for usb detached received");
|
|
UsbConnection.this.disconnect(true);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private final class UsbPermissionReceiver extends BroadcastReceiver {
|
|
private UsbPermissionReceiver() {
|
|
}
|
|
|
|
@Override // android.content.BroadcastReceiver
|
|
public void onReceive(Context context, Intent intent) {
|
|
switch (intent.getAction()) {
|
|
case "cn.com.magnity.magnitycx.USB_PERMISSION":
|
|
Logging.trace("Broadcast for usb permission received");
|
|
if (!intent.getBooleanExtra("permission", false)) {
|
|
Logging.warn("Permission rejected by user");
|
|
break;
|
|
} else {
|
|
UsbDevice device = (UsbDevice) intent.getParcelableExtra("device");
|
|
if (device != null) {
|
|
UsbConnection.this.connect(device);
|
|
UsbConnection.this.context_.unregisterReceiver(UsbConnection.this.usbPermissionReceiver_);
|
|
UsbConnection.this.usbPermissionReceiver_ = null;
|
|
break;
|
|
} else {
|
|
Logging.warn("Failed to get device");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private class PhoneReceiver extends BroadcastReceiver {
|
|
private PhoneReceiver() {
|
|
}
|
|
|
|
@Override // android.content.BroadcastReceiver
|
|
public void onReceive(Context context, Intent intent) {
|
|
switch (intent.getAction()) {
|
|
case "android.intent.action.PHONE_STATE":
|
|
if (MagApplication.magParameter.basePara1.swVersion > 100) {
|
|
String state = intent.getStringExtra("state");
|
|
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
|
|
Logging.trace("ring");
|
|
UsbConnection.this.deinit(true);
|
|
break;
|
|
} else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
|
|
Logging.trace("hang up");
|
|
UsbConnection.this.init();
|
|
break;
|
|
} else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
|
|
Logging.trace("hang on");
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|