Only grant runtime permissions to special components.

Now runtime permissions are granted only to components that are
part of the system or perform special system operations. For
exmple, the shell UID gets its runtime permissions granted by
default and the default phone app gets the phone permissions
granted by default.

bug:21764803

Change-Id: If8b8cadbd1980ffe7a6fc15bbb5f54a425f6e8f9
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index a5d536e..9a9a4ae 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -16,6 +16,7 @@
 package com.android.server;
 
 import android.annotation.NonNull;
+import android.content.pm.PackageManagerInternal;
 import com.android.internal.content.PackageMonitor;
 import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
 import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
@@ -31,6 +32,7 @@
 import com.android.internal.view.IInputMethodManager;
 import com.android.internal.view.IInputMethodSession;
 import com.android.internal.view.InputBindResult;
+import com.android.server.pm.UserManagerService;
 import com.android.server.statusbar.StatusBarManagerService;
 import com.android.server.wm.WindowManagerService;
 
@@ -859,6 +861,38 @@
         // mSettings should be created before buildInputMethodListLocked
         mSettings = new InputMethodSettings(
                 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
+
+        // Let the package manager query which are the default imes
+        // as they get certain permissions granted by default.
+        PackageManagerInternal packageManagerInternal = LocalServices.getService(
+                PackageManagerInternal.class);
+        packageManagerInternal.setImePackagesProvider(
+                new PackageManagerInternal.PackagesProvider() {
+                    @Override
+                    public String[] getPackages(int userId) {
+                        synchronized (mMethodMap) {
+                            final int currentUserId = mSettings.getCurrentUserId();
+                            // TODO: We are switching the current user id in the settings
+                            // object to query it and then revert the user id. Ideally, we
+                            // should call a API in settings with the user id as an argument.
+                            mSettings.setCurrentUserId(userId);
+                            List<InputMethodInfo> imes = mSettings
+                                    .getEnabledInputMethodListLocked();
+                            String[] packageNames = null;
+                            if (imes != null) {
+                                final int imeCount = imes.size();
+                                packageNames = new String[imeCount];
+                                for (int i = 0; i < imeCount; i++) {
+                                    InputMethodInfo ime = imes.get(i);
+                                    packageNames[i] = ime.getPackageName();
+                                }
+                            }
+                            mSettings.setCurrentUserId(currentUserId);
+                            return packageNames;
+                        }
+                    }
+                });
+
         updateCurrentProfileIds();
         mFileManager = new InputMethodFileManager(mMethodMap, userId);
         synchronized (mMethodMap) {