1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.widget;
import android.app.Activity;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.util.Base64;
import android.view.Menu;
import org.mozilla.gecko.GeckoApp;
import org.mozilla.gecko.R;
import org.mozilla.gecko.SnackbarBuilder;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.overlays.ui.ShareDialog;
import org.mozilla.gecko.menu.MenuItemSwitcherLayout;
import org.mozilla.gecko.util.IOUtils;
import org.mozilla.gecko.util.IntentUtils;
import org.mozilla.gecko.util.ThreadUtils;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
public class GeckoActionProvider {
private static final int MAX_HISTORY_SIZE_DEFAULT = 2;
/**
* A listener to know when a target was selected.
* When setting a provider, the activity can listen to this,
* to close the menu.
*/
public interface OnTargetSelectedListener {
public void onTargetSelected();
}
final Context mContext;
public final static String DEFAULT_MIME_TYPE = "text/plain";
public static final String DEFAULT_HISTORY_FILE_NAME = "history.xml";
// History file.
String mHistoryFileName = DEFAULT_HISTORY_FILE_NAME;
OnTargetSelectedListener mOnTargetListener;
private final Callbacks mCallbacks = new Callbacks();
private static final HashMap<String, GeckoActionProvider> mProviders = new HashMap<String, GeckoActionProvider>();
private static String getFilenameFromMimeType(String mimeType) {
String[] mime = mimeType.split("/");
// All text mimetypes use the default provider
if ("text".equals(mime[0])) {
return DEFAULT_HISTORY_FILE_NAME;
}
return "history-" + mime[0] + ".xml";
}
// Gets the action provider for a particular mimetype
public static GeckoActionProvider getForType(String mimeType, Context context) {
if (!mProviders.keySet().contains(mimeType)) {
GeckoActionProvider provider = new GeckoActionProvider(context);
// For empty types, we just return a default provider
if (TextUtils.isEmpty(mimeType)) {
return provider;
}
provider.setHistoryFileName(getFilenameFromMimeType(mimeType));
mProviders.put(mimeType, provider);
}
return mProviders.get(mimeType);
}
public GeckoActionProvider(Context context) {
mContext = context;
}
/**
* Creates the action view using the default history size.
*/
public View onCreateActionView(final ActionViewType viewType) {
return onCreateActionView(MAX_HISTORY_SIZE_DEFAULT, viewType);
}
public View onCreateActionView(final int maxHistorySize, final ActionViewType viewType) {
// Create the view and set its data model.
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
final MenuItemSwitcherLayout view;
switch (viewType) {
case DEFAULT:
view = new MenuItemSwitcherLayout(mContext, null);
break;
case CONTEXT_MENU:
view = new MenuItemSwitcherLayout(mContext, null);
view.initContextMenuStyles();
break;
default:
throw new IllegalArgumentException(
"Unknown " + ActionViewType.class.getSimpleName() + ": " + viewType);
}
view.addActionButtonClickListener(mCallbacks);
final PackageManager packageManager = mContext.getPackageManager();
int historySize = dataModel.getDistinctActivityCountInHistory();
if (historySize > maxHistorySize) {
historySize = maxHistorySize;
}
// Historical data is dependent on past selection of activities.
// Activity count is determined by the number of activities that can handle
// the particular intent. When no intent is set, the activity count is 0,
// while the history count can be a valid number.
if (historySize > dataModel.getActivityCount()) {
return view;
}
for (int i = 0; i < historySize; i++) {
view.addActionButton(dataModel.getActivity(i).loadIcon(packageManager),
dataModel.getActivity(i).loadLabel(packageManager));
}
return view;
}
public boolean hasSubMenu() {
return true;
}
public void onPrepareSubMenu(SubMenu subMenu) {
// Clear since the order of items may change.
subMenu.clear();
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
PackageManager packageManager = mContext.getPackageManager();
// Populate the sub-menu with a sub set of the activities.
final String shareDialogClassName = ShareDialog.class.getCanonicalName();
final String sendTabLabel = mContext.getResources().getString(R.string.overlay_share_send_other);
final int count = dataModel.getActivityCount();
for (int i = 0; i < count; i++) {
ResolveInfo activity = dataModel.getActivity(i);
final CharSequence activityLabel = activity.loadLabel(packageManager);
// Pin internal actions to the top. Note:
// the order here does not affect quick share.
final int order;
if (shareDialogClassName.equals(activity.activityInfo.name) &&
sendTabLabel.equals(activityLabel)) {
order = Menu.FIRST + i;
} else {
order = Menu.FIRST + (i | Menu.CATEGORY_SECONDARY);
}
subMenu.add(0, i, order, activityLabel)
.setIcon(activity.loadIcon(packageManager))
.setOnMenuItemClickListener(mCallbacks);
}
}
public void setHistoryFileName(String historyFile) {
mHistoryFileName = historyFile;
}
public Intent getIntent() {
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
return dataModel.getIntent();
}
public void setIntent(Intent intent) {
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
dataModel.setIntent(intent);
// Inform the target listener to refresh it's UI, if needed.
if (mOnTargetListener != null) {
mOnTargetListener.onTargetSelected();
}
}
public void setOnTargetSelectedListener(OnTargetSelectedListener listener) {
mOnTargetListener = listener;
}
public ArrayList<ResolveInfo> getSortedActivities() {
ArrayList<ResolveInfo> infos = new ArrayList<ResolveInfo>();
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
// Populate the sub-menu with a sub set of the activities.
final int count = dataModel.getActivityCount();
for (int i = 0; i < count; i++) {
infos.add(dataModel.getActivity(i));
}
return infos;
}
public void chooseActivity(int position) {
mCallbacks.chooseActivity(position);
}
/**
* Listener for handling default activity / menu item clicks.
*/
private class Callbacks implements OnMenuItemClickListener,
OnClickListener {
void chooseActivity(int index) {
final ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
final Intent launchIntent = dataModel.chooseActivity(index);
if (launchIntent != null) {
// This may cause a download to happen. Make sure we're on the background thread.
ThreadUtils.postToBackgroundThread(new Runnable() {
@Override
public void run() {
// Share image downloads the image before sharing it.
String type = launchIntent.getType();
if (Intent.ACTION_SEND.equals(launchIntent.getAction()) && type != null && type.startsWith("image/")) {
downloadImageForIntent(launchIntent);
}
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
mContext.startActivity(launchIntent);
}
});
}
if (mOnTargetListener != null) {
mOnTargetListener.onTargetSelected();
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
chooseActivity(item.getItemId());
// Context: Sharing via chrome mainmenu list (no explicit session is active)
Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.LIST, "actionprovider");
return true;
}
@Override
public void onClick(View view) {
Integer index = (Integer) view.getTag();
chooseActivity(index);
// Context: Sharing via chrome mainmenu and content contextmenu quickshare (no explicit session is active)
Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.BUTTON, "actionprovider");
}
}
public enum ActionViewType {
DEFAULT,
CONTEXT_MENU,
}
/**
* Downloads the URI pointed to by a share intent, and alters the intent to point to the
* locally stored file.
*
* @param intent share intent to alter in place.
*/
public void downloadImageForIntent(final Intent intent) {
final String src = IntentUtils.getStringExtraSafe(intent, Intent.EXTRA_TEXT);
final File dir = GeckoApp.getTempDirectory();
if (src == null || dir == null) {
// We should be, but currently aren't, statically guaranteed an Activity context.
// Try our best.
if (mContext instanceof Activity) {
SnackbarBuilder.builder((Activity) mContext)
.message(mContext.getApplicationContext().getString(R.string.share_image_failed))
.duration(Snackbar.LENGTH_LONG)
.buildAndShow();
}
return;
}
GeckoApp.deleteTempFiles();
String type = intent.getType();
OutputStream os = null;
try {
// Create a temporary file for the image
if (src.startsWith("data:")) {
final int dataStart = src.indexOf(",");
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
// If we weren't given an explicit mimetype, try to dig one out of the data uri.
if (TextUtils.isEmpty(extension) && dataStart > 5) {
type = src.substring(5, dataStart).replace(";base64", "");
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
}
final File imageFile = File.createTempFile("image", "." + extension, dir);
os = new FileOutputStream(imageFile);
byte[] buf = Base64.decode(src.substring(dataStart + 1), Base64.DEFAULT);
os.write(buf);
// Only alter the intent when we're sure everything has worked
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
} else {
InputStream is = null;
try {
final byte[] buf = new byte[2048];
final URL url = new URL(src);
final String filename = URLUtil.guessFileName(src, null, type);
is = url.openStream();
final File imageFile = new File(dir, filename);
os = new FileOutputStream(imageFile);
int length;
while ((length = is.read(buf)) != -1) {
os.write(buf, 0, length);
}
// Only alter the intent when we're sure everything has worked
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
} finally {
IOUtils.safeStreamClose(is);
}
}
} catch (IOException ex) {
// If something went wrong, we'll just leave the intent un-changed
} finally {
IOUtils.safeStreamClose(os);
}
}
}
|