| | |
| | | import android.widget.Toast; |
| | | |
| | | import com.fwupgrade.saymanss.adapter.FwFileListAdapter; |
| | | import com.fwupgrade.saymanss.deviceplug.CameraControlTransfer; |
| | | 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.NativeMethods; |
| | | import com.jni.UStorageTestModule; |
| | | import com.spca.usb.CUSBListener; |
| | | |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | |
| | | 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"; |
| | |
| | | protected TextView mTitle; |
| | | /** 返回 */ |
| | | protected TextView mAppVersion; |
| | | /** LoadingView */ |
| | | private LoadingView loadingView; |
| | | |
| | | /** 升级 */ |
| | | private TextView mFwUpDataBtn; |
| | | /** 提示 */ |
| | |
| | | /** fw list */ |
| | | protected ListView mListView; |
| | | |
| | | private TextView mDeviceFwVersion; |
| | | private TextView mFileFwVersion; |
| | | |
| | | private File[] mFileList; |
| | | |
| | | private HashMap<String, Integer> tty = new HashMap<>(); |
| | | private ArrayList<CameraControlTransfer> upgradeList = new ArrayList<>(); |
| | | private volatile boolean upgrading = false; |
| | | |
| | | private int mUpgradeSuccessNum = 0; |
| | | private int mUpgradeFailNum = 0; |
| | | private int mDevicesNum = 0; |
| | | |
| | | private Handler mHander = new Handler() { |
| | | @Override |
| | |
| | | mFwUpDataBtn.setText("升级失败"); |
| | | } |
| | | }*/ |
| | | String devName = (String) msg.obj; |
| | | int errCode = msg.arg1; |
| | | int succ_num = 0, fail_num = 0; |
| | | Log.d(TAG, "devicesNum:" + mDevicesNum + " mUpgradeSuccessNum:" + mUpgradeSuccessNum + " mUpgradeFailNum:" + mUpgradeFailNum); |
| | | |
| | | 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()) { |
| | | if (mUpgradeFailNum + mUpgradeSuccessNum == mDevicesNum) { |
| | | Log.d(TAG, "Upgrade Finish!"); |
| | | |
| | | Intent intent = new Intent(); |
| | | intent.putExtra("result", succ_num == tty.size()? 0 : 1); |
| | | intent.putExtra("result", mUpgradeSuccessNum == mDevicesNum? 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.putExtra("result", mUpgradeSuccessNum == mDevicesNum? 0 : 1); |
| | | intent.setAction("com.fwupgrade.saymanss.UPGRADE_COMPLETE"); |
| | | intent.setPackage("demo1.tech.anyun.com.myapplication"); |
| | | sendBroadcast(intent); |
| | |
| | | |
| | | initView(); |
| | | checkPermission(); |
| | | initTarget(); |
| | | initData(); |
| | | acceptFwList(); |
| | | initListener(); |
| | | initDeviceSDK(); |
| | | UStorageTestModule.getInstance().initUStorageDeviceModuleWithVirtualDisk(AppPathInfo.getLogPath()); |
| | | } |
| | | |
| | | |
| | |
| | | //顶部条 |
| | | 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); |
| | | mDeviceFwVersion = (TextView) findViewById(R.id.device_fw_version); |
| | | mFileFwVersion = (TextView) findViewById(R.id.file_fw_version); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化数据 |
| | |
| | | */ |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | @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(); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | 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) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void permissionFinish(boolean isSuccessful, CUSBListener.UsbControlBlock ctrlBlock) { |
| | | public void permissionFinish(boolean isSuccessful, com.fwupgrade.saymanss.deviceplug.CUSBListener.UsbControlBlock ctrlBlock, CameraControlTransfer cct) { |
| | | 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); |
| | | } |
| | | mDevicesNum++; |
| | | upgradeList.add(cct); |
| | | upgradeNext(); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 初始化升级环境 |
| | | */ |
| | | 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 |
| | |
| | | return errCode; |
| | | } |
| | | |
| | | public void onProgress(float progress) { |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | mFwUpDataBtn.setText(String.format("Progress: %.4f%%", progress * 100)); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void upgradeNext() { |
| | | if (upgradeList.isEmpty()) { |
| | | Log.d(TAG, "upgrade complete!"); |
| | | mHander.sendEmptyMessage(USB_DEVICE_UPGRADE); |
| | | } |
| | | |
| | | if (!upgrading && upgradeList.size() > 0) { |
| | | upgrading = true; |
| | | CameraControlTransfer cct = upgradeList.get(0); |
| | | |
| | | new Thread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (!cct.bPermission) { |
| | | return; |
| | | } |
| | | Log.d(TAG,"UpdateCameraInfo->dev" + cct.dev.getDeviceName()); |
| | | boolean bClaim = cct.connection.claimInterface(cct.dev.getInterface(0), true); |
| | | //devInfo.connection.releaseInterface(devInfo.dev.getInterface(0)); |
| | | Log.d(TAG,"UpdateCameraInfo->bClaim" + String.valueOf(bClaim)); |
| | | |
| | | Log.d(TAG, "Start init"); |
| | | boolean r = NativeMethods.getInstance().Initialize(cct); |
| | | if (r) { |
| | | Log.d(TAG, "Initialise success!"); |
| | | byte data[] = new byte[100]; |
| | | java.util.Arrays.fill(data, (byte) 0); |
| | | if (NativeMethods.getInstance().GetProduct(data, 100)) { |
| | | if (data[0] != 0xFF) { |
| | | String devName = new String(data).trim(); |
| | | |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | mFileFwVersion.setText("Device Name: " + devName + ", " + cct.dev.getDeviceName()); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | java.util.Arrays.fill(data, (byte) 0); |
| | | |
| | | if (NativeMethods.getInstance().GetFwVersion(data, 100, true)) { |
| | | if (data[0] != 0xFF) { |
| | | String fwVersion = new String(data).trim(); |
| | | |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | mDeviceFwVersion.setText("Fireware Name: " + fwVersion); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | final NativeMethods.SERIAL_FLASH_TYPE sft = NativeMethods.SERIAL_FLASH_TYPE.SFT_UNKNOW; |
| | | if(!NativeMethods.getInstance().GetSerialFlashType(sft, true)){ |
| | | Log.i("test", "GetSerialFlashType failed"); |
| | | } |
| | | sft.setValue(NativeMethods.SERIAL_FLASH_TYPE.SFT_MXIC.ordinal()); |
| | | Log.i("test", "sft= " + sft.getValue()); |
| | | |
| | | if(sft.getValue() == 0){ |
| | | //return; |
| | | Log.i("test", "set sft= " + NativeMethods.SERIAL_FLASH_TYPE.SFT_MXIC.toString()); |
| | | sft.setValue(NativeMethods.SERIAL_FLASH_TYPE.SFT_MXIC.ordinal()); |
| | | } |
| | | |
| | | if (mFileList != null && mFileList.length > 0) { |
| | | try { |
| | | String path = mFileList[0].getAbsolutePath(); |
| | | |
| | | NativeMethods.ASIC_ROM_TYPE romType = NativeMethods.ASIC_ROM_TYPE.ART_Unknow; |
| | | int chipID[] = new int[1]; |
| | | if (!NativeMethods.getInstance().GetAsicRomType(romType, chipID)) { |
| | | throw new Exception("get chip rom type failed!"); |
| | | } |
| | | Log.d("RomType", String.valueOf(chipID[0])); |
| | | FileInputStream fis = new FileInputStream(path); |
| | | fis.available(); |
| | | byte[] buffer = new byte[7]; |
| | | |
| | | if (fis.read(buffer, 0, 7) != -1) { |
| | | if (buffer[5] < 48 || buffer[5] >= 65 || buffer[6] < 48 || buffer[6] >= 65) { |
| | | throw new Exception("The selected firmware and device do not match!"); |
| | | } |
| | | int id = (buffer[5] - 48) * 16 + (buffer[6] - 48); |
| | | if (id != chipID[0]) { |
| | | throw new Exception("The selected firmware and device do not match!"); |
| | | } |
| | | } |
| | | Log.d(TAG, "BurnFw start..."); |
| | | if (!NativeMethods.getInstance().BurnerFW(HomePageActivity.this, path, sft, true)) { |
| | | mUpgradeFailNum++; |
| | | Log.d(TAG, "BurnFw fail!!!"); |
| | | } else { |
| | | mUpgradeSuccessNum++; |
| | | Log.d(TAG, "BurnFw success!!!"); |
| | | } |
| | | fis.close(); |
| | | SystemClock.sleep(500); |
| | | NativeMethods.getInstance().RestartDevice(); |
| | | } catch (Exception ex) { |
| | | Log.e(TAG, "upgrade fail: " + ex.getMessage()); |
| | | } |
| | | } else { |
| | | Log.d(TAG, "upgrade file is empty!"); |
| | | } |
| | | NativeMethods.getInstance().UnInitialize(); |
| | | } |
| | | upgradeList.remove(cct); |
| | | upgrading = false; |
| | | |
| | | SystemClock.sleep(1000); |
| | | |
| | | upgradeNext(); |
| | | } |
| | | }).start(); |
| | | } |
| | | } |
| | | |
| | | // -----------------------------------------------权限申请 相关------------------------------------------ |
| | | /** |