blob: c846b076c14f09207023269c217512e7fe56325e [file] [log] [blame]
Hai Zhangb7776682018-09-25 15:10:57 -07001/*
2 * Copyright (C) 2018 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.rolecontrollerservice;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.SystemApi;
22import android.app.Service;
23import android.app.role.IRoleManagerCallback;
24import android.app.role.RoleManager;
25import android.app.role.RoleManagerCallback;
26import android.content.Intent;
27import android.os.IBinder;
28import android.os.RemoteException;
29import android.os.UserHandle;
30import android.util.Log;
31
32import com.android.internal.util.Preconditions;
33
34import java.util.concurrent.Executor;
35
36/**
37 * Abstract base class for the role controller service.
38 * <p>
39 * Subclass should implement the business logic for role management, including enforcing role
40 * requirements and granting or revoking relevant privileges of roles. This class can only be
41 * implemented by the permission controller app which is registered in {@code PackageManager}.
42 *
43 * @hide
44 */
45@SystemApi
46public abstract class RoleControllerService extends Service {
47
48 private static final String LOG_TAG = RoleControllerService.class.getSimpleName();
49
50 /**
51 * The {@link Intent} that must be declared as handled by the service. The service should also
52 * require the {@link android.Manifest.permission#BIND_ROLE_CONTROLLER_SERVICE} permission so
53 * that other applications can not abuse it.
54 */
55 public static final String SERVICE_INTERFACE =
56 "android.rolecontrollerservice.RoleControllerService";
57
58 @Nullable
59 @Override
60 public final IBinder onBind(@Nullable Intent intent) {
61 return new IRoleControllerService.Stub() {
62
63 @Override
Hai Zhang71d70362019-02-04 16:17:38 -080064 public void onAddRoleHolder(String roleName, String packageName, int flags,
Hai Zhangb7776682018-09-25 15:10:57 -070065 IRoleManagerCallback callback) {
66 Preconditions.checkStringNotEmpty(roleName, "roleName cannot be null or empty");
67 Preconditions.checkStringNotEmpty(packageName,
68 "packageName cannot be null or empty");
69 Preconditions.checkNotNull(callback, "callback cannot be null");
Hai Zhang71d70362019-02-04 16:17:38 -080070 RoleControllerService.this.onAddRoleHolder(roleName, packageName, flags,
Hai Zhangb7776682018-09-25 15:10:57 -070071 new RoleManagerCallbackDelegate(callback));
72 }
73
74 @Override
Hai Zhang71d70362019-02-04 16:17:38 -080075 public void onRemoveRoleHolder(String roleName, String packageName, int flags,
Hai Zhangb7776682018-09-25 15:10:57 -070076 IRoleManagerCallback callback) {
77 Preconditions.checkStringNotEmpty(roleName, "roleName cannot be null or empty");
78 Preconditions.checkStringNotEmpty(packageName,
79 "packageName cannot be null or empty");
80 Preconditions.checkNotNull(callback, "callback cannot be null");
Hai Zhang71d70362019-02-04 16:17:38 -080081 RoleControllerService.this.onRemoveRoleHolder(roleName, packageName, flags,
Hai Zhangb7776682018-09-25 15:10:57 -070082 new RoleManagerCallbackDelegate(callback));
83 }
84
85 @Override
Hai Zhang71d70362019-02-04 16:17:38 -080086 public void onClearRoleHolders(String roleName, int flags,
87 IRoleManagerCallback callback) {
Hai Zhangb7776682018-09-25 15:10:57 -070088 Preconditions.checkStringNotEmpty(roleName, "roleName cannot be null or empty");
89 Preconditions.checkNotNull(callback, "callback cannot be null");
Hai Zhang71d70362019-02-04 16:17:38 -080090 RoleControllerService.this.onClearRoleHolders(roleName, flags,
Hai Zhangb7776682018-09-25 15:10:57 -070091 new RoleManagerCallbackDelegate(callback));
92 }
Eugene Suslaa4200f822018-11-09 18:06:43 -080093
94 @Override
95 public void onGrantDefaultRoles(IRoleManagerCallback callback) {
96 Preconditions.checkNotNull(callback, "callback cannot be null");
Hai Zhangb295ac42018-11-16 16:08:18 -080097 RoleControllerService.this.onGrantDefaultRoles(new RoleManagerCallbackDelegate(
98 callback));
Eugene Suslaa4200f822018-11-09 18:06:43 -080099 }
Eugene Susla34969062019-01-29 11:02:02 -0800100
101 @Override
102 public void onSmsKillSwitchToggled(boolean smsRestrictionEnabled) {
103 RoleControllerService.this.onSmsKillSwitchToggled(smsRestrictionEnabled);
104 }
Hai Zhangb7776682018-09-25 15:10:57 -0700105 };
106 }
107
108 /**
109 * Add a specific application to the holders of a role. If the role is exclusive, the previous
110 * holder will be replaced.
111 * <p>
112 * Implementation should enforce the role requirements and grant or revoke the relevant
113 * privileges of roles.
114 *
115 * @param roleName the name of the role to add the role holder for
116 * @param packageName the package name of the application to add to the role holders
Hai Zhang71d70362019-02-04 16:17:38 -0800117 * @param flags optional behavior flags
Hai Zhangb7776682018-09-25 15:10:57 -0700118 * @param callback the callback for whether this call is successful
119 *
Hai Zhang71d70362019-02-04 16:17:38 -0800120 * @see RoleManager#addRoleHolderAsUser(String, String, int, UserHandle, Executor,
Hai Zhangb7776682018-09-25 15:10:57 -0700121 * RoleManagerCallback)
122 */
123 public abstract void onAddRoleHolder(@NonNull String roleName, @NonNull String packageName,
Hai Zhang71d70362019-02-04 16:17:38 -0800124 @RoleManager.ManageHoldersFlags int flags, @NonNull RoleManagerCallback callback);
Hai Zhangb7776682018-09-25 15:10:57 -0700125
126 /**
127 * Remove a specific application from the holders of a role.
128 *
129 * @param roleName the name of the role to remove the role holder for
130 * @param packageName the package name of the application to remove from the role holders
Hai Zhang71d70362019-02-04 16:17:38 -0800131 * @param flags optional behavior flags
Hai Zhangb7776682018-09-25 15:10:57 -0700132 * @param callback the callback for whether this call is successful
133 *
Hai Zhang71d70362019-02-04 16:17:38 -0800134 * @see RoleManager#removeRoleHolderAsUser(String, String, int, UserHandle, Executor,
Hai Zhangb7776682018-09-25 15:10:57 -0700135 * RoleManagerCallback)
136 */
137 public abstract void onRemoveRoleHolder(@NonNull String roleName, @NonNull String packageName,
Hai Zhang71d70362019-02-04 16:17:38 -0800138 @RoleManager.ManageHoldersFlags int flags, @NonNull RoleManagerCallback callback);
Hai Zhangb7776682018-09-25 15:10:57 -0700139
140 /**
141 * Remove all holders of a role.
142 *
143 * @param roleName the name of the role to remove role holders for
Hai Zhang71d70362019-02-04 16:17:38 -0800144 * @param flags optional behavior flags
Hai Zhangb7776682018-09-25 15:10:57 -0700145 * @param callback the callback for whether this call is successful
146 *
Hai Zhang71d70362019-02-04 16:17:38 -0800147 * @see RoleManager#clearRoleHoldersAsUser(String, int, UserHandle, Executor,
148 * RoleManagerCallback)
Hai Zhangb7776682018-09-25 15:10:57 -0700149 */
150 public abstract void onClearRoleHolders(@NonNull String roleName,
Hai Zhang71d70362019-02-04 16:17:38 -0800151 @RoleManager.ManageHoldersFlags int flags, @NonNull RoleManagerCallback callback);
Hai Zhangb7776682018-09-25 15:10:57 -0700152
Eugene Suslaa4200f822018-11-09 18:06:43 -0800153 /**
Eugene Susla34969062019-01-29 11:02:02 -0800154 * Cleanup appop/permissions state in response to sms kill switch toggle
155 *
156 * @param smsRestrictionEnabled whether kill switch was turned on
157 */
158 //STOPSHIP: remove this api before shipping a final version
159 public abstract void onSmsKillSwitchToggled(boolean smsRestrictionEnabled);
160
161 /**
Eugene Suslaa4200f822018-11-09 18:06:43 -0800162 * Called by system to grant default permissions and roles.
163 * <p>
164 * This is typically when creating a new user or upgrading either system or
165 * permission controller package
166 *
167 * @param callback the callback for whether this call is successful
168 */
169 public abstract void onGrantDefaultRoles(@NonNull RoleManagerCallback callback);
170
Hai Zhangb7776682018-09-25 15:10:57 -0700171 private static class RoleManagerCallbackDelegate implements RoleManagerCallback {
172
173 private IRoleManagerCallback mCallback;
174
175 RoleManagerCallbackDelegate(IRoleManagerCallback callback) {
176 mCallback = callback;
177 }
178
179 @Override
180 public void onSuccess() {
181 try {
182 mCallback.onSuccess();
183 } catch (RemoteException e) {
184 Log.e(LOG_TAG, "Error calling onSuccess() callback");
185 }
186 }
187
188 @Override
189 public void onFailure() {
190 try {
191 mCallback.onFailure();
192 } catch (RemoteException e) {
193 Log.e(LOG_TAG, "Error calling onFailure() callback");
194 }
195 }
196 }
197}