ActivityHelper.java revision 62f9c4d2803382f89cf8a19ed12b53b639d547fe
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.activity;
18
19import com.android.email.Controller;
20import com.android.email.R;
21import com.android.email.Utility;
22
23import android.app.Activity;
24import android.content.ActivityNotFoundException;
25import android.content.Intent;
26import android.net.Uri;
27import android.provider.Browser;
28
29/**
30 * Various methods that are used by both 1-pane and 2-pane activities.
31 *
32 * <p>Common code used by {@link MessageListXL}, {@link MessageList} and other activities go here.
33 * Probably there's a nicer way to do this, if we re-design these classes more throughly.
34 * However, without knowing what the phone UI will be, all such work can easily end up being
35 * over-designed or totally useless.  For now this pattern will do...
36 */
37public final class ActivityHelper {
38    /**
39     * Loader IDs have to be unique in a fragment.  We reserve ID(s) here for loaders created
40     * outside of fragments.
41     */
42    public static final int GLOBAL_LOADER_ID_MOVE_TO_DIALOG_LOADER = 1000;
43
44    private ActivityHelper() {
45    }
46
47    /**
48     * Open an URL in a message.
49     *
50     * This is intended to mirror the operation of the original
51     * (see android.webkit.CallbackProxy) with one addition of intent flags
52     * "FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET".  This improves behavior when sublaunching
53     * other apps via embedded URI's.
54     *
55     * We also use this hook to catch "mailto:" links and handle them locally.
56     *
57     * @param activity parent activity
58     * @param url URL to open
59     * @param senderAccountId if the URL is mailto:, we use this account as the sender.
60     *        TODO When MessageCompose implements the account selector, this won't be necessary.
61     * @return true if the URI has successfully been opened.
62     */
63    public static boolean openUrlInMessage(Activity activity, String url, long senderAccountId) {
64        // hijack mailto: uri's and handle locally
65        if (url != null && url.toLowerCase().startsWith("mailto:")) {
66            return MessageCompose.actionCompose(activity, url, senderAccountId);
67        }
68
69        // Handle most uri's via intent launch
70        boolean result = false;
71        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
72        intent.addCategory(Intent.CATEGORY_BROWSABLE);
73        intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName());
74        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
75        try {
76            activity.startActivity(intent);
77            result = true;
78        } catch (ActivityNotFoundException ex) {
79            // No applications can handle it.  Ignore.
80        }
81        return result;
82    }
83
84    /**
85     * Open Calendar app with specific time
86     */
87    public static void openCalendar(Activity activity, long epochEventStartTime) {
88        Uri uri = Uri.parse("content://com.android.calendar/time/" + epochEventStartTime);
89        Intent intent = new Intent(Intent.ACTION_VIEW);
90        intent.setData(uri);
91        intent.putExtra("VIEW", "DAY");
92        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
93        activity.startActivity(intent);
94    }
95
96    public static void deleteMessage(Activity activity, long messageId) {
97        Controller.getInstance(activity).deleteMessage(messageId, -1);
98        Utility.showToast(activity,
99                activity.getResources().getQuantityString(R.plurals.message_deleted_toast, 1));
100    }
101
102    public static void moveMessages(Activity activity, long newMailboxId, long[] messageIds) {
103        // TODO Support moving multiple messages
104        Controller.getInstance(activity).moveMessage(messageIds[0], newMailboxId);
105        String message = activity.getResources().getQuantityString(R.plurals.message_moved_toast,
106                messageIds.length, messageIds.length , "a mailbox"); // STOPSHIP get mailbox name
107        Utility.showToast(activity, message);
108    }
109}
110