1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissionutils;
18 
19 import android.app.Activity;
20 import android.app.Instrumentation;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.PermissionInfo;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.util.Log;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * A utility to dump or grant all revoked runtime permissions
34  */
35 public class PermissionInstrumentation extends Instrumentation {
36 
37     private static final String DUMP_TAG = "DUMP";
38     private static final String PARAM_COMMAND = "command";
39     private static final String COMMAND_DUMP = "dump";
40     private static final String COMMAND_GRANTALL = "grant-all";
41 
42     private static enum Action {DUMP, GRANTALL};
43 
44     @Override
onCreate(Bundle arguments)45     public void onCreate(Bundle arguments) {
46         super.onCreate(arguments);
47         String command = arguments.getString(PARAM_COMMAND);
48         if (command == null) {
49             throw new IllegalArgumentException("missing command parameter");
50         }
51         if (COMMAND_DUMP.equals(command)) {
52             runCommand(Action.DUMP);
53         } else if (COMMAND_GRANTALL.equals(command)) {
54             runCommand(Action.GRANTALL);
55         } else {
56             throw new IllegalArgumentException(
57                     String.format("unrecognized command \"%s\"", command));
58         }
59         finish(Activity.RESULT_OK, new Bundle());
60     }
61 
runCommand(Action action)62     private void runCommand(Action action) {
63         PackageManager pm = getContext().getPackageManager();
64         List<PackageInfo> pkgInfos = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
65         List<String> permissions = new ArrayList<>();
66         for (PackageInfo info : pkgInfos) {
67             if (info.requestedPermissions == null) {
68                 continue;
69             }
70             for (String permission : info.requestedPermissions) {
71                 PermissionInfo pi = null;
72                 try {
73                     pi = pm.getPermissionInfo(permission, 0);
74                 } catch (NameNotFoundException nnfe) {
75                     // ignore
76                 }
77                 if (pi == null) {
78                     continue;
79                 }
80                 if (!isRuntime(pi)) {
81                     continue;
82                 }
83                 int flag = pm.checkPermission(permission, info.packageName);
84                 if (flag == PackageManager.PERMISSION_DENIED) {
85                     if (action == Action.DUMP) {
86                         permissions.add(permission);
87                     } else if (action == Action.GRANTALL) {
88                         pm.grantRuntimePermission(info.packageName, permission, UserHandle.OWNER);
89                     }
90                 }
91             }
92             if (action == Action.DUMP && !permissions.isEmpty()) {
93                 Log.e(DUMP_TAG, String.format("Revoked permissions for %s", info.packageName));
94                 for (String permission : permissions) {
95                     Log.e(DUMP_TAG, "    " + permission);
96                 }
97                 permissions.clear();
98             }
99         }
100     }
101 
isRuntime(PermissionInfo pi)102     private boolean isRuntime(PermissionInfo pi) {
103         return (pi.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
104                 == PermissionInfo.PROTECTION_DANGEROUS;
105     }
106 }
107