Skip to content

Instantly share code, notes, and snippets.

@dev-harsh1998
Last active October 8, 2022 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dev-harsh1998/7a266c80061478f822ce1194b5569178 to your computer and use it in GitHub Desktop.
Save dev-harsh1998/7a266c80061478f822ce1194b5569178 to your computer and use it in GitHub Desktop.
From 6e902d37d4a4170a85143c806425c662237889fe Mon Sep 17 00:00:00 2001
From: Karthick C <karthickrko61@gmail.com>
Date: Thu, 24 Sep 2020 21:35:37 +0000
Subject: [PATCH] BatteryService: Implement vooc charging support
* Realme devices support non standard charger type, i.e vooc charger that has its own crazy charge speeds
luckily vooc charger can be determined upon the value of one node, Let's use that and update user when
vooc charger is coonected and vooc charging is in progresss, Also guard this via a overlay flag and set
it to false by default to preserve the default aosp behaviour for non standard chargers if user uses them.
[@dev-harsh1998]: Forward Port to Android R's fuelgauge implementation
[@dev-harsh1998]: Forward Port to Android S changes.
Change-Id: If9a616cbdeeaaf658d960d2f92b26e1f928d2fb2
Co-authored-by: Harshit Jain <harshit@hyperteam.co.in>
Signed-off-by: Harshit Jain <harshit@hyperteam.co.in>
---
core/java/android/os/BatteryManager.java | 7 +++++
core/res/res/values/config.xml | 3 +++
core/res/res/values/strings.xml | 3 +++
core/res/res/values/symbols.xml | 3 +++
.../settingslib/fuelgauge/BatteryStatus.java | 14 ++++++++--
packages/SystemUI/res/values/strings.xml | 4 +++
.../keyguard/KeyguardUpdateMonitor.java | 7 ++++-
.../KeyguardIndicationController.java | 5 ++++
.../com/android/server/BatteryService.java | 27 +++++++++++++++++++
9 files changed, 70 insertions(+), 3 deletions(-)
diff --git a/core/java/android/os/BatteryManager.java b/core/java/android/os/BatteryManager.java
index 6d4593a2ce87..189dbdf6ce9a 100644
--- a/core/java/android/os/BatteryManager.java
+++ b/core/java/android/os/BatteryManager.java
@@ -163,6 +163,13 @@ public class BatteryManager {
@SystemApi
public static final String EXTRA_EVENT_TIMESTAMP = "android.os.extra.EVENT_TIMESTAMP";
+ /**
+ * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
+ * boolean value to detect fast charging
+ * {@hide}
+ */
+ public static final String EXTRA_VOOC_CHARGER = "vooc_charger";
+
// values for "status" field in the ACTION_BATTERY_CHANGED Intent
public static final int BATTERY_STATUS_UNKNOWN = Constants.BATTERY_STATUS_UNKNOWN;
public static final int BATTERY_STATUS_CHARGING = Constants.BATTERY_STATUS_CHARGING;
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index c6a487e175e7..29edd911b267 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -5065,4 +5065,7 @@
<!-- The list of components which should be forced to be enabled. -->
<string-array name="config_forceEnabledComponents" translatable="false">
</string-array>
+
+ <!-- Whether device has VOOC charging support -->
+ <bool name="config_hasVoocCharger">true</bool>
</resources>
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index a99a22009e3b..6c5afe372af4 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -6035,4 +6035,7 @@ ul.</string>
<string name="ui_translation_accessibility_translated_text"><xliff:g id="message" example="Hello">%1$s</xliff:g> Translated.</string>
<!-- Accessibility message announced to notify the user when the system has finished translating the content displayed on the screen to a different language after the user requested translation. [CHAR LIMIT=NONE] -->
<string name="ui_translation_accessibility_translation_finished">Message translated from <xliff:g id="from_language" example="English">%1$s</xliff:g> to <xliff:g id="to_language" example="French">%2$s</xliff:g>.</string>
+ <!-- Indication on the keyguard that is shown when the device is charging with a VOOC charger. Should match keyguard_plugged_in_vooc_charging [CHAR LIMIT=40]-->
+ <string name="keyguard_indication_vooc_charging_time"><xliff:g id="percentage">%2$s</xliff:g> • VOOC Charging (<xliff:g id="charging_time_left" example="4 hours and 2 minutes">%1$s</xliff:g> until full)</string>
+ <string name="keyguard_plugged_in_vooc_charging"><xliff:g id="percentage">%s</xliff:g> • VOOC Charging</string>
</resources>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 12ff97c89ae4..68d4ac1a4767 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -4433,4 +4433,7 @@
<java-symbol type="array" name="config_deviceDisabledComponents" />
<java-symbol type="array" name="config_globallyDisabledComponents" />
<java-symbol type="array" name="config_forceEnabledComponents" />
+
+ <!-- Whether device has VOOC charging support -->
+ <java-symbol type="bool" name="config_hasVoocCharger" />
</resources>
diff --git a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
index b3205d7563b2..9342861d3e6b 100644
--- a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
+++ b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
@@ -28,6 +28,9 @@ import static android.os.BatteryManager.EXTRA_PLUGGED;
import static android.os.BatteryManager.EXTRA_PRESENT;
import static android.os.BatteryManager.EXTRA_STATUS;
+//dev-harsh1998 Port vooc charging status to Android S
+import static android.os.BatteryManager.EXTRA_VOOC_CHARGER;
+
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
@@ -45,6 +48,8 @@ public class BatteryStatus {
public static final int CHARGING_SLOWLY = 0;
public static final int CHARGING_REGULAR = 1;
public static final int CHARGING_FAST = 2;
+ // dev-harsh1998 port vooc charging to Android S fuel gauge
+ public static final int CHARGING_VOOC = 3;
public final int status;
public final int level;
@@ -53,14 +58,17 @@ public class BatteryStatus {
public final int maxChargingWattage;
public final boolean present;
+ // dev-harsh1998 port vooc charging to Android S fuel gauge
+ public final boolean voocChargeStatus;
public BatteryStatus(int status, int level, int plugged, int health,
- int maxChargingWattage, boolean present) {
+ int maxChargingWattage, boolean present, boolean voocChargeStatus) {
this.status = status;
this.level = level;
this.plugged = plugged;
this.health = health;
this.maxChargingWattage = maxChargingWattage;
this.present = present;
+ this.voocChargeStatus = voocChargeStatus;
}
public BatteryStatus(Intent batteryChangedIntent) {
@@ -69,6 +77,7 @@ public class BatteryStatus {
level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0);
health = batteryChangedIntent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
present = batteryChangedIntent.getBooleanExtra(EXTRA_PRESENT, true);
+ voocChargeStatus = batteryChangedIntent.getBooleanExtra(EXTRA_VOOC_CHARGER, false);
final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT,
-1);
@@ -147,7 +156,8 @@ public class BatteryStatus {
R.integer.config_chargingSlowlyThreshold);
final int fastThreshold = context.getResources().getInteger(
R.integer.config_chargingFastThreshold);
- return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
+ return voocChargeStatus ? CHARGING_VOOC :
+ maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
maxChargingWattage > fastThreshold ? CHARGING_FAST :
CHARGING_REGULAR;
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index a67021611812..090e4d8e88bd 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -1126,6 +1126,10 @@
<!-- Indication on the keyguard that is shown when the device is charging slowly. Should match keyguard_plugged_in_charging_slowly [CHAR LIMIT=50]-->
<string name="keyguard_indication_charging_time_slowly"><xliff:g id="percentage">%2$s</xliff:g> • Charging slowly • Full in <xliff:g id="charging_time_left" example="4 hr, 2 min">%1$s</xliff:g></string>
+ <!-- Indication on the keyguard that is shown when the device is charging with a VOOC charger. Should match keyguard_plugged_in_vooc_charging [CHAR LIMIT=40]-->
+ <string name="keyguard_indication_vooc_charging_time"><xliff:g id="percentage">%2$s</xliff:g> • VOOC Charging (<xliff:g id="charging_time_left" example="4 hours and 2 minutes">%1$s</xliff:g> until full)</string>
+ <string name="keyguard_plugged_in_vooc_charging"><xliff:g id="percentage">%s</xliff:g> • VOOC Charging</string>
+
<!-- Related to user switcher --><skip/>
<!-- Accessibility label for the button that opens the user switcher. -->
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
index 877e76480b1e..6407ff922988 100644
--- a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -1917,7 +1917,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
}
// Take a guess at initial SIM state, battery status and PLMN until we get an update
- mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0, true);
+ mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0, 0, true, false);
// Watch for interesting updates
final IntentFilter filter = new IntentFilter();
@@ -3021,6 +3021,11 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
return true;
}
+ // change in VOOC charging while plugged in
+ if (nowPluggedIn && current.voocChargeStatus != old.voocChargeStatus) {
+ return true;
+ }
+
return false;
}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
index 503b5c0ee4b0..cd6900f7b8d3 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
@@ -747,6 +747,11 @@ public class KeyguardIndicationController {
? R.string.keyguard_indication_charging_time_slowly
: R.string.keyguard_plugged_in_charging_slowly;
break;
+ case BatteryStatus.CHARGING_VOOC:
+ chargingId = hasChargingTime
+ ? R.string.keyguard_indication_vooc_charging_time
+ : R.string.keyguard_plugged_in_vooc_charging;
+ break;
default:
chargingId = hasChargingTime
? R.string.keyguard_indication_charging_time
diff --git a/services/core/java/com/android/server/BatteryService.java b/services/core/java/com/android/server/BatteryService.java
index 1e608f5c1240..e298a7885315 100644
--- a/services/core/java/com/android/server/BatteryService.java
+++ b/services/core/java/com/android/server/BatteryService.java
@@ -74,9 +74,12 @@ import com.android.server.am.BatteryStatsService;
import com.android.server.lights.LightsManager;
import com.android.server.lights.LogicalLight;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
+import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
@@ -177,6 +180,10 @@ public final class BatteryService extends SystemService {
private boolean mBatteryLevelLow;
+ private boolean mVoocCharger;
+ private boolean mHasVoocCharger;
+ private boolean mLastVoocCharger;
+
private long mDischargeStartTime;
private int mDischargeStartLevel;
@@ -217,6 +224,8 @@ public final class BatteryService extends SystemService {
com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
mShutdownBatteryTemperature = mContext.getResources().getInteger(
com.android.internal.R.integer.config_shutdownBatteryTemperature);
+ mHasVoocCharger = mContext.getResources().getBoolean(
+ com.android.internal.R.bool.config_hasVoocCharger);
mBatteryLevelsEventQueue = new ArrayDeque<>();
mMetricsLogger = new MetricsLogger();
@@ -517,6 +526,7 @@ public final class BatteryService extends SystemService {
shutdownIfNoPowerLocked();
shutdownIfOverTempLocked();
+ mVoocCharger = mHasVoocCharger && isVoocCharger();
if (force || (mHealthInfo.batteryStatus != mLastBatteryStatus ||
mHealthInfo.batteryHealth != mLastBatteryHealth ||
mHealthInfo.batteryPresent != mLastBatteryPresent ||
@@ -527,6 +537,7 @@ public final class BatteryService extends SystemService {
mHealthInfo.maxChargingCurrent != mLastMaxChargingCurrent ||
mHealthInfo.maxChargingVoltage != mLastMaxChargingVoltage ||
mHealthInfo.batteryChargeCounter != mLastChargeCounter ||
+ mVoocCharger != mLastVoocCharger ||
mInvalidCharger != mLastInvalidCharger)) {
if (mPlugType != mLastPlugType) {
@@ -698,6 +709,7 @@ public final class BatteryService extends SystemService {
mLastChargeCounter = mHealthInfo.batteryChargeCounter;
mLastBatteryLevelCritical = mBatteryLevelCritical;
mLastInvalidCharger = mInvalidCharger;
+ mLastVoocCharger = mVoocCharger;
}
}
@@ -725,6 +737,7 @@ public final class BatteryService extends SystemService {
intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mHealthInfo.maxChargingCurrent);
intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mHealthInfo.maxChargingVoltage);
intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mHealthInfo.batteryChargeCounter);
+ intent.putExtra(BatteryManager.EXTRA_VOOC_CHARGER, mVoocCharger);
if (DEBUG) {
Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. scale:" + BATTERY_SCALE
+ ", info:" + mHealthInfo.toString());
@@ -779,6 +792,20 @@ public final class BatteryService extends SystemService {
mLastBatteryLevelChangedSentMs = SystemClock.elapsedRealtime();
}
+ private boolean isVoocCharger() {
+ try {
+ FileReader file = new FileReader("/sys/class/power_supply/battery/voocchg_ing");
+ BufferedReader br = new BufferedReader(file);
+ String state = br.readLine();
+ br.close();
+ file.close();
+ return "1".equals(state);
+ } catch (FileNotFoundException e) {
+ } catch (IOException e) {
+ }
+ return false;
+ }
+
// TODO: Current code doesn't work since "--unplugged" flag in BSS was purposefully removed.
private void logBatteryStatsLocked() {
IBinder batteryInfoService = ServiceManager.getService(BatteryStats.SERVICE_NAME);
--
2.25.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment