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