sdk: Implement Lineage health service

Change-Id: I772ccf6d323c24d681aa8468bf4318c7b73bd3f5
This commit is contained in:
Luofan Chen
2023-03-01 19:12:53 +08:00
committed by Michael Bestas
parent 89941a9622
commit 3ee210210d
12 changed files with 1525 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
/**
* Copyright (C) 2015, The CyanogenMod Project
* 2017-2022 The LineageOS Project
* 2017-2023 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -81,6 +81,17 @@ public final class LineageContextConstants {
*/
public static final String LINEAGE_TRUST_INTERFACE = "lineagetrust";
/**
* Use with {@link android.content.Context#getSystemService} to retrieve a
* {@link lineageos.health.HealthInterface} to access the Health interface.
*
* @see android.content.Context#getSystemService
* @see lineageos.health.HealthInterface
*
* @hide
*/
public static final String LINEAGE_HEALTH_INTERFACE = "lineagehealth";
/**
* Update power menu (GlobalActions)
*
@@ -155,5 +166,13 @@ public final class LineageContextConstants {
*/
@SdkConstant(SdkConstant.SdkConstantType.FEATURE)
public static final String GLOBAL_ACTIONS = "org.lineageos.globalactions";
/**
* Feature for {@link PackageManager#getSystemAvailableFeatures} and
* {@link PackageManager#hasSystemFeature}: The device includes the lineage health
* service utilized by the lineage sdk and LineageParts.
*/
@SdkConstant(SdkConstant.SdkConstantType.FEATURE)
public static final String HEALTH = "org.lineageos.health";
}
}

View File

@@ -0,0 +1,282 @@
/*
* Copyright (C) 2023 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lineageos.health;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import lineageos.app.LineageContextConstants;
public class HealthInterface {
/**
* No config set. This value is invalid and does not have any effects
*/
public static final int MODE_NONE = 0;
/**
* Automatic config
*/
public static final int MODE_AUTO = 1;
/**
* Manual config mode
*/
public static final int MODE_MANUAL = 2;
/**
* Limit config mode
*/
public static final int MODE_LIMIT = 3;
private static final String TAG = "HealthInterface";
private static IHealthInterface sService;
private static HealthInterface sInstance;
private Context mContext;
private HealthInterface(Context context) {
Context appContext = context.getApplicationContext();
mContext = appContext == null ? context : appContext;
sService = getService();
if (context.getPackageManager().hasSystemFeature(
LineageContextConstants.Features.HEALTH) && sService == null) {
throw new RuntimeException("Unable to get HealthInterfaceService. The service" +
" either crashed, was not started, or the interface has been called too early" +
" in SystemServer init");
}
}
/**
* Get or create an instance of the {@link lineageos.health.HealthInterface}
*
* @param context Used to get the service
* @return {@link HealthInterface}
*/
public static synchronized HealthInterface getInstance(Context context) {
if (sInstance == null) {
sInstance = new HealthInterface(context);
}
return sInstance;
}
/** @hide **/
public static IHealthInterface getService() {
if (sService != null) {
return sService;
}
IBinder b = ServiceManager.getService(LineageContextConstants.LINEAGE_HEALTH_INTERFACE);
sService = IHealthInterface.Stub.asInterface(b);
if (sService == null) {
Log.e(TAG, "null health service, SAD!");
return null;
}
return sService;
}
/**
* @return true if service is valid
*/
private boolean checkService() {
if (sService == null) {
Log.w(TAG, "not connected to LineageHardwareManagerService");
return false;
}
return true;
}
/**
* Returns whether charging control is supported
*
* @return true if charging control is supported
*/
public boolean isChargingControlSupported() {
try {
return checkService() && sService.isChargingControlSupported();
} catch (RemoteException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return false;
}
/**
* Returns the charging control enabled status
*
* @return whether charging control has been enabled
*/
public boolean getEnabled() {
try {
return checkService() && sService.getChargingControlEnabled();
} catch (RemoteException e) {
return false;
}
}
/**
* Set charging control enable status
*
* @param enabled whether charging control should be enabled
* @return true if the enabled status was successfully set
*/
public boolean setEnabled(boolean enabled) {
try {
return checkService() && sService.setChargingControlEnabled(enabled);
} catch (RemoteException e) {
return false;
}
}
/**
* Returns the current charging control mode
*
* @return id of the charging control mode
*/
public int getMode() {
try {
return checkService() ? sService.getChargingControlMode() : MODE_NONE;
} catch (RemoteException e) {
return MODE_NONE;
}
}
/**
* Selects the new charging control mode
*
* @param mode the new charging control mode
* @return true if the mode was successfully set
*/
public boolean setMode(int mode) {
try {
return checkService() && sService.setChargingControlMode(mode);
} catch (RemoteException e) {
return false;
}
}
/**
* Gets the charging control start time
*
* @return the seconds of the day of the start time
*/
public int getStartTime() {
try {
return checkService() ? sService.getChargingControlStartTime() : 0;
} catch (RemoteException e) {
return 0;
}
}
/**
* Sets the charging control start time
*
* @param time the seconds of the day of the start time
* @return true if the start time was successfully set
*/
public boolean setStartTime(int time) {
try {
return checkService() && sService.setChargingControlStartTime(time);
} catch (RemoteException e) {
return false;
}
}
/**
* Gets the charging control target time
*
* @return the seconds of the day of the target time
*/
public int getTargetTime() {
try {
return checkService() ? sService.getChargingControlTargetTime() : 0;
} catch (RemoteException e) {
return 0;
}
}
/**
* Sets the charging control target time
*
* @param time the seconds of the day of the target time
* @return true if the target time was successfully set
*/
public boolean setTargetTime(int time) {
try {
return checkService() && sService.setChargingControlTargetTime(time);
} catch (RemoteException e) {
return false;
}
}
/**
* Gets the charging control limit
*
* @return the charging control limit
*/
public int getLimit() {
try {
return checkService() ? sService.getChargingControlLimit() : 100;
} catch (RemoteException e) {
return 0;
}
}
/**
* Sets the charging control limit
*
* @param limit the charging control limit
* @return true if the limit was successfully set
*/
public boolean setLimit(int limit) {
try {
return checkService() && sService.setChargingControlLimit(limit);
} catch (RemoteException e) {
return false;
}
}
/**
* Resets the charging control setting to default
*
* @return true if the setting was successfully reset
*/
public boolean reset() {
try {
return checkService() && sService.resetChargingControl();
} catch (RemoteException e) {
return false;
}
}
/**
* Returns whether the device's battery control bypasses battery
*
* @return true if the charging control bypasses battery
*/
public boolean allowFineGrainedSettings() {
try {
return checkService() && sService.allowFineGrainedSettings();
} catch (RemoteException e) {
return false;
}
}
}

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2023 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lineageos.health;
/** @hide */
interface IHealthInterface {
boolean isChargingControlSupported();
boolean getChargingControlEnabled();
boolean setChargingControlEnabled(boolean enabled);
int getChargingControlMode();
boolean setChargingControlMode(int mode);
int getChargingControlStartTime();
boolean setChargingControlStartTime(int time);
int getChargingControlTargetTime();
boolean setChargingControlTargetTime(int time);
int getChargingControlLimit();
boolean setChargingControlLimit(int limit);
boolean resetChargingControl();
boolean allowFineGrainedSettings();
}