Index: chrome/android/java/src/org/chromium/chrome/browser/KeyguardBroadcastReceiver.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/KeyguardBroadcastReceiver.java b/chrome/android/java/src/org/chromium/chrome/browser/KeyguardBroadcastReceiver.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2a4c0b6e962f992a55fd72ea2ab78f7d08b612b1 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/KeyguardBroadcastReceiver.java |
@@ -0,0 +1,56 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser; |
+ |
+import android.content.BroadcastReceiver; |
+import android.content.Context; |
+import android.content.Intent; |
+ |
+import org.chromium.base.VisibleForTesting; |
+ |
+/** |
+ * Broadcast receiver when the screen is unlocked from pin, pattern, or password. |
Maria
2016/12/13 21:36:09
I would add a note that this class handles exactly
Sherry
2016/12/13 22:13:54
Will do.
|
+ */ |
+public class KeyguardBroadcastReceiver extends BroadcastReceiver { |
Ted C
2016/12/13 22:47:13
based on my comment in the other file about unregi
Sherry
2016/12/14 00:54:07
It sounds ideal, however, that will have this clas
Ted C
2016/12/14 19:55:06
Can you not use the application context (ContextUt
Sherry
2016/12/14 22:25:46
See my replies to each of your 4 points:
1.) Agree
Sherry
2016/12/16 21:10:59
I have a plan to make the delayed task for unregis
Sherry
2016/12/16 23:00:40
My bad, just realized Handler is running the task
|
+ private static final int VALID_DEFERRED_PERIOD_MS = 10000; |
+ |
+ private Intent mDeferredIntent; |
+ private long mDeferredIntentCreatedTime; |
+ private boolean mShouldFireIntent; |
+ |
+ public KeyguardBroadcastReceiver(Intent deferredIntent) { |
+ updateDeferredIntent(deferredIntent); |
+ mShouldFireIntent = true; |
Ted C
2016/12/13 22:47:13
can use just use the null-ness of mDeferredIntent
Sherry
2016/12/14 00:54:07
That was my first intuition as well. I used to thi
|
+ } |
+ |
+ @Override |
+ public void onReceive(Context context, Intent intent) { |
+ assert Intent.ACTION_USER_PRESENT.equals(intent.getAction()); |
+ |
+ if (Intent.ACTION_USER_PRESENT.equals(intent.getAction()) |
+ && hasValidDeferredIntent(mDeferredIntent) && mShouldFireIntent) { |
+ context.startActivity(mDeferredIntent); |
Ted C
2016/12/13 22:47:13
what context is this referring to?
In particular,
Sherry
2016/12/14 00:54:07
According to https://developer.android.com/referen
|
+ // Prevent the broadcast receiver from firing intent unexpectedly. |
+ mShouldFireIntent = false; |
+ } |
+ } |
+ |
+ public boolean hasValidDeferredIntent(final Intent intent) { |
Maria
2016/12/13 21:36:09
style: add javadoc to public methods
Sherry
2016/12/13 22:13:54
Will do.
|
+ return mDeferredIntent != null |
+ && System.currentTimeMillis() - mDeferredIntentCreatedTime |
Maria
2016/12/13 21:36:09
style: parenthesis around subtraction
Sherry
2016/12/13 22:13:54
Will update.
|
+ <= VALID_DEFERRED_PERIOD_MS |
+ && intent == mDeferredIntent; |
+ } |
+ |
+ public void updateDeferredIntent(final Intent intent) { |
+ mDeferredIntent = intent; |
+ mDeferredIntentCreatedTime = System.currentTimeMillis(); |
Maria
2016/12/13 21:36:09
For timing events use SystemClock.elapsedRealtime(
Sherry
2016/12/13 22:13:54
Will update.
|
+ } |
+ |
+ @VisibleForTesting |
+ void setCreateTimeForTesting(final long time) { |
+ mDeferredIntentCreatedTime = time; |
+ } |
+} |