summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/AppPermissions.java
blob: 658accc17f468ffe20527977f7cb3c1d4c50ae2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.packageinstaller.permission;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.os.Build;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Log;
import com.android.internal.util.ArrayUtils;

import java.util.ArrayList;
import java.util.List;

public final class AppPermissions {
    private static final String LOG_TAG = "AppPermissions";

    private final ArrayMap<String, PermissionGroup> mGroups = new ArrayMap<>();

    private final Context mContext;

    private final PackageInfo mPackageInfo;

    private final String[] mFilterPermissions;

    private final CharSequence mAppLabel;

    public AppPermissions(Context context, PackageInfo packageInfo, String[] permissions) {
        mContext = context;
        mPackageInfo = packageInfo;
        mFilterPermissions = permissions;
        mAppLabel = packageInfo.applicationInfo.loadLabel(context.getPackageManager());
        loadPermissionGroups();
    }

    public void refresh() {
        loadPermissionGroups();
    }

    public CharSequence getAppLabel() {
        return mAppLabel;
    }

    public PermissionGroup getPermissionGroup(String name) {
        return mGroups.get(name);
    }

    public List<PermissionGroup> getPermissionGroups() {
        return new ArrayList<>(mGroups.values());
    }

    private void loadPermissionGroups() {
        mGroups.clear();

        final boolean appSupportsRuntimePermissions = mPackageInfo.applicationInfo.targetSdkVersion
                > Build.VERSION_CODES.LOLLIPOP_MR1;

        for (int i = 0; i < mPackageInfo.requestedPermissions.length; i++) {
            String requestedPerm = mPackageInfo.requestedPermissions[i];

            final PermissionInfo permInfo;
            try {
                permInfo = mContext.getPackageManager().getPermissionInfo(requestedPerm, 0);
            } catch (NameNotFoundException e) {
                Log.w(LOG_TAG, "Unknown permission: " + requestedPerm);
                continue;
            }

            String permName = permInfo.name;
            String groupName = permInfo.group != null ? permInfo.group : permName;

            PermissionGroup group = mGroups.get(groupName);
            if (group == null) {
                PermissionGroupInfo groupInfo = null;
                if (permInfo.group != null) {
                    try {
                        groupInfo = mContext.getPackageManager().getPermissionGroupInfo(
                                permInfo.group, 0);
                    } catch (NameNotFoundException e) {
                        Log.w(LOG_TAG, "Unknown group: " + permInfo.group);
                    }
                }

                CharSequence groupLabel = (groupInfo != null)
                        ? groupInfo.loadLabel(mContext.getPackageManager())
                        : permInfo.loadLabel(mContext.getPackageManager());

                if (groupLabel == null) {
                    Log.w(LOG_TAG, "Neither permission nor group have name."
                            + " Ignoring permission: " + permInfo.name);
                    continue;
                }

                final int iconResId = (groupInfo != null) ? groupInfo.icon : permInfo.icon;

                group = new PermissionGroup(mContext, mPackageInfo.packageName,
                        groupName, groupLabel, iconResId);
                mGroups.put(groupName, group);
            }

            final boolean runtime = appSupportsRuntimePermissions
                    && permInfo.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS;
            final boolean granted = (mPackageInfo.requestedPermissionsFlags[i]
                    & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0;

            Permission permission = new Permission(permName, runtime, granted);
            group.addPermission(permission);
        }

        if (ArrayUtils.isEmpty(mFilterPermissions)) {
            return;
        }

        final int groupCount = mGroups.size();
        for (int i = groupCount - 1; i >= 0; i--) {
            PermissionGroup group = mGroups.valueAt(i);
            boolean groupHasPermission = false;
            for (String filterPerm : mFilterPermissions) {
                if (group.mPermissions.containsKey(filterPerm)) {
                    groupHasPermission = true;
                    break;
                }
            }
            if (!groupHasPermission) {
                mGroups.removeAt(i);
            }
        }
    }

    public static final class PermissionGroup {
        private final Context mContext;
        private final String mPackageName;

        private final String mName;
        private final CharSequence mLabel;
        private final ArrayMap<String, Permission> mPermissions = new ArrayMap<>();
        private final int mIconResId;

        private boolean mHasRuntimePermissions;

        public String getName() {
            return mName;
        }

        public int getIconResId() {
            return mIconResId;
        }

        public CharSequence getLabel() {
            return mLabel;
        }

        public PermissionGroup(Context context, String packageName,
                String name, CharSequence label, int iconResId) {
            mPackageName = packageName;
            mContext = context;
            mName = name;
            mLabel = label;
            mIconResId = iconResId;
        }

        public boolean hasRuntimePermissions() {
            return mHasRuntimePermissions;
        }

        public boolean areRuntimePermissionsGranted() {
            final int permissionCount = mPermissions.size();
            for (int i = 0; i < permissionCount; i++) {
                Permission permission = mPermissions.valueAt(i);
                if (permission.mRuntime && !permission.mGranted) {
                    return false;
                }
            }
            return true;
        }

        public boolean grantRuntimePermissions() {
            for (Permission permission : mPermissions.values()) {
                if (permission.mRuntime && !permission.mGranted) {
                    mContext.getPackageManager().grantPermission(mPackageName,
                            permission.mName, new UserHandle(mContext.getUserId()));
                    permission.mGranted = true;
                }
            }
            return true;
        }

        public boolean revokeRuntimePermissions() {
            for (Permission permission : mPermissions.values()) {
                if (permission.mRuntime && permission.mGranted) {
                    mContext.getPackageManager().revokePermission(mPackageName,
                            permission.mName, new UserHandle(mContext.getUserId()));
                    permission.mGranted = false;
                }
            }
            return true;
        }

        public List<Permission> getPermissions() {
            return new ArrayList<>(mPermissions.values());
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }

            if (obj == null) {
                return false;
            }

            if (getClass() != obj.getClass()) {
                return false;
            }

            PermissionGroup other = (PermissionGroup) obj;

            if (mName == null) {
                if (other.mName != null) {
                    return false;
                }
            } else if (!mName.equals(other.mName)) {
                return false;
            }

            return true;
        }

        @Override
        public int hashCode() {
            return mName != null ? mName.hashCode() : 0;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append(getClass().getSimpleName());
            builder.append("{name=").append(mName);
            if (!mPermissions.isEmpty()) {
                builder.append(", <has permissions>}");
            } else {
                builder.append('}');
            }
            return builder.toString();
        }

        void addPermission(Permission permission) {
            mPermissions.put(permission.mName, permission);
            if (permission.mRuntime) {
                mHasRuntimePermissions = true;
            }
        }
    }

    public static final class Permission {
        private final String mName;
        private final boolean mRuntime;
        private boolean mGranted;

        public Permission(String name, boolean runtime, boolean granted) {
            mName = name;
            mRuntime = runtime;
            mGranted = granted;
        }

        public String getName() {
            return mName;
        }

        public boolean isGranted() {
            return mGranted;
        }
    }
}