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.systemui.plugin.globalactions.wallet;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.view.ContextThemeWrapper;
22 import android.view.Gravity;
23 import android.view.View;
24 import android.view.View.MeasureSpec;
25 import android.view.WindowManager;
26 import android.widget.ArrayAdapter;
27 import android.widget.FrameLayout;
28 import android.widget.ListPopupWindow;
29 
30 /**
31  * This is the overflow popup menu. To maintain visual consistency within Global Actions, this
32  * implementation and {@code com.android.systemui.globalactions.GlobalActionsPopupMenu} should stay
33  * in sync.
34  */
35 class WalletPopupMenu extends ListPopupWindow {
36 
37     private final Context mContext;
38     private final ArrayAdapter<OverflowItem> mAdapter;
39 
WalletPopupMenu(Context context, View anchorView)40     WalletPopupMenu(Context context, View anchorView) {
41         super(new ContextThemeWrapper(context, R.style.Wallet_ListPopupWindow));
42         mContext = context;
43         setWindowLayoutType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
44         setInputMethodMode(INPUT_METHOD_NOT_NEEDED);
45         setModal(true);
46         setAnchorView(anchorView);
47         setDropDownGravity(Gravity.END);
48         mAdapter = new ArrayAdapter<>(context, R.layout.wallet_more_item);
49         setAdapter(mAdapter);
50         Resources res = context.getResources();
51         setBackgroundDrawable(res.getDrawable(R.drawable.wallet_overflow_popup_bg, null));
52         setVerticalOffset(res.getDimensionPixelSize(R.dimen.wallet_menu_vertical_offset));
53         int horizontalOffset = res.getDimensionPixelSize(R.dimen.wallet_menu_horizontal_offset);
54         boolean isLtr = res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
55         setHorizontalOffset(isLtr ? -horizontalOffset : horizontalOffset);
56         setOnItemClickListener((parent, view, position, id) -> {
57             mAdapter.getItem(position).onClickListener.run();
58             dismiss();
59         });
60     }
61 
setMenuItems(OverflowItem[] menuItems)62     void setMenuItems(OverflowItem[] menuItems) {
63         mAdapter.clear();
64         mAdapter.addAll(menuItems);
65         setContentWidth(measureContentWidth());
66     }
67 
measureContentWidth()68     private int measureContentWidth() {
69         int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
70         // width should be between [.5, .9] of screen
71         int width = Math.round(screenWidth * 0.5f);
72         int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
73         int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
74         int count = mAdapter.getCount();
75         FrameLayout measureParent = new FrameLayout(mContext);
76         View itemView = null;
77         for (int i = 0; i < count; i++) {
78             itemView = mAdapter.getView(i, itemView, measureParent);
79             itemView.measure(widthMeasureSpec, heightMeasureSpec);
80             width = Math.max(itemView.getMeasuredWidth(), width);
81         }
82         int maxWidth = Math.round(screenWidth * 0.9f);
83         return Math.min(maxWidth, width);
84     }
85 
86     static class OverflowItem {
87         final CharSequence label;
88         final Runnable onClickListener;
89 
OverflowItem(CharSequence label, Runnable onClickListener)90         OverflowItem(CharSequence label, Runnable onClickListener) {
91             this.label = label;
92             this.onClickListener = onClickListener;
93         }
94 
95         @Override
toString()96         public String toString() {
97             return label.toString();
98         }
99     }
100 }
101