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 package com.android.systemui.globalactions;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.util.LayoutDirection;
23 import android.view.View;
24 import android.view.View.MeasureSpec;
25 import android.widget.AdapterView;
26 import android.widget.ListAdapter;
27 import android.widget.ListPopupWindow;
28 import android.widget.ListView;
29 
30 import com.android.systemui.R;
31 
32 /**
33  * Customized widget for use in the GlobalActionsDialog. Ensures common positioning and user
34  * interactions.
35  *
36  * It should be created with a {@link Context} with the right theme
37  */
38 public class GlobalActionsPopupMenu extends ListPopupWindow {
39     private Context mContext;
40     private boolean mIsDropDownMode;
41     private int mMenuVerticalPadding = 0;
42     private int mGlobalActionsSidePadding = 0;
43     private ListAdapter mAdapter;
44     private AdapterView.OnItemLongClickListener mOnItemLongClickListener;
45 
GlobalActionsPopupMenu(@onNull Context context, boolean isDropDownMode)46     public GlobalActionsPopupMenu(@NonNull Context context, boolean isDropDownMode) {
47         super(context);
48         mContext = context;
49         Resources res = mContext.getResources();
50         setBackgroundDrawable(
51                 res.getDrawable(R.drawable.global_actions_popup_bg, context.getTheme()));
52         mIsDropDownMode = isDropDownMode;
53 
54         setInputMethodMode(INPUT_METHOD_NOT_NEEDED);
55         setModal(true);
56 
57         mGlobalActionsSidePadding = res.getDimensionPixelSize(R.dimen.global_actions_side_margin);
58         if (!isDropDownMode) {
59             mMenuVerticalPadding = res.getDimensionPixelSize(R.dimen.control_menu_vertical_padding);
60         }
61     }
62 
63     /**
64      * Set the listadapter used to populate this menu.
65      */
setAdapter(@ullable ListAdapter adapter)66     public void setAdapter(@Nullable ListAdapter adapter) {
67         mAdapter = adapter;
68         super.setAdapter(adapter);
69     }
70 
71     /**
72       * Show the dialog.
73       */
show()74     public void show() {
75         // need to call show() first in order to construct the listView
76         super.show();
77         if (mOnItemLongClickListener != null) {
78             getListView().setOnItemLongClickListener(mOnItemLongClickListener);
79         }
80 
81         ListView listView = getListView();
82         Resources res = mContext.getResources();
83 
84         setVerticalOffset(-getAnchorView().getHeight() / 2);
85 
86         if (mIsDropDownMode) {
87             // use a divider
88             listView.setDividerHeight(res.getDimensionPixelSize(R.dimen.control_list_divider));
89             listView.setDivider(res.getDrawable(R.drawable.controls_list_divider_inset));
90         } else {
91             if (mAdapter == null) return;
92 
93             // width should be between [.5, .9] of screen
94             int parentWidth = res.getSystem().getDisplayMetrics().widthPixels;
95             int widthSpec = MeasureSpec.makeMeasureSpec(
96                     (int) (parentWidth * 0.9), MeasureSpec.AT_MOST);
97             int maxWidth = 0;
98             for (int i = 0; i < mAdapter.getCount(); i++) {
99                 View child = mAdapter.getView(i, null, listView);
100                 child.measure(widthSpec, MeasureSpec.UNSPECIFIED);
101                 int w = child.getMeasuredWidth();
102                 maxWidth = Math.max(w, maxWidth);
103             }
104             int width = Math.max(maxWidth, (int) (parentWidth * 0.5));
105             listView.setPadding(0, mMenuVerticalPadding, 0, mMenuVerticalPadding);
106 
107             setWidth(width);
108             if (getAnchorView().getLayoutDirection() == LayoutDirection.LTR) {
109                 setHorizontalOffset(getAnchorView().getWidth() - mGlobalActionsSidePadding - width);
110             } else {
111                 setHorizontalOffset(mGlobalActionsSidePadding);
112             }
113         }
114 
115         super.show();
116     }
117 
setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener)118     public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
119         mOnItemLongClickListener = listener;
120     }
121 }
122