| OLD | NEW |
| 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; |
| 8 import android.content.Intent; | 9 import android.content.Intent; |
| 9 import android.net.Uri; | 10 import android.net.Uri; |
| 10 import android.text.Html; | 11 import android.text.Html; |
| 11 import android.text.TextUtils; | 12 import android.text.TextUtils; |
| 12 import android.util.Patterns; | 13 import android.util.Patterns; |
| 13 | 14 |
| 14 import org.chromium.base.ContentUriUtils; | 15 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 * @param email The email address to send to. | 32 * |
| 33 * @param context The context for issuing the intent. |
| 34 * @param email The email address to send to. |
| 33 * @param subject The subject of the email. | 35 * @param subject The subject of the email. |
| 34 * @param body The body of the email. | 36 * @param body The body of the email. |
| 35 * @param chooserTitle The title of the activity chooser. | 37 * @param chooserTitle The title of the activity chooser. |
| 36 * @param fileToAttach The file name of the attachment. | 38 * @param fileToAttach The file name of the attachment. |
| 37 */ | 39 */ |
| 38 @SuppressWarnings("deprecation") // Update usage of Html.fromHtml when API m
in is 24 | 40 @SuppressWarnings("deprecation") // Update usage of Html.fromHtml when API
min is 24 |
| 39 @CalledByNative | 41 @CalledByNative |
| 40 static void sendEmail( | 42 static void sendEmail(Context context, String email, String subject, String
body, |
| 41 String email, String subject, String body, String chooserTitle, Stri
ng fileToAttach) { | 43 String chooserTitle, String fileToAttach) { |
| 42 if (TextUtils.isEmpty(email)) { | 44 if (TextUtils.isEmpty(email)) { |
| 43 Account[] accounts = AccountManagerHelper.get().getGoogleAccounts(); | 45 Account[] accounts = AccountManagerHelper.get(context).getGoogleAcco
unts(); |
| 44 if (accounts != null && accounts.length == 1 | 46 if (accounts != null && accounts.length == 1 |
| 45 && Patterns.EMAIL_ADDRESS.matcher(accounts[0].name).matches(
)) { | 47 && Patterns.EMAIL_ADDRESS.matcher(accounts[0].name).matches(
)) { |
| 46 email = accounts[0].name; | 48 email = accounts[0].name; |
| 47 } | 49 } |
| 48 } | 50 } |
| 49 | 51 |
| 50 Intent send = new Intent(Intent.ACTION_SEND); | 52 Intent send = new Intent(Intent.ACTION_SEND); |
| 51 send.setType("message/rfc822"); | 53 send.setType("message/rfc822"); |
| 52 if (!TextUtils.isEmpty(email)) send.putExtra(Intent.EXTRA_EMAIL, new Str
ing[] { email }); | 54 if (!TextUtils.isEmpty(email)) send.putExtra(Intent.EXTRA_EMAIL, new Str
ing[] { email }); |
| 53 send.putExtra(Intent.EXTRA_SUBJECT, subject); | 55 send.putExtra(Intent.EXTRA_SUBJECT, subject); |
| 54 send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body)); | 56 send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body)); |
| 55 if (!TextUtils.isEmpty(fileToAttach)) { | 57 if (!TextUtils.isEmpty(fileToAttach)) { |
| 56 File fileIn = new File(fileToAttach); | 58 File fileIn = new File(fileToAttach); |
| 57 Uri fileUri; | 59 Uri fileUri; |
| 58 // Attempt to use a content Uri, for greater compatibility. If the
path isn't set | 60 // Attempt to use a content Uri, for greater compatibility. If the
path isn't set |
| 59 // up to be shared that way with a <paths> meta-data element, just u
se a file Uri | 61 // up to be shared that way with a <paths> meta-data element, just u
se a file Uri |
| 60 // instead. | 62 // instead. |
| 61 try { | 63 try { |
| 62 fileUri = ContentUriUtils.getContentUriFromFile(fileIn); | 64 fileUri = ContentUriUtils.getContentUriFromFile(fileIn); |
| 63 } catch (IllegalArgumentException ex) { | 65 } catch (IllegalArgumentException ex) { |
| 64 fileUri = Uri.fromFile(fileIn); | 66 fileUri = Uri.fromFile(fileIn); |
| 65 } | 67 } |
| 66 send.putExtra(Intent.EXTRA_STREAM, fileUri); | 68 send.putExtra(Intent.EXTRA_STREAM, fileUri); |
| 67 } | 69 } |
| 68 | 70 |
| 69 try { | 71 try { |
| 70 Intent chooser = Intent.createChooser(send, chooserTitle); | 72 Intent chooser = Intent.createChooser(send, chooserTitle); |
| 71 // we start this activity outside the main activity. | 73 // we start this activity outside the main activity. |
| 72 chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 74 chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 73 ContextUtils.getApplicationContext().startActivity(chooser); | 75 context.startActivity(chooser); |
| 74 } catch (android.content.ActivityNotFoundException ex) { | 76 } catch (android.content.ActivityNotFoundException ex) { |
| 75 // If no app handles it, do nothing. | 77 // If no app handles it, do nothing. |
| 76 } | 78 } |
| 77 } | 79 } |
| 78 | 80 |
| 79 /** | 81 /** |
| 80 * Opens date and time in Android settings. | 82 * Opens date and time in Android settings. |
| 81 * | 83 * |
| 84 * @param context The context for issuing the intent. |
| 82 */ | 85 */ |
| 83 @CalledByNative | 86 @CalledByNative |
| 84 static void openDateAndTimeSettings() { | 87 static void openDateAndTimeSettings(Context context) { |
| 85 Intent intent = new Intent(android.provider.Settings.ACTION_DATE_SETTING
S); | 88 Intent intent = new Intent(android.provider.Settings.ACTION_DATE_SETTING
S); |
| 86 | 89 |
| 87 try { | 90 try { |
| 88 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 91 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 89 ContextUtils.getApplicationContext().startActivity(intent); | 92 context.startActivity(intent); |
| 90 } catch (android.content.ActivityNotFoundException ex) { | 93 } catch (android.content.ActivityNotFoundException ex) { |
| 91 // If it doesn't work, avoid crashing. | 94 // If it doesn't work, avoid crashing. |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 } | 97 } |
| OLD | NEW |