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 | /** |
| 123 | * Returns the charging control enabled status |
| 124 | * |
| 125 | * @return whether charging control has been enabled |
| 126 | */ |
| 127 | public boolean getEnabled() { |
| 128 | try { |
| 129 | return checkService() && sService.getChargingControlEnabled(); |
| 130 | } catch (RemoteException e) { |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Set charging control enable status |
| 137 | * |
| 138 | * @param enabled whether charging control should be enabled |
| 139 | * @return true if the enabled status was successfully set |
| 140 | */ |
| 141 | public boolean setEnabled(boolean enabled) { |
| 142 | try { |
| 143 | return checkService() && sService.setChargingControlEnabled(enabled); |
| 144 | } catch (RemoteException e) { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the current charging control mode |
| 151 | * |
| 152 | * @return id of the charging control mode |
| 153 | */ |
| 154 | public int getMode() { |
| 155 | try { |
| 156 | return checkService() ? sService.getChargingControlMode() : MODE_NONE; |
| 157 | } catch (RemoteException e) { |
| 158 | return MODE_NONE; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Selects the new charging control mode |
| 164 | * |
| 165 | * @param mode the new charging control mode |
| 166 | * @return true if the mode was successfully set |
| 167 | */ |
| 168 | public boolean setMode(int mode) { |
| 169 | try { |
| 170 | return checkService() && sService.setChargingControlMode(mode); |
| 171 | } catch (RemoteException e) { |
| 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets the charging control start time |
| 178 | * |
| 179 | * @return the seconds of the day of the start time |
| 180 | */ |
| 181 | public int getStartTime() { |
| 182 | try { |
| 183 | return checkService() ? sService.getChargingControlStartTime() : 0; |
| 184 | } catch (RemoteException e) { |
| 185 | return 0; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets the charging control start time |
| 191 | * |
| 192 | * @param time the seconds of the day of the start time |
| 193 | * @return true if the start time was successfully set |
| 194 | */ |
| 195 | public boolean setStartTime(int time) { |
| 196 | try { |
| 197 | return checkService() && sService.setChargingControlStartTime(time); |
| 198 | } catch (RemoteException e) { |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Gets the charging control target time |
| 205 | * |
| 206 | * @return the seconds of the day of the target time |
| 207 | */ |
| 208 | public int getTargetTime() { |
| 209 | try { |
| 210 | return checkService() ? sService.getChargingControlTargetTime() : 0; |
| 211 | } catch (RemoteException e) { |
| 212 | return 0; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Sets the charging control target time |
| 218 | * |
| 219 | * @param time the seconds of the day of the target time |
| 220 | * @return true if the target time was successfully set |
| 221 | */ |
| 222 | public boolean setTargetTime(int time) { |
| 223 | try { |
| 224 | return checkService() && sService.setChargingControlTargetTime(time); |
| 225 | } catch (RemoteException e) { |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Gets the charging control limit |
| 232 | * |
| 233 | * @return the charging control limit |
| 234 | */ |
| 235 | public int getLimit() { |
| 236 | try { |
| 237 | return checkService() ? sService.getChargingControlLimit() : 100; |
| 238 | } catch (RemoteException e) { |
| 239 | return 0; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Sets the charging control limit |
| 245 | * |
| 246 | * @param limit the charging control limit |
| 247 | * @return true if the limit was successfully set |
| 248 | */ |
| 249 | public boolean setLimit(int limit) { |
| 250 | try { |
| 251 | return checkService() && sService.setChargingControlLimit(limit); |
| 252 | } catch (RemoteException e) { |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Resets the charging control setting to default |
| 259 | * |
| 260 | * @return true if the setting was successfully reset |
| 261 | */ |
| 262 | public boolean reset() { |
| 263 | try { |
| 264 | return checkService() && sService.resetChargingControl(); |
| 265 | } catch (RemoteException e) { |
| 266 | return false; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Returns whether the device's battery control bypasses battery |
| 272 | * |
| 273 | * @return true if the charging control bypasses battery |
| 274 | */ |
| 275 | public boolean allowFineGrainedSettings() { |
| 276 | try { |
| 277 | return checkService() && sService.allowFineGrainedSettings(); |
| 278 | } catch (RemoteException e) { |
| 279 | return false; |
| 280 | } |
| 281 | } |
| 282 | } |