调整实现

main
Matthew 12 months ago
parent e77821a79e
commit 68e81438dd

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

@ -1,6 +1,7 @@
<resources> <resources>
<string name="app_name">欣影微拍遥控</string> <string name="app_name">欣影微拍遥控</string>
<string name="err_dev_not_found">未发现微拍设备</string> <string name="err_dev_not_found">未发现微拍设备</string>
<string name="err_dev_failed_to_connect">连接设备失败</string>
<string name="title_activity_image"></string> <string name="title_activity_image"></string>
<string name="action_refresh">刷新</string> <string name="action_refresh">刷新</string>
<string name="action_settings">设置</string> <string name="action_settings">设置</string>

Loading…
Cancel
Save