1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.ui;
6 
7 import android.content.Context;
8 import android.os.Build;
9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.ListAdapter;
12 import android.widget.ListView;
13 import android.widget.PopupWindow;
14 
15 /**
16  * The dropdown popup window that decides what widget should be used for the popup.
17  * For Android K+, DropdownPopupWindow is used, which is based on AnchoredPopupWindow.
18  * For devices before Android K, DropdowPopupWindowJellyBean is used, which is based
19  * on ListPopupWindow.
20  * Note that AnchoredPopupWindow can not be used on Android J due to a focus issue
21  * that blocks user from selecting the items.
22  */
23 public class DropdownPopupWindow {
24     private DropdownPopupWindowInterface mPopup;
25 
26     /**
27      * Creates an DropdownPopupWindow with specified parameters.
28      * @param context Application context.
29      * @param anchorView Popup view to be anchored.
30      */
DropdownPopupWindow(Context context, View anchorView)31     public DropdownPopupWindow(Context context, View anchorView) {
32         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
33             mPopup = new DropdownPopupWindowImpl(context, anchorView);
34         } else {
35             mPopup = new DropdownPopupWindowJellyBean(context, anchorView);
36         }
37     }
38 
39     /**
40      * Sets the adapter that provides the data and the views to represent the data
41      * in this popup window.
42      *
43      * @param adapter The adapter to use to create this window's content.
44      */
setAdapter(ListAdapter adapter)45     public void setAdapter(ListAdapter adapter) {
46         mPopup.setAdapter(adapter);
47     }
48 
setInitialSelection(int initialSelection)49     public void setInitialSelection(int initialSelection) {
50         mPopup.setInitialSelection(initialSelection);
51     }
52 
53     /**
54      * Shows the popup. The adapter should be set before calling this method.
55      */
show()56     public void show() {
57         mPopup.show();
58     }
59 
60     /**
61      * Set a listener to receive a callback when the popup is dismissed.
62      *
63      * @param listener Listener that will be notified when the popup is dismissed.
64      */
setOnDismissListener(PopupWindow.OnDismissListener listener)65     public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
66         mPopup.setOnDismissListener(listener);
67     }
68 
69     /**
70      * Sets the text direction in the dropdown. Should be called before show().
71      * @param isRtl If true, then dropdown text direction is right to left.
72      */
setRtl(boolean isRtl)73     public void setRtl(boolean isRtl) {
74         mPopup.setRtl(isRtl);
75     }
76 
77     /**
78      * Disable hiding on outside tap so that tapping on a text input field associated with the popup
79      * will not hide the popup.
80      */
disableHideOnOutsideTap()81     public void disableHideOnOutsideTap() {
82         mPopup.disableHideOnOutsideTap();
83     }
84 
85     /**
86      * Sets the content description to be announced by accessibility services when the dropdown is
87      * shown.
88      * @param description The description of the content to be announced.
89      */
setContentDescriptionForAccessibility(CharSequence description)90     public void setContentDescriptionForAccessibility(CharSequence description) {
91         mPopup.setContentDescriptionForAccessibility(description);
92     }
93 
94     /**
95      * Sets a listener to receive events when a list item is clicked.
96      *
97      * @param clickListener Listener to register
98      */
setOnItemClickListener(AdapterView.OnItemClickListener clickListener)99     public void setOnItemClickListener(AdapterView.OnItemClickListener clickListener) {
100         mPopup.setOnItemClickListener(clickListener);
101     }
102 
103     /**
104      * Show the popup. Will have no effect if the popup is already showing.
105      * Post a {@link #show()} call to the UI thread.
106      */
postShow()107     public void postShow() {
108         mPopup.postShow();
109     }
110 
111     /**
112      * Disposes of the popup window.
113      */
dismiss()114     public void dismiss() {
115         mPopup.dismiss();
116     }
117 
118     /**
119      * @return The {@link ListView} displayed within the popup window.
120      */
getListView()121     public ListView getListView() {
122         return mPopup.getListView();
123     }
124 
125     /**
126      * @return Whether the popup is currently showing.
127      */
isShowing()128     public boolean isShowing() {
129         return mPopup.isShowing();
130     }
131 
132     /**
133      * See {@link DropdownPopupWindowInterface#setFooterView(View)}.
134      */
setFooterView(View footerItem)135     protected void setFooterView(View footerItem) {
136         mPopup.setFooterView(footerItem);
137     }
138 }
139