From a9ac419b3564590e5fccba048988e287490bc79c Mon Sep 17 00:00:00 2001
From: lzw <lzw@github.aaej.cn>
Date: 星期一, 19 二月 2024 13:50:06 +0800
Subject: [PATCH] 1.修改成无界面的apk 2.修改成服务

---
 build.gradle                                                   |    2 
 app/src/main/AndroidManifest.xml                               |   22 --
 UStorageTest/build.gradle                                      |    1 
 app/build.gradle                                               |    7 
 app/src/main/aidl/com/fwupgrade/saymanss/IFwAidlInterface.aidl |    9 +
 app/src/main/java/com/fwupgrade/saymanss/FwUpgradeService.java |  377 +++++++++++++++++++++++++++++++++++++++++++++++
 gradle.properties                                              |    5 
 7 files changed, 395 insertions(+), 28 deletions(-)

diff --git a/UStorageTest/build.gradle b/UStorageTest/build.gradle
index 829267a..4742da4 100644
--- a/UStorageTest/build.gradle
+++ b/UStorageTest/build.gradle
@@ -2,7 +2,6 @@
 
 android {
     compileSdkVersion 27
-    buildToolsVersion "27.0.3"
 
     defaultConfig {
         minSdkVersion 15
diff --git a/app/build.gradle b/app/build.gradle
index a562033..48ad18c 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -2,16 +2,17 @@
 
 android {
     compileSdkVersion 27
-//    buildToolsVersion "27.0.3"
     defaultConfig {
         applicationId "com.fwupgrade.saymanss"
-        minSdkVersion 15
+        minSdkVersion 21
         targetSdkVersion 27
         versionCode 6
         versionName "1.6"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     }
-
+    buildFeatures{
+        aidl true
+    }
     signingConfigs {
         release {
             storeFile file("../key/key.jks")
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 70b4af1..5c0b4a2 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -14,28 +14,8 @@
         android:label="@string/app_name"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
-        <activity android:name=".HomePageActivity"
-            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
-            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize"
-            android:launchMode="singleTask"
-            android:screenOrientation="portrait"
-            android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
 
-                <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>
-
-<!--            <intent-filter>-->
-<!--                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>-->
-<!--                <action android:name="android.hardware.usb.action.ACTION_USB_DEVICE_DETACHED"/>-->
-<!--                <action android:name="android.hardware.usb.action.USB_STATE"/>-->
-<!--            </intent-filter>-->
-
-<!--            <meta-data-->
-<!--                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"-->
-<!--                android:resource="@xml/device_filter"/>-->
-        </activity>
+        <service android:name=".FwUpgradeService" android:exported="true"/>
     </application>
 
 </manifest>
\ No newline at end of file
diff --git a/app/src/main/aidl/com/fwupgrade/saymanss/IFwAidlInterface.aidl b/app/src/main/aidl/com/fwupgrade/saymanss/IFwAidlInterface.aidl
new file mode 100644
index 0000000..d2dcbe8
--- /dev/null
+++ b/app/src/main/aidl/com/fwupgrade/saymanss/IFwAidlInterface.aidl
@@ -0,0 +1,9 @@
+// IFwAidlInterface.aidl
+package com.fwupgrade.saymanss;
+
+// Declare any non-default types here with import statements
+
+interface IFwAidlInterface {
+
+    void upgradeFw(String path,String device);
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/fwupgrade/saymanss/FwUpgradeService.java b/app/src/main/java/com/fwupgrade/saymanss/FwUpgradeService.java
new file mode 100644
index 0000000..aa7ac8b
--- /dev/null
+++ b/app/src/main/java/com/fwupgrade/saymanss/FwUpgradeService.java
@@ -0,0 +1,377 @@
+package com.fwupgrade.saymanss;
+
+import android.app.Service;
+import android.content.Intent;
+import android.hardware.usb.UsbDevice;
+import android.hardware.usb.UsbManager;
+import android.os.Build;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.Message;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.support.annotation.Nullable;
+import android.text.TextUtils;
+import android.util.Log;
+import android.view.View;
+import android.widget.Toast;
+
+import com.fwupgrade.saymanss.adapter.FwFileListAdapter;
+import com.fwupgrade.saymanss.deviceplug.IUsbDevicePlugDelegate;
+import com.fwupgrade.saymanss.deviceplug.UstorageDeviceInstance;
+import com.fwupgrade.saymanss.utils.AppPathInfo;
+import com.fwupgrade.saymanss.utils.UtilTools;
+import com.fwupgrade.saymanss.view.GeneralDialog;
+import com.jni.UStorageTestModule;
+import com.spca.usb.CUSBListener;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @ProjectName: FwUpgrade
+ * @Package: com.fwupgrade.saymanss
+ * @ClassName: FwUpgradeService
+ * @Description: java绫讳綔鐢ㄦ弿杩�
+ * @Author: zhanwei.li
+ * @CreateDate: 2024/2/19 10:02
+ * @UpdateUser: 鏇存柊鑰�
+ * @UpdateDate: 2024/2/19 10:02
+ * @UpdateRemark: 鏇存柊璇存槑
+ * @Version: 1.0
+ */
+public class FwUpgradeService extends Service implements IUsbDevicePlugDelegate {
+
+    /** usb鎻掑叆. */
+    private static final int USB_DEVICE_INSERT = 0;
+    /** usb鏉冮檺. */
+    private static final int USB_DEVICE_PERRMISSION = 1;
+    /** usb鎷斿嚭. */
+    private static final int USB_DEVICE_DETACHED = 2;
+    /** usb鎵撳紑. */
+    private static final int USB_DEVICE_OPEN = 3;
+    /** 鍗囩骇缁撴潫. */
+    private static final int USB_DEVICE_UPGRADE = 4;
+    /** 鏄剧ず */
+    private static final int USB_DEVICE_MESSAGE = 5;
+
+    private static final int UPGRADE_PRESET = 0;
+
+    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() {
+        @Override
+        public void upgradeFw(String path, String device) throws RemoteException {
+            Log.i(TAG,"瀹㈡埛绔皟鐢ㄦ洿鏂板浐浠舵柟娉�,device="+device+"_path="+path);
+
+        }
+    };
+    @Nullable
+    @Override
+    public IBinder onBind(Intent intent) {
+        return mBinder;
+    }
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        Log.i(TAG,"onCreate");
+        initTarget();
+        initData();
+        initDeviceSDK();
+
+    }
+
+
+    private File[] mFileList;
+
+    private HashMap<String, Integer> tty = new HashMap<>();
+
+    private final Handler mHander = new Handler() {
+        @Override
+        public void dispatchMessage(Message msg) {
+            switch (msg.what) {
+
+                case USB_DEVICE_INSERT:
+                    Toast.makeText(FwUpgradeService.this, "USB璁惧宸叉彃鍏�", Toast.LENGTH_SHORT).show();
+                    break;
+
+                case USB_DEVICE_PERRMISSION:
+                    boolean isSuccessful = (boolean) msg.obj;
+
+                    if (isSuccessful) {
+                        Toast.makeText(FwUpgradeService.this, "USB璁惧鏉冮檺鐢宠鎴愬姛", Toast.LENGTH_SHORT).show();
+                    } else {
+                        Toast.makeText(FwUpgradeService.this, "USB璁惧鏉冮檺鐢宠澶辫触", Toast.LENGTH_SHORT).show();
+                    }
+                    break;
+
+                case USB_DEVICE_DETACHED:
+                    Toast.makeText(FwUpgradeService.this, "USB璁惧宸叉嫈鍑猴紝绛夊緟璁惧鎻掑叆", Toast.LENGTH_SHORT).show();
+                    break;
+
+                case USB_DEVICE_OPEN:
+                    Log.d(TAG, "USB_DEVICE_OPEN");
+                    CameraInfo cameraInfo = (CameraInfo) msg.obj;
+
+                    Toast.makeText(FwUpgradeService.this, "USB璁惧鍒濆鍖栨垚鍔燂紝鍙互鍑嗗鍗囩骇", Toast.LENGTH_SHORT).show();
+                    acceptFwList();
+                    mCamInfo = cameraInfo;
+
+                    executeUpgrade(mCamInfo);
+
+                    break;
+
+                case GeneralDialog.BUTTON_CLICK_OK:
+
+                    break;
+
+                case USB_DEVICE_UPGRADE:
+
+                    String devName = (String) msg.obj;
+                    int errCode = msg.arg1;
+                    int succ_num = 0, fail_num = 0;
+
+                    Log.d("fwup","Upgrade result:" + devName + ", errCode: " + errCode);
+
+                    if (tty.containsKey(devName)) {
+                        tty.put(devName, errCode == 0? UPGRADE_SUCCESS:UPGRADE_FAIL);
+                    }
+
+                    for (Map.Entry<String, Integer> entry : tty.entrySet()) {
+                        if (entry.getValue() == UPGRADE_SUCCESS) {
+                            succ_num++;
+                        } else if (entry.getValue() == UPGRADE_FAIL) {
+                            fail_num++;
+                        }
+                    }
+
+                    if (succ_num + fail_num == tty.size()) {
+                        Log.d(TAG, "Upgrade Finish!");
+
+                        Intent intent = new Intent();
+                        intent.putExtra("result", succ_num == tty.size()? 0 : 1);
+                        intent.setAction("com.fwupgrade.saymanss.UPGRADE_COMPLETE");
+                        intent.setPackage("safeluck.drive.training");
+                        sendBroadcast(intent);
+
+                        intent = new Intent();
+                        intent.putExtra("result", succ_num == tty.size()? 0 : 1);
+                        intent.setAction("com.fwupgrade.saymanss.UPGRADE_COMPLETE");
+                        intent.setPackage("demo1.tech.anyun.com.myapplication");
+                        sendBroadcast(intent);
+
+
+//                        System.exit(0);
+                    }
+                    break;
+                case USB_DEVICE_MESSAGE:
+                    String text = (String) msg.obj;
+                    Toast.makeText(FwUpgradeService.this, text, Toast.LENGTH_SHORT).show();
+                    break;
+            }
+        }
+    };
+    // 姣忔鍗囩骇璺緞鍊兼渶灏忕殑
+    private void initTarget() {
+        ArrayList<UsbDevice> targets = UstorageDeviceInstance.getInstance().acceptOTGDeviceInfo((UsbManager) getSystemService(USB_SERVICE));
+        String min = "Z";
+
+        for (UsbDevice usbDevice: targets) {
+            if (usbDevice.getDeviceName().compareTo(min) < 0) {
+                min = usbDevice.getDeviceName();
+            }
+        }
+        tty.put(min, UPGRADE_PRESET);
+    }
+
+    /**
+     * 鑾峰彇fw鍒楄〃
+     */
+    private void acceptFwList() {
+        Log.d("fwup","acceptFwList: ");
+        File file = new File(AppPathInfo.getFwSavePath());
+        File[] files = file.listFiles();
+        if (files != null && files.length > 0) {
+            mFileList = files;
+        } else {
+        }
+    }
+    private void executeUpgrade(CameraInfo cameraInfo) {
+        if (mFileList != null && mFileList.length > 0) {
+            Log.i(TAG,"executeUpgrade");
+            new Thread(new Update(cameraInfo, mFileList[0])).start();
+        }
+    }
+
+
+    /**
+     * 鍒濆鍖栨暟鎹�
+     */
+    private void initData() {
+        UtilTools.createFolderInSdcard(AppPathInfo.getFwSavePath());
+
+    }
+
+    /**
+     * UstorageDeviceSDK鍒濆鍖�.
+     */
+    private void initDeviceSDK() {
+        UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(FwUpgradeService.this, FwUpgradeService.this);
+
+
+    }
+
+    @Override
+    public void usbDeviceInsert() {
+        Log.d(TAG,"usbDeviceInsert");
+    }
+
+    @Override
+    public void permissionFinish(boolean isSuccessful, CUSBListener.UsbControlBlock ctrlBlock) {
+        synchronized (this) {
+            Message msg = Message.obtain();
+            msg.obj = isSuccessful;
+            msg.what = USB_DEVICE_PERRMISSION;
+            mHander.sendMessage(msg);
+            Log.d(TAG,"permissionFinish:" + isSuccessful);
+            if (isSuccessful && ctrlBlock != null) {
+                initFwUpgrade(ctrlBlock);
+            }
+        }
+    }
+
+    @Override
+    public void usbDeviceDetached() {
+        Log.d(TAG,"usbDeviceDetached");
+    }
+
+    /**
+     * 鍒濆鍖栧崌绾х幆澧�
+     */
+    private void initFwUpgrade(CUSBListener.UsbControlBlock ctrlBlock) {
+        final int vendorId = ctrlBlock.getVenderId();
+        final int productId = ctrlBlock.getProductId();
+        int fileDescriptor = ctrlBlock.getFileDescriptor();
+        int busNum = ctrlBlock.getBusNum();
+        int devNum = ctrlBlock.getDevNum();
+        boolean isTarget = false;
+        String usbfsName = getUSBFSName(ctrlBlock);
+        String devName = ctrlBlock.getDeviceName();
+
+        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));
+
+
+        if (tty.containsKey(devName) && tty.get(devName) == 0) {
+            isTarget = true;
+            tty.put(devName, 1);
+        }
+
+        if (!isTarget) {
+            Log.d(TAG,"Not sonixCam, close");
+            ctrlBlock.close();
+            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);
+        msg.what = USB_DEVICE_OPEN;
+        mHander.sendMessage(msg);
+    }
+
+
+    private 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)) {
+//            Log.w(TAG, "failed to get USBFS path, try to use default path:" + name);
+            result = "/dev/bus/usb";
+        }
+        return result;
+    }
+
+    class Update implements Runnable {
+        CameraInfo cam;
+        File file;
+        public Update(CameraInfo cam, File file) {
+            this.cam = cam;
+            this.file = file;
+        }
+        @Override
+        public void run() {
+            int errCode = fwUpdate(cam, file);
+
+            Message msg = Message.obtain();
+            msg.obj = cam.devName;
+            msg.arg1 = errCode;
+            msg.what = USB_DEVICE_UPGRADE;
+            mHander.sendMessage(msg);
+        }
+    }
+
+    int fwUpdate(final CameraInfo cam, final File file) {
+        int errCode = -1;
+        int retry = 0;
+
+        synchronized (this) {
+            Log.d(TAG, String.format("Upgrade %s...", cam.devName));
+
+            Message msg = Message.obtain();
+            msg.obj = String.format("Upgrade %s...", cam.devName);
+            msg.what = USB_DEVICE_MESSAGE;
+            mHander.sendMessage(msg);
+
+            while (retry++ <= 3) {
+                errCode = UStorageTestModule.getInstance().sonixCamInit(cam.vendorId, cam.productId, cam.fileDescriptor, cam.busNum, cam.devNum, cam.usbfsName);
+                Log.d(TAG, "Initialize result:" + errCode);
+                if (errCode != 0) {
+                    return errCode;
+                }
+                errCode = UStorageTestModule.getInstance().updateFW(file.getPath());
+                Log.d(TAG, "Upgrade result:" + errCode);
+                UStorageTestModule.getInstance().sonixCamUnInit();
+                if (errCode == 0) {
+                    return 0;
+                }
+                SystemClock.sleep(1000);
+            }
+        }
+        return errCode;
+    }
+
+    static class CameraInfo {
+        int vendorId;
+        int productId;
+        int fileDescriptor;
+        int busNum;
+        int devNum;
+        String usbfsName;
+        String devName;
+        public CameraInfo(int vendorId, int productId, int fileDescriptor, int busNum, int devNum, String usbfsName, String devName) {
+            this.vendorId = vendorId;
+            this.productId = productId;
+            this.fileDescriptor = fileDescriptor;
+            this.busNum = busNum;
+            this.devNum = devNum;
+            this.usbfsName = usbfsName;
+            this.devName = devName;
+        }
+    }
+}
diff --git a/build.gradle b/build.gradle
index a868baa..de09b28 100644
--- a/build.gradle
+++ b/build.gradle
@@ -7,7 +7,7 @@
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.4.0'
+        classpath 'com.android.tools.build:gradle:4.2.2'
         
 
         // NOTE: Do not place your application dependencies here; they belong
diff --git a/gradle.properties b/gradle.properties
index 69b4e93..44b8275 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -9,10 +9,11 @@
 
 # Specifies the JVM arguments used for the daemon process.
 # The setting is particularly useful for tweaking memory settings.
-org.gradle.jvmargs=-Xmx1536m
+org.gradle.jvmargs=-Xmx1536m --add-opens java.base/java.io=ALL-UNNAMED
+
 
 # When configured, Gradle will run in incubating parallel mode.
 # This option should only be used with decoupled projects. More details, visit
 # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
 # org.gradle.parallel=true
-android.enableAapt2=false
+

--
Gitblit v1.8.0