yy1717
2025-12-26 3244dbd998aaed5ba13db688a5c0b85eb88e75a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.fwupgrade.saymanss.deviceplug;
 
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.text.TextUtils;
import android.util.Log;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
import static android.content.Context.USB_SERVICE;
 
 
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 CUSBListener mUSBListener;
    private Context g_cotext_;
 
    private ArrayList<CameraControlTransfer> curDevListArray;
    public ArrayList<CameraControlTransfer> devListArray;
 
    /** 设备发现回掉 */
    private IUsbDevicePlugDelegate iUsbDevicePlugDelegate;
 
    public static UstorageDeviceInstance getInstance() {
        if (instance == null) {
            mLock.lock();
            if (instance == null) {
                instance = new UstorageDeviceInstance();
            }
            mLock.unlock();
        }
        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 void tryAttcheDeviceHandle(Context context, IUsbDevicePlugDelegate iUsbDevicePlugDelegate) {
        this.iUsbDevicePlugDelegate = iUsbDevicePlugDelegate;
        mUsbManager = (UsbManager) context.getSystemService(USB_SERVICE);
 
        devListArray.clear();
        curDevListArray.clear();
 
        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;
//                }
 
 
//                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());
            }
        }
    }
 
    public boolean startAcceptLicense(int position, Context context, Object deviceInfo) {
        if(devListArray.get(position).usbListener.hasPermission((UsbDevice)deviceInfo)){
            return true;
        }
        try {
            devListArray.get(position).usbListener.requestPermission((UsbDevice) deviceInfo);
            mUsbManager.openDevice((UsbDevice) deviceInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
 
    //<<<--------------------------------------- Usb 相关处理--------------------------------------------
    /**
     * 获取符合信息的OTG设备
     * @param usbManager
     */
    private UsbDevice acceptOTGDeviceInfo(Context context, UsbManager usbManager) {
        UsbDevice device = null;
 
        //拿到OTG设备列表
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
 
        for (UsbDevice usbDevice : deviceList.values()) {
            int vendorId = usbDevice.getVendorId();
            if (vendorId == 0x0c45) {
                device = usbDevice;
                break;
            }
        }
        return device;
    }
 
    private void acceptLicense(Context context, Object deviceInfo) {
        try {
            mUSBListener = new CUSBListener(context, mOnDeviceConnectListener);
 
            mUSBListener.register();
 
            UsbDevice usbDevice = (UsbDevice) deviceInfo;
 
            // 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) {
            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) {
 
        }
 
        @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() {
 
        }
    };
 
}