lzw
2024-02-28 d4feb21868458d2db8275a7e8440922e5807ed0f
两次枚举 usb ,防止枚举不出来设备
4个文件已修改
100 ■■■■ 已修改文件
app/src/main/AndroidManifest.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/fwupgrade/saymanss/FwUpgradeService.java 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/fwupgrade/saymanss/HomePageActivity.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/fwupgrade/saymanss/deviceplug/UstorageDeviceInstance.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/AndroidManifest.xml
@@ -14,7 +14,7 @@
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
 <!--       <activity android:name=".HomePageActivity"
<!--        <activity android:name=".HomePageActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize"
            android:launchMode="singleTask"
app/src/main/java/com/fwupgrade/saymanss/FwUpgradeService.java
@@ -61,6 +61,7 @@
    private static final int UPGRADE_FAIL = 3;
    private static final int UPGRADE_SUCCESS = 4;
    private CameraInfo mCamInfo = null;
    private static final String TAG = FwUpgradeService.class.getCanonicalName();
    private IFwAidlInterface.Stub mBinder = new IFwAidlInterface.Stub() {
@@ -83,6 +84,7 @@
        super.onCreate();
        Log.i(TAG,"onCreate");
        initTarget();
        initData();
        initDeviceSDK();
        UStorageTestModule.getInstance().initUStorageDeviceModuleWithVirtualDisk(AppPathInfo.getLogPath());
@@ -117,13 +119,17 @@
                case USB_DEVICE_DETACHED:
                    boolean isSuccessful1 = (boolean) msg.obj;
                    if (isSuccessful1) {
                        Toast.makeText(FwUpgradeService.this, "USB设备权限申请成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.i(TAG,"发送失败结果广播给驾培");
                        sendToDriveTrainByBroadCast(0,1);
                        sendToNewDriveTrainByBroadCast(0,1);
                    int count = (int) msg.obj;
                    Log.i(TAG,"count="+count);
                    if (count<2) {
                        tty.clear();
                        initTarget();
                        int ret = UstorageDeviceInstance.getInstance().tryAgain(FwUpgradeService.this);
                        if (ret==1){
                            Log.i(TAG,"发送失败结果广播给驾培");
                            sendToDriveTrainByBroadCast(0,1);
                            sendToNewDriveTrainByBroadCast(0,1);
                        }
                    }
                    break;
@@ -150,7 +156,7 @@
                    int errCode = msg.arg1;
                    int succ_num = 0, fail_num = 0;
                    Log.d("fwup","Upgrade result:" + devName + ", errCode: " + errCode);
                    Log.d("fwup","Upgrade result:" + devName + ", errCode: " + errCode+".ttySize="+tty.size());
                    if (tty.containsKey(devName)) {
                        tty.put(devName, errCode == 0? UPGRADE_SUCCESS:UPGRADE_FAIL);
@@ -207,8 +213,10 @@
        for (UsbDevice usbDevice: targets) {
            if (usbDevice.getDeviceName().compareTo(min) < 0) {
                min = usbDevice.getDeviceName();
            }
        }
        Log.i(TAG,"min="+min);
        tty.put(min, UPGRADE_PRESET);
    }
@@ -244,16 +252,19 @@
     * UstorageDeviceSDK初始化.
     */
    private void initDeviceSDK() {
        int count = 0 ;
        int ret = UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(FwUpgradeService.this, FwUpgradeService.this);
        Log.i(TAG,"initDeviceSDk,ret="+ret);
        if (ret==1){
            ++count;
            Log.i(TAG,"再次的初始化");
            Message message = Message.obtain();
            message.obj = false;
            message.obj = count;
            message.what = USB_DEVICE_DETACHED;
            mHander.sendMessageDelayed(message,1000);
        }
    }
    @Override
@@ -298,7 +309,7 @@
        String serial = ctrlBlock.getSerial();
        Log.d(TAG,"sonixCamInit " + String.format("vendorId:%d productId:%d fileDescriptor:%d busNum:%d devNum:%d usbfsName:%s devName:%s serial:%s", vendorId, productId, fileDescriptor, busNum, devNum, usbfsName, devName, serial));
        Log.d(TAG,"sonixCamInit " + String.format("vendorId:%d productId:%d fileDescriptor:%d busNum:%d devNum:%d usbfsName:%s devName:%s serial:%s,tty.size=%d", vendorId, productId, fileDescriptor, busNum, devNum, usbfsName, devName, serial,tty.size()));
        if (tty.containsKey(devName) && tty.get(devName) == 0) {
@@ -306,16 +317,17 @@
            tty.put(devName, 1);
        }
        if (!isTarget) {
            Log.d(TAG,"Not sonixCam, close");
            ctrlBlock.close();
            sendToDriveTrainByBroadCast(0,1);
            sendToNewDriveTrainByBroadCast(0,1);
            return;
        }
        Log.d(TAG,"Is sonixCam, upgrade!");
        Message msg = Message.obtain();
        //msg.obj = errCode == 0;
        msg.obj = new CameraInfo(vendorId, productId, fileDescriptor, busNum, devNum, usbfsName, devName);
app/src/main/java/com/fwupgrade/saymanss/HomePageActivity.java
@@ -2,6 +2,7 @@
import android.Manifest;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -22,6 +23,7 @@
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.fwupgrade.saymanss.adapter.FwFileListAdapter;
import com.fwupgrade.saymanss.deviceplug.IUsbDevicePlugDelegate;
@@ -217,6 +219,33 @@
        UStorageTestModule.getInstance().initUStorageDeviceModuleWithVirtualDisk(AppPathInfo.getLogPath());
    }
    public void openThirdPartyApp(String pkgName) {
        try {
            // 替换为第三方应用程序的包名和MainActivity的完整类名
            String mainActivityName = pkgName+".MainActivity";
            // 创建Intent来启动第三方应用程序的MainActivity
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(pkgName, mainActivityName));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 启动Intent
            startActivity(intent);
        } catch (Exception e) {
            // 处理异常情况,例如应用程序未安装等
            e.printStackTrace();
            Toast.makeText(this, "无法启动第三方应用程序", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        //
//        openThirdPartyApp("safeluck.drive.training");
    }
    @Override
    protected void onNewIntent(Intent intent) {
        Log.d(TAG, "onNewIntent");
@@ -226,6 +255,7 @@
    @Override
    protected void onDestroy() {
        Log.i(TAG,"onDestroy");
        super.onDestroy();
    }
app/src/main/java/com/fwupgrade/saymanss/deviceplug/UstorageDeviceInstance.java
@@ -86,6 +86,28 @@
        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;
        }
        for (UsbDevice dev: devices) {
            acceptLicense(context, dev);
        }
        return 0;
    }
    //<<<--------------------------------------- Usb 相关处理--------------------------------------------
    /**
     * 获取符合信息的OTG设备
@@ -96,7 +118,9 @@
        ArrayList<UsbDevice> devices = new ArrayList<>();
        //拿到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) {