1 /*
2  * Copyright (C) 2020 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.app.admin;
18 
19 import android.annotation.MainThread;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.app.Service;
24 import android.content.Intent;
25 import android.os.Handler;
26 import android.os.IBinder;
27 import android.os.Looper;
28 import android.os.RemoteException;
29 import android.util.Log;
30 import android.view.SurfaceControlViewHost;
31 
32 /**
33  * Client interface for providing the SystemUI with secondary lockscreen information.
34  *
35  * <p>An implementation must be provided by the default configured supervision app that is set as
36  * Profile Owner or Device Owner when {@link DevicePolicyManager#setSecondaryLockscreenEnabled} is
37  * set to true and the service must be declared in the manifest as handling the action
38  * {@link DevicePolicyManager#ACTION_BIND_SECONDARY_LOCKSCREEN_SERVICE}, otherwise the keyguard
39  * will fail to bind to the service and continue to unlock.
40  *
41  * @see DevicePolicyManager#setSecondaryLockscreenEnabled
42  * @hide
43  */
44 @SystemApi
45 public class DevicePolicyKeyguardService extends Service {
46     private static final String TAG = "DevicePolicyKeyguardService";
47     private final Handler mHandler = new Handler(Looper.getMainLooper());
48     private IKeyguardCallback mCallback;
49 
50     private final IKeyguardClient mClient = new IKeyguardClient.Stub() {
51         @MainThread
52         @Override
53         public void onCreateKeyguardSurface(@Nullable IBinder hostInputToken,
54                 @NonNull IKeyguardCallback callback) {
55             mCallback = callback;
56             mHandler.post(() -> {
57                 SurfaceControlViewHost.SurfacePackage surfacePackage =
58                         DevicePolicyKeyguardService.this.onCreateKeyguardSurface(hostInputToken);
59 
60                 try {
61                     mCallback.onRemoteContentReady(surfacePackage);
62                 } catch (RemoteException e) {
63                     Log.e(TAG, "Failed to return created SurfacePackage", e);
64                 }
65             });
66         }
67     };
68 
69     @Override
onDestroy()70     public void onDestroy() {
71         mHandler.removeCallbacksAndMessages(null);
72     }
73 
74     @Override
75     @Nullable
onBind(@ullable Intent intent)76     public final IBinder onBind(@Nullable Intent intent) {
77         return mClient.asBinder();
78     }
79 
80     /**
81      * Called by keyguard once the host surface for the secondary lockscreen is created and ready to
82      * display remote content.
83      *
84      * <p>Implementations are expected to create a Surface hierarchy with view elements for the
85      * admin's desired secondary lockscreen UI, and optionally, interactive elements
86      * that will allow the user to dismiss the secondary lockscreen, subject to the implementation's
87      * requirements. The view hierarchy is expected to be embedded via the
88      * {@link SurfaceControlViewHost} APIs, and returned as a SurfacePackage via
89      * {@link SurfaceControlViewHost#getSurfacePackage}for the keyguard to reparent into its
90      * prepared SurfaceView.
91      *
92      * @param hostInputToken Token of the SurfaceView which will hosting the embedded hierarchy,
93      *                       primarily required by {@link SurfaceControlViewHost} for ANR reporting.
94      *                       It will be provided by the keyguard via
95      *                       {@link android.view.SurfaceView#getHostToken}.
96      * @return the {@link SurfaceControlViewHost.SurfacePackage} for the Surface the
97      *      secondary lockscreen content is attached to.
98      */
99     @Nullable
onCreateKeyguardSurface( @onNull IBinder hostInputToken)100     public SurfaceControlViewHost.SurfacePackage onCreateKeyguardSurface(
101             @NonNull IBinder hostInputToken) {
102         return null;
103     }
104 
105     /**
106      * Signals to keyguard that the secondary lock screen is ready to be dismissed.
107      */
108     @Nullable
dismiss()109     public void dismiss() {
110         if (mCallback == null) {
111             Log.w(TAG, "KeyguardCallback was unexpectedly null");
112             return;
113         }
114         try {
115             mCallback.onDismiss();
116         } catch (RemoteException e) {
117             Log.e(TAG, "onDismiss failed", e);
118         }
119     }
120 }
121