1bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/*
2ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * Copyright 2018 The Android Open Source Project
3bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
4bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * you may not use this file except in compliance with the License.
6bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * You may obtain a copy of the License at
7bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
8bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
10bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Unless required by applicable law or agreed to in writing, software
11bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * See the License for the specific language governing permissions and
14bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * limitations under the License.
15bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
16bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.core.view;
18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
19ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas
21bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context;
2234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powellimport android.util.Log;
2334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powellimport android.view.MenuItem;
2430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.SubMenu;
25bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.View;
26bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
279dede51868bbbe16aadcd65e04860bea8ea50e05Aurimas Liutikasimport androidx.annotation.RestrictTo;
289dede51868bbbe16aadcd65e04860bea8ea50e05Aurimas Liutikas
29bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/**
3020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * This class is a mediator for accomplishing a given task, for example sharing a file. It is
3120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * responsible for creating a view that performs an action that accomplishes the task. This class
3220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * also implements other functions such a performing a default action.
3320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns *
349dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <p class="note"><strong>Note:</strong> This class is included in the <a
359dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility
369dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * with API level 4 and higher. If you're developing your app for API level 14 and higher
379dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <em>only</em>, you should instead use the framework {@link android.view.ActionProvider}
389dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * class.</p>
399dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main *
4020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * <p>An ActionProvider can be
4130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell * optionally specified for a {@link android.view.MenuItem} and in such a case it will be
42e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes * responsible for
4320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * creating the action view that appears in the {@link android.app.ActionBar} as a substitute for
4420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * the menu item when the item is displayed as an action item. Also the provider is responsible for
4520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * performing a default action if a menu item placed on the overflow menu of the ActionBar is
4620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * selected and none of the menu item callbacks has handled the selection. For this case the
4720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * provider can also optionally provide a sub-menu for accomplishing the task at hand.
4820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns *
4920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * <p>There are two ways for using an action provider for creating and handling of action views:
5020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns *
5130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell * <ul><li> Setting the action provider on a {@link android.view.MenuItem} directly by
5230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell * calling {@link
53ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * androidx.core.view.MenuItemCompat#setActionProvider(android.view.MenuItem, ActionProvider)}.
5430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell * </li>
5520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns *
5620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * <li>Declaring the action provider in the menu XML resource. For example:
5720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns *
589dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <pre><code>
59bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *   &lt;item android:id="@+id/my_menu_item"
609dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main *     android:title="@string/my_menu_item_title"
61bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *     android:icon="@drawable/my_menu_item_icon"
62bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *     android:showAsAction="ifRoom"
63bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *     android:actionProviderClass="foo.bar.SomeActionProvider" /&gt;
649dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * </code></pre>
6520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns * </li></ul></p>
66bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
675a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <h3>Creating a custom action provider</h3>
685a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
695a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>To create a custom action provider, extend ActionProvider and implement
705a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * its callback methods as necessary. In particular, implement the following
715a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * methods:</p>
725a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
735a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dl>
745a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dt>{@link #ActionProvider ActionProvider()} constructor</dt>
755a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dd>This constructor is passed the application context. You should
765a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * save the context in a member field to use in the other callback methods.</dd>
775a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
785a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dt>{@link #onCreateActionView onCreateActionView(MenuItem)}</dt>
795a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dd>The system calls this method when the action provider is created.
805a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * You define the action provider's layout through the implementation of this
815a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * method. Use the context acquired
825a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * from the constructor to instantiate a {@link android.view.LayoutInflater} and
835a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * inflate your action provider's layout from an XML resource, then hook up
845a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * event listeners for the view's components. For example:
855a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
865a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *<pre>
875a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * public View onCreateActionView(MenuItem forItem) {
885a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     // Inflate the action provider to be shown on the action bar.
895a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     LayoutInflater layoutInflater = LayoutInflater.from(mContext);
905a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     View providerView =
915a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *         layoutInflater.inflate(R.layout.my_action_provider, null);
925a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     ImageButton button =
935a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *         (ImageButton) providerView.findViewById(R.id.button);
945a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     button.setOnClickListener(new View.OnClickListener() {
955a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *         &#64;Override
965a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *         public void onClick(View v) {
975a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *             // Do something...
985a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *         }
995a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     });
1005a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     return providerView;
1015a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * }</pre>
1025a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * </dd>
1035a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1045a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dt>{@link #onPerformDefaultAction onPerformDefaultAction()}</dt>
1055a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <dd><p>The system calls this method when the user selects a menu item from the action
1065a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * overflow. The action provider should perform a default action for the
1075a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * menu item. The system does not call this method if the menu item opens a submenu.</p>
1085a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1095a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>If your action provider presents a submenu through the
1105a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * {@link #onPrepareSubMenu onPrepareSubMenu()} callback, the submenu
1115a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * appears even if the action provider is in the overflow menu.
1125a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * Thus, the system never calls {@link #onPerformDefaultAction
1135a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * onPerformDefaultAction()} if there is a submenu.</p>
1145a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1155a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p class="note"> <strong>Note:</strong> An activity or a fragment that
1165a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * implements <code>onOptionsItemSelected()</code> can override the action
1175a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * provider's default behavior (unless it uses a submenu) by handling the
1185a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * item-selected event and returning <code>true</code>. In this case, the
1195a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * system does not call
1205a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * {@link #onPerformDefaultAction onPerformDefaultAction()}.</p></dd>
1215a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * </dl>
1225a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1235a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
124ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * @see androidx.core.view.MenuItemCompat#setActionProvider(android.view.MenuItem, ActionProvider)
125ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * @see androidx.core.view.MenuItemCompat#getActionProvider(android.view.MenuItem)
126bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
127bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpublic abstract class ActionProvider {
12834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    private static final String TAG = "ActionProvider(support)";
129e6072e2d918169bd827cf7431347fb648124c227Jeff Brown    private final Context mContext;
130bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
13120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    private SubUiVisibilityListener mSubUiVisibilityListener;
13234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    private VisibilityListener mVisibilityListener;
133bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
13420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
13520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Creates a new instance.
13620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
13720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @param context Context for accessing resources.
13820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
13920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public ActionProvider(Context context) {
140e6072e2d918169bd827cf7431347fb648124c227Jeff Brown        mContext = context;
141e6072e2d918169bd827cf7431347fb648124c227Jeff Brown    }
142e6072e2d918169bd827cf7431347fb648124c227Jeff Brown
143e6072e2d918169bd827cf7431347fb648124c227Jeff Brown    /**
144e6072e2d918169bd827cf7431347fb648124c227Jeff Brown     * Gets the context associated with this action provider.
145e6072e2d918169bd827cf7431347fb648124c227Jeff Brown     */
146e6072e2d918169bd827cf7431347fb648124c227Jeff Brown    public Context getContext() {
147e6072e2d918169bd827cf7431347fb648124c227Jeff Brown        return mContext;
14820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    }
149bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
15020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
15120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Factory method for creating new action views.
15220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
15320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @return A new action view.
15420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
15520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public abstract View onCreateActionView();
156bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
15720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
15834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * Factory method called by the Android framework to create new action views.
15934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * This method returns a new action view for the given MenuItem.
16034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
16134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * <p>If your ActionProvider implementation overrides the deprecated no-argument overload
16234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * {@link #onCreateActionView()}, overriding this method for devices running API 16 or later
16334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * is recommended but optional. The default implementation calls {@link #onCreateActionView()}
16434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * for compatibility with applications written for older platform versions.</p>
16534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
16634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @param forItem MenuItem to create the action view for
16734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @return the new action view
16834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     */
16934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    public View onCreateActionView(MenuItem forItem) {
17034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        return onCreateActionView();
17134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    }
17234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
17334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    /**
17434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * The result of this method determines whether or not {@link #isVisible()} will be used
17534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * by the {@link MenuItem} this ActionProvider is bound to help determine its visibility.
17634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
17734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @return true if this ActionProvider overrides the visibility of the MenuItem
17834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *         it is bound to, false otherwise. The default implementation returns false.
17934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @see #isVisible()
18034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     */
18134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    public boolean overridesItemVisibility() {
18234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        return false;
18334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    }
18434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
18534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    /**
18634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * If {@link #overridesItemVisibility()} returns true, the return value of this method
18734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * will help determine the visibility of the {@link MenuItem} this ActionProvider is bound to.
18834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
18934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * <p>If the MenuItem's visibility is explicitly set to false by the application,
19034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * the MenuItem will not be shown, even if this method returns true.</p>
19134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
19234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @return true if the MenuItem this ActionProvider is bound to is visible, false if
19334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *         it is invisible. The default implementation returns true.
19434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     */
19534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    public boolean isVisible() {
19634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        return true;
19734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    }
19834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
19934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    /**
20034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * If this ActionProvider is associated with an item in a menu,
20134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * refresh the visibility of the item based on {@link #overridesItemVisibility()} and
20234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * {@link #isVisible()}. If {@link #overridesItemVisibility()} returns false, this call
20334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * will have no effect.
20434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     */
20534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    public void refreshVisibility() {
20634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        if (mVisibilityListener != null && overridesItemVisibility()) {
20734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            mVisibilityListener.onActionProviderVisibilityChanged(isVisible());
20834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        }
20934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    }
21034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
21134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    /**
21220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Performs an optional default action.
21320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
21420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <p>For the case of an action provider placed in a menu
21520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * item not shown as an action this method is invoked if previous callbacks for processing menu
21620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * selection has handled the event.
21720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
21820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <p> A menu item selection is processed in the following order:
21920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
220e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes     * <ul><li>Receiving a call to
22130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell     * {@link android.view.MenuItem.OnMenuItemClickListener#onMenuItemClick
22220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * MenuItem.OnMenuItemClickListener.onMenuItemClick}.</li>
22320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
224e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes     * <li>Receiving a call to
22530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell     * {@link android.app.Activity#onOptionsItemSelected(android.view.MenuItem)}
22630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell     * FragmentActivity.onOptionsItemSelected(MenuItem)}
22720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * </li>
22820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
229e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes     * <li>Receiving a call to
230ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas     * {@link androidx.fragment.app.Fragment#onOptionsItemSelected(android.view.MenuItem)}
23130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell     * Fragment.onOptionsItemSelected(MenuItem)}</li>
23220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
23320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <li>Launching the {@link android.content.Intent} set via
23430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell     * {@link android.view.MenuItem#setIntent(android.content.Intent)
235e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes     * MenuItem.setIntent(android.content.Intent)}
23620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * </li>
23720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
23820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <li>Invoking this method.</li></ul>
23920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
24020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <p>The default implementation does not perform any action and returns false.
24120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
24220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public boolean onPerformDefaultAction() {
24320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        return false;
24420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    }
245bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
24620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
24720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Determines if this ActionProvider has a submenu associated with it.
24820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
24920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <p>Associated submenus will be shown when an action view is not. This provider instance will
25020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * receive a call to {@link #onPrepareSubMenu(SubMenu)} after the call to {@link
25120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * #onPerformDefaultAction()} and before a submenu is displayed to the user.
25220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
25320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @return true if the item backed by this provider should have an associated submenu
25420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
25520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public boolean hasSubMenu() {
25620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        return false;
25720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    }
25820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
25920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
26020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Called to prepare an associated submenu for the menu item backed by this ActionProvider.
26120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
26220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * <p>if {@link #hasSubMenu()} returns true, this method will be called when the menu item is
26320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * selected to prepare the submenu for presentation to the user. Apps may use this to create or
26420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * alter submenu content right before display.
26520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
26620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @param subMenu Submenu that will be displayed
26720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
26820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public void onPrepareSubMenu(SubMenu subMenu) {
26920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    }
270bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
27120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
27220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Notify the system that the visibility of an action view's sub-UI such as an anchored popup
27320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * has changed. This will affect how other system visibility notifications occur.
27420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *
27520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @hide Pending future API approval
27620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
2778e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas    @RestrictTo(LIBRARY_GROUP)
27820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public void subUiVisibilityChanged(boolean isVisible) {
27920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        if (mSubUiVisibilityListener != null) {
28020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            mSubUiVisibilityListener.onSubUiVisibilityChanged(isVisible);
28120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        }
282bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
283bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
28420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
28520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @hide Internal use only
28620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
2878e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas    @RestrictTo(LIBRARY_GROUP)
28820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public void setSubUiVisibilityListener(SubUiVisibilityListener listener) {
28920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        mSubUiVisibilityListener = listener;
29020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    }
29120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
29220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    /**
29334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * Set a listener to be notified when this ActionProvider's overridden visibility changes.
29434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * This should only be used by MenuItem implementations.
29534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
29634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @param listener listener to set
29734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     */
29834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    public void setVisibilityListener(VisibilityListener listener) {
29934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        if (mVisibilityListener != null && listener != null) {
30034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            Log.w(TAG, "setVisibilityListener: Setting a new ActionProvider.VisibilityListener " +
30134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell                    "when one is already set. Are you reusing this " + getClass().getSimpleName() +
30234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell                    " instance while it is still in use somewhere else?");
30334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        }
30434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        mVisibilityListener = listener;
30534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    }
30634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
30734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    /**
3085d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes     * @hide
3095d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes     */
3108e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas    @RestrictTo(LIBRARY_GROUP)
3115d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes    public void reset() {
3125d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes        mVisibilityListener = null;
3135d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes        mSubUiVisibilityListener = null;
3145d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes    }
3155d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes
3165d3c47dccb89d5616ec6f3625e586a5cc4fa61c9Chris Banes    /**
31720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @hide Internal use only
31820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     */
3198e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas    @RestrictTo(LIBRARY_GROUP)
32020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    public interface SubUiVisibilityListener {
321bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3225211222f71426ccfc1f2cca9f5f6bb89c61c9ed5Jake Wharton        void onSubUiVisibilityChanged(boolean isVisible);
32320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns    }
32434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
32534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    /**
32634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * Listens to changes in visibility as reported by {@link ActionProvider#refreshVisibility()}.
32734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     *
32834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @see ActionProvider#overridesItemVisibility()
32934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     * @see ActionProvider#isVisible()
33034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell     */
33134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    public interface VisibilityListener {
3325211222f71426ccfc1f2cca9f5f6bb89c61c9ed5Jake Wharton        void onActionProviderVisibilityChanged(boolean isVisible);
33334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell    }
334bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell}
335