1e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes/*
2e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Copyright (C) 2013 The Android Open Source Project
3e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes *
4e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
5e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * you may not use this file except in compliance with the License.
6e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * You may obtain a copy of the License at
7e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes *
8e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
9e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes *
10e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Unless required by applicable law or agreed to in writing, software
11e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
12e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * See the License for the specific language governing permissions and
14e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * limitations under the License.
15e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */
16e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.appcompat.widget;
18e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
19e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.content.Context;
20e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.content.Intent;
21e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.content.pm.PackageManager;
22e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.content.pm.ResolveInfo;
23e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.graphics.drawable.Drawable;
24b645de790756e27bbe92d133216b7ac79cca7679Chris Banesimport android.os.Build;
25e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.util.TypedValue;
26e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.view.Menu;
27e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.view.MenuItem;
28e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.view.MenuItem.OnMenuItemClickListener;
29e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.view.SubMenu;
30e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banesimport android.view.View;
31e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
323de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.R;
333de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.content.res.AppCompatResources;
343de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.widget.ActivityChooserModel.OnChooseActivityListener;
353de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.core.view.ActionProvider;
363de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikas
37e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes/**
385a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * Provides a share action, which is suitable for an activity's app bar. Creates
395a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * views that enable data sharing. If the provider appears in the
405a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * overflow menu, it creates a submenu with the appropriate sharing
415a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * actions.
425a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
435a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <h3 id="add-share-action">Adding a share action</h3>
445a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
455a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>To add a "share" action to your activity, put a
465a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <code>ShareActionProvider</code> in the app bar's menu resource. For
475a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * example:</p>
485a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
495a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <pre>
505a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * &lt;item android:id="&#64;+id/action_share"
515a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *      android:title="&#64;string/share"
525a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *      app:showAsAction="ifRoom"
53ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas *      app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"/&gt;
545a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * </pre>
555a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
565a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>You do not need to specify an icon, since the
575a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <code>ShareActionProvider</code> widget takes care of its own appearance and
585a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * behavior. However, you do need to specify a title with
595a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <code>android:title</code>, in case the action ends up in the overflow
605a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * menu.</p>
615a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
625a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>Next, set up the intent that contains the content your activity is
635a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * able to share. You should create this intent in your handler for
645a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * {@link android.app.Activity#onCreateOptionsMenu onCreateOptionsMenu()},
655a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * and update it every time the shareable content changes. To set up the
665a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * intent:</p>
675a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
685a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <ol>
695a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <li>Get a reference to the ShareActionProvider by calling {@link
705a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * android.view.MenuItem#getActionProvider getActionProvider()} and
715a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * passing the share action's {@link android.view.MenuItem}. For
725a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * example:
735a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
745a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <pre>
755a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * MenuItem shareItem = menu.findItem(R.id.action_share);
765a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * ShareActionProvider myShareActionProvider =
775a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *     (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);</pre></li>
785a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
795a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <li>Create an intent with the {@link android.content.Intent#ACTION_SEND}
805a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * action, and attach the content shared by the activity. For example, the
815a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * following intent shares an image:
825a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
835a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <pre>
845a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * Intent myShareIntent = new Intent(Intent.ACTION_SEND);
855a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * myShareIntent.setType("image/*");
865a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * myShareIntent.putExtra(Intent.EXTRA_STREAM, myImageUri);</pre></li>
875a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
885a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <li>Call {@link #setShareIntent setShareIntent()} to attach this intent to
895a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * the action provider:
905a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
915a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <pre>
925a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * myShareActionProvider.setShareIntent(myShareIntent);
935a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * </pre></li>
945a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
955a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <li>When the content changes, modify the intent or create a new one,
965a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * and call {@link #setShareIntent setShareIntent()} again. For example:
975a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
985a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <pre>
995a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * // Image has changed! Update the intent:
1005a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * myShareIntent.putExtra(Intent.EXTRA_STREAM, myNewImageUri);
1015a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * myShareActionProvider.setShareIntent(myShareIntent);</pre></li>
1025a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * </ol>
1035a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1045a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <h3 id="rankings">Share target rankings</h3>
1055a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1065a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>The share action provider retains a ranking for each share target,
1075a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * based on how often the user chooses each one. The more often a user
1085a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * chooses a target, the higher its rank; the
1095a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * most-commonly used target appears in the app bar as the default target.</p>
1105a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1115a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>By default, the target ranking information is stored in a private
1125a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * file with the name specified by {@link
1135a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * #DEFAULT_SHARE_HISTORY_FILE_NAME}. Ordinarily, the share action provider stores
1145a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * all the history in this single file. However, using a single set of
1155a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * rankings may not make sense if the
1165a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * share action provider is used for different kinds of content. For
1175a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * example, if the activity sometimes shares images and sometimes shares
1185a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * contacts, you would want to maintain two different sets of rankings.</p>
1195a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay *
1205a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * <p>To set the history file, call {@link #setShareHistoryFileName
1215a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * setShareHistoryFileName()} and pass the name of an XML file. The file
1225a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * you specify is used until the next time you call {@link
1235a390480c900f2c231a87d2a66ee13e18b082d63Andrew Solovay * #setShareHistoryFileName setShareHistoryFileName()}.</p>
1249dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main *
125e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @see ActionProvider
126e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */
127e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banespublic class ShareActionProvider extends ActionProvider {
128e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
129e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
130e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Listener for the event of selecting a share target.
131e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
132e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public interface OnShareTargetSelectedListener {
133e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
134e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        /**
135e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * Called when a share target has been selected. The client can
136e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * decide whether to perform some action before the sharing is
137e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * actually performed.
138e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * <p>
139e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * <strong>Note:</strong> Modifying the intent is not permitted and
140e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         *     any changes to the latter will be ignored.
141e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * </p>
142e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * <p>
143e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * <strong>Note:</strong> You should <strong>not</strong> handle the
144e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         *     intent here. This callback aims to notify the client that a
145e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         *     sharing is being performed, so the client can update the UI
146e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         *     if necessary.
147e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * </p>
148e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         *
149e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * @param source The source of the notification.
150e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * @param intent The intent for launching the chosen share target.
151e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         * @return The return result is ignored. Always return false for consistency.
152e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes         */
153e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        public boolean onShareTargetSelected(ShareActionProvider source, Intent intent);
154e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
155e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
156e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
157e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * The default for the maximal number of activities shown in the sub-menu.
158e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
159e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private static final int DEFAULT_INITIAL_ACTIVITY_COUNT = 4;
160e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
161e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
162e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * The the maximum number activities shown in the sub-menu.
163e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
164e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private int mMaxShownActivityCount = DEFAULT_INITIAL_ACTIVITY_COUNT;
165e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
166e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
167e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Listener for handling menu item clicks.
168e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
169e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private final ShareMenuItemOnMenuItemClickListener mOnMenuItemClickListener =
170e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            new ShareMenuItemOnMenuItemClickListener();
171e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
172e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
173e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * The default name for storing share history.
174e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
175e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public static final String DEFAULT_SHARE_HISTORY_FILE_NAME = "share_history.xml";
176e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
177e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
178e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Context for accessing resources.
179e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
1802c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas    final Context mContext;
181e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
182e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
183e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * The name of the file with share history data.
184e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
1852c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas    String mShareHistoryFileName = DEFAULT_SHARE_HISTORY_FILE_NAME;
186e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
1872c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas    OnShareTargetSelectedListener mOnShareTargetSelectedListener;
188e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
189e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private OnChooseActivityListener mOnChooseActivityListener;
190e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
191e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
192e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Creates a new instance.
193e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *
194e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * @param context Context for accessing resources.
195e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
196e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public ShareActionProvider(Context context) {
197e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        super(context);
198e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        mContext = context;
199e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
200e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
201e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
202e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Sets a listener to be notified when a share target has been selected.
203e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * The listener can optionally decide to handle the selection and
204e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * not rely on the default behavior which is to launch the activity.
205e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * <p>
206e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * <strong>Note:</strong> If you choose the backing share history file
207e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *     you will still be notified in this callback.
208e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * </p>
209e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * @param listener The listener.
210e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
211e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public void setOnShareTargetSelectedListener(OnShareTargetSelectedListener listener) {
212e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        mOnShareTargetSelectedListener = listener;
213e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        setActivityChooserPolicyIfNeeded();
214e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
215e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
216e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
217e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * {@inheritDoc}
218e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
219e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    @Override
220e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public View onCreateActionView() {
221e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        // Create the view and set its data model.
222e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        ActivityChooserView activityChooserView = new ActivityChooserView(mContext);
223b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        if (!activityChooserView.isInEditMode()) {
224b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
225b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            activityChooserView.setActivityChooserModel(dataModel);
226b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        }
227e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
228e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        // Lookup and set the expand action icon.
229e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        TypedValue outTypedValue = new TypedValue();
230e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, outTypedValue, true);
2314c99f0e29b0926d8e5de44b7e3980d47f052f04cChris Banes        Drawable drawable = AppCompatResources.getDrawable(mContext, outTypedValue.resourceId);
232e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        activityChooserView.setExpandActivityOverflowButtonDrawable(drawable);
233e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        activityChooserView.setProvider(this);
234e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
235e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        // Set content description.
236e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        activityChooserView.setDefaultActionButtonContentDescription(
237e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                R.string.abc_shareactionprovider_share_with_application);
238e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        activityChooserView.setExpandActivityOverflowButtonContentDescription(
239e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                R.string.abc_shareactionprovider_share_with);
240e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
241e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        return activityChooserView;
242e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
243e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
244e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
245e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * {@inheritDoc}
246e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
247e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    @Override
248e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public boolean hasSubMenu() {
249e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        return true;
250e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
251e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
252e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
253e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * {@inheritDoc}
254e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
255e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    @Override
256e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public void onPrepareSubMenu(SubMenu subMenu) {
257e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        // Clear since the order of items may change.
258e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        subMenu.clear();
259e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
260e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
261e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        PackageManager packageManager = mContext.getPackageManager();
262e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
263e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        final int expandedActivityCount = dataModel.getActivityCount();
264e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        final int collapsedActivityCount = Math.min(expandedActivityCount, mMaxShownActivityCount);
265e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
266e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        // Populate the sub-menu with a sub set of the activities.
267e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        for (int i = 0; i < collapsedActivityCount; i++) {
268e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            ResolveInfo activity = dataModel.getActivity(i);
269e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            subMenu.add(0, i, i, activity.loadLabel(packageManager))
270e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                    .setIcon(activity.loadIcon(packageManager))
271e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                    .setOnMenuItemClickListener(mOnMenuItemClickListener);
272e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        }
273e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
274e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        if (collapsedActivityCount < expandedActivityCount) {
275e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            // Add a sub-menu for showing all activities as a list item.
276e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            SubMenu expandedSubMenu = subMenu.addSubMenu(Menu.NONE, collapsedActivityCount,
277e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                    collapsedActivityCount,
278e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                    mContext.getString(R.string.abc_activity_chooser_view_see_all));
279e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            for (int i = 0; i < expandedActivityCount; i++) {
280e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                ResolveInfo activity = dataModel.getActivity(i);
281e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                expandedSubMenu.add(0, i, i, activity.loadLabel(packageManager))
282e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                        .setIcon(activity.loadIcon(packageManager))
283e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                        .setOnMenuItemClickListener(mOnMenuItemClickListener);
284e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            }
285e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        }
286e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
287e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
288e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
289e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Sets the file name of a file for persisting the share history which
290e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * history will be used for ordering share targets. This file will be used
291e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * for all view created by {@link #onCreateActionView()}. Defaults to
292e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * {@link #DEFAULT_SHARE_HISTORY_FILE_NAME}. Set to <code>null</code>
293e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * if share history should not be persisted between sessions.
294e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     *
295e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     * <p class="note">
296e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * <strong>Note:</strong> The history file name can be set any time, however
297e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * only the action views created by {@link #onCreateActionView()} after setting
298e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * the file name will be backed by the provided file. Therefore, if you want to
299e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * use different history files for sharing specific types of content, every time
300e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     * you change the history file with {@link #setShareHistoryFileName(String)} you must
301ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas     * call {@link androidx.appcompat.app.AppCompatActivity#supportInvalidateOptionsMenu()}
302e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     * to recreate the action view. You should <strong>not</strong> call
303ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas     * {@link androidx.appcompat.app.AppCompatActivity#supportInvalidateOptionsMenu()} from
304ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas     * {@link androidx.appcompat.app.AppCompatActivity#onCreateOptionsMenu(Menu)}.
305e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     *
306e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     * <pre>
307e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * private void doShare(Intent intent) {
308e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *     if (IMAGE.equals(intent.getMimeType())) {
309e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *         mShareActionProvider.setHistoryFileName(SHARE_IMAGE_HISTORY_FILE_NAME);
310e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *     } else if (TEXT.equals(intent.getMimeType())) {
311e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *         mShareActionProvider.setHistoryFileName(SHARE_TEXT_HISTORY_FILE_NAME);
312e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *     }
313e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *     mShareActionProvider.setIntent(intent);
314e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     *     supportInvalidateOptionsMenu();
315e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * }
316e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     * </pre>
317e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *
318e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * @param shareHistoryFile The share history file name.
319e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
320e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public void setShareHistoryFileName(String shareHistoryFile) {
321e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        mShareHistoryFileName = shareHistoryFile;
322e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        setActivityChooserPolicyIfNeeded();
323e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
324e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
325e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
326e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Sets an intent with information about the share action. Here is a
327e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * sample for constructing a share intent:
328e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay     *
329e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * <pre>
330e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *  Intent shareIntent = new Intent(Intent.ACTION_SEND);
331e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *  shareIntent.setType("image/*");
332e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *  Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
333e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *  shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
334e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * </pre>
335e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *
336e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * @param shareIntent The share intent.
337e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     *
338e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * @see Intent#ACTION_SEND
339e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * @see Intent#ACTION_SEND_MULTIPLE
340e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
341e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    public void setShareIntent(Intent shareIntent) {
342b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        if (shareIntent != null) {
343b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            final String action = shareIntent.getAction();
344b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) {
345b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                updateIntent(shareIntent);
346b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            }
347b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        }
348e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        ActivityChooserModel dataModel = ActivityChooserModel.get(mContext,
349e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                mShareHistoryFileName);
350e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        dataModel.setIntent(shareIntent);
351e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
352e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
353e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
354e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Reusable listener for handling share item clicks.
355e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
356e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private class ShareMenuItemOnMenuItemClickListener implements OnMenuItemClickListener {
3572c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas        ShareMenuItemOnMenuItemClickListener() {
3582c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas        }
3592c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas
360e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        @Override
361e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        public boolean onMenuItemClick(MenuItem item) {
362e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            ActivityChooserModel dataModel = ActivityChooserModel.get(mContext,
363e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                    mShareHistoryFileName);
364e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            final int itemId = item.getItemId();
365e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            Intent launchIntent = dataModel.chooseActivity(itemId);
366e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            if (launchIntent != null) {
367b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                final String action = launchIntent.getAction();
368b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                if (Intent.ACTION_SEND.equals(action) ||
369b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                        Intent.ACTION_SEND_MULTIPLE.equals(action)) {
370b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                    updateIntent(launchIntent);
371b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                }
372e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                mContext.startActivity(launchIntent);
373e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            }
374e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            return true;
375e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        }
376e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
377e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
378e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
379e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Set the activity chooser policy of the model backed by the current
380e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * share history file if needed which is if there is a registered callback.
381e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
382e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private void setActivityChooserPolicyIfNeeded() {
383e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        if (mOnShareTargetSelectedListener == null) {
384e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            return;
385e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        }
386e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        if (mOnChooseActivityListener == null) {
387e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            mOnChooseActivityListener = new ShareActivityChooserModelPolicy();
388e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        }
389e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
390e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        dataModel.setOnChooseActivityListener(mOnChooseActivityListener);
391e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
392e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes
393e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    /**
394e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     * Policy that delegates to the {@link OnShareTargetSelectedListener}, if such.
395e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes     */
396e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    private class ShareActivityChooserModelPolicy implements OnChooseActivityListener {
3972c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas        ShareActivityChooserModelPolicy() {
3982c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas        }
3992c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas
400e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        @Override
401e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        public boolean onChooseActivity(ActivityChooserModel host, Intent intent) {
402e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            if (mOnShareTargetSelectedListener != null) {
403e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                mOnShareTargetSelectedListener.onShareTargetSelected(
404e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes                        ShareActionProvider.this, intent);
405e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            }
406e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes            return false;
407e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes        }
408e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes    }
409b645de790756e27bbe92d133216b7ac79cca7679Chris Banes
4102c1bad7ecd5879bf0f29ce2ce1bc5bd67a3f4682Aurimas Liutikas    void updateIntent(Intent intent) {
411b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        if (Build.VERSION.SDK_INT >= 21) {
412b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            // If we're on Lollipop, we can open the intent as a document
413b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
414b645de790756e27bbe92d133216b7ac79cca7679Chris Banes                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
415b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        } else {
416b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            // Else, we will use the old CLEAR_WHEN_TASK_RESET flag
417b645de790756e27bbe92d133216b7ac79cca7679Chris Banes            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
418b645de790756e27bbe92d133216b7ac79cca7679Chris Banes        }
419b645de790756e27bbe92d133216b7ac79cca7679Chris Banes    }
420e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay}
421