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