Only process intents if user-visible

 Bug: 1873726

Change-Id: I2f5fe29bdf00e5ad441e8f50c89be64b93f7842a
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 77fac4f..1d53626 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -17,10 +17,12 @@
 package com.android.browser;
 
 import android.app.Activity;
+import android.app.KeyguardManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.Bundle;
+import android.os.PowerManager;
 import android.util.Log;
 import android.view.ActionMode;
 import android.view.ContextMenu;
@@ -56,6 +58,11 @@
         }
         super.onCreate(icicle);
 
+        if (shouldIgnoreIntents()) {
+            finish();
+            return;
+        }
+
         // If this was a web search request, pass it on to the default web
         // search provider and finish this activity.
         if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
@@ -86,6 +93,7 @@
 
     @Override
     protected void onNewIntent(Intent intent) {
+        if (shouldIgnoreIntents()) return;
         if (ACTION_RESTART.equals(intent.getAction())) {
             Bundle outState = new Bundle();
             mController.onSaveInstanceState(outState);
@@ -99,6 +107,25 @@
         mController.handleNewIntent(intent);
     }
 
+    private KeyguardManager mKeyguardManager;
+    private PowerManager mPowerManager;
+    private boolean shouldIgnoreIntents() {
+        // Only process intents if the screen is on and the device is unlocked
+        // aka, if we will be user-visible
+        if (mKeyguardManager == null) {
+            mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
+        }
+        if (mPowerManager == null) {
+            mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
+        }
+        boolean ignore = !mPowerManager.isScreenOn();
+        ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
+        if (LOGV_ENABLED) {
+            Log.v(LOGTAG, "ignore intents: " + ignore);
+        }
+        return ignore;
+    }
+
     @Override
     protected void onResume() {
         super.onResume();