diff --git a/app/src/main/java/com/xypower/mpremote/DeviceActivity.java b/app/src/main/java/com/xypower/mpremote/DeviceActivity.java index bcd15a5..c72d269 100644 --- a/app/src/main/java/com/xypower/mpremote/DeviceActivity.java +++ b/app/src/main/java/com/xypower/mpremote/DeviceActivity.java @@ -3,6 +3,7 @@ package com.xypower.mpremote; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.res.Resources; @@ -61,8 +62,10 @@ public class DeviceActivity extends AppCompatActivity { //[vendor.ril.nw.signalstrength.lte.2]: [-86,27] private ActivityDeviceBinding binding; + private ProgressDialog mProgressDialog; private Handler mHandler; - private Dadb mAdb; + private String mDeviceIp; + private AdbKeyPair mAdbKeyPair; private Map mProps = new HashMap<>(); @@ -80,6 +83,9 @@ public class DeviceActivity extends AppCompatActivity { ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); + Intent intent = getIntent(); + mDeviceIp = intent.getStringExtra("deviceIp"); + mHandler = new Handler(); File file = new File(getFilesDir(), ".keypair"); @@ -98,168 +104,141 @@ public class DeviceActivity extends AppCompatActivity { fileTmp.mkdirs(); } - final AdbKeyPair adbKeyPair = AdbKeyPair.read(priKeyFile, pubKeyFile); + mAdbKeyPair = AdbKeyPair.read(priKeyFile, pubKeyFile); final Context context = getApplicationContext(); (new Thread(new Runnable() { @Override public void run() { - WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); - - String ipAddressByWifi = null; - if (wifiManager != null) { - ipAddressByWifi = Formatter.formatIpAddress(wifiManager.getDhcpInfo().ipAddress); + try { + runImpl(); + } catch (Exception ex) { + ex.printStackTrace(); } - if (ipAddressByWifi.contains(WIFI_IP_PREFIX)) { - + mProgressDialog.dismiss(); + } - String deviceIP = Formatter.formatIpAddress(wifiManager.getDhcpInfo().gateway); - if (DEBUG) { - deviceIP = WIFI_IP_DEVICE; - } + private void runImpl() { + Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); - Socket mSocket = null; - try { - mSocket = new Socket(deviceIP, ADB_SERVER_PORT); - if (mSocket.isConnected()) { - mSocket.close(); + if (adb == null) { + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + Toast.makeText(DeviceActivity.this, R.string.err_dev_failed_to_connect, Toast.LENGTH_LONG).show(); } - } catch (Exception ex) { - ex.printStackTrace(); - } - - Dadb adb = Dadb.discover(deviceIP, adbKeyPair); - - if (adb != null) { - - mAdb = adb; + }, 100); + return; + } - AdbShellResponse adbShellResponse = null; - try { - adbShellResponse = mAdb.shell("getprop"); - } catch (Exception ex) { - ex.printStackTrace(); - } + AdbShellResponse adbShellResponse = null; + try { + adbShellResponse = adb.shell("getprop"); + } catch (Exception ex) { + ex.printStackTrace(); + } - if (adbShellResponse.getExitCode() == 0) { - String[] lines = FileUtils.splitLines(adbShellResponse.getAllOutput()); - for (String line : lines) { - int pos = line.indexOf("]: ["); - if (pos != -1) { - String key = line.substring(1, pos); - String val = line.substring(pos + 4, line.length() - 1); - mProps.put(key, val); - } - } + if (adbShellResponse.getExitCode() == 0) { + String[] lines = FileUtils.splitLines(adbShellResponse.getAllOutput()); + for (String line : lines) { + int pos = line.indexOf("]: ["); + if (pos != -1) { + String key = line.substring(1, pos); + String val = line.substring(pos + 4, line.length() - 1); + mProps.put(key, val); } + } + } + // adb shell pm dump com.xypower.mpremote | grep "versionName" + adbShellResponse = null; + try { + adbShellResponse = adb.shell("pm dump " + PACKAGE_NAME_MP + " | grep \"versionName\""); + } catch (Exception ex) { + ex.printStackTrace(); + } - // adb shell pm dump com.xypower.mpremote | grep "versionName" - adbShellResponse = null; - try { - adbShellResponse = mAdb.shell("pm dump " + PACKAGE_NAME_MP + " | grep \"versionName\""); - } catch (Exception ex) { - ex.printStackTrace(); - } - - if (adbShellResponse.getExitCode() == 0) { - String[] lines = FileUtils.splitLines(adbShellResponse.getAllOutput()); - for (String line : lines) { - int pos = line.indexOf("versionName="); - if (pos != -1) { - String val = line.substring(pos + 12); - mProps.put(KEY_APP_MP_VERSION, val); - } - } + if (adbShellResponse.getExitCode() == 0) { + String[] lines = FileUtils.splitLines(adbShellResponse.getAllOutput()); + for (String line : lines) { + int pos = line.indexOf("versionName="); + if (pos != -1) { + String val = line.substring(pos + 12); + mProps.put(KEY_APP_MP_VERSION, val); } + } + } - String remoteFilePath = REMOTE_PATH_TMP + "bv.txt"; - String cmd = "am start -n " + PACKAGE_NAME_MP + "/" + PACKAGE_NAME_MP + ".BridgeActivity --es action \"query_bv\" --es path \"" + remoteFilePath + "\""; - adbShellResponse = null; - try { - adbShellResponse = mAdb.shell(cmd); - } catch (Exception ex) { - ex.printStackTrace(); - } + String remoteFilePath = REMOTE_PATH_TMP + "bv.txt"; + String cmd = "am start -n " + PACKAGE_NAME_MP + "/" + PACKAGE_NAME_MP + ".BridgeActivity --es action \"query_bv\" --es path \"" + remoteFilePath + "\""; + adbShellResponse = null; + try { + adbShellResponse = adb.shell(cmd); + } catch (Exception ex) { + ex.printStackTrace(); + } - if (adbShellResponse.getExitCode() == 0) { - File localFilePath = new File(fileTmp, "bv.txt"); - if (localFilePath.exists()) { - localFilePath.delete(); - } + if (adbShellResponse.getExitCode() == 0) { + File localFilePath = new File(fileTmp, "bv.txt"); + if (localFilePath.exists()) { + localFilePath.delete(); + } - for (int idx = 0; idx < 10; idx++) { + for (int idx = 0; idx < 10; idx++) { - boolean res = pullFile(mAdb, remoteFilePath, localFilePath); + boolean res = pullFile(adb, remoteFilePath, localFilePath); - if (res) { - String content = FileUtils.readTextFile(localFilePath.getAbsolutePath()); - if (!TextUtils.isEmpty(content)) { - int pos = content.indexOf(" "); - if (pos != -1) { - String bv = content.substring(0, pos); - String bcv = content.substring(pos + 1); + if (res) { + String content = FileUtils.readTextFile(localFilePath.getAbsolutePath()); + if (!TextUtils.isEmpty(content)) { + int pos = content.indexOf(" "); + if (pos != -1) { + String bv = content.substring(0, pos); + String bcv = content.substring(pos + 1); - if (!TextUtils.isEmpty(bv)) { - mBatteryVoltage = Integer.parseInt(bv); - } - if (!TextUtils.isEmpty(bcv)) { - mBatteryChargingVoltage = Integer.parseInt(bcv); - } - } + if (!TextUtils.isEmpty(bv)) { + mBatteryVoltage = Integer.parseInt(bv); + } + if (!TextUtils.isEmpty(bcv)) { + mBatteryChargingVoltage = Integer.parseInt(bcv); } - - localFilePath.delete(); - break; - } - - try { - Thread.sleep(1000); - } catch (Exception ex) { - ex.printStackTrace(); } } - try { - mAdb.shell("rm " + remoteFilePath); - } catch (Exception ex) { - ex.printStackTrace(); - } + localFilePath.delete(); + break; } - File appConfigFile = new File(fileTmp, "App.json"); - boolean res = pullFile(adb, REMOTE_PATH_DATA + "App.json", appConfigFile); - if (res) { - - final MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(context, appConfigFile.getAbsolutePath()); - mAppConfig = appConfig; - - DeviceActivity.this.mHandler.post(new Runnable() { - @Override - public void run() { - DeviceActivity.this.showAppInfo(); - } - }); + try { + Thread.sleep(1000); + } catch (Exception ex) { + ex.printStackTrace(); } - - } else { - mHandler.post(new Runnable() { - @Override - public void run() { - Toast.makeText(DeviceActivity.this, R.string.err_dev_not_found, Toast.LENGTH_LONG).show(); - } - }); } + try { + adb.shell("rm " + remoteFilePath); + } catch (Exception ex) { + ex.printStackTrace(); + } } - // Dadb dadb = Dadb.create("localhost", 5555, adbKeyPair); - // Dadb.discover(); - + File appConfigFile = new File(fileTmp, "App.json"); + boolean res = pullFile(adb, REMOTE_PATH_DATA + "App.json", appConfigFile); + if (res) { + final MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(context, appConfigFile.getAbsolutePath()); + mAppConfig = appConfig; + DeviceActivity.this.mHandler.post(new Runnable() { + @Override + public void run() { + DeviceActivity.this.showAppInfo(); + } + }); + } } })).start(); @@ -277,6 +256,7 @@ public class DeviceActivity extends AppCompatActivity { } }); + mProgressDialog = ProgressDialog.show(context, "", "连接中", true); } @@ -302,16 +282,22 @@ public class DeviceActivity extends AppCompatActivity { Thread th = new Thread(new Runnable() { @Override public void run() { - if (sleepTime > 0) { - try { - Thread.sleep(sleepTime); - } catch (Exception ex) { - } + + Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); + + if (adb == null) { + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + Toast.makeText(DeviceActivity.this, R.string.err_dev_failed_to_connect, Toast.LENGTH_LONG).show(); + } + }, 100); + return; } AdbShellResponse adbShellResponse = null; try { - adbShellResponse = mAdb.shell(cmd); + adbShellResponse = adb.shell(cmd); } catch (Exception ex) { ex.printStackTrace(); } @@ -319,11 +305,18 @@ public class DeviceActivity extends AppCompatActivity { if (adbShellResponse != null) { if (adbShellResponse.getExitCode() == 0) { + if (sleepTime > 0) { + try { + Thread.sleep(sleepTime); + } catch (Exception ex) { + } + } + File localFilePath = new File(getFilesDir(), localFileName); for (int idx = 0; idx < 10; idx++) { - boolean res = pullFile(mAdb, remoteFilePath, localFilePath); + boolean res = pullFile(adb, remoteFilePath, localFilePath); if (res) { @@ -362,9 +355,6 @@ public class DeviceActivity extends AppCompatActivity { } protected long takePhoto(int channel, int preset, final boolean photoOrVideo) { - if (mAdb == null) { - return 0; - } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); Date dt = new Date(); @@ -393,9 +383,6 @@ public class DeviceActivity extends AppCompatActivity { } protected long takeVideo(int channel, int preset, final boolean photoOrVideo) { - if (mAdb == null) { - return 0; - } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); Date dt = new Date(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0529913..53342a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,7 @@ 欣影微拍遥控 未发现微拍设备 + 连接设备失败 刷新 设置