1 /*
2  * Copyright (C) 2023 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 com.android.role;
18 
19 import android.annotation.CallbackExecutor;
20 import android.app.role.RoleControllerService;
21 import android.app.role.RoleManager;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.HandlerThread;
26 import android.os.RemoteCallback;
27 import android.os.UserHandle;
28 
29 import androidx.annotation.NonNull;
30 
31 import com.android.role.controller.service.RoleControllerServiceImpl;
32 
33 import java.util.List;
34 import java.util.concurrent.Executor;
35 import java.util.function.Consumer;
36 
37 public class LocalRoleController implements RoleController {
38 
39     @NonNull
40     private final RoleControllerServiceImpl mService;
41     @NonNull
42     private final HandlerThread mWorkerThread;
43     @NonNull
44     private final Handler mWorkerHandler;
45 
LocalRoleController(@onNull UserHandle user, @NonNull Context context)46     public LocalRoleController(@NonNull UserHandle user, @NonNull Context context) {
47         mService = new RoleControllerServiceImpl(user, context);
48         mWorkerThread = new HandlerThread(RoleControllerService.class.getSimpleName());
49         mWorkerThread.start();
50         mWorkerHandler = new Handler(mWorkerThread.getLooper());
51     }
52 
53     @Override
grantDefaultRoles(@onNull Executor executor, @NonNull Consumer<Boolean> callback)54     public void grantDefaultRoles(@NonNull Executor executor, @NonNull Consumer<Boolean> callback) {
55         mWorkerHandler.post(() -> {
56             boolean successful = mService.onGrantDefaultRoles();
57             executor.execute(() -> callback.accept(successful));
58         });
59     }
60 
61     @Override
onAddRoleHolder(@onNull String roleName, @NonNull String packageName, @RoleManager.ManageHoldersFlags int flags, @NonNull RemoteCallback callback)62     public void onAddRoleHolder(@NonNull String roleName, @NonNull String packageName,
63             @RoleManager.ManageHoldersFlags int flags, @NonNull RemoteCallback callback) {
64         mWorkerHandler.post(() -> {
65             boolean successful = mService.onAddRoleHolder(roleName, packageName, flags);
66             callback.sendResult(successful ? Bundle.EMPTY : null);
67         });
68     }
69 
70     @Override
onRemoveRoleHolder(@onNull String roleName, @NonNull String packageName, @RoleManager.ManageHoldersFlags int flags, @NonNull RemoteCallback callback)71     public void onRemoveRoleHolder(@NonNull String roleName, @NonNull String packageName,
72             @RoleManager.ManageHoldersFlags int flags, @NonNull RemoteCallback callback) {
73         mWorkerHandler.post(() -> {
74             boolean successful = mService.onRemoveRoleHolder(roleName, packageName, flags);
75             callback.sendResult(successful ? Bundle.EMPTY : null);
76         });
77     }
78 
79     @Override
onClearRoleHolders(@onNull String roleName, @RoleManager.ManageHoldersFlags int flags, @NonNull RemoteCallback callback)80     public void onClearRoleHolders(@NonNull String roleName,
81             @RoleManager.ManageHoldersFlags int flags, @NonNull RemoteCallback callback) {
82         mWorkerHandler.post(() -> {
83             boolean successful = mService.onClearRoleHolders(roleName, flags);
84             callback.sendResult(successful ? Bundle.EMPTY : null);
85         });
86     }
87 
88     @Override
isRoleVisible(@onNull String roleName)89     public boolean isRoleVisible(@NonNull String roleName) {
90         return mService.onIsRoleVisible(roleName);
91     }
92 
93     @Override
isApplicationVisibleForRole(@onNull String roleName, @NonNull String packageName)94     public boolean isApplicationVisibleForRole(@NonNull String roleName,
95             @NonNull String packageName) {
96         return mService.onIsApplicationVisibleForRole(roleName, packageName);
97     }
98 
99     @Override
getLegacyFallbackDisabledRoles(@onNull @allbackExecutor Executor executor, @NonNull Consumer<List<String>> callback)100     public void getLegacyFallbackDisabledRoles(@NonNull @CallbackExecutor Executor executor,
101             @NonNull Consumer<List<String>> callback) {
102         throw new UnsupportedOperationException();
103     }
104 }
105