ShareActionProvider.java revision 8c6c79f0909ceabeb8abe1013648c31c7582b7ad
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.pm.PackageManager; 22import android.content.pm.ResolveInfo; 23import android.graphics.drawable.Drawable; 24import android.util.TypedValue; 25import android.view.ActionProvider; 26import android.view.Menu; 27import android.view.MenuItem; 28import android.view.MenuItem.OnMenuItemClickListener; 29import android.view.SubMenu; 30import android.view.View; 31import android.widget.ActivityChooserModel.OnChooseActivityListener; 32 33import com.android.internal.R; 34 35/** 36 * This is a provider for a share action. It is responsible for creating views 37 * that enable data sharing and also to show a sub menu with sharing activities 38 * if the hosting item is placed on the overflow menu. 39 * <p> 40 * Here is how to use the action provider with custom backing file in a {@link MenuItem}: 41 * </p> 42 * <p> 43 * <pre> 44 * <code> 45 * // In Activity#onCreateOptionsMenu 46 * public boolean onCreateOptionsMenu(Menu menu) { 47 * // Get the menu item. 48 * MenuItem menuItem = menu.findItem(R.id.my_menu_item); 49 * // Get the provider and hold onto it to set/change the share intent. 50 * mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider(); 51 * // Set history different from the default before getting the action 52 * // view since a call to {@link MenuItem#getActionView() MenuItem.getActionView()} calls 53 * // {@link ActionProvider#onCreateActionView()} which uses the backing file name. Omit this 54 * // line if using the default share history file is desired. 55 * mShareActionProvider.setShareHistoryFileName("custom_share_history.xml"); 56 * . . . 57 * } 58 * 59 * // Somewhere in the application. 60 * public void doShare(Intent shareIntent) { 61 * // When you want to share set the share intent. 62 * mShareActionProvider.setShareIntent(shareIntent); 63 * } 64 * </pre> 65 * </code> 66 * </p> 67 * <p> 68 * <strong>Note:</strong> While the sample snippet demonstrates how to use this provider 69 * in the context of a menu item, the use of the provider is not limited to menu items. 70 * </p> 71 * 72 * @see ActionProvider 73 */ 74public class ShareActionProvider extends ActionProvider { 75 76 /** 77 * Listener for the event of selecting a share target. 78 */ 79 public interface OnShareTargetSelectedListener { 80 81 /** 82 * Called when a share target has been selected. The client can 83 * decide whether to handle the intent or rely on the default 84 * behavior which is launching it. 85 * <p> 86 * <strong>Note:</strong> Modifying the intent is not permitted and 87 * any changes to the latter will be ignored. 88 * </p> 89 * 90 * @param source The source of the notification. 91 * @param intent The intent for launching the chosen share target. 92 * @return Whether the client has handled the intent. 93 */ 94 public boolean onShareTargetSelected(ShareActionProvider source, Intent intent); 95 } 96 97 /** 98 * The default for the maximal number of activities shown in the sub-menu. 99 */ 100 private static final int DEFAULT_INITIAL_ACTIVITY_COUNT = 4; 101 102 /** 103 * The the maximum number activities shown in the sub-menu. 104 */ 105 private int mMaxShownActivityCount = DEFAULT_INITIAL_ACTIVITY_COUNT; 106 107 /** 108 * Listener for handling menu item clicks. 109 */ 110 private final ShareMenuItemOnMenuItemClickListener mOnMenuItemClickListener = 111 new ShareMenuItemOnMenuItemClickListener(); 112 113 /** 114 * The default name for storing share history. 115 */ 116 public static final String DEFAULT_SHARE_HISTORY_FILE_NAME = "share_history.xml"; 117 118 /** 119 * Context for accessing resources. 120 */ 121 private final Context mContext; 122 123 /** 124 * The name of the file with share history data. 125 */ 126 private String mShareHistoryFileName = DEFAULT_SHARE_HISTORY_FILE_NAME; 127 128 private OnShareTargetSelectedListener mOnShareTargetSelectedListener; 129 130 private OnChooseActivityListener mOnChooseActivityListener; 131 132 /** 133 * Creates a new instance. 134 * 135 * @param context Context for accessing resources. 136 */ 137 public ShareActionProvider(Context context) { 138 super(context); 139 mContext = context; 140 } 141 142 /** 143 * Sets a listener to be notified when a share target has been selected. 144 * The listener can optionally decide to handle the selection and 145 * not rely on the default behavior which is to launch the activity. 146 * <p> 147 * <strong>Note:</strong> If you choose the backing share history file 148 * you will still be notified in this callback. 149 * </p> 150 * @param listener The listener. 151 */ 152 public void setOnShareTargetSelectedListener(OnShareTargetSelectedListener listener) { 153 mOnShareTargetSelectedListener = listener; 154 setActivityChooserPolicyIfNeeded(); 155 } 156 157 /** 158 * {@inheritDoc} 159 */ 160 @Override 161 public View onCreateActionView() { 162 // Create the view and set its data model. 163 ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName); 164 ActivityChooserView activityChooserView = new ActivityChooserView(mContext); 165 activityChooserView.setActivityChooserModel(dataModel); 166 167 // Lookup and set the expand action icon. 168 TypedValue outTypedValue = new TypedValue(); 169 mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, outTypedValue, true); 170 Drawable drawable = mContext.getResources().getDrawable(outTypedValue.resourceId); 171 activityChooserView.setExpandActivityOverflowButtonDrawable(drawable); 172 173 return activityChooserView; 174 } 175 176 /** 177 * {@inheritDoc} 178 */ 179 @Override 180 public boolean hasSubMenu() { 181 return true; 182 } 183 184 /** 185 * {@inheritDoc} 186 */ 187 @Override 188 public void onPrepareSubMenu(SubMenu subMenu) { 189 // Clear since the order of items may change. 190 subMenu.clear(); 191 192 ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName); 193 PackageManager packageManager = mContext.getPackageManager(); 194 195 final int expandedActivityCount = dataModel.getActivityCount(); 196 final int collapsedActivityCount = Math.min(expandedActivityCount, mMaxShownActivityCount); 197 198 // Populate the sub-menu with a sub set of the activities. 199 for (int i = 0; i < collapsedActivityCount; i++) { 200 ResolveInfo activity = dataModel.getActivity(i); 201 subMenu.add(0, i, i, activity.loadLabel(packageManager)) 202 .setIcon(activity.loadIcon(packageManager)) 203 .setOnMenuItemClickListener(mOnMenuItemClickListener); 204 } 205 206 if (collapsedActivityCount < expandedActivityCount) { 207 // Add a sub-menu for showing all activities as a list item. 208 SubMenu expandedSubMenu = subMenu.addSubMenu(Menu.NONE, collapsedActivityCount, 209 collapsedActivityCount, 210 mContext.getString(R.string.activity_chooser_view_see_all)); 211 for (int i = 0; i < expandedActivityCount; i++) { 212 ResolveInfo activity = dataModel.getActivity(i); 213 expandedSubMenu.add(0, i, i, activity.loadLabel(packageManager)) 214 .setIcon(activity.loadIcon(packageManager)) 215 .setOnMenuItemClickListener(mOnMenuItemClickListener); 216 } 217 } 218 } 219 220 /** 221 * Sets the file name of a file for persisting the share history which 222 * history will be used for ordering share targets. This file will be used 223 * for all view created by {@link #onCreateActionView()}. Defaults to 224 * {@link #DEFAULT_SHARE_HISTORY_FILE_NAME}. Set to <code>null</code> 225 * if share history should not be persisted between sessions. 226 * <p> 227 * <strong>Note:</strong> The history file name can be set any time, however 228 * only the action views created by {@link #onCreateActionView()} after setting 229 * the file name will be backed by the provided file. 230 * <p> 231 * 232 * @param shareHistoryFile The share history file name. 233 */ 234 public void setShareHistoryFileName(String shareHistoryFile) { 235 mShareHistoryFileName = shareHistoryFile; 236 setActivityChooserPolicyIfNeeded(); 237 } 238 239 /** 240 * Sets an intent with information about the share action. Here is a 241 * sample for constructing a share intent: 242 * <p> 243 * <pre> 244 * <code> 245 * Intent shareIntent = new Intent(Intent.ACTION_SEND); 246 * shareIntent.setType("image/*"); 247 * Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg")); 248 * shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString()); 249 * </pre> 250 * </code> 251 * </p> 252 * 253 * @param shareIntent The share intent. 254 * 255 * @see Intent#ACTION_SEND 256 * @see Intent#ACTION_SEND_MULTIPLE 257 */ 258 public void setShareIntent(Intent shareIntent) { 259 ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, 260 mShareHistoryFileName); 261 dataModel.setIntent(shareIntent); 262 } 263 264 /** 265 * Reusable listener for handling share item clicks. 266 */ 267 private class ShareMenuItemOnMenuItemClickListener implements OnMenuItemClickListener { 268 @Override 269 public boolean onMenuItemClick(MenuItem item) { 270 ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, 271 mShareHistoryFileName); 272 final int itemId = item.getItemId(); 273 Intent launchIntent = dataModel.chooseActivity(itemId); 274 if (launchIntent != null) { 275 mContext.startActivity(launchIntent); 276 } 277 return true; 278 } 279 } 280 281 /** 282 * Set the activity chooser policy of the model backed by the current 283 * share history file if needed which is if there is a registered callback. 284 */ 285 private void setActivityChooserPolicyIfNeeded() { 286 if (mOnShareTargetSelectedListener == null) { 287 return; 288 } 289 if (mOnChooseActivityListener == null) { 290 mOnChooseActivityListener = new ShareAcitivityChooserModelPolicy(); 291 } 292 ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName); 293 dataModel.setOnChooseActivityListener(mOnChooseActivityListener); 294 } 295 296 /** 297 * Policy that delegates to the {@link OnShareTargetSelectedListener}, if such. 298 */ 299 private class ShareAcitivityChooserModelPolicy implements OnChooseActivityListener { 300 @Override 301 public boolean onChooseActivity(ActivityChooserModel host, Intent intent) { 302 if (mOnShareTargetSelectedListener != null) { 303 return mOnShareTargetSelectedListener.onShareTargetSelected( 304 ShareActionProvider.this, intent); 305 } 306 return false; 307 } 308 } 309} 310