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
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser;
7 import android.accounts.Account;
8 import android.accounts.AccountManager;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.text.Html;
13 import android.text.TextUtils;
14 import android.util.Patterns;
16 import org.chromium.base.CalledByNative;
19 import java.util.HashSet;
21 import java.util.regex.Pattern;
24 * Helper for issuing intents to the android framework.
26 public abstract class IntentHelper {
28 private IntentHelper() {}
31 * Triggers a send email intent. If no application has registered to receive these intents,
32 * this will fail silently.
34 * @param context The context for issuing the intent.
35 * @param email The email address to send to.
36 * @param subject The subject of the email.
37 * @param body The body of the email.
38 * @param chooserTitle The title of the activity chooser.
39 * @param fileToAttach The file name of the attachment.
42 static void sendEmail(Context context, String email, String subject, String body,
43 String chooserTitle, String fileToAttach) {
44 Set<String> possibleEmails = new HashSet<String>();
46 if (!TextUtils.isEmpty(email)) {
47 possibleEmails.add(email);
49 Pattern emailPattern = Patterns.EMAIL_ADDRESS;
50 Account[] accounts = AccountManager.get(context).getAccounts();
51 for (Account account : accounts) {
52 if (emailPattern.matcher(account.name).matches()) {
53 possibleEmails.add(account.name);
58 Intent send = new Intent(Intent.ACTION_SEND);
59 send.setType("message/rfc822");
60 if (possibleEmails.size() != 0) {
61 send.putExtra(Intent.EXTRA_EMAIL,
62 possibleEmails.toArray(new String[possibleEmails.size()]));
64 send.putExtra(Intent.EXTRA_SUBJECT, subject);
65 send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
66 if (!TextUtils.isEmpty(fileToAttach)) {
67 File fileIn = new File(fileToAttach);
68 send.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileIn));
72 Intent chooser = Intent.createChooser(send, chooserTitle);
73 // we start this activity outside the main activity.
74 chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
75 context.startActivity(chooser);
76 } catch (android.content.ActivityNotFoundException ex) {
77 // If no app handles it, do nothing.
82 * Opens date and time in Android settings.
84 * @param context The context for issuing the intent.
87 static void openDateAndTimeSettings(Context context) {
88 Intent intent = new Intent(android.provider.Settings.ACTION_DATE_SETTINGS);
91 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
92 context.startActivity(intent);
93 } catch (android.content.ActivityNotFoundException ex) {
94 // If it doesn't work, avoid crashing.