5454import android .os .ShellCallback ;
5555import android .os .ShellCommand ;
5656import android .os .SystemClock ;
57+ import android .os .SystemProperties ;
5758import android .os .Trace ;
5859import android .os .UEventObserver ;
5960import android .os .UserHandle ;
7576import org .lineageos .internal .notification .LedValues ;
7677import org .lineageos .internal .notification .LineageBatteryLights ;
7778
79+ import java .io .BufferedReader ;
7880import java .io .File ;
7981import java .io .FileDescriptor ;
8082import java .io .FileOutputStream ;
83+ import java .io .FileReader ;
8184import java .io .IOException ;
8285import java .io .PrintWriter ;
8386import java .util .ArrayDeque ;
8891import java .util .Objects ;
8992import java .util .concurrent .atomic .AtomicReference ;
9093
94+ import motorola .hardware .health .V1_0 .BatteryProperties ;
95+ import motorola .hardware .health .V1_0 .IMotHealth ;
96+
9197/**
9298 * <p>BatteryService monitors the charging status, and charge level of the device
9399 * battery. When these values change this service broadcasts the new values
@@ -201,6 +207,16 @@ public final class BatteryService extends SystemService {
201207
202208 private LineageBatteryLights mLineageBatteryLights ;
203209
210+ private static final int MOD_TYPE_EMERGENCY = 3 ;
211+ private static final int MOD_TYPE_SUPPLEMENTAL = 2 ;
212+ private BatteryProperties mBatteryModProps ;
213+ private IMotHealth mMotHealthService = null ;
214+ private int mLastModFlag ;
215+ private int mLastModLevel ;
216+ private int mLastModPowerSource ;
217+ private int mLastModStatus ;
218+ private int mLastModType ;
219+
204220 public BatteryService (Context context ) {
205221 super (context );
206222
@@ -238,6 +254,20 @@ public void onUEvent(UEvent event) {
238254 invalidChargerObserver .startObserving (
239255 "DEVPATH=/devices/virtual/switch/invalid_charger" );
240256 }
257+
258+ mBatteryModProps = new BatteryProperties ();
259+ mBatteryModProps .modLevel = -1 ;
260+ mBatteryModProps .modStatus = 1 ;
261+ mBatteryModProps .modFlag = 0 ;
262+ mBatteryModProps .modType = 0 ;
263+ mBatteryModProps .modPowerSource = 0 ;
264+ try {
265+ mMotHealthService = IMotHealth .getService ();
266+ } catch (RemoteException e ) {
267+ Slog .e (TAG , "health: cannot get service. (RemoteException)" );
268+ } catch (NoSuchElementException e2 ) {
269+ Slog .e (TAG , "mothealth: cannot get service. (no supported health HAL service)" );
270+ }
241271 }
242272
243273 @ Override
@@ -357,6 +387,10 @@ private boolean isPoweredLocked(int plugTypeSet) {
357387 if ((plugTypeSet & BatteryManager .BATTERY_PLUGGED_WIRELESS ) != 0 && mHealthInfo .chargerWirelessOnline ) {
358388 return true ;
359389 }
390+ if ((plugTypeSet & BatteryManager .BATTERY_PLUGGED_MOD ) != 0 ||
391+ mPlugType == BatteryManager .BATTERY_PLUGGED_MOD || isModBatteryActive ()) {
392+ return true ;
393+ }
360394 return false ;
361395 }
362396
@@ -449,6 +483,16 @@ private void update(android.hardware.health.V2_1.HealthInfo info) {
449483 if (!mUpdatesStopped ) {
450484 mHealthInfo = info .legacy .legacy ;
451485 mHealthInfo2p1 = info ;
486+ if (mMotHealthService != null ) {
487+ try {
488+ mBatteryModProps = mMotHealthService .getModBatteryProperties ();
489+ if (mBatteryModProps .modFlag > 0 ) {
490+ mHealthInfo .batteryLevel = mBatteryModProps .batteryLevel ;
491+ }
492+ } catch (RemoteException e ) {
493+ Slog .e (TAG , "getModBatteryProperties fail!" );
494+ }
495+ }
452496 // Process the new values.
453497 processValuesLocked (false );
454498 mLock .notifyAll (); // for any waiters on new info
@@ -491,6 +535,8 @@ private void processValuesLocked(boolean force) {
491535 mPlugType = BatteryManager .BATTERY_PLUGGED_USB ;
492536 } else if (mHealthInfo .chargerWirelessOnline ) {
493537 mPlugType = BatteryManager .BATTERY_PLUGGED_WIRELESS ;
538+ } else if (supplementalOrEmergencyModOnline ()) {
539+ mPlugType = BatteryManager .BATTERY_PLUGGED_MOD ;
494540 } else {
495541 mPlugType = BATTERY_PLUGGED_NONE ;
496542 }
@@ -505,7 +551,7 @@ private void processValuesLocked(boolean force) {
505551 // Let the battery stats keep track of the current level.
506552 try {
507553 mBatteryStats .setBatteryState (mHealthInfo .batteryStatus , mHealthInfo .batteryHealth ,
508- mPlugType , mHealthInfo .batteryLevel , mHealthInfo .batteryTemperature ,
554+ maybeTranslatePlugType ( mPlugType ) , mHealthInfo .batteryLevel , mHealthInfo .batteryTemperature ,
509555 mHealthInfo .batteryVoltage , mHealthInfo .batteryChargeCounter ,
510556 mHealthInfo .batteryFullCharge ,
511557 mHealthInfo2p1 .batteryChargeTimeToFullNowSeconds );
@@ -526,7 +572,12 @@ private void processValuesLocked(boolean force) {
526572 mHealthInfo .maxChargingCurrent != mLastMaxChargingCurrent ||
527573 mHealthInfo .maxChargingVoltage != mLastMaxChargingVoltage ||
528574 mHealthInfo .batteryChargeCounter != mLastChargeCounter ||
529- mInvalidCharger != mLastInvalidCharger )) {
575+ mInvalidCharger != mLastInvalidCharger ||
576+ mBatteryModProps .modLevel != mLastModLevel ||
577+ mBatteryModProps .modStatus != mLastModStatus ||
578+ mBatteryModProps .modFlag != mLastModFlag ||
579+ mBatteryModProps .modType != mLastModType ||
580+ mBatteryModProps .modPowerSource != mLastModPowerSource )) {
530581
531582 if (mPlugType != mLastPlugType ) {
532583 if (mLastPlugType == BATTERY_PLUGGED_NONE ) {
@@ -697,6 +748,12 @@ public void run() {
697748 mLastChargeCounter = mHealthInfo .batteryChargeCounter ;
698749 mLastBatteryLevelCritical = mBatteryLevelCritical ;
699750 mLastInvalidCharger = mInvalidCharger ;
751+ mLastModLevel = mBatteryModProps .modLevel ;
752+ mLastModStatus = mBatteryModProps .modStatus ;
753+ mLastModFlag = mBatteryModProps .modFlag ;
754+ mLastModType = mBatteryModProps .modType ;
755+ mLastModPowerSource = mBatteryModProps .modPowerSource ;
756+
700757 }
701758 }
702759
@@ -716,14 +773,20 @@ private void sendBatteryChangedIntentLocked() {
716773 intent .putExtra (BatteryManager .EXTRA_BATTERY_LOW , mSentLowBatteryBroadcast );
717774 intent .putExtra (BatteryManager .EXTRA_SCALE , BATTERY_SCALE );
718775 intent .putExtra (BatteryManager .EXTRA_ICON_SMALL , icon );
719- intent .putExtra (BatteryManager .EXTRA_PLUGGED , mPlugType );
776+ intent .putExtra (BatteryManager .EXTRA_PLUGGED , maybeTranslatePlugType ( mPlugType ) );
720777 intent .putExtra (BatteryManager .EXTRA_VOLTAGE , mHealthInfo .batteryVoltage );
721778 intent .putExtra (BatteryManager .EXTRA_TEMPERATURE , mHealthInfo .batteryTemperature );
722779 intent .putExtra (BatteryManager .EXTRA_TECHNOLOGY , mHealthInfo .batteryTechnology );
723780 intent .putExtra (BatteryManager .EXTRA_INVALID_CHARGER , mInvalidCharger );
724781 intent .putExtra (BatteryManager .EXTRA_MAX_CHARGING_CURRENT , mHealthInfo .maxChargingCurrent );
725782 intent .putExtra (BatteryManager .EXTRA_MAX_CHARGING_VOLTAGE , mHealthInfo .maxChargingVoltage );
726783 intent .putExtra (BatteryManager .EXTRA_CHARGE_COUNTER , mHealthInfo .batteryChargeCounter );
784+ intent .putExtra (BatteryManager .EXTRA_MOD_LEVEL , mBatteryModProps .modLevel );
785+ intent .putExtra (BatteryManager .EXTRA_MOD_STATUS , mBatteryModProps .modStatus );
786+ intent .putExtra (BatteryManager .EXTRA_MOD_FLAG , mBatteryModProps .modFlag );
787+ intent .putExtra (BatteryManager .EXTRA_PLUGGED_RAW , mPlugType );
788+ intent .putExtra (BatteryManager .EXTRA_MOD_TYPE , mBatteryModProps .modType );
789+ intent .putExtra (BatteryManager .EXTRA_MOD_POWER_SOURCE , mBatteryModProps .modPowerSource );
727790 if (DEBUG ) {
728791 Slog .d (TAG , "Sending ACTION_BATTERY_CHANGED. scale:" + BATTERY_SCALE
729792 + ", info:" + mHealthInfo .toString ());
@@ -862,6 +925,36 @@ private int getIconLocked(int level) {
862925 }
863926 }
864927
928+ private int maybeTranslatePlugType (int plugType ) {
929+ if (plugType != BatteryManager .BATTERY_PLUGGED_MOD ) {
930+ return plugType ;
931+ }
932+ if (this .mHealthInfo .batteryStatus == BatteryManager .BATTERY_STATUS_CHARGING ) {
933+ return 1 ;
934+ }
935+ return 0 ;
936+ }
937+
938+ private boolean supplementalOrEmergencyModOnline () {
939+ return mBatteryModProps .modLevel > 0 &&
940+ (mBatteryModProps .modType == MOD_TYPE_SUPPLEMENTAL ||
941+ mBatteryModProps .modType == MOD_TYPE_EMERGENCY );
942+ }
943+
944+ private boolean isModBatteryActive () {
945+ if (mBatteryModProps .modLevel <= 0 || mBatteryModProps .modType != MOD_TYPE_SUPPLEMENTAL ) {
946+ return false ;
947+ }
948+ String batteryMode = SystemProperties .get ("sys.mod.batterymode" );
949+ if ("0" .equals (batteryMode )) {
950+ return true ;
951+ }
952+ if (!"2" .equals (batteryMode ) && mHealthInfo .batteryLevel <= 80 ) {
953+ return true ;
954+ }
955+ return false ;
956+ }
957+
865958 class Shell extends ShellCommand {
866959 @ Override
867960 public int onCommand (String cmd ) {
0 commit comments