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 com.android.cts.deviceandprofileowner;
18 
19 import android.app.admin.DevicePolicyKeyguardService;
20 import android.content.Context;
21 import android.graphics.Color;
22 import android.hardware.display.DisplayManager;
23 import android.os.IBinder;
24 import android.util.DisplayMetrics;
25 import android.util.Log;
26 import android.view.Display;
27 import android.view.Gravity;
28 import android.view.SurfaceControlViewHost;
29 import android.view.View;
30 import android.widget.Button;
31 import android.widget.LinearLayout;
32 import android.widget.TextView;
33 
34 /**
35  * Service that provides the secondary lockscreen content when the secondary lockscreen is enabled.
36  *
37  * <p>Handles the {@link DevicePolicyManager#ACTION_BIND_SECONDARY_LOCKSCREEN_SERVICE} action and
38  * in used in conjunction with {@link DevicePolicyManager.setSecondaryLockscreenEnabled}.
39  */
40 public class SimpleKeyguardService extends DevicePolicyKeyguardService {
41 
42     public static final String TITLE_LABEL = "SimpleKeyguardService Title";
43     public static final String DISMISS_BUTTON_LABEL = "Dismiss";
44     private static final String TAG = "SimpleKeyguardService";
45 
46     @Override
onCreateKeyguardSurface(IBinder hostInputToken)47     public SurfaceControlViewHost.SurfacePackage onCreateKeyguardSurface(IBinder hostInputToken) {
48         Log.d(TAG, "onCreateKeyguardSurface()");
49         Context context = getApplicationContext();
50 
51         DisplayManager displayManager = context.getSystemService(DisplayManager.class);
52         Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
53         DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
54 
55         SurfaceControlViewHost wvr = new SurfaceControlViewHost(context, display, hostInputToken);
56         wvr.setView(createView(context), displayMetrics.widthPixels, displayMetrics.heightPixels);
57 
58         return wvr.getSurfacePackage();
59     }
60 
createView(Context context)61     private View createView(Context context) {
62         TextView title = new TextView(context);
63         title.setText(TITLE_LABEL);
64 
65         Button button = new Button(context);
66         // Avoid potential all caps text transformation on button. (eg. b/172993563)
67         button.setTransformationMethod(null);
68         button.setText(DISMISS_BUTTON_LABEL);
69         button.setOnClickListener(ignored -> {
70             button.setText("Dismissing...");
71             button.setEnabled(false);
72             dismiss();
73         });
74 
75         LinearLayout rootView = new LinearLayout(context);
76         rootView.setOrientation(LinearLayout.VERTICAL);
77         rootView.setGravity(Gravity.CENTER);
78         rootView.setBackgroundColor(Color.WHITE);
79         rootView.addView(title);
80         rootView.addView(button);
81         return rootView;
82     }
83 }
84