ActivityHelper.java revision 629b93d67d85fa52cc144b463087bfb7aa58371b
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.Email;
21import com.android.email.R;
22import com.android.emailcommon.provider.EmailContent.Mailbox;
23import com.android.emailcommon.utility.EmailAsyncTask;
24import com.android.emailcommon.utility.Utility;
25
26import android.app.Activity;
27import android.content.ActivityNotFoundException;
28import android.content.Intent;
29import android.net.Uri;
30import android.provider.Browser;
31import android.view.MenuItem;
32import android.view.WindowManager;
33
34/**
35 * Various methods that are used by both 1-pane and 2-pane activities.
36 *
37 * <p>Common code used by {@link MessageListXL}, {@link MessageList} and other activities go here.
38 * Probably there's a nicer way to do this, if we re-design these classes more throughly.
39 * However, without knowing what the phone UI will be, all such work can easily end up being
40 * over-designed or totally useless.  For now this pattern will do...
41 */
42public final class ActivityHelper {
43    /**
44     * Loader IDs have to be unique in a fragment.  We reserve IDs here for loaders created
45     * outside of fragments.
46     */
47    public static final int GLOBAL_LOADER_ID_MOVE_TO_DIALOG_MAILBOX_LOADER = 1000;
48    public static final int GLOBAL_LOADER_ID_MOVE_TO_DIALOG_MESSAGE_CHECKER = 1001;
49
50    private ActivityHelper() {
51    }
52
53    /**
54     * Open an URL in a message.
55     *
56     * This is intended to mirror the operation of the original
57     * (see android.webkit.CallbackProxy) with one addition of intent flags
58     * "FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET".  This improves behavior when sublaunching
59     * other apps via embedded URI's.
60     *
61     * We also use this hook to catch "mailto:" links and handle them locally.
62     *
63     * @param activity parent activity
64     * @param url URL to open
65     * @param senderAccountId if the URL is mailto:, we use this account as the sender.
66     *        TODO When MessageCompose implements the account selector, this won't be necessary.
67     * @return true if the URI has successfully been opened.
68     */
69    public static boolean openUrlInMessage(Activity activity, String url, long senderAccountId) {
70        // hijack mailto: uri's and handle locally
71        if (url != null && url.toLowerCase().startsWith("mailto:")) {
72            return MessageCompose.actionCompose(activity, url, senderAccountId);
73        }
74
75        // Handle most uri's via intent launch
76        boolean result = false;
77        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
78        intent.addCategory(Intent.CATEGORY_BROWSABLE);
79        intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName());
80        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
81        try {
82            activity.startActivity(intent);
83            result = true;
84        } catch (ActivityNotFoundException ex) {
85            // No applications can handle it.  Ignore.
86        }
87        return result;
88    }
89
90    /**
91     * Open Calendar app with specific time
92     */
93    public static void openCalendar(Activity activity, long epochEventStartTime) {
94        Uri uri = Uri.parse("content://com.android.calendar/time/" + epochEventStartTime);
95        Intent intent = new Intent(Intent.ACTION_VIEW);
96        intent.setData(uri);
97        intent.putExtra("VIEW", "DAY");
98        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
99        activity.startActivity(intent);
100    }
101
102    public static void deleteMessage(Activity activity, long messageId) {
103        Controller.getInstance(activity).deleteMessage(messageId, -1);
104        Utility.showToast(activity,
105                activity.getResources().getQuantityString(R.plurals.message_deleted_toast, 1));
106    }
107
108    public static void moveMessages(final Activity activity, final long newMailboxId,
109            final long[] messageIds) {
110        Controller.getInstance(activity).moveMessage(messageIds, newMailboxId);
111        EmailAsyncTask.runAsyncSerial(new Runnable() {
112            @Override
113            public void run() {
114                String mailboxName = Mailbox.getDisplayName(activity, newMailboxId);
115                if (mailboxName == null) {
116                    return; // Mailbox gone??
117                }
118                String message = activity.getResources().getQuantityString(
119                        R.plurals.message_moved_toast, messageIds.length, messageIds.length ,
120                        mailboxName);
121                Utility.showToast(activity, message);
122            }
123        });
124    }
125
126    /**
127     * If configured via debug flags, inhibit hardware graphics acceleration.  Must be called
128     * early in onCreate().
129     *
130     * NOTE: Currently, this only works if HW accel is *not* enabled via the manifest.
131     */
132    public static void debugSetWindowFlags(Activity activity) {
133        if (Email.sDebugInhibitGraphicsAcceleration) {
134            // Clear the flag in the activity's window
135            activity.getWindow().setFlags(0,
136                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
137        } else {
138            // Set the flag in the activity's window
139            activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
140                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
141        }
142    }
143
144    public static void updateRefreshMenuIcon(MenuItem item, boolean show, boolean animate) {
145        if (show) {
146            item.setVisible(true);
147            if (animate) {
148                item.setActionView(R.layout.action_bar_indeterminate_progress);
149            } else {
150                item.setActionView(null);
151            }
152        } else {
153            item.setVisible(false);
154        }
155    }
156}
157