update
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
package run.evan.chargetool;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@ -17,8 +19,19 @@ public class MainActivity extends Activity {
|
||||
|
||||
private static final int CHARGE_FULL_DESIGN_MI14P = 4880000;
|
||||
|
||||
private static final int CHARGE_FULL_DESIGN_MI15P = 6100000;
|
||||
|
||||
|
||||
private static int chargeFull = 0;
|
||||
|
||||
private static final String MODEL_14_PRO = "shennong";
|
||||
private static final String MODEL_15_PRO = "haotian";
|
||||
|
||||
private static final String SMART_CHARGE_RESET = "0x10";
|
||||
|
||||
private MainActivityBinding binding;
|
||||
|
||||
private static int currentProgress;
|
||||
|
||||
Object IMiChargeInstance;
|
||||
|
||||
@ -59,7 +72,6 @@ public class MainActivity extends Activity {
|
||||
|
||||
Method getBatterySoh;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -67,6 +79,17 @@ public class MainActivity extends Activity {
|
||||
binding = MainActivityBinding.inflate(getLayoutInflater());
|
||||
setContentView(binding.getRoot());
|
||||
|
||||
//fit for mi14pro and 15pro
|
||||
String model = getProductDevice();
|
||||
switch (model) {
|
||||
case MODEL_15_PRO:
|
||||
chargeFull = CHARGE_FULL_DESIGN_MI15P;
|
||||
break;
|
||||
case MODEL_14_PRO:
|
||||
chargeFull = CHARGE_FULL_DESIGN_MI14P;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Class IMiCharge = Class.forName("miui.util.IMiCharge");
|
||||
Method getInstance = IMiCharge.getDeclaredMethod("getInstance");
|
||||
@ -102,9 +125,9 @@ public class MainActivity extends Activity {
|
||||
getBatteryVbat.setAccessible(true);
|
||||
getBatteryTbat = IMiCharge.getDeclaredMethod("getBatteryTbat");
|
||||
getBatteryTbat.setAccessible(true);
|
||||
getMiChargePath = IMiCharge.getDeclaredMethod("getMiChargePath",String.class);
|
||||
getMiChargePath = IMiCharge.getDeclaredMethod("getMiChargePath", String.class);
|
||||
getMiChargePath.setAccessible(true);
|
||||
setMiChargePath = IMiCharge.getDeclaredMethod("setMiChargePath",String.class,String.class);
|
||||
setMiChargePath = IMiCharge.getDeclaredMethod("setMiChargePath", String.class, String.class);
|
||||
setMiChargePath.setAccessible(true);
|
||||
getBatterySoh = IMiCharge.getDeclaredMethod("getBatterySoh");
|
||||
getBatterySoh.setAccessible(true);
|
||||
@ -121,16 +144,18 @@ public class MainActivity extends Activity {
|
||||
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());
|
||||
//Old
|
||||
//Boolean invoke = (Boolean) setMiChargePath.invoke(IMiChargeInstance, "smart_chg",checked ? "0x3" : "0x2");
|
||||
if (checked) {
|
||||
setChargeStop(1);
|
||||
currentProgress = 1;
|
||||
binding.chargeBar.setProgress(1);
|
||||
binding.progressText.setText(String.valueOf(1));
|
||||
} else {
|
||||
setChargeStop(100);
|
||||
currentProgress = 100;
|
||||
binding.chargeBar.setProgress(100);
|
||||
binding.progressText.setText(String.valueOf(100));
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -153,6 +178,34 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
});
|
||||
|
||||
binding.chargeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
binding.progressText.setText(String.valueOf(progress));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
int progress = seekBar.getProgress();
|
||||
try {
|
||||
setChargeStop(progress);
|
||||
currentProgress = progress;
|
||||
if (progress == 100) {
|
||||
binding.switchStopCharge.setChecked(false);
|
||||
} else {
|
||||
binding.switchStopCharge.setChecked(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
binding.textView.setText(e.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Timer timer = new Timer();
|
||||
TimerTask task = new TimerTask() {
|
||||
@ -187,6 +240,20 @@ public class MainActivity extends Activity {
|
||||
|
||||
}
|
||||
|
||||
private void setChargeStop(int progress) {
|
||||
Boolean invoke = null;
|
||||
try {
|
||||
invoke = (Boolean) setMiChargePath.invoke(IMiChargeInstance, "smart_chg", progress != 100 ? getSmartChargeFullValue(progress) : SMART_CHARGE_RESET);
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
@ -202,21 +269,34 @@ public class MainActivity extends Activity {
|
||||
binding.switchDisableUsb.setChecked(false);
|
||||
}
|
||||
|
||||
String pathResult = (String) getMiChargePath.invoke(IMiChargeInstance,"smart_chg");
|
||||
String pathResult = (String) getMiChargePath.invoke(IMiChargeInstance, "smart_chg");
|
||||
//binding.textView.setText(pathResult);
|
||||
if ("2".equals(pathResult) || "6".equals(pathResult)) {
|
||||
binding.switchStopCharge.setChecked(true);
|
||||
} else {
|
||||
|
||||
if ("0".equals(pathResult) || "64".equals(pathResult)) {
|
||||
binding.switchStopCharge.setChecked(false);
|
||||
} else {
|
||||
binding.switchStopCharge.setChecked(true);
|
||||
binding.progressText.setText(String.valueOf(currentProgress));
|
||||
binding.chargeBar.setProgress(currentProgress);
|
||||
}
|
||||
|
||||
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 + "%");
|
||||
int chargeFullValue = Integer.parseInt(getBatteryChargeFull.invoke(IMiChargeInstance).toString());
|
||||
String life = BigDecimal.valueOf(chargeFullValue).divide(BigDecimal.valueOf(chargeFull), 4, RoundingMode.FLOOR).multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toPlainString();
|
||||
binding.textLife.setText("Battery Life: " + (chargeFullValue / 1000) + "mAh / " + (chargeFull / 1000) + "mAh = " + life + "%");
|
||||
} catch (Exception e) {
|
||||
binding.textView.setText(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getSmartChargeFullValue(int percent) {
|
||||
String str = "0x" + Integer.toHexString((percent << 16) | 17);
|
||||
return str;
|
||||
}
|
||||
|
||||
private String getProductDevice() {
|
||||
return Build.DEVICE; // 获取设备标识符
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -78,6 +78,25 @@
|
||||
android:text="@string/switch_stop_charge" />
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="16dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/chargeBar"
|
||||
android:layout_width="300dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:max="100"
|
||||
android:min="1"
|
||||
android:progress="100" />
|
||||
<TextView
|
||||
android:id="@+id/progressText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
@ -85,6 +104,8 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:text="" />
|
||||
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
Reference in New Issue
Block a user