Luofan Chen | 120a40d | 2023-03-01 19:12:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The LineageOS Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package omnirom.health; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.IBinder; |
| 21 | import android.os.RemoteException; |
| 22 | import android.os.ServiceManager; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import omnirom.app.OmniRomContextConstants; |
| 26 | |
| 27 | public class HealthInterface { |
| 28 | /** |
| 29 | * No config set. This value is invalid and does not have any effects |
| 30 | */ |
| 31 | public static final int MODE_NONE = 0; |
| 32 | |
| 33 | /** |
| 34 | * Automatic config |
| 35 | */ |
| 36 | public static final int MODE_AUTO = 1; |
| 37 | |
| 38 | /** |
| 39 | * Manual config mode |
| 40 | */ |
| 41 | public static final int MODE_MANUAL = 2; |
| 42 | |
| 43 | /** |
| 44 | * Limit config mode |
| 45 | */ |
| 46 | public static final int MODE_LIMIT = 3; |
| 47 | |
| 48 | private static final String TAG = "HealthInterface"; |
| 49 | private static IHealthInterface sService; |
| 50 | private static HealthInterface sInstance; |
| 51 | private Context mContext; |
| 52 | |
| 53 | private HealthInterface(Context context) { |
| 54 | Context appContext = context.getApplicationContext(); |
| 55 | mContext = appContext == null ? context : appContext; |
| 56 | sService = getService(); |
| 57 | |
| 58 | if (context.getPackageManager().hasSystemFeature( |
| 59 | OmniRomContextConstants.Features.HEALTH) && sService == null) { |
| 60 | throw new RuntimeException("Unable to get HealthInterfaceService. The service" + |
| 61 | " either crashed, was not started, or the interface has been called too early" + |
| 62 | " in SystemServer init"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get or create an instance of the {@link omnirom.health.HealthInterface} |
| 68 | * |
| 69 | * @param context Used to get the service |
| 70 | * @return {@link HealthInterface} |
| 71 | */ |
| 72 | public static synchronized HealthInterface getInstance(Context context) { |
| 73 | if (sInstance == null) { |
| 74 | sInstance = new HealthInterface(context); |
| 75 | } |
| 76 | |
| 77 | return sInstance; |
| 78 | } |
| 79 | |
| 80 | /** @hide **/ |
| 81 | public static IHealthInterface getService() { |
| 82 | if (sService != null) { |
| 83 | return sService; |
| 84 | } |
| 85 | IBinder b = ServiceManager.getService(OmniRomContextConstants.LINEAGE_HEALTH_INTERFACE); |
| 86 | sService = IHealthInterface.Stub.asInterface(b); |
| 87 | |
| 88 | if (sService == null) { |
| 89 | Log.e(TAG, "null health service, SAD!"); |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | return sService; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return true if service is valid |
| 98 | */ |
| 99 | private boolean checkService() { |
| 100 | if (sService == null) { |
| 101 | Log.w(TAG, "not connected to OmniRomHardwareManagerService"); |
| 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns whether charging control is supported |
| 109 | * |
| 110 | * @return true if charging control is supported |
| 111 | */ |
| 112 | public boolean isChargingControlSupported() { |
| 113 | try { |
| 114 | return checkService() && sService.isChargingControlSupported(); |
| 115 | } catch (RemoteException e) { |
| 116 | Log.e(TAG, e.getLocalizedMessage(), e); |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /** |
Ćukasz Patron | 22805e6 | 2025-01-13 16:31:55 -0500 | [diff] [blame] | 123 | * Returns whether charging control is supported |
| 124 | * |
| 125 | * @return true if charging control is supported |
| 126 | */ |
| 127 | public static boolean isChargingControlSupported(Context context) { |
| 128 | try { |
| 129 | return getInstance(context).isChargingControlSupported(); |
| 130 | } catch (RuntimeException e) { |
| 131 | Log.e(TAG, e.getLocalizedMessage(), e); |
| 132 | } |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | /** |
Luofan Chen | 120a40d | 2023-03-01 19:12:53 +0800 | [diff] [blame] | 138 | * Returns the charging control enabled status |
| 139 | * |
| 140 | * @return whether charging control has been enabled |
| 141 | */ |
| 142 | public boolean getEnabled() { |
| 143 | try { |
| 144 | return checkService() && sService.getChargingControlEnabled(); |
| 145 | } catch (RemoteException e) { |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set charging control enable status |
| 152 | * |
| 153 | * @param enabled whether charging control should be enabled |
| 154 | * @return true if the enabled status was successfully set |
| 155 | */ |
| 156 | public boolean setEnabled(boolean enabled) { |
| 157 | try { |
| 158 | return checkService() && sService.setChargingControlEnabled(enabled); |
| 159 | } catch (RemoteException e) { |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns the current charging control mode |
| 166 | * |
| 167 | * @return id of the charging control mode |
| 168 | */ |
| 169 | public int getMode() { |
| 170 | try { |
| 171 | return checkService() ? sService.getChargingControlMode() : MODE_NONE; |
| 172 | } catch (RemoteException e) { |
| 173 | return MODE_NONE; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Selects the new charging control mode |
| 179 | * |
| 180 | * @param mode the new charging control mode |
| 181 | * @return true if the mode was successfully set |
| 182 | */ |
| 183 | public boolean setMode(int mode) { |
| 184 | try { |
| 185 | return checkService() && sService.setChargingControlMode(mode); |
| 186 | } catch (RemoteException e) { |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Gets the charging control start time |
| 193 | * |
| 194 | * @return the seconds of the day of the start time |
| 195 | */ |
| 196 | public int getStartTime() { |
| 197 | try { |
| 198 | return checkService() ? sService.getChargingControlStartTime() : 0; |
| 199 | } catch (RemoteException e) { |
| 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the charging control start time |
| 206 | * |
| 207 | * @param time the seconds of the day of the start time |
| 208 | * @return true if the start time was successfully set |
| 209 | */ |
| 210 | public boolean setStartTime(int time) { |
| 211 | try { |
| 212 | return checkService() && sService.setChargingControlStartTime(time); |
| 213 | } catch (RemoteException e) { |
| 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Gets the charging control target time |
| 220 | * |
| 221 | * @return the seconds of the day of the target time |
| 222 | */ |
| 223 | public int getTargetTime() { |
| 224 | try { |
| 225 | return checkService() ? sService.getChargingControlTargetTime() : 0; |
| 226 | } catch (RemoteException e) { |
| 227 | return 0; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Sets the charging control target time |
| 233 | * |
| 234 | * @param time the seconds of the day of the target time |
| 235 | * @return true if the target time was successfully set |
| 236 | */ |
| 237 | public boolean setTargetTime(int time) { |
| 238 | try { |
| 239 | return checkService() && sService.setChargingControlTargetTime(time); |
| 240 | } catch (RemoteException e) { |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Gets the charging control limit |
| 247 | * |
| 248 | * @return the charging control limit |
| 249 | */ |
| 250 | public int getLimit() { |
| 251 | try { |
| 252 | return checkService() ? sService.getChargingControlLimit() : 100; |
| 253 | } catch (RemoteException e) { |
| 254 | return 0; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Sets the charging control limit |
| 260 | * |
| 261 | * @param limit the charging control limit |
| 262 | * @return true if the limit was successfully set |
| 263 | */ |
| 264 | public boolean setLimit(int limit) { |
| 265 | try { |
| 266 | return checkService() && sService.setChargingControlLimit(limit); |
| 267 | } catch (RemoteException e) { |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Resets the charging control setting to default |
| 274 | * |
| 275 | * @return true if the setting was successfully reset |
| 276 | */ |
| 277 | public boolean reset() { |
| 278 | try { |
| 279 | return checkService() && sService.resetChargingControl(); |
| 280 | } catch (RemoteException e) { |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns whether the device's battery control bypasses battery |
| 287 | * |
| 288 | * @return true if the charging control bypasses battery |
| 289 | */ |
| 290 | public boolean allowFineGrainedSettings() { |
| 291 | try { |
| 292 | return checkService() && sService.allowFineGrainedSettings(); |
| 293 | } catch (RemoteException e) { |
| 294 | return false; |
| 295 | } |
| 296 | } |
| 297 | } |