• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 android.support.v4.view;
18 
19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.content.Context;
22 import android.support.annotation.RestrictTo;
23 import android.util.Log;
24 import android.view.MenuItem;
25 import android.view.SubMenu;
26 import android.view.View;
27 
28 /**
29  * This class is a mediator for accomplishing a given task, for example sharing a file. It is
30  * responsible for creating a view that performs an action that accomplishes the task. This class
31  * also implements other functions such a performing a default action.
32  *
33  * <p class="note"><strong>Note:</strong> This class is included in the <a
34  * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
35  * with API level 4 and higher. If you're developing your app for API level 14 and higher
36  * <em>only</em>, you should instead use the framework {@link android.view.ActionProvider}
37  * class.</p>
38  *
39  * <p>An ActionProvider can be
40  * optionally specified for a {@link android.view.MenuItem} and in such a case it will be
41  * responsible for
42  * creating the action view that appears in the {@link android.app.ActionBar} as a substitute for
43  * the menu item when the item is displayed as an action item. Also the provider is responsible for
44  * performing a default action if a menu item placed on the overflow menu of the ActionBar is
45  * selected and none of the menu item callbacks has handled the selection. For this case the
46  * provider can also optionally provide a sub-menu for accomplishing the task at hand.
47  *
48  * <p>There are two ways for using an action provider for creating and handling of action views:
49  *
50  * <ul><li> Setting the action provider on a {@link android.view.MenuItem} directly by
51  * calling {@link
52  * android.support.v4.view.MenuItemCompat#setActionProvider(android.view.MenuItem, ActionProvider)}.
53  * </li>
54  *
55  * <li>Declaring the action provider in the menu XML resource. For example:
56  *
57  * <pre><code>
58  *   &lt;item android:id="@+id/my_menu_item"
59  *     android:title="@string/my_menu_item_title"
60  *     android:icon="@drawable/my_menu_item_icon"
61  *     android:showAsAction="ifRoom"
62  *     android:actionProviderClass="foo.bar.SomeActionProvider" /&gt;
63  * </code></pre>
64  * </li></ul></p>
65  *
66  * <h3>Creating a custom action provider</h3>
67  *
68  * <p>To create a custom action provider, extend ActionProvider and implement
69  * its callback methods as necessary. In particular, implement the following
70  * methods:</p>
71  *
72  * <dl>
73  * <dt>{@link #ActionProvider ActionProvider()} constructor</dt>
74  * <dd>This constructor is passed the application context. You should
75  * save the context in a member field to use in the other callback methods.</dd>
76  *
77  * <dt>{@link #onCreateActionView onCreateActionView(MenuItem)}</dt>
78  * <dd>The system calls this method when the action provider is created.
79  * You define the action provider's layout through the implementation of this
80  * method. Use the context acquired
81  * from the constructor to instantiate a {@link android.view.LayoutInflater} and
82  * inflate your action provider's layout from an XML resource, then hook up
83  * event listeners for the view's components. For example:
84  *
85  *<pre>
86  * public View onCreateActionView(MenuItem forItem) {
87  *     // Inflate the action provider to be shown on the action bar.
88  *     LayoutInflater layoutInflater = LayoutInflater.from(mContext);
89  *     View providerView =
90  *         layoutInflater.inflate(R.layout.my_action_provider, null);
91  *     ImageButton button =
92  *         (ImageButton) providerView.findViewById(R.id.button);
93  *     button.setOnClickListener(new View.OnClickListener() {
94  *         &#64;Override
95  *         public void onClick(View v) {
96  *             // Do something...
97  *         }
98  *     });
99  *     return providerView;
100  * }</pre>
101  * </dd>
102  *
103  * <dt>{@link #onPerformDefaultAction onPerformDefaultAction()}</dt>
104  * <dd><p>The system calls this method when the user selects a menu item from the action
105  * overflow. The action provider should perform a default action for the
106  * menu item. The system does not call this method if the menu item opens a submenu.</p>
107  *
108  * <p>If your action provider presents a submenu through the
109  * {@link #onPrepareSubMenu onPrepareSubMenu()} callback, the submenu
110  * appears even if the action provider is in the overflow menu.
111  * Thus, the system never calls {@link #onPerformDefaultAction
112  * onPerformDefaultAction()} if there is a submenu.</p>
113  *
114  * <p class="note"> <strong>Note:</strong> An activity or a fragment that
115  * implements <code>onOptionsItemSelected()</code> can override the action
116  * provider's default behavior (unless it uses a submenu) by handling the
117  * item-selected event and returning <code>true</code>. In this case, the
118  * system does not call
119  * {@link #onPerformDefaultAction onPerformDefaultAction()}.</p></dd>
120  * </dl>
121  *
122  *
123  * @see android.support.v4.view.MenuItemCompat#setActionProvider(android.view.MenuItem, ActionProvider)
124  * @see android.support.v4.view.MenuItemCompat#getActionProvider(android.view.MenuItem)
125  */
126 public abstract class ActionProvider {
127     private static final String TAG = "ActionProvider(support)";
128     private final Context mContext;
129 
130     private SubUiVisibilityListener mSubUiVisibilityListener;
131     private VisibilityListener mVisibilityListener;
132 
133     /**
134      * Creates a new instance.
135      *
136      * @param context Context for accessing resources.
137      */
ActionProvider(Context context)138     public ActionProvider(Context context) {
139         mContext = context;
140     }
141 
142     /**
143      * Gets the context associated with this action provider.
144      */
getContext()145     public Context getContext() {
146         return mContext;
147     }
148 
149     /**
150      * Factory method for creating new action views.
151      *
152      * @return A new action view.
153      */
onCreateActionView()154     public abstract View onCreateActionView();
155 
156     /**
157      * Factory method called by the Android framework to create new action views.
158      * This method returns a new action view for the given MenuItem.
159      *
160      * <p>If your ActionProvider implementation overrides the deprecated no-argument overload
161      * {@link #onCreateActionView()}, overriding this method for devices running API 16 or later
162      * is recommended but optional. The default implementation calls {@link #onCreateActionView()}
163      * for compatibility with applications written for older platform versions.</p>
164      *
165      * @param forItem MenuItem to create the action view for
166      * @return the new action view
167      */
onCreateActionView(MenuItem forItem)168     public View onCreateActionView(MenuItem forItem) {
169         return onCreateActionView();
170     }
171 
172     /**
173      * The result of this method determines whether or not {@link #isVisible()} will be used
174      * by the {@link MenuItem} this ActionProvider is bound to help determine its visibility.
175      *
176      * @return true if this ActionProvider overrides the visibility of the MenuItem
177      *         it is bound to, false otherwise. The default implementation returns false.
178      * @see #isVisible()
179      */
overridesItemVisibility()180     public boolean overridesItemVisibility() {
181         return false;
182     }
183 
184     /**
185      * If {@link #overridesItemVisibility()} returns true, the return value of this method
186      * will help determine the visibility of the {@link MenuItem} this ActionProvider is bound to.
187      *
188      * <p>If the MenuItem's visibility is explicitly set to false by the application,
189      * the MenuItem will not be shown, even if this method returns true.</p>
190      *
191      * @return true if the MenuItem this ActionProvider is bound to is visible, false if
192      *         it is invisible. The default implementation returns true.
193      */
isVisible()194     public boolean isVisible() {
195         return true;
196     }
197 
198     /**
199      * If this ActionProvider is associated with an item in a menu,
200      * refresh the visibility of the item based on {@link #overridesItemVisibility()} and
201      * {@link #isVisible()}. If {@link #overridesItemVisibility()} returns false, this call
202      * will have no effect.
203      */
refreshVisibility()204     public void refreshVisibility() {
205         if (mVisibilityListener != null && overridesItemVisibility()) {
206             mVisibilityListener.onActionProviderVisibilityChanged(isVisible());
207         }
208     }
209 
210     /**
211      * Performs an optional default action.
212      *
213      * <p>For the case of an action provider placed in a menu
214      * item not shown as an action this method is invoked if previous callbacks for processing menu
215      * selection has handled the event.
216      *
217      * <p> A menu item selection is processed in the following order:
218      *
219      * <ul><li>Receiving a call to
220      * {@link android.view.MenuItem.OnMenuItemClickListener#onMenuItemClick
221      * MenuItem.OnMenuItemClickListener.onMenuItemClick}.</li>
222      *
223      * <li>Receiving a call to
224      * {@link android.app.Activity#onOptionsItemSelected(android.view.MenuItem)}
225      * FragmentActivity.onOptionsItemSelected(MenuItem)}
226      * </li>
227      *
228      * <li>Receiving a call to
229      * {@link android.support.v4.app.Fragment#onOptionsItemSelected(android.view.MenuItem)}
230      * Fragment.onOptionsItemSelected(MenuItem)}</li>
231      *
232      * <li>Launching the {@link android.content.Intent} set via
233      * {@link android.view.MenuItem#setIntent(android.content.Intent)
234      * MenuItem.setIntent(android.content.Intent)}
235      * </li>
236      *
237      * <li>Invoking this method.</li></ul>
238      *
239      * <p>The default implementation does not perform any action and returns false.
240      */
onPerformDefaultAction()241     public boolean onPerformDefaultAction() {
242         return false;
243     }
244 
245     /**
246      * Determines if this ActionProvider has a submenu associated with it.
247      *
248      * <p>Associated submenus will be shown when an action view is not. This provider instance will
249      * receive a call to {@link #onPrepareSubMenu(SubMenu)} after the call to {@link
250      * #onPerformDefaultAction()} and before a submenu is displayed to the user.
251      *
252      * @return true if the item backed by this provider should have an associated submenu
253      */
hasSubMenu()254     public boolean hasSubMenu() {
255         return false;
256     }
257 
258     /**
259      * Called to prepare an associated submenu for the menu item backed by this ActionProvider.
260      *
261      * <p>if {@link #hasSubMenu()} returns true, this method will be called when the menu item is
262      * selected to prepare the submenu for presentation to the user. Apps may use this to create or
263      * alter submenu content right before display.
264      *
265      * @param subMenu Submenu that will be displayed
266      */
onPrepareSubMenu(SubMenu subMenu)267     public void onPrepareSubMenu(SubMenu subMenu) {
268     }
269 
270     /**
271      * Notify the system that the visibility of an action view's sub-UI such as an anchored popup
272      * has changed. This will affect how other system visibility notifications occur.
273      *
274      * @hide Pending future API approval
275      */
276     @RestrictTo(LIBRARY_GROUP)
subUiVisibilityChanged(boolean isVisible)277     public void subUiVisibilityChanged(boolean isVisible) {
278         if (mSubUiVisibilityListener != null) {
279             mSubUiVisibilityListener.onSubUiVisibilityChanged(isVisible);
280         }
281     }
282 
283     /**
284      * @hide Internal use only
285      */
286     @RestrictTo(LIBRARY_GROUP)
setSubUiVisibilityListener(SubUiVisibilityListener listener)287     public void setSubUiVisibilityListener(SubUiVisibilityListener listener) {
288         mSubUiVisibilityListener = listener;
289     }
290 
291     /**
292      * Set a listener to be notified when this ActionProvider's overridden visibility changes.
293      * This should only be used by MenuItem implementations.
294      *
295      * @param listener listener to set
296      */
setVisibilityListener(VisibilityListener listener)297     public void setVisibilityListener(VisibilityListener listener) {
298         if (mVisibilityListener != null && listener != null) {
299             Log.w(TAG, "setVisibilityListener: Setting a new ActionProvider.VisibilityListener " +
300                     "when one is already set. Are you reusing this " + getClass().getSimpleName() +
301                     " instance while it is still in use somewhere else?");
302         }
303         mVisibilityListener = listener;
304     }
305 
306     /**
307      * @hide
308      */
309     @RestrictTo(LIBRARY_GROUP)
reset()310     public void reset() {
311         mVisibilityListener = null;
312         mSubUiVisibilityListener = null;
313     }
314 
315     /**
316      * @hide Internal use only
317      */
318     @RestrictTo(LIBRARY_GROUP)
319     public interface SubUiVisibilityListener {
320 
onSubUiVisibilityChanged(boolean isVisible)321         public void onSubUiVisibilityChanged(boolean isVisible);
322     }
323 
324     /**
325      * Listens to changes in visibility as reported by {@link ActionProvider#refreshVisibility()}.
326      *
327      * @see ActionProvider#overridesItemVisibility()
328      * @see ActionProvider#isVisible()
329      */
330     public interface VisibilityListener {
onActionProviderVisibilityChanged(boolean isVisible)331         public void onActionProviderVisibilityChanged(boolean isVisible);
332     }
333 }
334