| | |
| | | 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.text.TextUtils; |
| | | 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 String TAG = UstorageDeviceInstance.class.getSimpleName(); |
| | | |
| | | 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 CUSBListener mUSBListener; |
| | | private Context g_cotext_; |
| | | |
| | | private ArrayList<CameraControlTransfer> curDevListArray; |
| | | public ArrayList<CameraControlTransfer> devListArray; |
| | | |
| | | /** 设备发现回掉 */ |
| | | private IUsbDevicePlugDelegate iUsbDevicePlugDelegate; |
| | | |
| | | |
| | | private UstorageDeviceInstance() { |
| | | } |
| | | |
| | | public static UstorageDeviceInstance getInstance() { |
| | | if (instance == null) { |
| | |
| | | return instance; |
| | | } |
| | | |
| | | UstorageDeviceInstance() { |
| | | devListArray = new ArrayList<CameraControlTransfer>(); |
| | | curDevListArray = new ArrayList<CameraControlTransfer>(); |
| | | } |
| | | |
| | | |
| | | final String getUSBFSName(final CUSBListener.UsbControlBlock ctrlBlock) { |
| | | String result = null; |
| | | final String name = ctrlBlock.getDeviceName(); |
| | | final String[] v = !TextUtils.isEmpty(name) ? name.split("/") : null; |
| | | if ((v != null) && (v.length > 2)) { |
| | | final StringBuilder sb = new StringBuilder(v[0]); |
| | | for (int i = 1; i < v.length - 2; i++) |
| | | sb.append("/").append(v[i]); |
| | | result = sb.toString(); |
| | | } |
| | | if (TextUtils.isEmpty(result)) { |
| | | result = "/dev/bus/usb"; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 尝试是否有连接设备(OTG) |
| | | */ |
| | | public int tryAttcheDeviceHandle(Context context, IUsbDevicePlugDelegate iUsbDevicePlugDelegate) { |
| | | Log.d("fwup","tryAttcheDeviceHandle: "); |
| | | public void tryAttcheDeviceHandle(Context context, IUsbDevicePlugDelegate iUsbDevicePlugDelegate) { |
| | | this.iUsbDevicePlugDelegate = iUsbDevicePlugDelegate; |
| | | mUsbManager = (UsbManager) context.getSystemService(USB_SERVICE); |
| | | |
| | | /* Object deviceInfo = null; |
| | | devListArray.clear(); |
| | | curDevListArray.clear(); |
| | | |
| | | if (null != (deviceInfo = acceptOTGDeviceInfo(mUsbManager))) { |
| | | //满足 即当前存在OTG设备插入 OTG设备类型 |
| | | // initOTGDeviceModule(context, (UsbDevice) deviceInfo); |
| | | acceptLicense(context, deviceInfo); |
| | | }*/ |
| | | g_cotext_ = context; |
| | | // //拿到OTG设备列表 |
| | | HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList(); |
| | | Log.d("deviceList", String.valueOf(deviceList.size())); |
| | | for (UsbDevice usbDevice : deviceList.values()) { |
| | | Log.i("Start", String.valueOf(usbDevice.getProductName())); |
| | | Boolean find = false; |
| | | for (int i = 0; i < curDevListArray.size(); i++) { |
| | | if(usbDevice.equals(curDevListArray.get(i))){ |
| | | find= true; |
| | | Log.e("ERROR", "find device"); |
| | | Log.e("ERROR", String.valueOf(usbDevice.getDeviceClass())); |
| | | Log.e("ERROR", String.valueOf(usbDevice.getDeviceName())); |
| | | Log.e("ERROR", String.valueOf(usbDevice.getProductName())); |
| | | continue; |
| | | } |
| | | } |
| | | if(usbDevice.getDeviceClass() != 0xEF) { |
| | | Log.e("ERROR", "getDeviceClass!=0xEF"); |
| | | Log.e("ERROR", String.valueOf(usbDevice.getDeviceClass())); |
| | | Log.e("ERROR", String.valueOf(usbDevice.getDeviceName())); |
| | | Log.e("ERROR", String.valueOf(usbDevice.getProductName())); |
| | | continue; |
| | | } |
| | | if(!find){ |
| | | // if(usbDevice.getVendorId() != 0x0581 || usbDevice.getProductId() != 0x0b20){ |
| | | // continue; |
| | | // } |
| | | |
| | | ArrayList<UsbDevice> devices = acceptOTGDeviceInfo(mUsbManager); |
| | | if (devices==null || devices.size()==0){ |
| | | Log.i("fwup","devices==null"); |
| | | return 1; |
| | | |
| | | // Log.d("usbDevice", String.valueOf(usbDevice.toString())); |
| | | // Log.d("getDeviceSubclass", String.valueOf(usbDevice.getDeviceSubclass())); |
| | | Log.d("devName", usbDevice.getDeviceName()); |
| | | Log.d("proName", usbDevice.getProductName()); |
| | | Log.d("vid", String.valueOf(usbDevice.getVendorId())); |
| | | Log.d("pid", String.valueOf(usbDevice.getProductId())); |
| | | CameraControlTransfer infos = new CameraControlTransfer(); |
| | | infos.dev = usbDevice; |
| | | infos.bPermission = false; |
| | | try { |
| | | infos.usbListener = mUSBListener; |
| | | mUSBListener = new CUSBListener(context, mOnDeviceConnectListener); |
| | | mUSBListener.register(); |
| | | mUSBListener.requestPermission(usbDevice); |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | curDevListArray.add(infos); |
| | | Log.d(TAG, "curDevListArraySize: " + curDevListArray.size()); |
| | | } |
| | | } |
| | | for (UsbDevice dev: devices) { |
| | | acceptLicense(context, dev); |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | public int tryAgain(Context context){ |
| | | 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","tryAgain devices==null"); |
| | | return 1; |
| | | public boolean startAcceptLicense(int position, Context context, Object deviceInfo) { |
| | | if(devListArray.get(position).usbListener.hasPermission((UsbDevice)deviceInfo)){ |
| | | return true; |
| | | } |
| | | for (UsbDevice dev: devices) { |
| | | acceptLicense(context, dev); |
| | | try { |
| | | devListArray.get(position).usbListener.requestPermission((UsbDevice) deviceInfo); |
| | | mUsbManager.openDevice((UsbDevice) deviceInfo); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return 0; |
| | | return false; |
| | | } |
| | | |
| | | |
| | | //<<<--------------------------------------- Usb 相关处理-------------------------------------------- |
| | | /** |
| | | * 获取符合信息的OTG设备 |
| | | * @param usbManager |
| | | */ |
| | | public ArrayList<UsbDevice> acceptOTGDeviceInfo(UsbManager usbManager) { |
| | | // UsbDevice device = null; |
| | | ArrayList<UsbDevice> devices = new ArrayList<>(); |
| | | private UsbDevice acceptOTGDeviceInfo(Context context, UsbManager usbManager) { |
| | | UsbDevice device = null; |
| | | |
| | | //拿到OTG设备列表 |
| | | HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); |
| | | if (deviceList==null || deviceList.size()==0){ |
| | | Log.d("fwup","deviceList为空 "); |
| | | } |
| | | |
| | | 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); |
| | | if (vendorId == 0x0c45) { |
| | | device = usbDevice; |
| | | break; |
| | | } |
| | | } |
| | | return devices; |
| | | return device; |
| | | } |
| | | |
| | | |
| | | CUSBListener mUSBListener; |
| | | private SpcaJNI mSpcaJNI; |
| | | |
| | | private void acceptLicense(Context context, Object deviceInfo) { |
| | | try { |
| | |
| | | |
| | | 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); |
| | | mUsbManager.openDevice((UsbDevice)usbDevice); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | // implement OnDeviceConnectListener |
| | | private final CUSBListener.OnDeviceConnectListener mOnDeviceConnectListener = new CUSBListener.OnDeviceConnectListener() { |
| | |
| | | |
| | | @Override |
| | | public void onConnect(final UsbDevice device, final CUSBListener.UsbControlBlock ctrlBlock, final boolean createNew) { |
| | | iUsbDevicePlugDelegate.permissionFinish(true, ctrlBlock); |
| | | for (int i = 0; i < curDevListArray.size(); i++) { |
| | | if(device.equals(curDevListArray.get(i).dev)){ |
| | | Log.d(TAG, "Get Permission:" + device.getDeviceName()); |
| | | CameraControlTransfer cct = curDevListArray.get(i); |
| | | cct.bPermission = true; |
| | | cct.connection = ctrlBlock.mConnection; |
| | | String fsName = new String(); |
| | | fsName = ctrlBlock.getDeviceName();; |
| | | cct.usbFSName = fsName; |
| | | cct.fd = ctrlBlock.getFileDescriptor(); |
| | | mUsbManager.openDevice(device); |
| | | devListArray.add(cct); |
| | | // int a = ctrlBlock.getBusNum(); |
| | | // int b = ctrlBlock.getDevNum(); |
| | | |
| | | iUsbDevicePlugDelegate.permissionFinish(true, ctrlBlock, cct); |
| | | break; |
| | | } |
| | | } |
| | | // 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) { |
| | | for (int i = 0; i < devListArray.size(); i++) { |
| | | if(device.equals(device)){ |
| | | devListArray.remove(i); |
| | | break; |
| | | } |
| | | } |
| | | iUsbDevicePlugDelegate.usbDeviceDetached(); |
| | | } |
| | | |
| | | @Override |
| | | public void onCancel() { |
| | | |
| | | } |
| | | }; |
| | | } |
| | | |
| | | } |