OSDN Git Service

Handle rotation (or refresh) before callback is received
[android-x86/packages-apps-Settings.git] / src / com / android / settings / applications / PermissionsSummaryHelper.java
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 package com.android.settings.applications;
17
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22
23 public class PermissionsSummaryHelper {
24
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";
29
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);
35     }
36
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);
41     }
42
43     private static BroadcastReceiver sendPermissionRequest(Context context, String action,
44             Intent request, final PermissionsResultCallback callback) {
45         BroadcastReceiver receiver = new BroadcastReceiver() {
46             @Override
47             public void onReceive(Context context, Intent intent) {
48                 int[] counts = intent.getIntArrayExtra(Intent.EXTRA_GET_PERMISSIONS_COUNT_RESULT);
49
50                 CharSequence[] groups = intent.getCharSequenceArrayExtra(
51                         Intent.EXTRA_GET_PERMISSIONS_GROUP_LIST_RESULT);
52
53                 callback.onPermissionSummaryResult(counts, groups);
54
55                 context.unregisterReceiver(this);
56             }
57         };
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);
62         return receiver;
63     }
64
65     public interface PermissionsResultCallback {
66         void onPermissionSummaryResult(int[] counts, CharSequence[] groupLabels);
67     }
68 }