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

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

Issue 2784353002: Android: Remove GetApplicationContext part 2 (Closed)
Patch Set: Fix tests Created 3 years, 8 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
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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; 5 package org.chromium.chrome.browser;
6 6
7 import android.accounts.Account; 7 import android.accounts.Account;
8 import android.content.Context;
9 import android.content.Intent; 8 import android.content.Intent;
10 import android.net.Uri; 9 import android.net.Uri;
11 import android.text.Html; 10 import android.text.Html;
12 import android.text.TextUtils; 11 import android.text.TextUtils;
13 import android.util.Patterns; 12 import android.util.Patterns;
14 13
15 import org.chromium.base.ContentUriUtils; 14 import org.chromium.base.ContentUriUtils;
15 import org.chromium.base.ContextUtils;
16 import org.chromium.base.annotations.CalledByNative; 16 import org.chromium.base.annotations.CalledByNative;
17 import org.chromium.components.signin.AccountManagerHelper; 17 import org.chromium.components.signin.AccountManagerHelper;
18 18
19 import java.io.File; 19 import java.io.File;
20 20
21 /** 21 /**
22 * Helper for issuing intents to the android framework. 22 * Helper for issuing intents to the android framework.
23 */ 23 */
24 public abstract class IntentHelper { 24 public abstract class IntentHelper {
25 25
26 private IntentHelper() {} 26 private IntentHelper() {}
27 27
28 /** 28 /**
29 * Triggers a send email intent. If no application has registered to receiv e these intents, 29 * Triggers a send email intent. If no application has registered to receiv e these intents,
30 * this will fail silently. If an email is not specified and the device has exactly one 30 * this will fail silently. If an email is not specified and the device has exactly one
31 * account and the account name matches the email format, the email is set t o the account name. 31 * account and the account name matches the email format, the email is set t o the account name.
32 * 32 * @param email The email address to send to.
33 * @param context The context for issuing the intent.
34 * @param email The email address to send to.
35 * @param subject The subject of the email. 33 * @param subject The subject of the email.
36 * @param body The body of the email. 34 * @param body The body of the email.
37 * @param chooserTitle The title of the activity chooser. 35 * @param chooserTitle The title of the activity chooser.
38 * @param fileToAttach The file name of the attachment. 36 * @param fileToAttach The file name of the attachment.
39 */ 37 */
40 @SuppressWarnings("deprecation") // Update usage of Html.fromHtml when API min is 24 38 @SuppressWarnings("deprecation") // Update usage of Html.fromHtml when API m in is 24
41 @CalledByNative 39 @CalledByNative
42 static void sendEmail(Context context, String email, String subject, String body, 40 static void sendEmail(
43 String chooserTitle, String fileToAttach) { 41 String email, String subject, String body, String chooserTitle, Stri ng fileToAttach) {
44 if (TextUtils.isEmpty(email)) { 42 if (TextUtils.isEmpty(email)) {
45 Account[] accounts = AccountManagerHelper.get(context).getGoogleAcco unts(); 43 Account[] accounts = AccountManagerHelper.get().getGoogleAccounts();
46 if (accounts != null && accounts.length == 1 44 if (accounts != null && accounts.length == 1
47 && Patterns.EMAIL_ADDRESS.matcher(accounts[0].name).matches( )) { 45 && Patterns.EMAIL_ADDRESS.matcher(accounts[0].name).matches( )) {
48 email = accounts[0].name; 46 email = accounts[0].name;
49 } 47 }
50 } 48 }
51 49
52 Intent send = new Intent(Intent.ACTION_SEND); 50 Intent send = new Intent(Intent.ACTION_SEND);
53 send.setType("message/rfc822"); 51 send.setType("message/rfc822");
54 if (!TextUtils.isEmpty(email)) send.putExtra(Intent.EXTRA_EMAIL, new Str ing[] { email }); 52 if (!TextUtils.isEmpty(email)) send.putExtra(Intent.EXTRA_EMAIL, new Str ing[] { email });
55 send.putExtra(Intent.EXTRA_SUBJECT, subject); 53 send.putExtra(Intent.EXTRA_SUBJECT, subject);
56 send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body)); 54 send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
57 if (!TextUtils.isEmpty(fileToAttach)) { 55 if (!TextUtils.isEmpty(fileToAttach)) {
58 File fileIn = new File(fileToAttach); 56 File fileIn = new File(fileToAttach);
59 Uri fileUri; 57 Uri fileUri;
60 // Attempt to use a content Uri, for greater compatibility. If the path isn't set 58 // Attempt to use a content Uri, for greater compatibility. If the path isn't set
61 // up to be shared that way with a <paths> meta-data element, just u se a file Uri 59 // up to be shared that way with a <paths> meta-data element, just u se a file Uri
62 // instead. 60 // instead.
63 try { 61 try {
64 fileUri = ContentUriUtils.getContentUriFromFile(fileIn); 62 fileUri = ContentUriUtils.getContentUriFromFile(fileIn);
65 } catch (IllegalArgumentException ex) { 63 } catch (IllegalArgumentException ex) {
66 fileUri = Uri.fromFile(fileIn); 64 fileUri = Uri.fromFile(fileIn);
67 } 65 }
68 send.putExtra(Intent.EXTRA_STREAM, fileUri); 66 send.putExtra(Intent.EXTRA_STREAM, fileUri);
69 } 67 }
70 68
71 try { 69 try {
72 Intent chooser = Intent.createChooser(send, chooserTitle); 70 Intent chooser = Intent.createChooser(send, chooserTitle);
73 // we start this activity outside the main activity. 71 // we start this activity outside the main activity.
74 chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 72 chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
75 context.startActivity(chooser); 73 ContextUtils.getApplicationContext().startActivity(chooser);
76 } catch (android.content.ActivityNotFoundException ex) { 74 } catch (android.content.ActivityNotFoundException ex) {
77 // If no app handles it, do nothing. 75 // If no app handles it, do nothing.
78 } 76 }
79 } 77 }
80 78
81 /** 79 /**
82 * Opens date and time in Android settings. 80 * Opens date and time in Android settings.
83 * 81 *
84 * @param context The context for issuing the intent.
85 */ 82 */
86 @CalledByNative 83 @CalledByNative
87 static void openDateAndTimeSettings(Context context) { 84 static void openDateAndTimeSettings() {
88 Intent intent = new Intent(android.provider.Settings.ACTION_DATE_SETTING S); 85 Intent intent = new Intent(android.provider.Settings.ACTION_DATE_SETTING S);
89 86
90 try { 87 try {
91 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 88 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
92 context.startActivity(intent); 89 ContextUtils.getApplicationContext().startActivity(intent);
93 } catch (android.content.ActivityNotFoundException ex) { 90 } catch (android.content.ActivityNotFoundException ex) {
94 // If it doesn't work, avoid crashing. 91 // If it doesn't work, avoid crashing.
95 } 92 }
96 } 93 }
97 } 94 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698