Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source 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 | package com.android.server.power.batterysaver; |
| 17 | |
| 18 | import android.Manifest; |
Makoto Onuki | e098b759 | 2017-11-27 17:50:41 -0800 | [diff] [blame] | 19 | import android.app.ActivityManagerInternal; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.hardware.power.V1_0.PowerHint; |
| 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| 28 | import android.os.PowerManager; |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 29 | import android.os.PowerManagerInternal; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 30 | import android.os.PowerManagerInternal.LowPowerModeListener; |
| 31 | import android.os.PowerSaveState; |
| 32 | import android.os.UserHandle; |
| 33 | import android.util.ArrayMap; |
| 34 | import android.util.Slog; |
| 35 | import android.widget.Toast; |
| 36 | |
| 37 | import com.android.internal.annotations.GuardedBy; |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 38 | import com.android.internal.util.ArrayUtils; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 39 | import com.android.internal.util.Preconditions; |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 40 | import com.android.server.LocalServices; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 41 | import com.android.server.power.BatterySaverPolicy; |
| 42 | import com.android.server.power.BatterySaverPolicy.BatterySaverPolicyListener; |
| 43 | import com.android.server.power.PowerManagerService; |
| 44 | |
| 45 | import java.util.ArrayList; |
| 46 | |
| 47 | /** |
| 48 | * Responsible for battery saver mode transition logic. |
| 49 | */ |
| 50 | public class BatterySaverController implements BatterySaverPolicyListener { |
| 51 | static final String TAG = "BatterySaverController"; |
| 52 | |
Makoto Onuki | ce643a3 | 2017-11-20 14:31:06 -0800 | [diff] [blame] | 53 | static final boolean DEBUG = BatterySaverPolicy.DEBUG; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 54 | |
| 55 | private final Object mLock = new Object(); |
| 56 | private final Context mContext; |
| 57 | private final MyHandler mHandler; |
| 58 | private final FileUpdater mFileUpdater; |
| 59 | |
| 60 | private PowerManager mPowerManager; |
| 61 | |
| 62 | private final BatterySaverPolicy mBatterySaverPolicy; |
| 63 | |
| 64 | @GuardedBy("mLock") |
| 65 | private final ArrayList<LowPowerModeListener> mListeners = new ArrayList<>(); |
| 66 | |
| 67 | @GuardedBy("mLock") |
| 68 | private boolean mEnabled; |
| 69 | |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 70 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 71 | @Override |
| 72 | public void onReceive(Context context, Intent intent) { |
| 73 | switch (intent.getAction()) { |
| 74 | case Intent.ACTION_SCREEN_ON: |
| 75 | case Intent.ACTION_SCREEN_OFF: |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 76 | if (!isEnabled()) { |
| 77 | return; // No need to send it if not enabled. |
| 78 | } |
| 79 | // Don't send the broadcast, because we never did so in this case. |
| 80 | mHandler.postStateChanged(/*sendBroadcast=*/ false); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Constructor. |
| 88 | */ |
| 89 | public BatterySaverController(Context context, Looper looper, BatterySaverPolicy policy) { |
| 90 | mContext = context; |
| 91 | mHandler = new MyHandler(looper); |
| 92 | mBatterySaverPolicy = policy; |
| 93 | mBatterySaverPolicy.addListener(this); |
| 94 | mFileUpdater = new FileUpdater(context); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Add a listener. |
| 99 | */ |
| 100 | public void addListener(LowPowerModeListener listener) { |
| 101 | synchronized (mLock) { |
| 102 | mListeners.add(listener); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Called by {@link PowerManagerService} on system ready.. |
| 108 | */ |
| 109 | public void systemReady() { |
| 110 | final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); |
| 111 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 112 | mContext.registerReceiver(mReceiver, filter); |
Makoto Onuki | e098b759 | 2017-11-27 17:50:41 -0800 | [diff] [blame] | 113 | |
| 114 | mFileUpdater.systemReady(LocalServices.getService(ActivityManagerInternal.class) |
| 115 | .isRuntimeRestarted()); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private PowerManager getPowerManager() { |
| 119 | if (mPowerManager == null) { |
| 120 | mPowerManager = |
| 121 | Preconditions.checkNotNull(mContext.getSystemService(PowerManager.class)); |
| 122 | } |
| 123 | return mPowerManager; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onBatterySaverPolicyChanged(BatterySaverPolicy policy) { |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 128 | if (!isEnabled()) { |
| 129 | return; // No need to send it if not enabled. |
| 130 | } |
| 131 | mHandler.postStateChanged(/*sendBroadcast=*/ true); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | private class MyHandler extends Handler { |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 135 | private static final int MSG_STATE_CHANGED = 1; |
| 136 | |
| 137 | private static final int ARG_DONT_SEND_BROADCAST = 0; |
| 138 | private static final int ARG_SEND_BROADCAST = 1; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 139 | |
| 140 | public MyHandler(Looper looper) { |
| 141 | super(looper); |
| 142 | } |
| 143 | |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 144 | public void postStateChanged(boolean sendBroadcast) { |
| 145 | obtainMessage(MSG_STATE_CHANGED, sendBroadcast ? |
| 146 | ARG_SEND_BROADCAST : ARG_DONT_SEND_BROADCAST, 0).sendToTarget(); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void dispatchMessage(Message msg) { |
| 151 | switch (msg.what) { |
| 152 | case MSG_STATE_CHANGED: |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 153 | handleBatterySaverStateChanged(msg.arg1 == ARG_SEND_BROADCAST); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Called by {@link PowerManagerService} to update the battery saver stete. |
| 161 | */ |
| 162 | public void enableBatterySaver(boolean enable) { |
| 163 | synchronized (mLock) { |
| 164 | if (mEnabled == enable) { |
| 165 | return; |
| 166 | } |
| 167 | mEnabled = enable; |
| 168 | |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 169 | mHandler.postStateChanged(/*sendBroadcast=*/ true); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** @return whether battery saver is enabled or not. */ |
| 174 | boolean isEnabled() { |
| 175 | synchronized (mLock) { |
| 176 | return mEnabled; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Makoto Onuki | a7d8c4d | 2017-11-20 15:20:16 -0800 | [diff] [blame] | 181 | * @return true if launch boost should currently be disabled. |
| 182 | */ |
| 183 | public boolean isLaunchBoostDisabled() { |
| 184 | return isEnabled() && mBatterySaverPolicy.isLaunchBoostDisabled(); |
| 185 | } |
| 186 | |
| 187 | /** |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 188 | * Dispatch power save events to the listeners. |
| 189 | * |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 190 | * This method is always called on the handler thread. |
| 191 | * |
| 192 | * This method is called only in the following cases: |
| 193 | * - When battery saver becomes activated. |
| 194 | * - When battery saver becomes deactivated. |
| 195 | * - When battery saver is on the interactive state changes. |
| 196 | * - When battery saver is on the battery saver policy changes. |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 197 | */ |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 198 | void handleBatterySaverStateChanged(boolean sendBroadcast) { |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 199 | final LowPowerModeListener[] listeners; |
| 200 | |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 201 | final boolean enabled; |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 202 | final boolean isInteractive = getPowerManager().isInteractive(); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 203 | final ArrayMap<String, String> fileValues; |
| 204 | |
| 205 | synchronized (mLock) { |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 206 | Slog.i(TAG, "Battery saver " + (mEnabled ? "enabled" : "disabled") |
| 207 | + ": isInteractive=" + isInteractive); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 208 | |
| 209 | listeners = mListeners.toArray(new LowPowerModeListener[mListeners.size()]); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 210 | enabled = mEnabled; |
| 211 | |
| 212 | if (enabled) { |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 213 | fileValues = mBatterySaverPolicy.getFileValues(isInteractive); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 214 | } else { |
| 215 | fileValues = null; |
| 216 | } |
| 217 | } |
| 218 | |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 219 | final PowerManagerInternal pmi = LocalServices.getService(PowerManagerInternal.class); |
| 220 | if (pmi != null) { |
| 221 | pmi.powerHint(PowerHint.LOW_POWER, enabled ? 1 : 0); |
| 222 | } |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 223 | |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 224 | if (ArrayUtils.isEmpty(fileValues)) { |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 225 | mFileUpdater.restoreDefault(); |
| 226 | } else { |
| 227 | mFileUpdater.writeFiles(fileValues); |
| 228 | } |
| 229 | |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 230 | if (sendBroadcast) { |
Makoto Onuki | 1ecaf7c | 2017-11-27 14:05:53 -0800 | [diff] [blame] | 231 | if (enabled) { |
| 232 | // STOPSHIP Remove the toast. |
| 233 | Toast.makeText(mContext, |
| 234 | com.android.internal.R.string.battery_saver_warning, |
| 235 | Toast.LENGTH_LONG).show(); |
| 236 | } |
| 237 | |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 238 | if (DEBUG) { |
| 239 | Slog.i(TAG, "Sending broadcasts for mode: " + enabled); |
| 240 | } |
| 241 | |
| 242 | // Send the broadcasts and notify the listeners. We only do this when the battery saver |
| 243 | // mode changes, but not when only the screen state changes. |
| 244 | Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING) |
| 245 | .putExtra(PowerManager.EXTRA_POWER_SAVE_MODE, enabled) |
| 246 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 247 | mContext.sendBroadcast(intent); |
| 248 | |
| 249 | intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); |
| 250 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 251 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 252 | |
| 253 | // Send internal version that requires signature permission. |
| 254 | intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED_INTERNAL); |
| 255 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 256 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 257 | Manifest.permission.DEVICE_POWER); |
| 258 | |
| 259 | |
| 260 | for (LowPowerModeListener listener : listeners) { |
| 261 | final PowerSaveState result = |
| 262 | mBatterySaverPolicy.getBatterySaverPolicy( |
| 263 | listener.getServiceType(), enabled); |
| 264 | listener.onLowPowerModeChanged(result); |
| 265 | } |
| 266 | } |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 267 | } |
| 268 | } |