package com.fwupgrade.saymanss.deviceplug;
|
|
import android.app.PendingIntent;
|
import android.content.BroadcastReceiver;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.IntentFilter;
|
import android.hardware.usb.UsbAccessory;
|
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbManager;
|
import android.os.Build;
|
import android.os.SystemClock;
|
import android.support.annotation.RequiresApi;
|
import android.util.Log;
|
|
|
import com.spca.usb.CUSBListener;
|
import com.spca.usb.SpcaJNI;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.ReentrantLock;
|
|
import static android.content.Context.USB_SERVICE;
|
|
/**
|
* Created by Wangj on 2016/10/25.
|
*/
|
|
public class UstorageDeviceInstance {
|
|
private static UstorageDeviceInstance instance;
|
private static Lock mLock= new ReentrantLock();
|
|
public static final String ACTION_USB_PERMISSION = "wfd.action.USB_PERMISSION";
|
|
private UsbManager mUsbManager;
|
private PendingIntent mPermissionIntent;
|
|
/** 设备发现回掉 */
|
private IUsbDevicePlugDelegate iUsbDevicePlugDelegate;
|
|
|
private UstorageDeviceInstance() {
|
}
|
|
public static UstorageDeviceInstance getInstance() {
|
if (instance == null) {
|
mLock.lock();
|
if (instance == null) {
|
instance = new UstorageDeviceInstance();
|
}
|
mLock.unlock();
|
}
|
return instance;
|
}
|
|
/**
|
* 尝试是否有连接设备(OTG)
|
*/
|
public int tryAttcheDeviceHandle(Context context, IUsbDevicePlugDelegate iUsbDevicePlugDelegate) {
|
Log.d("fwup","tryAttcheDeviceHandle: ");
|
this.iUsbDevicePlugDelegate = iUsbDevicePlugDelegate;
|
mUsbManager = (UsbManager) context.getSystemService(USB_SERVICE);
|
|
/* Object deviceInfo = null;
|
|
if (null != (deviceInfo = acceptOTGDeviceInfo(mUsbManager))) {
|
//满足 即当前存在OTG设备插入 OTG设备类型
|
// initOTGDeviceModule(context, (UsbDevice) deviceInfo);
|
acceptLicense(context, deviceInfo);
|
}*/
|
|
ArrayList<UsbDevice> devices = acceptOTGDeviceInfo(mUsbManager);
|
if (devices==null || devices.size()==0){
|
Log.i("fwup","devices==null");
|
return 1;
|
}
|
for (UsbDevice dev: devices) {
|
acceptLicense(context, dev);
|
}
|
return 0;
|
}
|
|
//<<<--------------------------------------- Usb 相关处理--------------------------------------------
|
/**
|
* 获取符合信息的OTG设备
|
* @param usbManager
|
*/
|
public ArrayList<UsbDevice> acceptOTGDeviceInfo(UsbManager usbManager) {
|
// UsbDevice device = null;
|
ArrayList<UsbDevice> devices = new ArrayList<>();
|
//拿到OTG设备列表
|
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
|
|
for (UsbDevice usbDevice : deviceList.values()) {
|
int productId = usbDevice.getProductId();
|
// if (productId != 36940) {
|
// device = usbDevice; // 可以增加厂商 版本等判断
|
// break;
|
// }
|
int vendorId = usbDevice.getVendorId();
|
Log.d("fwup","vendorId: " + vendorId);
|
if (vendorId == 3141) {
|
// device = usbDevice; // 可以增加厂商 版本等判断
|
devices.add(usbDevice);
|
}
|
}
|
return devices;
|
}
|
|
|
CUSBListener mUSBListener;
|
private SpcaJNI mSpcaJNI;
|
|
private void acceptLicense(Context context, Object deviceInfo) {
|
try {
|
mUSBListener = new CUSBListener(context, mOnDeviceConnectListener);
|
|
mUSBListener.register();
|
|
UsbDevice usbDevice = (UsbDevice) deviceInfo;
|
|
Log.d("fwup", "acceptLicense:" + usbDevice.getDeviceName());
|
// request permission [frank.chang 7/25/2018]
|
mUSBListener.requestPermission((UsbDevice)usbDevice);
|
|
// open the device [frank.chang 7/25/2018]
|
// mUsbManager.openDevice((UsbDevice)usbDevice);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
// implement OnDeviceConnectListener
|
private final CUSBListener.OnDeviceConnectListener mOnDeviceConnectListener = new CUSBListener.OnDeviceConnectListener() {
|
@Override
|
public void onAttach(final UsbDevice device) {
|
iUsbDevicePlugDelegate.usbDeviceInsert();
|
}
|
|
@Override
|
public void onConnect(final UsbDevice device, final CUSBListener.UsbControlBlock ctrlBlock, final boolean createNew) {
|
iUsbDevicePlugDelegate.permissionFinish(true, ctrlBlock);
|
}
|
|
@Override
|
public void onDisconnect(final UsbDevice device, final CUSBListener.UsbControlBlock ctrlBlock) {
|
if (mSpcaJNI != null) {
|
mSpcaJNI.close();
|
}
|
}
|
|
@Override
|
public void onDettach(final UsbDevice device) {
|
iUsbDevicePlugDelegate.usbDeviceDetached();
|
}
|
|
@Override
|
public void onCancel() {
|
}
|
};
|
}
|