1 /* 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 17 package android.permissionpresenterservice; 18 19 import android.annotation.SystemApi; 20 import android.app.Service; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.permission.IRuntimePermissionPresenter; 24 import android.content.pm.permission.RuntimePermissionPresentationInfo; 25 import android.content.pm.permission.RuntimePermissionPresenter; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.os.IBinder; 29 import android.os.Looper; 30 import android.os.Message; 31 import android.os.RemoteCallback; 32 33 import com.android.internal.os.SomeArgs; 34 35 import 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 49 public 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 attachBaseContext(Context base)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 */ onGetAppPermissions(String packageName)73 public abstract List<RuntimePermissionPresentationInfo> onGetAppPermissions(String packageName); 74 75 /** 76 * Revoke the permission {@code permissionName} for app {@code packageName} 77 * 78 * @param packageName The package for which to revoke 79 * @param permissionName The permission to revoke 80 * 81 * @hide 82 */ onRevokeRuntimePermission(String packageName, String permissionName)83 public abstract void onRevokeRuntimePermission(String packageName, String permissionName); 84 85 @Override onBind(Intent intent)86 public final IBinder onBind(Intent intent) { 87 return new IRuntimePermissionPresenter.Stub() { 88 @Override 89 public void getAppPermissions(String packageName, RemoteCallback callback) { 90 SomeArgs args = SomeArgs.obtain(); 91 args.arg1 = packageName; 92 args.arg2 = callback; 93 mHandler.obtainMessage(MyHandler.MSG_GET_APP_PERMISSIONS, 94 args).sendToTarget(); 95 } 96 97 @Override 98 public void revokeRuntimePermission(String packageName, String permissionName) { 99 SomeArgs args = SomeArgs.obtain(); 100 args.arg1 = packageName; 101 args.arg2 = permissionName; 102 mHandler.obtainMessage(MyHandler.MSG_REVOKE_APP_PERMISSION, 103 args).sendToTarget(); 104 } 105 }; 106 } 107 108 private final class MyHandler extends Handler { 109 public static final int MSG_GET_APP_PERMISSIONS = 1; 110 public static final int MSG_GET_APPS_USING_PERMISSIONS = 2; 111 public static final int MSG_REVOKE_APP_PERMISSION = 3; 112 113 public MyHandler(Looper looper) { 114 super(looper, null, false); 115 } 116 117 @Override 118 public void handleMessage(Message msg) { 119 switch (msg.what) { 120 case MSG_GET_APP_PERMISSIONS: { 121 SomeArgs args = (SomeArgs) msg.obj; 122 String packageName = (String) args.arg1; 123 RemoteCallback callback = (RemoteCallback) args.arg2; 124 args.recycle(); 125 List<RuntimePermissionPresentationInfo> permissions = 126 onGetAppPermissions(packageName); 127 if (permissions != null && !permissions.isEmpty()) { 128 Bundle result = new Bundle(); 129 result.putParcelableList(RuntimePermissionPresenter.KEY_RESULT, 130 permissions); 131 callback.sendResult(result); 132 } else { 133 callback.sendResult(null); 134 } 135 } break; 136 case MSG_REVOKE_APP_PERMISSION: { 137 SomeArgs args = (SomeArgs) msg.obj; 138 String packageName = (String) args.arg1; 139 String permissionName = (String) args.arg2; 140 args.recycle(); 141 142 onRevokeRuntimePermission(packageName, permissionName); 143 } break; 144 } 145 } 146 } 147 } 148