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

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: Remove unused constant 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 /** 113 /**
113 * Creates and shows a share intent picker dialog. 114 * Creates and shows a share intent picker dialog.
114 * 115 *
115 * @param activity Activity that is used to access package manager. 116 * @param activity Activity that is used to access package manager.
116 * @param title Title of the page to be shared. 117 * @param title Title of the page to be shared.
117 * @param url URL of the page to be shared. 118 * @param url URL of the page to be shared.
118 * @param screenshot Screenshot of the page to be shared. 119 * @param screenshot Screenshot of the page to be shared.
119 */ 120 */
120 private static void showShareDialog(final Activity activity, final String ti tle, 121 private static void showShareDialog(final Activity activity, final String ti tle,
121 final String url, final Bitmap screenshot) { 122 final String url, final Bitmap screenshot) {
122 Intent intent = getShareIntent(activity, title, url, null); 123 Intent intent = getShareIntent(title, url, null);
123 PackageManager manager = activity.getPackageManager(); 124 PackageManager manager = activity.getPackageManager();
124 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent , 0); 125 List<ResolveInfo> resolveInfoList = manager.queryIntentActivities(intent , 0);
125 assert resolveInfoList.size() > 0; 126 assert resolveInfoList.size() > 0;
126 if (resolveInfoList.size() == 0) return; 127 if (resolveInfoList.size() == 0) return;
127 Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator( manager)); 128 Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator( manager));
128 129
129 final ShareDialogAdapter adapter = 130 final ShareDialogAdapter adapter =
130 new ShareDialogAdapter(activity, manager, resolveInfoList); 131 new ShareDialogAdapter(activity, manager, resolveInfoList);
131 AlertDialog.Builder builder = new AlertDialog.Builder(activity); 132 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
132 builder.setTitle(activity.getString(R.string.share_link_chooser_title)); 133 builder.setTitle(activity.getString(R.string.share_link_chooser_title));
(...skipping 26 matching lines...) Expand all
159 private static void shareWithLastUsed( 160 private static void shareWithLastUsed(
160 Activity activity, String title, String url, Bitmap screenshot) { 161 Activity activity, String title, String url, Bitmap screenshot) {
161 ComponentName component = getLastShareComponentName(activity); 162 ComponentName component = getLastShareComponentName(activity);
162 if (component == null) return; 163 if (component == null) return;
163 makeIntentAndShare(activity, title, url, screenshot, component); 164 makeIntentAndShare(activity, title, url, screenshot, component);
164 } 165 }
165 166
166 private static void makeIntentAndShare(final Activity activity, final String title, 167 private static void makeIntentAndShare(final Activity activity, final String title,
167 final String url, final Bitmap screenshot, final ComponentName compo nent) { 168 final String url, final Bitmap screenshot, final ComponentName compo nent) {
168 if (screenshot == null) { 169 if (screenshot == null) {
169 activity.startActivity( 170 activity.startActivity(getDirectShareIntentForComponent(title, url, null, component));
170 getDirectShareIntentForComponent(activity, title, url, null, component));
171 } else { 171 } else {
172 new AsyncTask<Void, Void, Intent>() { 172 new AsyncTask<Void, Void, File>() {
173 @Override 173 @Override
174 protected Intent doInBackground(Void... params) { 174 protected File doInBackground(Void... params) {
175 return getDirectShareIntentForComponent( 175 FileOutputStream fOut = null;
176 activity, title, url, screenshot, component); 176 try {
177 File path = new File(UiUtils.getDirectoryForImageCapture (activity),
178 SCREENSHOT_DIRECTORY_NAME);
179 if (path.exists() || path.mkdir()) {
180 File saveFile = File.createTempFile(
181 String.valueOf(System.currentTimeMillis()), ".jpg", path);
182 fOut = new FileOutputStream(saveFile);
183 screenshot.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
184 fOut.flush();
185 fOut.close();
186
187 return saveFile;
188 }
189 } catch (IOException ie) {
190 if (fOut != null) {
191 try {
192 fOut.close();
193 } catch (IOException e) {
194 // Ignore exception.
195 }
196 }
197 }
198
199 return null;
177 } 200 }
178 201
179 @Override 202 @Override
180 protected void onPostExecute(Intent intent) { 203 protected void onPostExecute(File saveFile) {
181 if (ApplicationStatus.getStateForApplication() 204 if (ApplicationStatus.getStateForApplication()
182 != ApplicationState.HAS_DESTROYED_ACTIVITIES) { 205 != ApplicationState.HAS_DESTROYED_ACTIVITIES) {
183 activity.startActivity(intent); 206 Uri screenshotUri = saveFile == null
207 ? null : UiUtils.getUriForImageCaptureFile(activ ity, saveFile);
208 activity.startActivity(getDirectShareIntentForComponent(
209 title, url, screenshotUri, component));
184 } 210 }
185 } 211 }
186 }.execute(); 212 }.execute();
187 } 213 }
188 } 214 }
189 215
190 /** 216 /**
191 * Set the icon and the title for the menu item used for direct share. 217 * Set the icon and the title for the menu item used for direct share.
192 * 218 *
193 * @param activity Activity that is used to access the package manager. 219 * @param activity Activity that is used to access the package manager.
(...skipping 16 matching lines...) Expand all
210 } 236 }
211 237
212 item.setIcon(directShareIcon); 238 item.setIcon(directShareIcon);
213 if (directShareTitle != null) { 239 if (directShareTitle != null) {
214 item.setTitle(activity.getString(R.string.accessibility_menu_share_v ia, 240 item.setTitle(activity.getString(R.string.accessibility_menu_share_v ia,
215 directShareTitle)); 241 directShareTitle));
216 } 242 }
217 } 243 }
218 244
219 @VisibleForTesting 245 @VisibleForTesting
220 public static Intent getShareIntent( 246 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); 247 url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
223 Intent intent = new Intent(Intent.ACTION_SEND); 248 Intent intent = new Intent(Intent.ACTION_SEND);
224 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); 249 intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag());
225 intent.setType("text/plain"); 250 intent.setType("text/plain");
226 intent.putExtra(Intent.EXTRA_SUBJECT, title); 251 intent.putExtra(Intent.EXTRA_SUBJECT, title);
227 intent.putExtra(Intent.EXTRA_TEXT, url); 252 intent.putExtra(Intent.EXTRA_TEXT, url);
228 if (screenshot != null) { 253 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; 254 return intent;
256 } 255 }
257 256
258 private static Intent getDirectShareIntentForComponent( 257 private static Intent getDirectShareIntentForComponent(
259 Context context, String title, String url, Bitmap screenshot, Compon entName component) { 258 String title, String url, Uri screenshotUri, ComponentName component ) {
260 Intent intent = getShareIntent(context, title, url, screenshot); 259 Intent intent = getShareIntent(title, url, screenshotUri);
261 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 260 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
262 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 261 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
263 intent.setComponent(component); 262 intent.setComponent(component);
264 return intent; 263 return intent;
265 } 264 }
266 265
267 private static ComponentName getLastShareComponentName(Context context) { 266 private static ComponentName getLastShareComponentName(Context context) {
268 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context); 267 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context);
269 String packageName = preferences.getString(PACKAGE_NAME_KEY, null); 268 String packageName = preferences.getString(PACKAGE_NAME_KEY, null);
270 String className = preferences.getString(CLASS_NAME_KEY, null); 269 String className = preferences.getString(CLASS_NAME_KEY, null);
271 if (packageName == null || className == null) return null; 270 if (packageName == null || className == null) return null;
272 return new ComponentName(packageName, className); 271 return new ComponentName(packageName, className);
273 } 272 }
274 273
275 private static void setLastShareComponentName(Context context, ComponentName component) { 274 private static void setLastShareComponentName(Context context, ComponentName component) {
276 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context); 275 SharedPreferences preferences = PreferenceManager.getDefaultSharedPrefer ences(context);
277 SharedPreferences.Editor editor = preferences.edit(); 276 SharedPreferences.Editor editor = preferences.edit();
278 editor.putString(PACKAGE_NAME_KEY, component.getPackageName()); 277 editor.putString(PACKAGE_NAME_KEY, component.getPackageName());
279 editor.putString(CLASS_NAME_KEY, component.getClassName()); 278 editor.putString(CLASS_NAME_KEY, component.getClassName());
280 editor.apply(); 279 editor.apply();
281 } 280 }
282 } 281 }
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