This commit is contained in:
wenyifan
2024-05-20 18:11:06 +08:00
commit 1cca3f816b
23 changed files with 841 additions and 0 deletions

2
app/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/build
/release

38
app/build.gradle Normal file
View File

@ -0,0 +1,38 @@
plugins {
id 'com.android.application'
}
android {
namespace 'run.evan.chargetool'
compileSdk 34
defaultConfig {
applicationId "run.evan.chargetool"
minSdk 33
targetSdk 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
viewBinding = true
aidl true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.annotation:annotation:1.3.0'
}

21
app/proguard-rules.pro vendored Normal file
View File

@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@ -0,0 +1,26 @@
package run.evan.chargetool;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("run.evan.chargetool", appContext.getPackageName());
}
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:sharedUserId="android.uid.system"
>
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Light"
tools:targetApi="31"
android:process="system">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -0,0 +1,222 @@
package run.evan.chargetool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Timer;
import java.util.TimerTask;
import run.evan.chargetool.databinding.MainActivityBinding;
public class MainActivity extends Activity {
private static final int CHARGE_FULL_DESIGN_MI14P = 4880000;
private MainActivityBinding binding;
Object IMiChargeInstance;
Method setNightChargeStatus;
Method getNightChargeStatus;
Method getUsbVoltage;
Method getUsbCurrent;
Method getBatteryCycleCount;
Method getInputSuspendState;
Method setInputSuspendState;
Method getBatteryChargeFull;
Method getBatteryCapacity;
Method getBatteryAuthentic;
Method getTxAdapt;
Method getBtTransferStartState;
Method getBatteryIbat;
Method getBatteryVbat;
Method getBatteryTbat;
Method getMiChargePath;
Method setMiChargePath;
Method getBatterySoh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = MainActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
try {
Class IMiCharge = Class.forName("miui.util.IMiCharge");
Method getInstance = IMiCharge.getDeclaredMethod("getInstance");
getInstance.setAccessible(true);
IMiChargeInstance = getInstance.invoke(null);
getNightChargeStatus = IMiCharge.getDeclaredMethod("getNightChargingState");
getNightChargeStatus.setAccessible(true);
setNightChargeStatus = IMiCharge.getDeclaredMethod("setNightChargingState", int.class);
setNightChargeStatus.setAccessible(true);
getUsbVoltage = IMiCharge.getDeclaredMethod("getUsbVoltage");
getUsbVoltage.setAccessible(true);
getUsbCurrent = IMiCharge.getDeclaredMethod("getUsbCurrent");
getUsbCurrent.setAccessible(true);
getBatteryCycleCount = IMiCharge.getDeclaredMethod("getBatteryCycleCount");
getBatteryCycleCount.setAccessible(true);
getInputSuspendState = IMiCharge.getDeclaredMethod("getInputSuspendState");
getInputSuspendState.setAccessible(true);
setInputSuspendState = IMiCharge.getDeclaredMethod("setInputSuspendState", String.class);
setInputSuspendState.setAccessible(true);
getBatteryChargeFull = IMiCharge.getDeclaredMethod("getBatteryChargeFull");
getBatteryChargeFull.setAccessible(true);
getBatteryCapacity = IMiCharge.getDeclaredMethod("getBatteryCapacity");
getBatteryCapacity.setAccessible(true);
getBatteryAuthentic = IMiCharge.getDeclaredMethod("getBatteryAuthentic");
getBatteryAuthentic.setAccessible(true);
getTxAdapt = IMiCharge.getDeclaredMethod("getTxAdapt");
getTxAdapt.setAccessible(true);
getBtTransferStartState = IMiCharge.getDeclaredMethod("getBtTransferStartState");
getBtTransferStartState.setAccessible(true);
getBatteryIbat = IMiCharge.getDeclaredMethod("getBatteryIbat");
getBatteryIbat.setAccessible(true);
getBatteryVbat = IMiCharge.getDeclaredMethod("getBatteryVbat");
getBatteryVbat.setAccessible(true);
getBatteryTbat = IMiCharge.getDeclaredMethod("getBatteryTbat");
getBatteryTbat.setAccessible(true);
getMiChargePath = IMiCharge.getDeclaredMethod("getMiChargePath",String.class);
getMiChargePath.setAccessible(true);
setMiChargePath = IMiCharge.getDeclaredMethod("setMiChargePath",String.class,String.class);
setMiChargePath.setAccessible(true);
getBatterySoh = IMiCharge.getDeclaredMethod("getBatterySoh");
getBatterySoh.setAccessible(true);
} catch (Exception e) {
binding.textView.setText(e.toString());
}
refresh();
binding.switchStopCharge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = binding.switchStopCharge.isChecked();
try {
Boolean invoke = (Boolean) setMiChargePath.invoke(IMiChargeInstance, "smart_chg",checked ? "0x3" : "0x2");
if (invoke.booleanValue()) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
binding.textView.setText(e.toString());
}
}
});
binding.switchDisableUsb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = binding.switchDisableUsb.isChecked();
try {
Boolean invoke = (Boolean) setInputSuspendState.invoke(IMiChargeInstance, checked ? "1" : "0");
if (invoke.booleanValue()) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
binding.textView.setText(e.toString());
}
}
});
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
try {
BigDecimal voltage = new BigDecimal((String) getBatteryVbat.invoke(IMiChargeInstance));
BigDecimal current = new BigDecimal((String) getBatteryIbat.invoke(IMiChargeInstance));
BigDecimal v = voltage.divide(BigDecimal.valueOf(1000 * 1000), 4, RoundingMode.CEILING);
BigDecimal a = current.divide(BigDecimal.valueOf(1000 * 1000), 4, RoundingMode.CEILING);
binding.textBat.setText("Battery V=" + v.stripTrailingZeros().toPlainString() + "V I=" + a.stripTrailingZeros().toPlainString() + "A");
binding.textPower.setText("Battery Power=" + v.multiply(a).stripTrailingZeros().toPlainString() + "W");
float temp = Integer.parseInt((String) getBatteryTbat.invoke(IMiChargeInstance)) / 10f;
binding.textTemp.setText("Battery Temp=" + temp);
} catch (Exception e) {
}
try {
BigDecimal voltage = new BigDecimal((String) getUsbVoltage.invoke(IMiChargeInstance));
BigDecimal current = new BigDecimal((String) getUsbCurrent.invoke(IMiChargeInstance));
BigDecimal v = voltage.divide(BigDecimal.valueOf(1000 * 1000), 4, RoundingMode.CEILING);
BigDecimal a = current.divide(BigDecimal.valueOf(1000 * 1000), 4, RoundingMode.CEILING);
binding.textUsb.setText("USB V=" + v.stripTrailingZeros().toPlainString() + "V I=" + a.stripTrailingZeros().toPlainString() + "A");
binding.textUsbPower.setText("USB Power=" + v.multiply(a).stripTrailingZeros().toPlainString() + "W");
} catch (Exception e) {
}
}
};
timer.schedule(task, 0, 1000);
}
@Override
protected void onResume() {
super.onResume();
refresh();
}
private void refresh() {
try {
String suspendResult = (String) getInputSuspendState.invoke(IMiChargeInstance);
if ("1".equals(suspendResult)) {
binding.switchDisableUsb.setChecked(true);
} else {
binding.switchDisableUsb.setChecked(false);
}
String pathResult = (String) getMiChargePath.invoke(IMiChargeInstance,"smart_chg");
//binding.textView.setText(pathResult);
if ("2".equals(pathResult) || "6".equals(pathResult)) {
binding.switchStopCharge.setChecked(true);
} else {
binding.switchStopCharge.setChecked(false);
}
binding.textCycle.setText("Cycle Count=" + getBatteryCycleCount.invoke(IMiChargeInstance).toString());
int chargeFull = Integer.parseInt(getBatteryChargeFull.invoke(IMiChargeInstance).toString());
String life = BigDecimal.valueOf(chargeFull).divide(BigDecimal.valueOf(CHARGE_FULL_DESIGN_MI14P),4,RoundingMode.FLOOR).multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toPlainString();
binding.textLife.setText("Battery Life: " + (chargeFull/1000) + "mAh / " + (CHARGE_FULL_DESIGN_MI14P/1000) + "mAh = " + life + "%");
} catch (Exception e) {
binding.textView.setText(e.toString());
}
}
}

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/text_bat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/text_power"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/text_temp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/text_usb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/text_usb_power"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/text_cycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<TextView
android:id="@+id/text_life"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView" />
<Switch
android:id="@+id/switch_disable_usb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/switch_disable_usb" />
<Switch
android:id="@+id/switch_stop_charge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/switch_stop_charge" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,9 @@
<resources>
<string name="app_name">Charge Tool</string>
<string name="switch_disable_usb">Disable USB</string>
<string name="switch_stop_charge">Stop Charge</string>
<string name="switch_protect_80">Protect 80%</string>
<string name="warn_shizuku_not_ready">Shizuku is not ready</string>
<string name="warn_denied_permission">User denied permission</string>
<string name="warn_device_type_qcom">Only support Qualcomm platform</string>
</resources>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Light" parent="@android:style/Theme.DeviceDefault.Light">
<item name="android:statusBarColor">#43A047</item>
<item name="android:colorPrimary">#43A047</item>
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:colorAccent">#43A047</item>
</style>
</resources>

View File

@ -0,0 +1,17 @@
package run.evan.chargetool;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}