2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.applications;
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
23 public class PermissionsSummaryHelper {
25 private static final String ACTION_PERM_COUNT_RESPONSE
26 = "com.android.settings.PERM_COUNT_RESPONSE";
27 private static final String ACTION_APP_COUNT_RESPONSE
28 = "com.android.settings.APP_COUNT_RESPONSE";
30 public static BroadcastReceiver getPermissionSummary(Context context, String pkg,
31 PermissionsResultCallback callback) {
32 Intent request = new Intent(Intent.ACTION_GET_PERMISSIONS_COUNT);
33 request.putExtra(Intent.EXTRA_PACKAGE_NAME, pkg);
34 return sendPermissionRequest(context, ACTION_PERM_COUNT_RESPONSE, request, callback);
37 public static BroadcastReceiver getAppWithPermissionsCounts(Context context,
38 PermissionsResultCallback callback) {
39 Intent request = new Intent(Intent.ACTION_GET_PERMISSIONS_COUNT);
40 return sendPermissionRequest(context, ACTION_APP_COUNT_RESPONSE, request, callback);
43 private static BroadcastReceiver sendPermissionRequest(Context context, String action,
44 Intent request, final PermissionsResultCallback callback) {
45 BroadcastReceiver receiver = new BroadcastReceiver() {
47 public void onReceive(Context context, Intent intent) {
48 int[] counts = intent.getIntArrayExtra(Intent.EXTRA_GET_PERMISSIONS_COUNT_RESULT);
50 CharSequence[] groups = intent.getCharSequenceArrayExtra(
51 Intent.EXTRA_GET_PERMISSIONS_GROUP_LIST_RESULT);
53 callback.onPermissionSummaryResult(counts, groups);
55 context.unregisterReceiver(this);
58 context.registerReceiver(receiver, new IntentFilter(action));
59 request.putExtra(Intent.EXTRA_GET_PERMISSIONS_RESPONSE_INTENT, action);
60 request.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
61 context.sendBroadcast(request);
65 public interface PermissionsResultCallback {
66 void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels);