From e3790a355b3d091b01f4ddcbd057ee47f482e621 Mon Sep 17 00:00:00 2001 From: LuK1337 Date: Wed, 13 Aug 2025 23:02:15 +0200 Subject: [PATCH] sdk: Add lineagehealth IFastCharge interface support Change-Id: I89eb3efbc79146b08684fa41f3c8d62ef2fb60c4 --- .../internal/health/FastChargeController.java | 132 ++++++++++++++++++ .../health/HealthInterfaceService.java | 27 +++- lineage/res/res/values/arrays.xml | 15 +- lineage/res/res/values/strings.xml | 8 +- lineage/res/res/values/symbols.xml | 7 +- .../lineageos/health/HealthInterface.java | 63 ++++++++- .../lineageos/health/IHealthInterface.aidl | 7 +- .../lineageos/providers/LineageSettings.java | 18 ++- 8 files changed, 270 insertions(+), 7 deletions(-) create mode 100644 lineage/lib/main/java/org/lineageos/platform/internal/health/FastChargeController.java diff --git a/lineage/lib/main/java/org/lineageos/platform/internal/health/FastChargeController.java b/lineage/lib/main/java/org/lineageos/platform/internal/health/FastChargeController.java new file mode 100644 index 00000000..cba2b60a --- /dev/null +++ b/lineage/lib/main/java/org/lineageos/platform/internal/health/FastChargeController.java @@ -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(); + } +} diff --git a/lineage/lib/main/java/org/lineageos/platform/internal/health/HealthInterfaceService.java b/lineage/lib/main/java/org/lineageos/platform/internal/health/HealthInterfaceService.java index 699d5ff0..0431c3eb 100644 --- a/lineage/lib/main/java/org/lineageos/platform/internal/health/HealthInterfaceService.java +++ b/lineage/lib/main/java/org/lineageos/platform/internal/health/HealthInterfaceService.java @@ -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); diff --git a/lineage/res/res/values/arrays.xml b/lineage/res/res/values/arrays.xml index 577c9e75..fc115bb2 100644 --- a/lineage/res/res/values/arrays.xml +++ b/lineage/res/res/values/arrays.xml @@ -1,10 +1,23 @@ + + + @string/charging_speed_slow + @string/charging_speed_fast + @string/charging_speed_super_fast + + + + 1 + 2 + 4 + + @string/live_display_auto diff --git a/lineage/res/res/values/strings.xml b/lineage/res/res/values/strings.xml index e14ca223..a88c7ba0 100644 --- a/lineage/res/res/values/strings.xml +++ b/lineage/res/res/values/strings.xml @@ -1,7 +1,7 @@ @@ -43,6 +43,12 @@ Other + + Charging speed + Slow + Fast + Super fast + LiveDisplay Automatic diff --git a/lineage/res/res/values/symbols.xml b/lineage/res/res/values/symbols.xml index e5e8f8fd..99427a37 100644 --- a/lineage/res/res/values/symbols.xml +++ b/lineage/res/res/values/symbols.xml @@ -1,7 +1,7 @@ @@ -24,6 +24,11 @@ + + + + + diff --git a/sdk/src/java/lineageos/health/HealthInterface.java b/sdk/src/java/lineageos/health/HealthInterface.java index a61a3682..40f438a9 100644 --- a/sdk/src/java/lineageos/health/HealthInterface.java +++ b/sdk/src/java/lineageos/health/HealthInterface.java @@ -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; + } } diff --git a/sdk/src/java/lineageos/health/IHealthInterface.aidl b/sdk/src/java/lineageos/health/IHealthInterface.aidl index 8abe64d7..633f104b 100644 --- a/sdk/src/java/lineageos/health/IHealthInterface.aidl +++ b/sdk/src/java/lineageos/health/IHealthInterface.aidl @@ -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); } diff --git a/sdk/src/java/lineageos/providers/LineageSettings.java b/sdk/src/java/lineageos/providers/LineageSettings.java index 4b9403eb..9fe5cb41 100644 --- a/sdk/src/java/lineageos/providers/LineageSettings.java +++ b/sdk/src/java/lineageos/providers/LineageSettings.java @@ -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);