From 765449a5abd433ede055289041fb1ab8ff9c3cb4 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 7 Sep 2024 22:38:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E6=89=93=E5=BC=80=E8=BF=9E=E6=8E=A5=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpremote/DeviceActivity.java | 222 ++++++++++-------- .../com/xypower/mpremote/ImageActivity.java | 90 +++++++ .../com/xypower/mpremote/MainActivity.java | 3 - .../com/xypower/mpremote/utils/AdbUtils.java | 69 ++++++ app/src/main/res/layout/activity_image.xml | 11 +- app/src/main/res/layout/img_list_item.xml | 27 +++ 6 files changed, 319 insertions(+), 103 deletions(-) create mode 100644 app/src/main/java/com/xypower/mpremote/utils/AdbUtils.java create mode 100644 app/src/main/res/layout/img_list_item.xml diff --git a/app/src/main/java/com/xypower/mpremote/DeviceActivity.java b/app/src/main/java/com/xypower/mpremote/DeviceActivity.java index f204246..b7ddc2e 100644 --- a/app/src/main/java/com/xypower/mpremote/DeviceActivity.java +++ b/app/src/main/java/com/xypower/mpremote/DeviceActivity.java @@ -3,18 +3,16 @@ package com.xypower.mpremote; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; +import android.database.Cursor; import android.net.ConnectivityManager; -import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; -import android.telephony.SignalStrength; -import android.telephony.TelephonyManager; -import android.telephony.ims.ImsMmTelManager; import android.text.Html; import android.text.TextUtils; import android.text.format.Formatter; @@ -30,6 +28,7 @@ import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; import com.xypower.mpremote.databinding.ActivityDeviceBinding; import com.xypower.mpremote.databinding.ActivityMainBinding; +import com.xypower.mpremote.utils.AdbUtils; 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_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; // [vendor.ril.nw.signalstrength.lte.1]: [-85,27] @@ -192,8 +196,12 @@ public class DeviceActivity extends AppCompatActivity { } 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) { mHandler.postDelayed(new Runnable() { @Override @@ -222,6 +230,8 @@ public class DeviceActivity extends AppCompatActivity { }); } + Log.d(TAG, "App.json Pulled"); + final int numberOfChannels = (mAppConfig != null) ? mAppConfig.channels : 0; mHandler.post(new Runnable() { @Override @@ -249,7 +259,30 @@ public class DeviceActivity extends AppCompatActivity { } } + Log.d(TAG, "Channel config Pulled"); + 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 { adbShellResponse = adb.shell("getprop"); } catch (Exception ex) { @@ -268,6 +301,8 @@ public class DeviceActivity extends AppCompatActivity { } } + Log.d(TAG, "getprop Finished"); + if (mProps.containsKey(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"; - String cmd = "am start -n " + PACKAGE_NAME_MP + "/" + PACKAGE_NAME_MP + ".BridgeActivity --es action \"query_bv\" --es path \"" + remoteFilePath + "\""; + Log.d(TAG, "Dump App version Finished"); + + String cmd = "content query --uri content://com.xypower.mpapp.provider/queryBatVol"; adbShellResponse = null; try { adbShellResponse = adb.shell(cmd); @@ -301,49 +337,22 @@ public class DeviceActivity extends AppCompatActivity { } if (adbShellResponse.getExitCode() == 0) { - File localFilePath = new File(fileTmp, "bv.txt"); - if (localFilePath.exists()) { - localFilePath.delete(); - } - - for (int idx = 0; idx < 10; idx++) { - - res = pullFile(adb, remoteFilePath, localFilePath); - - 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; + Cursor cursor = AdbUtils.parseContentResponse(adbShellResponse.getAllOutput()); + if (cursor != null && cursor.getCount() > 0) { + cursor.moveToFirst(); + @SuppressLint("Range") String bv = cursor.getString(cursor.getColumnIndex("bv")); + @SuppressLint("Range") String bcv = cursor.getString(cursor.getColumnIndex("bcv")); + + if (!TextUtils.isEmpty(bv)) { + mBatteryVoltage = Integer.parseInt(bv); } - - try { - Thread.sleep(1000); - } catch (Exception ex) { - ex.printStackTrace(); + if (!TextUtils.isEmpty(bcv)) { + mBatteryChargingVoltage = Integer.parseInt(bcv); } } - - try { - adb.shell("rm " + remoteFilePath); - } catch (Exception ex) { - ex.printStackTrace(); - } } + + Log.d(TAG, "queryBatVol Finished"); } })).start(); } @@ -371,76 +380,91 @@ public class DeviceActivity extends AppCompatActivity { @Override 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 { - adbShellResponse = adb.shell(cmd); - } catch (Exception ex) { - ex.printStackTrace(); - } + Log.i(TAG, "Start connecting " + mDeviceIp); + adb = Dadb.create(mDeviceIp, AdbUtils.DEFAULT_ADB_PORT, mAdbKeyPair); + + 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) { - if (adbShellResponse.getExitCode() == 0) { + AdbShellResponse adbShellResponse = null; + try { + adbShellResponse = adb.shell(cmd); + } catch (Exception ex) { + ex.printStackTrace(); + } - if (sleepTime > 0) { - try { - Thread.sleep(sleepTime); - } catch (Exception ex) { + if (adbShellResponse != null) { + if (adbShellResponse.getExitCode() == 0) { + + if (sleepTime > 0) { + try { + Thread.sleep(sleepTime); + } catch (Exception ex) { + } } - } - File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo); - if (!localDevicePath.exists()) { - localDevicePath.mkdirs(); - } + File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo); + if (!localDevicePath.exists()) { + 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() { - @Override - public void run() { - if (photoOrVideo) { - showPhoto(localFilePath.getAbsolutePath()); - } else { - showVideo(localFilePath.getAbsolutePath()); + mHandler.post(new Runnable() { + @Override + public void run() { + if (photoOrVideo) { + showPhoto(localFilePath.getAbsolutePath()); + } else { + showVideo(localFilePath.getAbsolutePath()); + } } - } - }); - break; + }); + break; + } + + try { + Thread.sleep(1000); + } catch (Exception ex) { + ex.printStackTrace(); + } } try { - Thread.sleep(1000); + // mAdb.shell("rm " + remoteFilePath); } catch (Exception ex) { 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); } + 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) { appendKeyValue(stringBuilder, "电池电压", Integer.toString(mBatteryVoltage / 1000) + "." + Integer.toString((mBatteryVoltage % 1000) / 100)); } diff --git a/app/src/main/java/com/xypower/mpremote/ImageActivity.java b/app/src/main/java/com/xypower/mpremote/ImageActivity.java index 149dfe5..d4ad32d 100644 --- a/app/src/main/java/com/xypower/mpremote/ImageActivity.java +++ b/app/src/main/java/com/xypower/mpremote/ImageActivity.java @@ -5,24 +5,38 @@ import android.annotation.SuppressLint; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Color; +import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; 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.Bundle; import android.os.Handler; import android.os.Looper; +import android.text.TextUtils; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; +import android.view.ViewGroup; import android.view.WindowInsets; +import android.widget.SimpleAdapter; +import android.widget.TextView; import com.xypower.mpremote.databinding.ActivityImageBinding; 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. @@ -48,6 +62,12 @@ public class ImageActivity extends AppCompatActivity { private static final int UI_ANIMATION_DELAY = 300; private final Handler mHideHandler = new Handler(Looper.myLooper()); + private String mSerialNo = ""; + + private List mImageFiles = new ArrayList<>(); + private ImagesAdaper mAdapter; + private List> mItems = new ArrayList>(); + /** * 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 @@ -151,4 +171,74 @@ public class ImageActivity extends AppCompatActivity { return drawable; } + protected void loadAllImages() { + File localDevicePath = new File(getFilesDir(), "Photos" + File.separator + mSerialNo); + if (!localDevicePath.exists()) { + localDevicePath.mkdirs(); + } + + // binding.imagesView. + } + + private List> 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(); + + + // 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> 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; + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/xypower/mpremote/MainActivity.java b/app/src/main/java/com/xypower/mpremote/MainActivity.java index 9744794..ddd62d9 100644 --- a/app/src/main/java/com/xypower/mpremote/MainActivity.java +++ b/app/src/main/java/com/xypower/mpremote/MainActivity.java @@ -27,7 +27,6 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiNetworkSpecifier; import android.net.wifi.WifiNetworkSuggestion; -import android.os.Build; import android.os.Bundle; import android.os.Handler; 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 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 Handler mHandler; diff --git a/app/src/main/java/com/xypower/mpremote/utils/AdbUtils.java b/app/src/main/java/com/xypower/mpremote/utils/AdbUtils.java new file mode 100644 index 0000000..026dfe1 --- /dev/null +++ b/app/src/main/java/com/xypower/mpremote/utils/AdbUtils.java @@ -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 columns = new ArrayList<>(); + List 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; + } +} diff --git a/app/src/main/res/layout/activity_image.xml b/app/src/main/res/layout/activity_image.xml index 9d85c46..3d30528 100644 --- a/app/src/main/res/layout/activity_image.xml +++ b/app/src/main/res/layout/activity_image.xml @@ -10,7 +10,7 @@ - \ No newline at end of file diff --git a/app/src/main/res/layout/img_list_item.xml b/app/src/main/res/layout/img_list_item.xml new file mode 100644 index 0000000..4816aa4 --- /dev/null +++ b/app/src/main/res/layout/img_list_item.xml @@ -0,0 +1,27 @@ + + + + + + + + \ No newline at end of file