OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import android.accounts.Account; | |
8 import android.accounts.AccountManager; | |
9 import android.content.Context; | |
10 import android.content.Intent; | |
11 import android.text.Html; | |
12 import android.text.TextUtils; | |
13 import android.util.Patterns; | |
14 | |
15 import org.chromium.base.CalledByNative; | |
16 | |
17 import java.util.HashSet; | |
18 import java.util.Set; | |
19 import java.util.regex.Pattern; | |
20 | |
21 /** | |
22 * Helper for issuing intents to the android framework. | |
23 */ | |
24 public abstract class IntentHelper { | |
25 | |
26 private IntentHelper() {} | |
27 | |
28 /** | |
29 * Triggers a send email intent. If no application has registered to receiv e these intents, | |
30 * this will fail silently. | |
31 * | |
32 * @param context The context for issuing the intent. | |
33 * @param email The email address to send to. | |
34 * @param subj The subject of the email. | |
Yaron
2012/07/23 23:49:43
nit: why not just call it "subject"
Ted C
2012/07/24 00:28:51
Done.
| |
35 * @param body The body of the email. | |
36 * @param inv The title of the activity chooser. | |
Yaron
2012/07/23 23:49:43
nit: what's "inv"?
Ted C
2012/07/24 00:28:51
Changed to chooserTitle
| |
37 */ | |
38 @CalledByNative | |
39 static void sendEmail(Context context, String email, String subj, String bod y, String inv) { | |
40 Set<String> possibleEmails = new HashSet<String>(); | |
41 | |
42 if (!TextUtils.isEmpty(email)) { | |
43 possibleEmails.add(email); | |
44 } else { | |
45 Pattern emailPattern = Patterns.EMAIL_ADDRESS; | |
46 Account[] accounts = AccountManager.get(context).getAccounts(); | |
47 for (Account account : accounts) { | |
48 if (emailPattern.matcher(account.name).matches()) { | |
49 possibleEmails.add(account.name); | |
50 } | |
51 } | |
52 } | |
53 | |
54 Intent send = new Intent(Intent.ACTION_SEND); | |
55 send.setType("message/rfc822"); | |
56 if (possibleEmails.size() != 0) { | |
57 send.putExtra(Intent.EXTRA_EMAIL, | |
58 possibleEmails.toArray(new String[possibleEmails.size()])); | |
59 } | |
60 send.putExtra(Intent.EXTRA_SUBJECT, subj); | |
61 send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body)); | |
62 try { | |
63 context.startActivity(Intent.createChooser(send, inv)); | |
64 } catch (android.content.ActivityNotFoundException ex) { | |
65 // If no app handles it, do nothing. | |
66 } | |
67 } | |
68 } | |
OLD | NEW |