OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser; |
| 6 |
| 7 import android.content.BroadcastReceiver; |
| 8 import android.content.Context; |
| 9 import android.content.Intent; |
| 10 import android.content.IntentFilter; |
| 11 import android.os.Handler; |
| 12 import android.os.Looper; |
| 13 |
| 14 import org.chromium.base.ApiCompatibilityUtils; |
| 15 import org.chromium.base.ApplicationStatus; |
| 16 import org.chromium.base.VisibleForTesting; |
| 17 import org.chromium.chrome.browser.omaha.OmahaClient; |
| 18 import org.chromium.chrome.browser.sync.DelayedSyncController; |
| 19 |
| 20 import java.util.concurrent.atomic.AtomicBoolean; |
| 21 |
| 22 /** |
| 23 * Monitors the event that indicates the screen is turning on. Used to run acti
ons that shouldn't |
| 24 * occur while the phone's screen is off (i.e. when the user expects the phone t
o be "asleep"). |
| 25 */ |
| 26 public class PowerBroadcastReceiver extends BroadcastReceiver { |
| 27 private final AtomicBoolean mNeedToRunActions = new AtomicBoolean(true); |
| 28 private final AtomicBoolean mIsRegistered = new AtomicBoolean(false); |
| 29 private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 30 |
| 31 private PowerManagerHelper mPowerManagerHelper; |
| 32 private ServiceRunnable mServiceRunnable; |
| 33 |
| 34 /** |
| 35 * Stubs out interaction with the PowerManager. |
| 36 */ |
| 37 @VisibleForTesting |
| 38 static class PowerManagerHelper { |
| 39 /** @return whether the screen is on or not. */ |
| 40 public boolean isScreenOn(Context context) { |
| 41 return ApiCompatibilityUtils.isInteractive(context); |
| 42 } |
| 43 } |
| 44 |
| 45 /** |
| 46 * Defines a set of actions to perform when the conditions are correct. |
| 47 */ |
| 48 @VisibleForTesting |
| 49 public static class ServiceRunnable implements Runnable { |
| 50 /** |
| 51 * ANRs are triggered if the app fails to respond to a touch event withi
n 5 seconds. Posting |
| 52 * this runnable after 5 seconds lets ChromeTabbedActivity.onResume() pe
rform whatever more |
| 53 * important tasks are necessary. |
| 54 */ |
| 55 private static final long DELAY_TO_POST_MS = 5000; |
| 56 |
| 57 /** |
| 58 * @returns how long the runnable should be delayed before it is run. |
| 59 */ |
| 60 public long delayToRun() { |
| 61 return DELAY_TO_POST_MS; |
| 62 } |
| 63 |
| 64 /** |
| 65 * Unless testing, do not override this function. |
| 66 */ |
| 67 @Override |
| 68 public void run() { |
| 69 Context context = ApplicationStatus.getApplicationContext(); |
| 70 |
| 71 // Resume communication with the Omaha Update Server. |
| 72 if (ChromeVersionInfo.isOfficialBuild()) { |
| 73 Intent omahaIntent = OmahaClient.createInitializeIntent(context)
; |
| 74 context.startService(omahaIntent); |
| 75 } |
| 76 |
| 77 DelayedSyncController.getInstance().resumeDelayedSyncs(context); |
| 78 } |
| 79 } |
| 80 |
| 81 public PowerBroadcastReceiver() { |
| 82 mServiceRunnable = new ServiceRunnable(); |
| 83 mPowerManagerHelper = new PowerManagerHelper(); |
| 84 } |
| 85 |
| 86 @Override |
| 87 public void onReceive(Context context, Intent intent) { |
| 88 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON) |
| 89 && ApplicationStatus.hasVisibleActivities()) { |
| 90 runActions(context, false); |
| 91 } |
| 92 } |
| 93 |
| 94 /** |
| 95 * @return Whether or not this is registered with a context. |
| 96 */ |
| 97 @VisibleForTesting |
| 98 public boolean isRegistered() { |
| 99 return mIsRegistered.get(); |
| 100 } |
| 101 |
| 102 /** |
| 103 * Unregisters this broadcast receiver so it no longer receives Intents. |
| 104 * Also cancels any Runnables waiting to be executed. |
| 105 * @param context Context to unregister the receiver from. |
| 106 */ |
| 107 public void unregisterReceiver(Context context) { |
| 108 mHandler.removeCallbacks(mServiceRunnable); |
| 109 if (mIsRegistered.getAndSet(false)) { |
| 110 context.unregisterReceiver(this); |
| 111 mNeedToRunActions.set(false); |
| 112 } |
| 113 } |
| 114 |
| 115 /** |
| 116 * Registers this broadcast receiver so it receives Intents. |
| 117 * This should only be done by the owning Activity. |
| 118 * @param context Context to register the receiver with. |
| 119 */ |
| 120 public void registerReceiver(Context context) { |
| 121 assert Looper.getMainLooper() == Looper.myLooper(); |
| 122 if (!mIsRegistered.getAndSet(true)) { |
| 123 context.registerReceiver(this, new IntentFilter(Intent.ACTION_SCREEN
_ON)); |
| 124 mNeedToRunActions.set(true); |
| 125 } |
| 126 } |
| 127 |
| 128 /** |
| 129 * Posts a task to run the necessary actions. The task is delayed to preven
t spin-locking in |
| 130 * ChromeTabbedActivity.onResume(): http://b/issue?id=5864891&query=5864891 |
| 131 * @param onlyIfScreenIsOn Whether or not the screen must be on for the acti
ons to be run. |
| 132 */ |
| 133 public void runActions(Context context, boolean onlyIfScreenIsOn) { |
| 134 assert mServiceRunnable != null; |
| 135 assert mPowerManagerHelper != null; |
| 136 if (!onlyIfScreenIsOn || mPowerManagerHelper.isScreenOn(context)) { |
| 137 if (mNeedToRunActions.getAndSet(false)) { |
| 138 unregisterReceiver(context); |
| 139 mHandler.postDelayed(mServiceRunnable, mServiceRunnable.delayToR
un()); |
| 140 } |
| 141 } |
| 142 } |
| 143 |
| 144 /** |
| 145 * Sets the runnable that contains the actions to do when the screen is on. |
| 146 */ |
| 147 @VisibleForTesting |
| 148 void setServiceRunnableForTests(ServiceRunnable runnable) { |
| 149 assert mServiceRunnable != null; |
| 150 mHandler.removeCallbacks(mServiceRunnable); |
| 151 mServiceRunnable = runnable; |
| 152 } |
| 153 |
| 154 /** |
| 155 * Sets the PowerManagerHelper that will be used to check if the screen is o
n. |
| 156 */ |
| 157 @VisibleForTesting |
| 158 void setPowerManagerHelperForTests(PowerManagerHelper helper) { |
| 159 mPowerManagerHelper = helper; |
| 160 } |
| 161 } |
OLD | NEW |