Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java

Issue 972293003: Refactor codes to run only the bitmap converting logic in background (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/android/javatests/src/org/chromium/chrome/browser/share/ShareUrlTest.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.share; 5 package org.chromium.chrome.browser.share;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.app.AlertDialog; 8 import android.app.AlertDialog;
9 import android.content.ComponentName; 9 import android.content.ComponentName;
10 import android.content.Context; 10 import android.content.Context;
11 import android.content.Intent; 11 import android.content.Intent;
12 import android.content.SharedPreferences; 12 import android.content.SharedPreferences;
13 import android.content.pm.ActivityInfo; 13 import android.content.pm.ActivityInfo;
14 import android.content.pm.ApplicationInfo; 14 import android.content.pm.ApplicationInfo;
15 import android.content.pm.PackageManager; 15 import android.content.pm.PackageManager;
16 import android.content.pm.PackageManager.NameNotFoundException; 16 import android.content.pm.PackageManager.NameNotFoundException;
17 import android.content.pm.ResolveInfo; 17 import android.content.pm.ResolveInfo;
18 import android.graphics.Bitmap; 18 import android.graphics.Bitmap;
19 import android.graphics.drawable.Drawable; 19 import android.graphics.drawable.Drawable;
20 import android.net.Uri;
20 import android.os.AsyncTask; 21 import android.os.AsyncTask;
21 import android.preference.PreferenceManager; 22 import android.preference.PreferenceManager;
22 import android.util.Log; 23 import android.util.Log;
23 import android.view.MenuItem; 24 import android.view.MenuItem;
24 import android.view.View; 25 import android.view.View;
25 import android.widget.AdapterView; 26 import android.widget.AdapterView;
26 import android.widget.AdapterView.OnItemClickListener; 27 import android.widget.AdapterView.OnItemClickListener;
27 28
28 import org.chromium.base.ApiCompatibilityUtils; 29 import org.chromium.base.ApiCompatibilityUtils;
29 import org.chromium.base.ApplicationState; 30 import org.chromium.base.ApplicationState;
(...skipping 11 matching lines...) Expand all
41 42
42 /** 43 /**
43 * A helper class that helps to start an intent to share titles and URLs. 44 * A helper class that helps to start an intent to share titles and URLs.
44 */ 45 */
45 public class ShareHelper { 46 public class ShareHelper {
46 47
47 private static final String TAG = "ShareHelper"; 48 private static final String TAG = "ShareHelper";
48 49
49 private static final String PACKAGE_NAME_KEY = "last_shared_package_name"; 50 private static final String PACKAGE_NAME_KEY = "last_shared_package_name";
50 private static final String CLASS_NAME_KEY = "last_shared_class_name"; 51 private static final String CLASS_NAME_KEY = "last_shared_class_name";
52 private static final String EXTRA_RAW_FILE_PATH = "raw-file-path";
Ted C 2015/03/04 00:43:35 unused?
Jaekyun Seok (inactive) 2015/03/04 00:48:13 Done.
51 53
52 /** 54 /**
53 * Directory name for screenshots. 55 * Directory name for screenshots.
54 */ 56 */
55 private static final String SCREENSHOT_DIRECTORY_NAME = "screenshot"; 57 private static final String SCREENSHOT_DIRECTORY_NAME = "screenshot";
56 58
57 private ShareHelper() {} 59 private ShareHelper() {}
58 60
59 private static void deleteScreenshotFiles(File file) { 61 private static void deleteScreenshotFiles(File file) {
60 if (!file.exists()) return; 62 if (!file.exists()) return;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 /** 114 /**
113 * Creates and shows a share intent picker dialog. 115 * Creates and shows a share intent picker dialog.
114 * 116 *
115 * @param activity Activity that is used to access package manager. 117 * @param activity Activity that is used to access package manager.
116 * @param title Title of the page to be shared. 118 * @param title Title of the page to be shared.
117 * @param url URL of the page to be shared. 119 * @param url URL of the page to be shared.
118 * @param screenshot Screenshot of the page to be shared. 120 * @param screenshot Screenshot of the page to be shared.
119 */ 121 */
120 private static void showShareDialog(final Activity activity, final String ti tle, 122 private static void showShareDialog(final Activity activity, final String ti tle,
121 final String url, final Bitmap screenshot) { 123 final String url, final Bitmap screenshot) {
122 Intent intent = getShareIntent(activity, title, url, null); 124 Intent intent = getShareIntent(title, url, null);
123 PackageManager manager = activity.getPackageManager(); 125 PackageManager manager = activity.getPackageManager();
124 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent , 0); 126 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent , 0);
125 assert resolveInfoList.size() > 0; 127 assert resolveInfoList.size() > 0;
126 if (resolveInfoList.size() == 0) return; 128 if (resolveInfoList.size() == 0) return;
127 Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator( manager)); 129 Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator( manager));
128 130
129 final ShareDialogAdapter adapter = 131 final ShareDialogAdapter adapter =
130 new ShareDialogAdapter(activity, manager, resolveInfoList); 132 new ShareDialogAdapter(activity, manager, resolveInfoList);
131 AlertDialog.Builder builder = new AlertDialog.Builder(activity); 133 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
132 builder.setTitle(activity.getString(R.string.share_link_chooser_title)); 134 builder.setTitle(activity.getString(R.string.share_link_chooser_title));
(...skipping 26 matching lines...) Expand all
159 private static void shareWithLastUsed( 161 private static void shareWithLastUsed(
160 Activity activity, String title, String url, Bitmap screenshot) { 162 Activity activity, String title, String url, Bitmap screenshot) {
161 ComponentName component = getLastShareComponentName(activity); 163 ComponentName component = getLastShareComponentName(activity);
162 if (component == null) return; 164 if (component == null) return;
163 makeIntentAndShare(activity, title, url, screenshot, component); 165 makeIntentAndShare(activity, title, url, screenshot, component);
164 } 166 }
165 167
166 private static void makeIntentAndShare(final Activity activity, final String title, 168 private static void makeIntentAndShare(final Activity activity, final String title,
167 final String url, final Bitmap screenshot, final ComponentName compo nent) { 169 final String url, final Bitmap screenshot, final ComponentName compo nent) {
168 if (screenshot == null) { 170 if (screenshot == null) {
169 activity.startActivity( 171 activity.startActivity(getDirectShareIntentForComponent(title, url, null, component));
170 getDirectShareIntentForComponent(activity, title, url, null, component));
171 } else { 172 } else {
172 new AsyncTask<Void, Void, Intent>() { 173 new AsyncTask<Void, Void, File>() {
173 @Override 174 @Override
174 protected Intent doInBackground(Void... params) { 175 protected File doInBackground(Void... params) {
175 return getDirectShareIntentForComponent( 176 FileOutputStream fOut = null;
176 activity, title, url, screenshot, component); 177 try {
178 File path = new File(UiUtils.getDirectoryForImageCapture (activity),
179 SCREENSHOT_DIRECTORY_NAME);
180 if (path.exists() || path.mkdir()) {
181 File saveFile = File.createTempFile(
182 String.valueOf(System.currentTimeMillis()), ".jpg", path);
183 fOut = new FileOutputStream(saveFile);
184 screenshot.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
185 fOut.flush();
186 fOut.close();
187
188 return saveFile;
189 }
190 } catch (IOException ie) {
191 if (fOut != null) {
192 try {
193 fOut.close();
194 } catch (IOException e) {
195 // Ignore exception.
196 }
197 }
198 }
199
200 return null;
177 } 201 }
178 202
179 @Override 203 @Override
180 protected void onPostExecute(Intent intent) { 204 protected void onPostExecute(File saveFile) {
181 if (ApplicationStatus.getStateForApplication() 205 if (ApplicationStatus.getStateForApplication()
182 != ApplicationState.HAS_DESTROYED_ACTIVITIES) { 206 != ApplicationState.HAS_DESTROYED_ACTIVITIES) {
183 activity.startActivity(intent); 207 Uri screenshotUri = saveFile == null
208 ? null : UiUtils.getUriForImageCaptureFile(activ ity, saveFile);
209 activity.startActivity(getDirectShareIntentForComponent(
210 title, url, screenshotUri, component));
184 } 211 }
185 } 212 }
186 }.execute(); 213 }.execute();
187 } 214 }
188 } 215 }
189 216
190 /** 217 /**
191 * Set the icon and the title for the menu item used for direct share. 218 * Set the icon and the title for the menu item used for direct share.
192 * 219 *
193 * @param activity Activity that is used to access the package manager. 220 * @param activity Activity that is used to access the package manager.
(...skipping 16 matching lines...) Expand all
210 } 237 }
211 238
212 item.setIcon(directShareIcon); 239 item.setIcon(directShareIcon);
213 if (directShareTitle != null) { 240 if (directShareTitle != null) {
214 item.setTitle(activity.getString(R.string.accessibility_menu_share_v ia, 241 item.setTitle(activity.getString(R.string.accessibility_menu_share_v ia,
215 directShareTitle)); 242 directShareTitle));
216 } 243 }
217 } 244 }
218 245
219 @VisibleForTesting 246 @VisibleForTesting
220 public static Intent getShareIntent( 247 public static Intent getShareIntent(String title, String url, Uri screenshot Uri) {
221 Context context, String title, String url, Bitmap screenshot) {
222 url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url); 248 url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
223 Intent intent = new Intent(Intent.ACTION_SEND); 249 Intent intent = new Intent(Intent.ACTION_SEND);
224 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); 250 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag());
225 intent.setType("text/plain"); 251 intent.setType("text/plain");
226 intent.putExtra(Intent.EXTRA_SUBJECT, title); 252 intent.putExtra(Intent.EXTRA_SUBJECT, title);
227 intent.putExtra(Intent.EXTRA_TEXT, url); 253 intent.putExtra(Intent.EXTRA_TEXT, url);
228 if (screenshot != null) { 254 if (screenshotUri != null) intent.putExtra(Intent.EXTRA_STREAM, screensh otUri);
229 FileOutputStream fOut = null;
230 try {
231 File path = new File(
232 UiUtils.getDirectoryForImageCapture(context), SCREENSHOT _DIRECTORY_NAME);
233 if (path.exists() || path.mkdir()) {
234 File saveFile = File.createTempFile(
235 String.valueOf(System.currentTimeMillis()), ".jpg", path);
236 fOut = new FileOutputStream(saveFile);
237 screenshot.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
238 fOut.flush();
239 fOut.close();
240
241 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
242 intent.putExtra(Intent.EXTRA_STREAM,
243 UiUtils.getUriForImageCaptureFile(context, saveFile) );
244 }
245 } catch (IOException ie) {
246 if (fOut != null) {
247 try {
248 fOut.close();
249 } catch (IOException e) {
250 // Ignore exception.
251 }
252 }
253 }
254 }
255 return intent; 255 return intent;
256 } 256 }
257 257
258 private static Intent getDirectShareIntentForComponent( 258 private static Intent getDirectShareIntentForComponent(
259 Context context, String title, String url, Bitmap screenshot, Compon entName component) { 259 String title, String url, Uri screenshotUri, ComponentName component ) {
260 Intent intent = getShareIntent(context, title, url, screenshot); 260 Intent intent = getShareIntent(title, url, screenshotUri);
261 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 261 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
262 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 262 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
263 intent.setComponent(component); 263 intent.setComponent(component);
264 return intent; 264 return intent;
265 } 265 }
266 266
267 private static ComponentName getLastShareComponentName(Context context) { 267 private static ComponentName getLastShareComponentName(Context context) {
268 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context); 268 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context);
269 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); 269 String packageName = preferences.getString(PACKAGE_NAME_KEY, null);
270 String className = preferences.getString(CLASS_NAME_KEY, null); 270 String className = preferences.getString(CLASS_NAME_KEY, null);
271 if (packageName == null || className == null) return null; 271 if (packageName == null || className == null) return null;
272 return new ComponentName(packageName, className); 272 return new ComponentName(packageName, className);
273 } 273 }
274 274
275 private static void setLastShareComponentName(Context context, ComponentName component) { 275 private static void setLastShareComponentName(Context context, ComponentName component) {
276 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context); 276 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context);
277 SharedPreferences.Editor editor = preferences.edit(); 277 SharedPreferences.Editor editor = preferences.edit();
278 editor.putString(PACKAGE_NAME_KEY, component.getPackageName()); 278 editor.putString(PACKAGE_NAME_KEY, component.getPackageName());
279 editor.putString(CLASS_NAME_KEY, component.getClassName()); 279 editor.putString(CLASS_NAME_KEY, component.getClassName());
280 editor.apply(); 280 editor.apply();
281 } 281 }
282 } 282 }
OLDNEW
« no previous file with comments | « no previous file | chrome/android/javatests/src/org/chromium/chrome/browser/share/ShareUrlTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698