ShareActionProvider.java revision e21c8fc54201f7ba818dc089c0bdb4e8e4ee289f
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/** 39e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * This is a provider for a share action. It is responsible for creating views 40e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * that enable data sharing and also to show a sub menu with sharing activities 41e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * if the hosting item is placed on the overflow menu. 429dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * 439dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <p class="note"><strong>Note:</strong> This class is included in the <a 449dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * href="{@docRoot}tools/extras/support-library.html">support library</a> for compatibility 459dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * with API level 7 and higher. If you're developing your app for API level 14 and higher 469dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <em>only</em>, you should instead use the framework {@link android.widget.ShareActionProvider} 479dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * class.</p> 489dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * 49e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <p> 50e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Here is how to use the action provider with custom backing file in a {@link MenuItem}: 51e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * </p> 529dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <pre><code> 539dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * // In {@link android.app.Activity#onCreateOptionsMenu Activity.onCreateOptionsMenu()} 54e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * public boolean onCreateOptionsMenu(Menu menu) { 55e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // Get the menu item. 56e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * MenuItem menuItem = menu.findItem(R.id.my_menu_item); 57e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // Get the provider and hold onto it to set/change the share intent. 58e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 59e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // Set history different from the default before getting the action 60e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // view since a call to {@link android.support.v4.view.MenuItemCompat#getActionView(android.view.MenuItem) MenuItemCompat.getActionView()} calls 61e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // {@link ActionProvider#onCreateActionView()} which uses the backing file name. Omit this 62e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // line if using the default share history file is desired. 63e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * mShareActionProvider.setShareHistoryFileName("custom_share_history.xml"); 64e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * . . . 65e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * } 66e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 67e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // Somewhere in the application. 68e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * public void doShare(Intent shareIntent) { 69e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * // When you want to share set the share intent. 70e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * mShareActionProvider.setShareIntent(shareIntent); 71e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * } 729dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * </code></pre> 73e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <p> 74e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <strong>Note:</strong> While the sample snippet demonstrates how to use this provider 75e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * in the context of a menu item, the use of the provider is not limited to menu items. 76e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * </p> 77e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 789dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <div class="special reference"> 799dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <h3>Developer Guides</h3> 809dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * 819dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <p>For information about how to use {@link ShareActionProvider}, see the 829dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <a href="{@docRoot}guide/topics/ui/actionbar.html#ActionProvider">Action Bar</a> API guide.</p> 839dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * </div> 849dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * 85e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @see ActionProvider 86e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 87e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banespublic class ShareActionProvider extends ActionProvider { 88e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 89e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 90e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Listener for the event of selecting a share target. 91e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 92e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public interface OnShareTargetSelectedListener { 93e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 94e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 95e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Called when a share target has been selected. The client can 96e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * decide whether to perform some action before the sharing is 97e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * actually performed. 98e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <p> 99e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <strong>Note:</strong> Modifying the intent is not permitted and 100e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * any changes to the latter will be ignored. 101e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * </p> 102e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <p> 103e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <strong>Note:</strong> You should <strong>not</strong> handle the 104e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * intent here. This callback aims to notify the client that a 105e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * sharing is being performed, so the client can update the UI 106e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * if necessary. 107e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * </p> 108e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 109e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @param source The source of the notification. 110e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @param intent The intent for launching the chosen share target. 111e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @return The return result is ignored. Always return false for consistency. 112e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 113e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public boolean onShareTargetSelected(ShareActionProvider source, Intent intent); 114e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 115e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 116e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 117e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * The default for the maximal number of activities shown in the sub-menu. 118e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 119e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private static final int DEFAULT_INITIAL_ACTIVITY_COUNT = 4; 120e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 121e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 122e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * The the maximum number activities shown in the sub-menu. 123e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 124e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private int mMaxShownActivityCount = DEFAULT_INITIAL_ACTIVITY_COUNT; 125e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 126e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 127e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Listener for handling menu item clicks. 128e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 129e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private final ShareMenuItemOnMenuItemClickListener mOnMenuItemClickListener = 130e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes new ShareMenuItemOnMenuItemClickListener(); 131e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 132e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 133e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * The default name for storing share history. 134e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 135e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public static final String DEFAULT_SHARE_HISTORY_FILE_NAME = "share_history.xml"; 136e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 137e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 138e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Context for accessing resources. 139e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 140e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private final Context mContext; 141e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 142e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 143e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * The name of the file with share history data. 144e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 145e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private String mShareHistoryFileName = DEFAULT_SHARE_HISTORY_FILE_NAME; 146e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 147e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private OnShareTargetSelectedListener mOnShareTargetSelectedListener; 148e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 149e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private OnChooseActivityListener mOnChooseActivityListener; 150e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 151e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 152e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Creates a new instance. 153e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 154e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @param context Context for accessing resources. 155e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 156e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public ShareActionProvider(Context context) { 157e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes super(context); 158e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mContext = context; 159e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 160e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 161e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 162e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Sets a listener to be notified when a share target has been selected. 163e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * The listener can optionally decide to handle the selection and 164e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * not rely on the default behavior which is to launch the activity. 165e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <p> 166e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <strong>Note:</strong> If you choose the backing share history file 167e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * you will still be notified in this callback. 168e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * </p> 169e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @param listener The listener. 170e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 171e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public void setOnShareTargetSelectedListener(OnShareTargetSelectedListener listener) { 172e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mOnShareTargetSelectedListener = listener; 173e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes setActivityChooserPolicyIfNeeded(); 174e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 175e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 176e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 177e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * {@inheritDoc} 178e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 179e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes @Override 180e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public View onCreateActionView() { 181e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes // Create the view and set its data model. 182e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ActivityChooserView activityChooserView = new ActivityChooserView(mContext); 183b645de790756e27bbe92d133216b7ac79cca7679Chris Banes if (!activityChooserView.isInEditMode()) { 184b645de790756e27bbe92d133216b7ac79cca7679Chris Banes ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName); 185b645de790756e27bbe92d133216b7ac79cca7679Chris Banes activityChooserView.setActivityChooserModel(dataModel); 186b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } 187e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 188e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes // Lookup and set the expand action icon. 189e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes TypedValue outTypedValue = new TypedValue(); 190e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, outTypedValue, true); 191469286122bcbbecbdd0bef74fb50f9d8920e77b9Chris Banes Drawable drawable = TintManager.getDrawable(mContext, outTypedValue.resourceId); 192e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes activityChooserView.setExpandActivityOverflowButtonDrawable(drawable); 193e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes activityChooserView.setProvider(this); 194e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 195e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes // Set content description. 196e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes activityChooserView.setDefaultActionButtonContentDescription( 197e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes R.string.abc_shareactionprovider_share_with_application); 198e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes activityChooserView.setExpandActivityOverflowButtonContentDescription( 199e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes R.string.abc_shareactionprovider_share_with); 200e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 201e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes return activityChooserView; 202e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 203e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 204e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 205e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * {@inheritDoc} 206e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 207e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes @Override 208e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public boolean hasSubMenu() { 209e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes return true; 210e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 211e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 212e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 213e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * {@inheritDoc} 214e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 215e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes @Override 216e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public void onPrepareSubMenu(SubMenu subMenu) { 217e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes // Clear since the order of items may change. 218e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes subMenu.clear(); 219e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 220e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName); 221e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes PackageManager packageManager = mContext.getPackageManager(); 222e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 223e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes final int expandedActivityCount = dataModel.getActivityCount(); 224e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes final int collapsedActivityCount = Math.min(expandedActivityCount, mMaxShownActivityCount); 225e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 226e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes // Populate the sub-menu with a sub set of the activities. 227e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes for (int i = 0; i < collapsedActivityCount; i++) { 228e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ResolveInfo activity = dataModel.getActivity(i); 229e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes subMenu.add(0, i, i, activity.loadLabel(packageManager)) 230e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes .setIcon(activity.loadIcon(packageManager)) 231e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes .setOnMenuItemClickListener(mOnMenuItemClickListener); 232e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 233e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 234e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes if (collapsedActivityCount < expandedActivityCount) { 235e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes // Add a sub-menu for showing all activities as a list item. 236e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes SubMenu expandedSubMenu = subMenu.addSubMenu(Menu.NONE, collapsedActivityCount, 237e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes collapsedActivityCount, 238e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mContext.getString(R.string.abc_activity_chooser_view_see_all)); 239e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes for (int i = 0; i < expandedActivityCount; i++) { 240e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ResolveInfo activity = dataModel.getActivity(i); 241e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes expandedSubMenu.add(0, i, i, activity.loadLabel(packageManager)) 242e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes .setIcon(activity.loadIcon(packageManager)) 243e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes .setOnMenuItemClickListener(mOnMenuItemClickListener); 244e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 245e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 246e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 247e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 248e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 249e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Sets the file name of a file for persisting the share history which 250e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * history will be used for ordering share targets. This file will be used 251e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * for all view created by {@link #onCreateActionView()}. Defaults to 252e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * {@link #DEFAULT_SHARE_HISTORY_FILE_NAME}. Set to <code>null</code> 253e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * if share history should not be persisted between sessions. 254e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * 255e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * <p class="note"> 256e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <strong>Note:</strong> The history file name can be set any time, however 257e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * only the action views created by {@link #onCreateActionView()} after setting 258e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * the file name will be backed by the provided file. Therefore, if you want to 259e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * use different history files for sharing specific types of content, every time 260e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * you change the history file with {@link #setShareHistoryFileName(String)} you must 261e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * call {@link android.support.v7.app.AppCompatActivity#supportInvalidateOptionsMenu()} 262e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * to recreate the action view. You should <strong>not</strong> call 263e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * {@link android.support.v7.app.AppCompatActivity#supportInvalidateOptionsMenu()} from 264e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * {@link android.support.v7.app.AppCompatActivity#onCreateOptionsMenu(Menu)}. 265e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * 266e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * <pre> 267e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * private void doShare(Intent intent) { 268e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * if (IMAGE.equals(intent.getMimeType())) { 269e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * mShareActionProvider.setHistoryFileName(SHARE_IMAGE_HISTORY_FILE_NAME); 270e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * } else if (TEXT.equals(intent.getMimeType())) { 271e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * mShareActionProvider.setHistoryFileName(SHARE_TEXT_HISTORY_FILE_NAME); 272e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * } 273e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * mShareActionProvider.setIntent(intent); 274e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * supportInvalidateOptionsMenu(); 275e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * } 276e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * </pre> 277e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 278e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @param shareHistoryFile The share history file name. 279e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 280e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public void setShareHistoryFileName(String shareHistoryFile) { 281e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mShareHistoryFileName = shareHistoryFile; 282e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes setActivityChooserPolicyIfNeeded(); 283e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 284e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 285e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 286e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Sets an intent with information about the share action. Here is a 287e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * sample for constructing a share intent: 288e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay * 289e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * <pre> 290e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Intent shareIntent = new Intent(Intent.ACTION_SEND); 291e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * shareIntent.setType("image/*"); 292e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg")); 293e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString()); 294e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * </pre> 295e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 296e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @param shareIntent The share intent. 297e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * 298e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @see Intent#ACTION_SEND 299e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * @see Intent#ACTION_SEND_MULTIPLE 300e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 301e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public void setShareIntent(Intent shareIntent) { 302b645de790756e27bbe92d133216b7ac79cca7679Chris Banes if (shareIntent != null) { 303b645de790756e27bbe92d133216b7ac79cca7679Chris Banes final String action = shareIntent.getAction(); 304b645de790756e27bbe92d133216b7ac79cca7679Chris Banes if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) { 305b645de790756e27bbe92d133216b7ac79cca7679Chris Banes updateIntent(shareIntent); 306b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } 307b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } 308e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, 309e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mShareHistoryFileName); 310e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes dataModel.setIntent(shareIntent); 311e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 312e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 313e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 314e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Reusable listener for handling share item clicks. 315e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 316e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private class ShareMenuItemOnMenuItemClickListener implements OnMenuItemClickListener { 317e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes @Override 318e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public boolean onMenuItemClick(MenuItem item) { 319e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, 320e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mShareHistoryFileName); 321e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes final int itemId = item.getItemId(); 322e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes Intent launchIntent = dataModel.chooseActivity(itemId); 323e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes if (launchIntent != null) { 324b645de790756e27bbe92d133216b7ac79cca7679Chris Banes final String action = launchIntent.getAction(); 325b645de790756e27bbe92d133216b7ac79cca7679Chris Banes if (Intent.ACTION_SEND.equals(action) || 326b645de790756e27bbe92d133216b7ac79cca7679Chris Banes Intent.ACTION_SEND_MULTIPLE.equals(action)) { 327b645de790756e27bbe92d133216b7ac79cca7679Chris Banes updateIntent(launchIntent); 328b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } 329e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mContext.startActivity(launchIntent); 330e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 331e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes return true; 332e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 333e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 334e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 335e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 336e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Set the activity chooser policy of the model backed by the current 337e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * share history file if needed which is if there is a registered callback. 338e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 339e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private void setActivityChooserPolicyIfNeeded() { 340e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes if (mOnShareTargetSelectedListener == null) { 341e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes return; 342e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 343e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes if (mOnChooseActivityListener == null) { 344e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mOnChooseActivityListener = new ShareActivityChooserModelPolicy(); 345e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 346e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName); 347e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes dataModel.setOnChooseActivityListener(mOnChooseActivityListener); 348e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 349e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes 350e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes /** 351e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes * Policy that delegates to the {@link OnShareTargetSelectedListener}, if such. 352e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes */ 353e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes private class ShareActivityChooserModelPolicy implements OnChooseActivityListener { 354e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes @Override 355e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes public boolean onChooseActivity(ActivityChooserModel host, Intent intent) { 356e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes if (mOnShareTargetSelectedListener != null) { 357e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes mOnShareTargetSelectedListener.onShareTargetSelected( 358e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes ShareActionProvider.this, intent); 359e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 360e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes return false; 361e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 362e290ed32f85ff6307a53922a78684b31d30b8dc5Chris Banes } 363b645de790756e27bbe92d133216b7ac79cca7679Chris Banes 364b645de790756e27bbe92d133216b7ac79cca7679Chris Banes private void updateIntent(Intent intent) { 365b645de790756e27bbe92d133216b7ac79cca7679Chris Banes if (Build.VERSION.SDK_INT >= 21) { 366b645de790756e27bbe92d133216b7ac79cca7679Chris Banes // If we're on Lollipop, we can open the intent as a document 367b645de790756e27bbe92d133216b7ac79cca7679Chris Banes intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | 368b645de790756e27bbe92d133216b7ac79cca7679Chris Banes Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 369b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } else { 370b645de790756e27bbe92d133216b7ac79cca7679Chris Banes // Else, we will use the old CLEAR_WHEN_TASK_RESET flag 371b645de790756e27bbe92d133216b7ac79cca7679Chris Banes intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 372b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } 373b645de790756e27bbe92d133216b7ac79cca7679Chris Banes } 374e21c8fc54201f7ba818dc089c0bdb4e8e4ee289fAndrew Solovay} 375