Skip to content
This repository has been archived by the owner on Oct 5, 2019. It is now read-only.

Battery: add custom AC, USB, and Wireless charge control #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.grarak.kerneladiutor.views.recyclerview.CardView;
import com.grarak.kerneladiutor.views.recyclerview.RecyclerViewItem;
import com.grarak.kerneladiutor.views.recyclerview.SeekBarView;
import com.grarak.kerneladiutor.views.recyclerview.SelectView;
import com.grarak.kerneladiutor.views.recyclerview.StatsView;
import com.grarak.kerneladiutor.views.recyclerview.SwitchView;

Expand Down Expand Up @@ -66,6 +67,15 @@ protected void addItems(List<RecyclerViewItem> items) {
if (mBattery.hasForceFastCharge()) {
forceFastChargeInit(items);
}
if (mBattery.hasFastChargeControlAC()) {
FastChargeControlACinit(items);
}
if (mBattery.hasFastChargeControlUSB()) {
FastChargeControlUSBinit(items);
}
if (mBattery.hasFastChargeControlWIRELESS()) {
FastChargeControlWirelessinit(items);
}
if (mBattery.hasBlx()) {
blxInit(items);
}
Expand Down Expand Up @@ -98,14 +108,65 @@ private void voltageInit(List<RecyclerViewItem> items) {
}

private void forceFastChargeInit(List<RecyclerViewItem> items) {
SwitchView forceFastCharge = new SwitchView();
forceFastCharge.setTitle(getString(R.string.usb_fast_charge));
forceFastCharge.setSummary(getString(R.string.usb_fast_charge_summary));
forceFastCharge.setChecked(mBattery.isForceFastChargeEnabled());
forceFastCharge.addOnSwitchListener((switchView, isChecked)
-> mBattery.enableForceFastCharge(isChecked, getActivity()));

items.add(forceFastCharge);
SelectView forceFastCharge = new SelectView();
forceFastCharge.setTitle(getString(R.string.usb_fast_charge));
forceFastCharge.setSummary(getString(R.string.usb_fast_charge_summary));
forceFastCharge.setItems(mBattery.enableForceFastCharge(getActivity()));
forceFastCharge.setItem(mBattery.getForceFastCharge());
forceFastCharge.setOnItemSelected(new SelectView.OnItemSelected() {
@Override
public void onItemSelected(SelectView selectView, int position, String item) {
mBattery.setForceFastCharge(position, getActivity());
}
});

items.add(forceFastCharge);

}

private void FastChargeControlACinit(List<RecyclerViewItem> items) {
SelectView FastChargeControl = new SelectView();
FastChargeControl.setTitle(getString(R.string.charge_level_ac));
FastChargeControl.setSummary(getString(R.string.charge_level_ac_summary));
FastChargeControl.setItems(mBattery.getFastChargeControlAC());
FastChargeControl.setItem(mBattery.getFastChargeCustomAC());
FastChargeControl.setOnItemSelected(new SelectView.OnItemSelected() {
@Override
public void onItemSelected(SelectView selectView, int position, String item) {
mBattery.setFastChargeControlAC(item, getActivity());
}
});
items.add(FastChargeControl);
}

private void FastChargeControlUSBinit(List<RecyclerViewItem> items) {
SelectView FastChargeControl = new SelectView();
FastChargeControl.setTitle(getString(R.string.charge_level_usb));
FastChargeControl.setSummary(getString(R.string.charge_level_usb_summary));
FastChargeControl.setItems(mBattery.getFastChargeControlUSB());
FastChargeControl.setItem(mBattery.getFastChargeCustomUSB());
FastChargeControl.setOnItemSelected(new SelectView.OnItemSelected() {
@Override
public void onItemSelected(SelectView selectView, int position, String item) {
mBattery.setFastChargeControlUSB(item, getActivity());
}
});
items.add(FastChargeControl);
}

private void FastChargeControlWirelessinit(List<RecyclerViewItem> items) {
SelectView FastChargeControlWireless = new SelectView();
FastChargeControlWireless.setTitle(getString(R.string.charge_level_wireless));
FastChargeControlWireless.setSummary(getString(R.string.charge_level_wireless_summary));
FastChargeControlWireless.setItems(mBattery.getFastChargeControlWIRELESS());
FastChargeControlWireless.setItem(mBattery.getFastChargeCustomWIRELESS());
FastChargeControlWireless.setOnItemSelected(new SelectView.OnItemSelected() {
@Override
public void onItemSelected(SelectView selectView, int position, String item) {
mBattery.setFastChargeControlWIRELESS(item, getActivity());
}
});
items.add(FastChargeControlWireless);
}

private void blxInit(List<RecyclerViewItem> items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package com.grarak.kerneladiutor.utils.kernel.battery;

import android.content.Context;
import android.text.TextUtils;

import com.grarak.kerneladiutor.R;
import android.support.annotation.NonNull;

import com.grarak.kerneladiutor.fragments.ApplyOnBootFragment;
Expand All @@ -28,6 +31,9 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Created by willi on 26.06.16.
Expand All @@ -43,14 +49,30 @@ public static Battery getInstance(@NonNull Context context) {
return sInstance;
}

private static final String FORCE_FAST_CHARGE = "/sys/kernel/fast_charge/force_fast_charge";
private static final String FAST_CHARGE = "/sys/kernel/fast_charge";
private static final String FORCE_FAST_CHARGE = FAST_CHARGE + "/force_fast_charge";
private static final String CUSTOM_AC_CHARGE_LEVEL = FAST_CHARGE + "/ac_charge_level";
private static final String CUSTOM_USB_CHARGE_LEVEL = FAST_CHARGE + "/usb_charge_level";
private static final String CUSTOM_WIRELESS_CHARGE_LEVEL = FAST_CHARGE + "/wireless_charge_level";

private static final String MTP_FORCE_FAST_CHARGE = FAST_CHARGE + "/use_mtp_during_fast_charge";
private static final String SCREEN_ON_CURRENT_LIMT = FAST_CHARGE + "/screen_on_current_limit";

private static final String AC_CHARGE_LEVEL = FAST_CHARGE + "/ac_levels";
private static final String USB_CHARGE_LEVEL = FAST_CHARGE + "/usb_levels";
private static final String WIRELESS_CHARGE_LEVEL = FAST_CHARGE + "/wireless_levels";
private static final String FAILSAFE_CONTROL = FAST_CHARGE + "/failsafe";

private static final String BLX = "/sys/devices/virtual/misc/batterylifeextender/charging_limit";

private static final String CHARGE_RATE = "/sys/kernel/thundercharge_control";
private static final String CHARGE_RATE_ENABLE = CHARGE_RATE + "/enabled";
private static final String CUSTOM_CURRENT = CHARGE_RATE + "/custom_current";

private int mCapacity;
private static String[] sBatteryAvailable;
private static String[] sBatteryUSBAvailable;
private static String[] sBatteryWIRELESSAvailable;

private Battery(Context context) {
if (mCapacity == 0) {
Expand Down Expand Up @@ -104,16 +126,81 @@ public boolean hasBlx() {
return Utils.existFile(BLX);
}

public void enableForceFastCharge(boolean enable, Context context) {
run(Control.write(enable ? "1" : "0", FORCE_FAST_CHARGE), FORCE_FAST_CHARGE, context);
public static List<String> enableForceFastCharge(Context context) {
List<String> list = new ArrayList<>();
list.add(context.getString(R.string.disabled));
list.add(context.getString(R.string.enabled));
list.add(context.getString(R.string.custom_ma));
return list;
}

public static boolean hasForceFastCharge() {
return Utils.existFile(FORCE_FAST_CHARGE);
}

public static int getForceFastCharge() {
return Utils.strToInt(Utils.readFile(FORCE_FAST_CHARGE));
}

public void setForceFastCharge(int value, Context context) {
run(Control.write(String.valueOf(value), FORCE_FAST_CHARGE), FORCE_FAST_CHARGE, context);
}

public boolean isForceFastChargeEnabled() {
return Utils.readFile(FORCE_FAST_CHARGE).equals("1");
public static boolean hasFastChargeControlAC() {
return Utils.existFile(AC_CHARGE_LEVEL);
}

public static String getFastChargeCustomAC() {
return Utils.readFile(CUSTOM_AC_CHARGE_LEVEL);
}

public boolean hasForceFastCharge() {
return Utils.existFile(FORCE_FAST_CHARGE);
public void setFastChargeControlAC (String value, Context context) {
run(Control.write(String.valueOf(value), CUSTOM_AC_CHARGE_LEVEL), CUSTOM_AC_CHARGE_LEVEL, context);
}

public static List<String> getFastChargeControlAC() {
if (sBatteryAvailable == null) {
sBatteryAvailable = Utils.readFile(AC_CHARGE_LEVEL).split(" ");
}
return new ArrayList<>(Arrays.asList(sBatteryAvailable));
}

public static boolean hasFastChargeControlUSB() {
return Utils.existFile(USB_CHARGE_LEVEL);
}

public static String getFastChargeCustomUSB() {
return Utils.readFile(CUSTOM_USB_CHARGE_LEVEL);
}

public static List<String> getFastChargeControlUSB() {
if (sBatteryUSBAvailable == null) {
sBatteryUSBAvailable = Utils.readFile(USB_CHARGE_LEVEL).split(" ");
}
return new ArrayList<>(Arrays.asList(sBatteryUSBAvailable));
}

public void setFastChargeControlUSB (String value, Context context) {
run(Control.write(String.valueOf(value), CUSTOM_USB_CHARGE_LEVEL), CUSTOM_USB_CHARGE_LEVEL, context);
}

public boolean hasFastChargeControlWIRELESS() {
return Utils.existFile(WIRELESS_CHARGE_LEVEL);
}

public static String getFastChargeCustomWIRELESS() {
return Utils.readFile(CUSTOM_WIRELESS_CHARGE_LEVEL);
}

public static List<String> getFastChargeControlWIRELESS() {
if (sBatteryWIRELESSAvailable == null) {
sBatteryWIRELESSAvailable = Utils.readFile(WIRELESS_CHARGE_LEVEL).split(" ");
}
return new ArrayList<>(Arrays.asList(sBatteryWIRELESSAvailable));
}

public void setFastChargeControlWIRELESS (String value, Context context) {
run(Control.write(String.valueOf(value), CUSTOM_WIRELESS_CHARGE_LEVEL), CUSTOM_WIRELESS_CHARGE_LEVEL, context);
}

public int getCapacity() {
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,15 @@
<string name="level">Level</string>
<string name="voltage">Voltage</string>
<string name="capacity">Capacity</string>
<string name="usb_fast_charge">USB Fast Charge</string>
<string name="usb_fast_charge_summary">Device will charge faster when connected with USB. Replug your charger when changing this option.</string>
<string name="usb_fast_charge">Force Fast Charge</string>
<string name="usb_fast_charge_summary">If enabled, the device will charge faster when connected to USB. Select custom mode to charge your device in a custom level. Replug your charger when changing these options.</string>
<string name="custom_ma">Custom Mode</string>
<string name="charge_level_ac">AC Charge Level (mA)</string>
<string name="charge_level_ac_summary">Choose your Level</string>
<string name="charge_level_usb">USB Charge Level (mA)</string>
<string name="charge_level_usb_summary">Choose your Level</string>
<string name="charge_level_wireless">WIRELESS Charge Level (mA)</string>
<string name="charge_level_wireless_summary">Choose your Level</string>
<string name="blx">Battery Life eXtender</string>
<string name="blx_summary">Set a limit for the capacity to which the battery will be charged by passing a value between 0 and 100.</string>
<string name="charge_rate">Charge Rate</string>
Expand Down