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