Skip to content

Commit

Permalink
base: Implement Lineage health service
Browse files Browse the repository at this point in the history
Change-Id: I772ccf6d323c24d681aa8468bf4318c7b73bd3f5
Signed-off-by: Mohammad Hasan Keramat J <ikeramat@protonmail.com>
  • Loading branch information
hellobbn authored and bheatleyyy committed Sep 28, 2023
1 parent fd66587 commit d0bf39e
Show file tree
Hide file tree
Showing 13 changed files with 1,481 additions and 0 deletions.
1 change: 1 addition & 0 deletions Android.bp
Expand Up @@ -251,6 +251,7 @@ java_library {
"vendor.lineage.livedisplay-V2.0-java",
"vendor.lineage.livedisplay-V2.1-java",
"vendor.lineage.touch-V1.0-java",
"vendor.lineage.health-V1-java",
],
sdk_version: "core_platform",
installable: false,
Expand Down
Expand Up @@ -43,6 +43,17 @@ private LineageContextConstants() {
*/
public static final String LINEAGE_HARDWARE_SERVICE = "lineagehardware";

/**
* 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";

/**
* Manages display color adjustments
*
Expand Down
269 changes: 269 additions & 0 deletions core/java/com/android/internal/lineage/health/HealthInterface.java
@@ -0,0 +1,269 @@
/*
* 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 com.android.internal.lineage.health;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

import com.android.internal.lineage.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();
}
/**
* 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;
}
}
}
@@ -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 com.android.internal.lineage.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();
}
3 changes: 3 additions & 0 deletions core/res/AndroidManifest.xml
Expand Up @@ -6611,6 +6611,9 @@
<protected-broadcast android:name="lineageos.platform.intent.action.UPDATE_TWILIGHT_STATE" />
<protected-broadcast android:name="lineageos.intent.action.INITIALIZE_LIVEDISPLAY" />

<!-- LineageHealth -->
<protected-broadcast android:name="lineageos.platform.intent.action.CHARGING_CONTROL_CANCEL_ONCE" />

<!-- @hide -->
<permission android:name="lineageos.permission.HARDWARE_ABSTRACTION_ACCESS"
android:protectionLevel="signature|privileged" />
Expand Down
27 changes: 27 additions & 0 deletions core/res/res/drawable/ic_charging_control.xml
@@ -0,0 +1,27 @@
<!--
Copyright (C) 2018 The Android Open Source 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M16.2,22.5H7.8c-1.3,0 -2.3,-1 -2.3,-2.3V5.8c0,-1.3 1,-2.3 2.3,-2.3h0.7v-2h7v2h0.7c1.3,0 2.3,1.1 2.3,2.3v14.3C18.5,21.5 17.5,22.5 16.2,22.5zM7.8,5.5c-0.2,0 -0.3,0.2 -0.3,0.3v14.3c0,0.2 0.2,0.3 0.3,0.3h8.3c0.2,0 0.3,-0.1 0.3,-0.3V5.8c0,-0.2 -0.1,-0.3 -0.3,-0.3h-2.7v-2h-3v2H7.8z"/>
<path
android:fillColor="#FF000000"
android:pathData="M11.17,18.42v-4.58H9.5l3.33,-6.25v4.58h1.67L11.17,18.42z"/>
</vector>

0 comments on commit d0bf39e

Please sign in to comment.