sdk: Add lineagehealth IFastCharge interface support

Change-Id: I89eb3efbc79146b08684fa41f3c8d62ef2fb60c4
This commit is contained in:
LuK1337
2025-08-13 23:02:15 +02:00
parent 16a2a81e14
commit e3790a355b
8 changed files with 270 additions and 7 deletions

View File

@@ -0,0 +1,132 @@
/*
* SPDX-FileCopyrightText: 2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.platform.internal.health;
import android.content.res.Resources;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import com.android.internal.util.ArrayUtils;
import lineageos.providers.LineageSettings;
import org.lineageos.platform.internal.health.LineageHealthFeature;
import org.lineageos.platform.internal.R;
import vendor.lineage.health.FastChargeMode;
import vendor.lineage.health.IFastCharge;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.List;
public class FastChargeController extends LineageHealthFeature {
private final int[] mChargingSpeedValues;
private final ContentResolver mContentResolver;
private final IFastCharge mFastCharge;
// Settings uris
private final Uri MODE_URI = LineageSettings.System.getUriFor(
LineageSettings.System.FAST_CHARGE_MODE);
public FastChargeController(Context context, Handler handler) {
super(context, handler);
mContentResolver = mContext.getContentResolver();
mFastCharge = IFastCharge.Stub.asInterface(
ServiceManager.waitForDeclaredService(
IFastCharge.DESCRIPTOR + "/default"));
Resources res = mContext.getResources();
mChargingSpeedValues = Stream.of(res.getStringArray(R.array.charging_speed_values))
.mapToInt(Integer::parseInt)
.toArray();
if (mFastCharge == null) {
Log.i(TAG, "Lineage Health HAL not found");
return;
}
}
@Override
public boolean isSupported() {
try {
return mFastCharge != null && mFastCharge.getSupportedFastChargeModes() > 0;
} catch (RemoteException e) {
return false;
}
}
public int[] getSupportedFastChargeModes() {
try {
long supportedFastChargeModes = mFastCharge.getSupportedFastChargeModes();
return IntStream.of(mChargingSpeedValues)
.filter(mode -> (supportedFastChargeModes & mode) != 0)
.toArray();
} catch (RemoteException e) {
return new int[0];
}
}
public int getFastChargeMode() {
int[] supportedFastChargeModes = getSupportedFastChargeModes();
int defaultMode = supportedFastChargeModes[supportedFastChargeModes.length - 1];
int mode = LineageSettings.System.getInt(mContentResolver,
LineageSettings.System.FAST_CHARGE_MODE,
defaultMode);
if (mode != defaultMode && !ArrayUtils.contains(supportedFastChargeModes, mode)) {
return defaultMode;
}
return mode;
}
public boolean setFastChargeMode(int mode) {
putInt(LineageSettings.System.FAST_CHARGE_MODE, mode);
return true;
}
@Override
public void onStart() {
if (mFastCharge == null) {
return;
}
// Register setting observer
registerSettings(MODE_URI);
handleSettingChange();
}
private void handleSettingChange() {
try {
mFastCharge.setFastChargeMode(getFastChargeMode());
} catch (RemoteException e) {
}
}
@Override
protected void onSettingsChanged(Uri uri) {
handleSettingChange();
}
@Override
public void dump(PrintWriter pw) {
pw.println();
pw.println("FastChargeController Configuration:");
pw.println(" Mode: " + getFastChargeMode());
pw.println();
}
}

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023-2024 The LineageOS Project
* SPDX-FileCopyrightText: 2023-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
@@ -37,6 +37,7 @@ public class HealthInterfaceService extends LineageSystemService {
// Health features
private ChargingControlController mCCC;
private FastChargeController mFCC;
public HealthInterfaceService(Context context) {
super(context);
@@ -69,6 +70,10 @@ public class HealthInterfaceService extends LineageSystemService {
if (mCCC.isSupported()) {
mFeatures.add(mCCC);
}
mFCC = new FastChargeController(mContext, mHandler);
if (mFCC.isSupported()) {
mFeatures.add(mFCC);
}
if (!mFeatures.isEmpty()) {
publishBinderService(LineageContextConstants.LINEAGE_HEALTH_INTERFACE, mService);
@@ -156,6 +161,26 @@ public class HealthInterfaceService extends LineageSystemService {
|| mCCC.isChargingModeSupported(ChargingControlSupportedMode.LIMIT);
}
@Override
public boolean isFastChargeSupported() {
return mFCC.isSupported();
}
@Override
public int[] getSupportedFastChargeModes() {
return mFCC.getSupportedFastChargeModes();
}
@Override
public int getFastChargeMode() {
return mFCC.getFastChargeMode();
}
@Override
public boolean setFastChargeMode(int mode) {
return mFCC.setFastChargeMode(mode);
}
@Override
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
mContext.enforceCallingOrSelfPermission(Manifest.permission.DUMP, TAG);

View File

@@ -1,10 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2015 The CyanogenMod Project
SPDX-FileCopyrightText: 2022 The LineageOS Project
SPDX-FileCopyrightText: 2022-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Fast charge -->
<string-array name="charging_speed_entries" translatable="false">
<item>@string/charging_speed_slow</item>
<item>@string/charging_speed_fast</item>
<item>@string/charging_speed_super_fast</item>
</string-array>
<string-array name="charging_speed_values" translatable="false">
<item>1</item> <!-- FastChargeMode.NONE -->
<item>2</item> <!-- FastChargeMode.FAST_CHARGE -->
<item>4</item> <!-- FastChargeMode.SUPER_FAST_CHARGE -->
</string-array>
<!-- LiveDisplay -->
<string-array name="live_display_entries" translatable="false">
<item>@string/live_display_auto</item>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2015 The CyanogenMod Project
SPDX-FileCopyrightText: 2017-2023 The LineageOS Project
SPDX-FileCopyrightText: 2017-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
@@ -43,6 +43,12 @@
<!-- Name of wildcard profile. -->
<string name="wildcardProfile">Other</string>
<!-- Charging speed strings -->
<string name="charging_speed">Charging speed</string>
<string name="charging_speed_slow">Slow</string>
<string name="charging_speed_fast">Fast</string>
<string name="charging_speed_super_fast">Super fast</string>
<!-- LiveDisplay strings -->
<string name="live_display_title" translatable="false">LiveDisplay</string>
<string name="live_display_auto">Automatic</string>

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2015 The CyanogenMod Project
SPDX-FileCopyrightText: 2017-2024 The LineageOS Project
SPDX-FileCopyrightText: 2017-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<resources>
@@ -24,6 +24,11 @@
<java-symbol type="bool" name="config_dt2sGestureAvailable" />
<java-symbol type="bool" name="config_dt2sGestureEnabledByDefault" />
<!-- Charging speed -->
<java-symbol type="string" name="charging_speed" />
<java-symbol type="array" name="charging_speed_entries" />
<java-symbol type="array" name="charging_speed_values" />
<!-- LiveDisplay -->
<java-symbol type="bool" name="config_enableLiveDisplay" />
<java-symbol type="string" name="live_display_title" />

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 The LineageOS Project
* SPDX-FileCopyrightText: 2023-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
@@ -283,4 +283,65 @@ public class HealthInterface {
return false;
}
}
/**
* Returns whether fast charge is supported
*
* @return true if fast charge is supported
*/
public boolean isFastChargeSupported() {
try {
return checkService() && sService.isFastChargeSupported();
} catch (RemoteException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return false;
}
/**
* Gets supported fast charge mode
*
* @return true supported fast charge modes
*/
public int[] getSupportedFastChargeModes() {
try {
return checkService() ? sService.getSupportedFastChargeModes() : new int[0];
} catch (RemoteException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return new int[0];
}
/**
* Gets current fast charge mode
*
* @return true current fast charge mode
*/
public int getFastChargeMode() {
try {
return checkService() ? sService.getFastChargeMode() : 0;
} catch (RemoteException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return 0;
}
/**
* Sets selected fast charge mode
*
* @param mode the fast charge mode
* @return true if fast charge was set
*/
public boolean setFastChargeMode(int mode) {
try {
return checkService() && sService.setFastChargeMode(mode);
} catch (RemoteException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return false;
}
}

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 The LineageOS Project
* SPDX-FileCopyrightText: 2023-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
@@ -26,4 +26,9 @@ interface IHealthInterface {
boolean resetChargingControl();
boolean allowFineGrainedSettings();
boolean isFastChargeSupported();
int[] getSupportedFastChargeModes();
int getFastChargeMode();
boolean setFastChargeMode(int mode);
}

View File

@@ -1,6 +1,6 @@
/*
* SPDX-FileCopyrightText: 2015-2016 The CyanogenMod Project
* SPDX-FileCopyrightText: 2017-2023 The LineageOS Project
* SPDX-FileCopyrightText: 2017-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
@@ -26,6 +26,8 @@ import com.android.internal.util.ArrayUtils;
import lineageos.trust.TrustInterface;
import vendor.lineage.health.FastChargeMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -1401,6 +1403,19 @@ public final class LineageSettings {
public static final Validator CHARGING_CONTROL_LIMIT_VALIDATOR =
new InclusiveIntegerRangeValidator(70, 100);
/**
* Fast charging mode
*/
public static final String FAST_CHARGE_MODE = "fast_charge_mode";
/** @hide */
public static final Validator FAST_CHARGE_MODE_VALIDATOR =
new DiscreteValueValidator(new String[] {
String.valueOf(FastChargeMode.NONE),
String.valueOf(FastChargeMode.FAST_CHARGE),
String.valueOf(FastChargeMode.SUPER_FAST_CHARGE),
});
/**
* Whether the battery light should be enabled (if hardware supports it)
* The value is boolean (1 or 0).
@@ -2166,6 +2181,7 @@ public final class LineageSettings {
VALIDATORS.put(CHARGING_CONTROL_START_TIME, CHARGING_CONTROL_START_TIME_VALIDATOR);
VALIDATORS.put(CHARGING_CONTROL_TARGET_TIME, CHARGING_CONTROL_TARGET_TIME_VALIDATOR);
VALIDATORS.put(CHARGING_CONTROL_LIMIT, CHARGING_CONTROL_LIMIT_VALIDATOR);
VALIDATORS.put(FAST_CHARGE_MODE, FAST_CHARGE_MODE_VALIDATOR);
VALIDATORS.put(BATTERY_LIGHT_ENABLED, BATTERY_LIGHT_ENABLED_VALIDATOR);
VALIDATORS.put(BATTERY_LIGHT_FULL_CHARGE_DISABLED,
BATTERY_LIGHT_FULL_CHARGE_DISABLED_VALIDATOR);