纠正错误的打开连接的方法

main
Matthew 9 months ago
parent b672bc0a98
commit 765449a5ab

@ -3,18 +3,16 @@ 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.annotation.SuppressLint;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.ims.ImsMmTelManager;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.Formatter; import android.text.format.Formatter;
@ -30,6 +28,7 @@ import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
import com.xypower.mpremote.databinding.ActivityDeviceBinding; import com.xypower.mpremote.databinding.ActivityDeviceBinding;
import com.xypower.mpremote.databinding.ActivityMainBinding; import com.xypower.mpremote.databinding.ActivityMainBinding;
import com.xypower.mpremote.utils.AdbUtils;
import org.json.JSONObject; import org.json.JSONObject;
@ -87,6 +86,11 @@ public class DeviceActivity extends AppCompatActivity {
private static final String KEY_APP_MP_VERSION = "mpapp.version"; private static final String KEY_APP_MP_VERSION = "mpapp.version";
private static final String KEY_PREFIX_GLOBAL_SETTING = "settings.global.";
private static final String KEY_IMEI = KEY_PREFIX_GLOBAL_SETTING + "dev_imei";
private static final String KEY_IMEI2 = KEY_PREFIX_GLOBAL_SETTING + "dev_imei2";
private static final int ADB_SERVER_PORT = 5555; private static final int ADB_SERVER_PORT = 5555;
// [vendor.ril.nw.signalstrength.lte.1]: [-85,27] // [vendor.ril.nw.signalstrength.lte.1]: [-85,27]
@ -192,8 +196,12 @@ public class DeviceActivity extends AppCompatActivity {
} }
private void runImpl() { private void runImpl() {
Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); Log.d(TAG, "Adb discovering " + mDeviceIp);
// Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair);
Dadb adb = Dadb.create(mDeviceIp, AdbUtils.DEFAULT_ADB_PORT, mAdbKeyPair);
Log.d(TAG, "Adb Connected");
if (adb == null) { if (adb == null) {
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {
@Override @Override
@ -222,6 +230,8 @@ public class DeviceActivity extends AppCompatActivity {
}); });
} }
Log.d(TAG, "App.json Pulled");
final int numberOfChannels = (mAppConfig != null) ? mAppConfig.channels : 0; final int numberOfChannels = (mAppConfig != null) ? mAppConfig.channels : 0;
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@Override @Override
@ -249,7 +259,30 @@ public class DeviceActivity extends AppCompatActivity {
} }
} }
Log.d(TAG, "Channel config Pulled");
AdbShellResponse adbShellResponse = null; AdbShellResponse adbShellResponse = null;
try {
adbShellResponse = adb.shell("settings list global");
} catch (Exception ex) {
ex.printStackTrace();
}
if (adbShellResponse.getExitCode() == 0) {
String[] lines = FilesUtils.splitLines(adbShellResponse.getAllOutput());
for (String line : lines) {
int pos = line.indexOf("=");
if (pos != -1) {
String key = line.substring(0, pos);
String val = line.substring(pos + 1);
mProps.put(KEY_PREFIX_GLOBAL_SETTING + key, val);
}
}
}
Log.d(TAG, "Global settings Pulled");
adbShellResponse = null;
try { try {
adbShellResponse = adb.shell("getprop"); adbShellResponse = adb.shell("getprop");
} catch (Exception ex) { } catch (Exception ex) {
@ -268,6 +301,8 @@ public class DeviceActivity extends AppCompatActivity {
} }
} }
Log.d(TAG, "getprop Finished");
if (mProps.containsKey(KEY_RO_SERIALNO)) { if (mProps.containsKey(KEY_RO_SERIALNO)) {
mSerialNo = (String)mProps.get(KEY_RO_SERIALNO); mSerialNo = (String)mProps.get(KEY_RO_SERIALNO);
} }
@ -291,8 +326,9 @@ public class DeviceActivity extends AppCompatActivity {
} }
} }
String remoteFilePath = REMOTE_PATH_TMP + "bv.txt"; Log.d(TAG, "Dump App version Finished");
String cmd = "am start -n " + PACKAGE_NAME_MP + "/" + PACKAGE_NAME_MP + ".BridgeActivity --es action \"query_bv\" --es path \"" + remoteFilePath + "\"";
String cmd = "content query --uri content://com.xypower.mpapp.provider/queryBatVol";
adbShellResponse = null; adbShellResponse = null;
try { try {
adbShellResponse = adb.shell(cmd); adbShellResponse = adb.shell(cmd);
@ -301,49 +337,22 @@ public class DeviceActivity extends AppCompatActivity {
} }
if (adbShellResponse.getExitCode() == 0) { if (adbShellResponse.getExitCode() == 0) {
File localFilePath = new File(fileTmp, "bv.txt"); Cursor cursor = AdbUtils.parseContentResponse(adbShellResponse.getAllOutput());
if (localFilePath.exists()) { if (cursor != null && cursor.getCount() > 0) {
localFilePath.delete(); cursor.moveToFirst();
} @SuppressLint("Range") String bv = cursor.getString(cursor.getColumnIndex("bv"));
@SuppressLint("Range") String bcv = cursor.getString(cursor.getColumnIndex("bcv"));
for (int idx = 0; idx < 10; idx++) {
if (!TextUtils.isEmpty(bv)) {
res = pullFile(adb, remoteFilePath, localFilePath); mBatteryVoltage = Integer.parseInt(bv);
if (res) {
String content = FilesUtils.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);
}
}
}
localFilePath.delete();
break;
} }
if (!TextUtils.isEmpty(bcv)) {
try { mBatteryChargingVoltage = Integer.parseInt(bcv);
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
} }
} }
try {
adb.shell("rm " + remoteFilePath);
} catch (Exception ex) {
ex.printStackTrace();
}
} }
Log.d(TAG, "queryBatVol Finished");
} }
})).start(); })).start();
} }
@ -371,76 +380,91 @@ public class DeviceActivity extends AppCompatActivity {
@Override @Override
public void run() { public void run() {
Dadb adb = Dadb.discover(mDeviceIp, mAdbKeyPair); Dadb adb = null;
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 { try {
adbShellResponse = adb.shell(cmd); Log.i(TAG, "Start connecting " + mDeviceIp);
} catch (Exception ex) { adb = Dadb.create(mDeviceIp, AdbUtils.DEFAULT_ADB_PORT, mAdbKeyPair);
ex.printStackTrace();
} Log.i(TAG, "Finish connecting " + mDeviceIp);
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;
}
if (adbShellResponse != null) { AdbShellResponse adbShellResponse = null;
if (adbShellResponse.getExitCode() == 0) { try {
adbShellResponse = adb.shell(cmd);
} catch (Exception ex) {
ex.printStackTrace();
}
if (sleepTime > 0) { if (adbShellResponse != null) {
try { if (adbShellResponse.getExitCode() == 0) {
Thread.sleep(sleepTime);
} catch (Exception ex) { if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (Exception ex) {
}
} }
}
File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo); File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo);
if (!localDevicePath.exists()) { if (!localDevicePath.exists()) {
localDevicePath.mkdirs(); localDevicePath.mkdirs();
} }
final File localFilePath = new File(localDevicePath, localFileName); final File localFilePath = new File(localDevicePath, localFileName);
for (int idx = 0; idx < 10; idx++) { for (int idx = 0; idx < 10; idx++) {
boolean res = pullFile(adb, remoteFilePath, localFilePath); boolean res = pullFile(adb, remoteFilePath, localFilePath);
if (res) { if (res) {
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (photoOrVideo) { if (photoOrVideo) {
showPhoto(localFilePath.getAbsolutePath()); showPhoto(localFilePath.getAbsolutePath());
} else { } else {
showVideo(localFilePath.getAbsolutePath()); showVideo(localFilePath.getAbsolutePath());
}
} }
} });
}); break;
break; }
try {
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
}
} }
try { try {
Thread.sleep(1000); // mAdb.shell("rm " + remoteFilePath);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
}
try {
// mAdb.shell("rm " + remoteFilePath);
} catch (Exception ex) {
ex.printStackTrace();
} }
}
} finally {
try {
if (adb != null) {
adb.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} }
} }
} }
}); });
@ -678,6 +702,16 @@ public class DeviceActivity extends AppCompatActivity {
appendKeyValue(stringBuilder, "ICCID", iccid); appendKeyValue(stringBuilder, "ICCID", iccid);
} }
if (mProps.containsKey(KEY_IMEI)) {
String val = (String)mProps.get(KEY_IMEI);
appendKeyValue(stringBuilder, "IMEI1", val);
}
if (mProps.containsKey(KEY_IMEI2)) {
String val = (String)mProps.get(KEY_IMEI2);
appendKeyValue(stringBuilder, "IMEI2", val);
}
if (mBatteryVoltage != -1) { if (mBatteryVoltage != -1) {
appendKeyValue(stringBuilder, "电池电压", Integer.toString(mBatteryVoltage / 1000) + "." + Integer.toString((mBatteryVoltage % 1000) / 100)); appendKeyValue(stringBuilder, "电池电压", Integer.toString(mBatteryVoltage / 1000) + "." + Integer.toString((mBatteryVoltage % 1000) / 100));
} }

@ -5,24 +5,38 @@ import android.annotation.SuppressLint;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.text.TextUtils;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets; import android.view.WindowInsets;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.xypower.mpremote.databinding.ActivityImageBinding; import com.xypower.mpremote.databinding.ActivityImageBinding;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* An example full-screen activity that shows and hides the system UI (i.e. * An example full-screen activity that shows and hides the system UI (i.e.
@ -48,6 +62,12 @@ public class ImageActivity extends AppCompatActivity {
private static final int UI_ANIMATION_DELAY = 300; private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler(Looper.myLooper()); private final Handler mHideHandler = new Handler(Looper.myLooper());
private String mSerialNo = "";
private List<File> mImageFiles = new ArrayList<>();
private ImagesAdaper mAdapter;
private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();
/** /**
* Touch listener to use for in-layout UI controls to delay hiding the * Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away * system UI. This is to prevent the jarring behavior of controls going away
@ -151,4 +171,74 @@ public class ImageActivity extends AppCompatActivity {
return drawable; return drawable;
} }
protected void loadAllImages() {
File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo);
if (!localDevicePath.exists()) {
localDevicePath.mkdirs();
}
// binding.imagesView.
}
private List<Map<String, Object>> getData() {
mItems.clear();
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = "";
if (wifiInfo != null) {
ssid = wifiInfo.getSSID();
if (!TextUtils.isEmpty(ssid)) {
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length() - 1);
}
}
}
for (File file : mImageFiles) {
Map map = new HashMap<String, Object>();
// Bitmap bm =
map.put("img", null);
// if (scanR)
map.put("text", file.getName());
mItems.add(map);
}
return mItems;
}
private void refreshListView() {
mAdapter = new ImagesAdaper(this, getData(), R.layout.list_item, new String[] { "img", "text" },
new int[] { R.id.id_img, R.id.id_text });
binding.imagesView.setAdapter(mAdapter);
}
public class ImagesAdaper extends SimpleAdapter {
public ImagesAdaper(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent){
convertView = super.getView(position, convertView, parent);
TextView textView = (TextView)convertView.findViewById(R.id.id_channel);
File file = mImageFiles.get(position);
// if (scanResult.)
String text = (String)mItems.get(position).get("text");
if (text.startsWith("XY") || text.startsWith("xy")) {
textView.setTextColor(Color.RED);
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
} else {
textView.setTextColor(Color.BLACK);
textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
}
return convertView;
}
}
} }

@ -27,7 +27,6 @@ import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.net.wifi.WifiNetworkSpecifier; import android.net.wifi.WifiNetworkSpecifier;
import android.net.wifi.WifiNetworkSuggestion; import android.net.wifi.WifiNetworkSuggestion;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Parcelable; import android.os.Parcelable;
@ -64,8 +63,6 @@ public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SETTINGS = SettingsActivity.REQUEST_CODE_SETTINGS; private static final int REQUEST_CODE_SETTINGS = SettingsActivity.REQUEST_CODE_SETTINGS;
private static final String WIFI_IP_PREFIX = "192.168."; private static final String WIFI_IP_PREFIX = "192.168.";
// private static final String WIFI_IP_DEVIDE = "192.168.50.1";
private static final String WIFI_IP_DEVICE = "192.168.50.92";
private ActivityMainBinding binding; private ActivityMainBinding binding;
private Handler mHandler; private Handler mHandler;

@ -0,0 +1,69 @@
package com.xypower.mpremote.utils;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.text.TextUtils;
import com.xypower.common.FilesUtils;
import java.util.ArrayList;
import java.util.List;
public class AdbUtils {
public final static int DEFAULT_ADB_PORT = 5555;
public static Cursor parseContentResponse(String output) {
if (TextUtils.isEmpty(output) || output.startsWith("No result found")) {
return null;
}
String[] rows = FilesUtils.splitLines(output);
if (rows == null) {
return null;
}
MatrixCursor cursor = null;
boolean firstRow = true;
List<String> columns = new ArrayList<>();
List<String> values = new ArrayList<>();
for (String row : rows) {
int pos = row.indexOf(' ', 5);
if (pos == -1) {
continue;
}
values.clear();
String rowContent = row.substring(pos + 1);
String[] fields = TextUtils.split(rowContent, ", ");
if (fields == null || fields.length == 0) {
continue;
}
for (String field : fields) {
pos = field.indexOf('=');
if (firstRow) {
columns.add(field.substring(0, pos));
}
values.add(field.substring(pos + 1));
}
if (firstRow) {
String[] cs = new String[columns.size()];
columns.toArray(cs);
cursor = new MatrixCursor(cs);
firstRow = false;
}
cursor.addRow(values);
}
return cursor;
}
}

@ -10,7 +10,7 @@
<ImageView <ImageView
android:id="@+id/imageView" android:id="@+id/imageView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:scaleType="fitStart" android:scaleType="fitStart"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
@ -19,14 +19,13 @@
app:srcCompat="@android:drawable/screen_background_light_transparent" /> app:srcCompat="@android:drawable/screen_background_light_transparent" />
<ListView <ListView
android:id="@+id/imagesView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
/> />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/id_channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="2dp"
android:text="" />
<TextView
android:id="@+id/id_datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="2dp"
android:text="" />
</LinearLayout>
Loading…
Cancel
Save