1 /*
2  * Copyright (C) 2021 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 package com.android.keyguard;
17 
18 import android.annotation.NonNull;
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Canvas;
22 import android.graphics.drawable.ShapeDrawable;
23 import android.view.MotionEvent;
24 import android.view.View;
25 import android.widget.ListPopupWindow;
26 import android.widget.ListView;
27 
28 import com.android.systemui.plugins.FalsingManager;
29 import com.android.systemui.res.R;
30 
31 /**
32  * Custom user-switcher for use on the bouncer.
33  */
34 public class KeyguardUserSwitcherPopupMenu extends ListPopupWindow {
35     private Context mContext;
36     private FalsingManager mFalsingManager;
37 
KeyguardUserSwitcherPopupMenu(@onNull Context context, @NonNull FalsingManager falsingManager)38     public KeyguardUserSwitcherPopupMenu(@NonNull Context context,
39             @NonNull FalsingManager falsingManager) {
40         super(context);
41         mContext = context;
42         mFalsingManager = falsingManager;
43         Resources res = mContext.getResources();
44         setBackgroundDrawable(
45                 res.getDrawable(R.drawable.bouncer_user_switcher_popup_bg, context.getTheme()));
46         setModal(true);
47         setOverlapAnchor(true);
48     }
49 
50     /**
51       * Show the dialog.
52       */
53     @Override
show()54     public void show() {
55         // need to call show() first in order to construct the listView
56         super.show();
57         ListView listView = getListView();
58 
59         listView.setVerticalScrollBarEnabled(false);
60         listView.setHorizontalScrollBarEnabled(false);
61 
62         // Creates a transparent spacer between items
63         ShapeDrawable shape = new ShapeDrawable();
64         shape.setAlpha(0);
65         listView.setDivider(shape);
66         listView.setDividerHeight(mContext.getResources().getDimensionPixelSize(
67                 R.dimen.bouncer_user_switcher_popup_divider_height));
68 
69         if (listView.getTag(R.id.header_footer_views_added_tag_key) == null) {
70             int height = mContext.getResources().getDimensionPixelSize(
71                     R.dimen.bouncer_user_switcher_popup_header_height);
72             listView.addHeaderView(createSpacer(height), null, false);
73             listView.addFooterView(createSpacer(height), null, false);
74             listView.setTag(R.id.header_footer_views_added_tag_key, new Object());
75         }
76 
77         listView.setOnTouchListener((v, ev) -> {
78             if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
79                 return mFalsingManager.isFalseTap(FalsingManager.LOW_PENALTY);
80             }
81             return false;
82         });
83         super.show();
84     }
85 
createSpacer(int height)86     private View createSpacer(int height) {
87         return new View(mContext) {
88             @Override
89             protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
90                 setMeasuredDimension(1, height);
91             }
92 
93             @Override
94             public void draw(Canvas canvas) {
95             }
96         };
97     }
98 }
99