Merge commit 'bd10777948fe5a0d9452f73c30941f46c6b4a2bf' into HEAD

* commit 'bd10777948fe5a0d9452f73c30941f46c6b4a2bf':
  Forward package removed broadcast to KeyChainService

Test: make full -j30
Fix: 36568026
Change-Id: Icd3cda64e403212132e2c7d731e9793eb92c368a
diff --git a/services/core/java/com/android/server/security/KeyChainSystemService.java b/services/core/java/com/android/server/security/KeyChainSystemService.java
index bfeaee6..2f681a3 100644
--- a/services/core/java/com/android/server/security/KeyChainSystemService.java
+++ b/services/core/java/com/android/server/security/KeyChainSystemService.java
@@ -22,10 +22,13 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
+import android.os.Process;
 import android.os.UserHandle;
 import android.security.IKeyChainService;
 import android.util.Slog;
 
+import com.android.server.DeviceIdleController;
+import com.android.server.LocalServices;
 import com.android.server.SystemService;
 
 /**
@@ -45,6 +48,11 @@
 
     private static final String TAG = "KeyChainSystemService";
 
+    /**
+     * Maximum time limit for the KeyChain app to deal with packages being removed.
+     */
+    private static final int KEYCHAIN_IDLE_WHITELIST_DURATION_MS = 30 * 1000;
+
     public KeyChainSystemService(final Context context) {
         super(context);
     }
@@ -77,10 +85,25 @@
                 }
                 intent.setComponent(service);
                 intent.setAction(broadcastIntent.getAction());
-                getContext().startServiceAsUser(intent, UserHandle.of(getSendingUserId()));
+                startServiceInBackgroundAsUser(intent, UserHandle.of(getSendingUserId()));
             } catch (RuntimeException e) {
                 Slog.e(TAG, "Unable to forward package removed broadcast to KeyChain", e);
             }
         }
     };
+
+
+    private void startServiceInBackgroundAsUser(final Intent intent, final UserHandle user) {
+        if (intent.getComponent() == null) {
+            return;
+        }
+
+        final String packageName = intent.getComponent().getPackageName();
+        final DeviceIdleController.LocalService idleController =
+                LocalServices.getService(DeviceIdleController.LocalService.class);
+        idleController.addPowerSaveTempWhitelistApp(Process.myUid(), packageName,
+                KEYCHAIN_IDLE_WHITELIST_DURATION_MS, user.getIdentifier(), false, "keychain");
+
+        getContext().startServiceAsUser(intent, user);
+    }
 }