lzw
2024-02-19 a9ac419b3564590e5fccba048988e287490bc79c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package com.fwupgrade.saymanss;
 
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.icu.text.MessagePattern;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
 
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.SystemUtil;
import com.fwupgrade.saymanss.utils.UtilTools;
import com.fwupgrade.saymanss.view.GeneralDialog;
import com.jni.UStorageTestModule;
import com.spca.usb.CUSBListener;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
import app.dinus.com.loadingdrawable.LoadingView;
 
public class HomePageActivity extends AppCompatActivity implements IUsbDevicePlugDelegate, AdapterView.OnItemClickListener {
    static final String TAG = "fwup";
    /** 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_PREPARE = 1;
    private static final int UPGRADE_START = 2;
    private static final int UPGRADE_FAIL = 3;
    private static final int UPGRADE_SUCCESS = 4;
 
    /** 需要请求授权的权限. */
    private ArrayList<String> needRPDatas;
 
    /** 应用所需要请求的所有权限. */
    private String[] needRequestPermissions = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
 
    /** 动态权限请求码. */
    final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
 
 
    protected TextView mTitle;
    /** 返回 */
    protected TextView mAppVersion;
    /** LoadingView */
    private LoadingView loadingView;
    /** 升级 */
    private TextView mFwUpDataBtn;
    /** 提示 */
    private TextView mFwUpNo;
    /** fw list */
    protected ListView mListView;
 
    private File[] mFileList;
 
    private HashMap<String, Integer> tty = new HashMap<>();
 
    private Handler mHander = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
 
                case USB_DEVICE_INSERT:
                    mFwUpDataBtn.setText("USB设备已插入");
                    break;
 
                case USB_DEVICE_PERRMISSION:
                    boolean isSuccessful = (boolean) msg.obj;
 
                    if (isSuccessful) {
                        mFwUpDataBtn.setText("USB设备权限申请成功");
                    } else {
                        mFwUpDataBtn.setText("USB设备权限申请失败");
                    }
                    break;
 
                case USB_DEVICE_DETACHED:
                    mFwUpDataBtn.setEnabled(false);
                    mFwUpDataBtn.setText("USB设备已拔出,等待设备插入");
                    break;
 
                case USB_DEVICE_OPEN:
                    Log.d(TAG, "USB_DEVICE_OPEN");
                    CameraInfo cameraInfo = (CameraInfo)msg.obj;
 
                    mFwUpDataBtn.setText("USB设备初始化成功,可以准备升级");
                    mFwUpDataBtn.setEnabled(true);
                    acceptFwList();
 
                    if (mFileList != null && mFileList.length > 0) {
                        new Thread(new Update(cameraInfo, mFileList[0])).start();
                    }
 
                    /*boolean initSuccessful = (boolean) msg.obj;
                    if (initSuccessful) {
                        mFwUpDataBtn.setText("USB设备初始化成功,可以准备升级");
                        mFwUpDataBtn.setEnabled(true);
                        acceptFwList();
                    } else {
                        mFwUpDataBtn.setText("USB设备初始化失败");
                        mFwUpDataBtn.setEnabled(false);
                    }*/
                    break;
 
                case GeneralDialog.BUTTON_CLICK_OK:
                    /*File file = (File) msg.obj;
                    startFwUpdata(file);*/
                    break;
 
                case USB_DEVICE_UPGRADE:
                    /*loadingView.setVisibility(View.GONE);
                    boolean upgradeSuccessful = (boolean) msg.obj;
                    int errCode = msg.arg1;
                    if (upgradeSuccessful) {
                        mFwUpDataBtn.setText("升级成功");
                    } else {
                        if (errCode == -1100) {
                            mFwUpDataBtn.setText("非法固件");
                        } else {
                            mFwUpDataBtn.setText("升级失败");
                        }
                    }*/
                    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);
 
                        finish();
                        System.exit(0);
                    }
                    break;
                case USB_DEVICE_MESSAGE:
                    String text = (String) msg.obj;
                    mFwUpDataBtn.setText(text);
                    break;
            }
        }
    };
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        SystemUtil.setStatusBarColor(this);
 
        initView();
        checkPermission();
        initTarget();
        initData();
        initListener();
        initDeviceSDK();
        UStorageTestModule.getInstance().initUStorageDeviceModuleWithVirtualDisk(AppPathInfo.getLogPath());
    }
 
    @Override
    protected void onNewIntent(Intent intent) {
        Log.d(TAG, "onNewIntent");
        super.onNewIntent(intent);
        initDeviceSDK();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
 
    /**
     * 初始化view
     */
    private void initView() {
        //顶部条
        mTitle = (TextView) findViewById(R.id.app_topbar_center_text);
        mAppVersion = (TextView) findViewById(R.id.app_topbar_left_text);
        loadingView = (LoadingView) findViewById(R.id.progress_loading);
        loadingView.setColorFilter(getResources().getColor(R.color.apptakeaction));
 
        mFwUpDataBtn = (TextView) findViewById(R.id.app_fwupdata_btn);
        mFwUpNo = (TextView) findViewById(R.id.app_topbar_no);
        mListView = (ListView) findViewById(R.id.app_fw_list);
    }
    // 每次升级路径值最小的
    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);
    }
 
    /**
     * 初始化数据
     */
    private void initData() {
        UtilTools.createFolderInSdcard(AppPathInfo.getFwSavePath());
        mTitle.setText("FwUpgradeDemo");
        mFwUpDataBtn.setEnabled(false);
        mAppVersion.setVisibility(View.VISIBLE);
 
        setAppVersionInfo();
    }
 
    /**
     * 设置app版本
     */
    private void setAppVersionInfo() {
        PackageManager manager = getPackageManager();
        String version = "";
        try {
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
            version = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            version = "";
        }
        mAppVersion.setText("App版本:" + version);
    }
 
    /**
     * UstorageDeviceSDK初始化.
     */
    private void initDeviceSDK() {
        UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(HomePageActivity.this, HomePageActivity.this);
//        Thread thread = new Thread() {
//            @Override
//            public void run() {
//                UstorageDeviceInstance.getInstance().tryAttcheDeviceHandle(HomePageActivity.this, HomePageActivity.this);
//            }
//        };
//        thread.start();
    }
 
    /**
     * 初始化 监听
     */
    private void initListener() {
        mListView.setOnItemClickListener(this);
    }
 
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        int visibility = loadingView.getVisibility();
        if (mFileList != null && visibility != View.VISIBLE) {
            File file = mFileList[position];
            String point = "确定升级此固件吗?";
            GeneralDialog generalDialog = new GeneralDialog(this, R.style.wdDialog, mHander, point, file);
            generalDialog.setCanceledOnTouchOutside(false);
            generalDialog.show();
        }
    }
 
 
    /**
     * 获取fw列表
     */
    private void acceptFwList() {
        Log.d("fwup","acceptFwList: ");
        loadingView.setVisibility(View.GONE);
        File file = new File(AppPathInfo.getFwSavePath());
        File[] files = file.listFiles();
        if (files != null && files.length > 0) {
            mFwUpNo.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
            mFileList = files;
            FwFileListAdapter apListAdapter = new FwFileListAdapter(this, files);
            mListView.setAdapter(apListAdapter);
        } else {
            mFwUpNo.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
        }
    }
 
 
    @Override
    public void usbDeviceInsert() {
        Log.d("fwup","usbDeviceInsert");
//        mHander.sendEmptyMessage(USB_DEVICE_INSERT);
    }
 
    @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("fwup","permissionFinish:" + isSuccessful);
            if (isSuccessful && ctrlBlock != null) {
                initFwUpgrade(ctrlBlock);
            }
        }
    }
 
    @Override
    public void usbDeviceDetached() {
        Log.d("fwup","usbDeviceDetached");
//        mHander.sendEmptyMessage(USB_DEVICE_DETACHED);
//        unInitFwUpgrade();
    }
 
    /**
     * 初始化升级环境
     */
    private void unInitFwUpgrade() {
        int errCode = UStorageTestModule.getInstance().sonixCamUnInit();
    }
 
    static class UpdateTarget {
        String devName;
        int status;
        public UpdateTarget(String devName, int status) {
            this.devName = devName;
            this.status = status;
        }
    }
 
    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;
        }
    }
 
    /**
     * 初始化升级环境
     */
    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("fwup","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));
 
        //final int errCode = UStorageTestModule.getInstance().sonixCamInit(vendorId, productId, fileDescriptor, busNum, devNum, usbfsName);
        //Log.d("fwup","sonixCamInit errCode:" + errCode);
 
        /*runOnUiThread(new Runnable() {
            @Override
            public void run() {
                UtilTools.showToast(HomePageActivity.this, "vid: " + vendorId + " pid: " + productId + " err: " + errCode);
            }
        });*/
 
        if (tty.containsKey(devName) && tty.get(devName) == 0) {
            isTarget = true;
            tty.put(devName, 1);
        }
 
        if (!isTarget) {
            Log.d("fwup","Not sonixCam, close");
            ctrlBlock.close();
            return;
        }
 
        Log.d("fwup","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;
    }
 
 
    /**
     * 升级
     * @param file
     */
    private void startFwUpdata(final File file) {
        loadingView.setVisibility(View.VISIBLE);
        mFwUpDataBtn.setText("正在升级,请勿断开连接");
        Thread thread = new Thread() {
            @Override
            public void run() {
                int errCode = UStorageTestModule.getInstance().updateFW(file.getPath());
                Log.d("fwup","errCode: " + errCode);
                Message msg = Message.obtain();
                msg.obj = errCode == 0;
                msg.arg1 = errCode;
                msg.what = USB_DEVICE_UPGRADE;
                mHander.sendMessage(msg);
            }
        };
        thread.start();
    }
 
    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;
    }
 
 
    // -----------------------------------------------权限申请 相关------------------------------------------
    /**
     * 检查权限
     */
    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            needRPDatas = checkAllPermissions();
            if (needRPDatas.size() == 0) {
                initData();
            } else {
                requestPermissions(needRPDatas.toArray(new String[needRPDatas.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            }
        } else {
            initData();
        }
    }
 
    /**
     * 判断所有需要的权限是否已经授权.
     * @return 所有授权: true; 有任何一个没授权: false;
     */
    public ArrayList<String> checkAllPermissions() {
        ArrayList<String> needRequestDatas = new ArrayList<>();
        for (String permission : needRequestPermissions) {
            if (checkPermission(permission)) {
                needRequestDatas.add(permission);
            }
        }
        return needRequestDatas;
    }
 
    /**
     * 判断权限是否没有授权..
     * @param permission 权限名称.
     * @return  没授权: true; 授权:false
     */
    @TargetApi(Build.VERSION_CODES.M)
    private boolean checkPermission(String permission) {
        return checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED;
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
 
                boolean isAllPass = true;
                for (int i = 0; i < permissions.length; i++) {
                    final String permissionResoult =  permissions[i];
                    if (isAllPass) {
                        for (String permission : needRPDatas) {
                            if (permissionResoult.equals(permission) && grantResults[i] == PackageManager.PERMISSION_DENIED) {
                                isAllPass = false;
                                break;
                            }
                        }
                    } else {
                        break;
                    }
                }
 
                if(isAllPass) {
                    //初始化
                } else {
                    UtilTools.showToast(this, "请前往设置检查存储权限并打开");
                }
            }
            break;
 
            default:
                break;
        }
    }
 
 
 
    /**
     * 这里用来实现,再点一次返回键退出应用
     * @param keyCode
     * @param event
     * @return
     */
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                UtilTools.showToast(this, "再按一次退出");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
 
 
}