ShareActionProvider.java revision 823f074a73cfc23c40a7b576c71daa096ee9ed6a
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        activityChooserView.setProvider(this);
173
174        return activityChooserView;
175    }
176
177    /**
178     * {@inheritDoc}
179     */
180    @Override
181    public boolean hasSubMenu() {
182        return true;
183    }
184
185    /**
186     * {@inheritDoc}
187     */
188    @Override
189    public void onPrepareSubMenu(SubMenu subMenu) {
190        // Clear since the order of items may change.
191        subMenu.clear();
192
193        ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
194        PackageManager packageManager = mContext.getPackageManager();
195
196        final int expandedActivityCount = dataModel.getActivityCount();
197        final int collapsedActivityCount = Math.min(expandedActivityCount, mMaxShownActivityCount);
198
199        // Populate the sub-menu with a sub set of the activities.
200        for (int i = 0; i < collapsedActivityCount; i++) {
201            ResolveInfo activity = dataModel.getActivity(i);
202            subMenu.add(0, i, i, activity.loadLabel(packageManager))
203                .setIcon(activity.loadIcon(packageManager))
204                .setOnMenuItemClickListener(mOnMenuItemClickListener);
205        }
206
207        if (collapsedActivityCount < expandedActivityCount) {
208            // Add a sub-menu for showing all activities as a list item.
209            SubMenu expandedSubMenu = subMenu.addSubMenu(Menu.NONE, collapsedActivityCount,
210                    collapsedActivityCount,
211                    mContext.getString(R.string.activity_chooser_view_see_all));
212            for (int i = 0; i < expandedActivityCount; i++) {
213                ResolveInfo activity = dataModel.getActivity(i);
214                expandedSubMenu.add(0, i, i, activity.loadLabel(packageManager))
215                    .setIcon(activity.loadIcon(packageManager))
216                    .setOnMenuItemClickListener(mOnMenuItemClickListener);
217            }
218        }
219    }
220
221    /**
222     * Sets the file name of a file for persisting the share history which
223     * history will be used for ordering share targets. This file will be used
224     * for all view created by {@link #onCreateActionView()}. Defaults to
225     * {@link #DEFAULT_SHARE_HISTORY_FILE_NAME}. Set to <code>null</code>
226     * if share history should not be persisted between sessions.
227     * <p>
228     * <strong>Note:</strong> The history file name can be set any time, however
229     * only the action views created by {@link #onCreateActionView()} after setting
230     * the file name will be backed by the provided file.
231     * <p>
232     *
233     * @param shareHistoryFile The share history file name.
234     */
235    public void setShareHistoryFileName(String shareHistoryFile) {
236        mShareHistoryFileName = shareHistoryFile;
237        setActivityChooserPolicyIfNeeded();
238    }
239
240    /**
241     * Sets an intent with information about the share action. Here is a
242     * sample for constructing a share intent:
243     * <p>
244     * <pre>
245     * <code>
246     *  Intent shareIntent = new Intent(Intent.ACTION_SEND);
247     *  shareIntent.setType("image/*");
248     *  Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
249     *  shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
250     * </pre>
251     * </code>
252     * </p>
253     *
254     * @param shareIntent The share intent.
255     *
256     * @see Intent#ACTION_SEND
257     * @see Intent#ACTION_SEND_MULTIPLE
258     */
259    public void setShareIntent(Intent shareIntent) {
260        ActivityChooserModel dataModel = ActivityChooserModel.get(mContext,
261            mShareHistoryFileName);
262        dataModel.setIntent(shareIntent);
263    }
264
265    /**
266     * Reusable listener for handling share item clicks.
267     */
268    private class ShareMenuItemOnMenuItemClickListener implements OnMenuItemClickListener {
269        @Override
270        public boolean onMenuItemClick(MenuItem item) {
271            ActivityChooserModel dataModel = ActivityChooserModel.get(mContext,
272                    mShareHistoryFileName);
273            final int itemId = item.getItemId();
274            Intent launchIntent = dataModel.chooseActivity(itemId);
275            if (launchIntent != null) {
276                mContext.startActivity(launchIntent);
277            }
278            return true;
279        }
280    }
281
282    /**
283     * Set the activity chooser policy of the model backed by the current
284     * share history file if needed which is if there is a registered callback.
285     */
286    private void setActivityChooserPolicyIfNeeded() {
287        if (mOnShareTargetSelectedListener == null) {
288            return;
289        }
290        if (mOnChooseActivityListener == null) {
291            mOnChooseActivityListener = new ShareAcitivityChooserModelPolicy();
292        }
293        ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mShareHistoryFileName);
294        dataModel.setOnChooseActivityListener(mOnChooseActivityListener);
295    }
296
297    /**
298     * Policy that delegates to the {@link OnShareTargetSelectedListener}, if such.
299     */
300    private class ShareAcitivityChooserModelPolicy implements OnChooseActivityListener {
301        @Override
302        public boolean onChooseActivity(ActivityChooserModel host, Intent intent) {
303            if (mOnShareTargetSelectedListener != null) {
304                return mOnShareTargetSelectedListener.onShareTargetSelected(
305                        ShareActionProvider.this, intent);
306            }
307            return false;
308        }
309    }
310}
311