blob: 344d947a3ad38e6d79069b6f3b5da76a2bd29edf [file] [log] [blame]
Svet Ganovae0e03a2016-02-25 18:22:10 -08001/*
2 * Copyright (C) 2016 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
17package android.permissionpresenterservice;
18
19import android.annotation.SystemApi;
20import android.app.Service;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.permission.IRuntimePermissionPresenter;
25import android.content.pm.permission.RuntimePermissionPresentationInfo;
26import android.content.pm.permission.RuntimePermissionPresenter;
27import android.os.Bundle;
28import android.os.Handler;
29import android.os.IBinder;
30import android.os.Looper;
31import android.os.Message;
32import android.os.RemoteCallback;
33import com.android.internal.os.SomeArgs;
34
35import java.util.List;
36
37/**
38 * This service presents information regarding runtime permissions that is
39 * used for presenting them in the UI. Runtime permissions are presented as
40 * a single permission in the UI but may be composed of several individual
41 * permissions.
42 *
43 * @see RuntimePermissionPresenter
44 * @see RuntimePermissionPresentationInfo
45 *
46 * @hide
47 */
48@SystemApi
49public abstract class RuntimePermissionPresenterService extends Service {
50
51 /**
52 * The {@link Intent} action that must be declared as handled by a service
53 * in its manifest for the system to recognize it as a runtime permission
54 * presenter service.
55 */
56 public static final String SERVICE_INTERFACE =
57 "android.permissionpresenterservice.RuntimePermissionPresenterService";
58
59 // No need for locking - always set first and never modified
60 private Handler mHandler;
61
62 @Override
63 public final void attachBaseContext(Context base) {
64 super.attachBaseContext(base);
65 mHandler = new MyHandler(base.getMainLooper());
66 }
67
68 /**
69 * Gets the runtime permissions for an app.
70 *
71 * @param packageName The package for which to query.
72 */
73 public abstract List<RuntimePermissionPresentationInfo> onGetAppPermissions(String packageName);
74
Svet Ganovae0e03a2016-02-25 18:22:10 -080075 @Override
76 public final IBinder onBind(Intent intent) {
77 return new IRuntimePermissionPresenter.Stub() {
78 @Override
79 public void getAppPermissions(String packageName, RemoteCallback callback) {
80 SomeArgs args = SomeArgs.obtain();
81 args.arg1 = packageName;
82 args.arg2 = callback;
83 mHandler.obtainMessage(MyHandler.MSG_GET_APP_PERMISSIONS,
84 args).sendToTarget();
85 }
Svet Ganovae0e03a2016-02-25 18:22:10 -080086 };
87 }
88
89 private final class MyHandler extends Handler {
90 public static final int MSG_GET_APP_PERMISSIONS = 1;
91 public static final int MSG_GET_APPS_USING_PERMISSIONS = 2;
92
93 public MyHandler(Looper looper) {
94 super(looper, null, false);
95 }
96
97 @Override
98 public void handleMessage(Message msg) {
99 switch (msg.what) {
100 case MSG_GET_APP_PERMISSIONS: {
101 SomeArgs args = (SomeArgs) msg.obj;
102 String packageName = (String) args.arg1;
103 RemoteCallback callback = (RemoteCallback) args.arg2;
104 args.recycle();
105 List<RuntimePermissionPresentationInfo> permissions =
106 onGetAppPermissions(packageName);
107 if (permissions != null && !permissions.isEmpty()) {
108 Bundle result = new Bundle();
109 result.putParcelableList(RuntimePermissionPresenter.KEY_RESULT,
110 permissions);
111 callback.sendResult(result);
112 } else {
113 callback.sendResult(null);
114 }
115 } break;
Svet Ganovae0e03a2016-02-25 18:22:10 -0800116 }
117 }
118 }
119}