ActivityHelper.java revision 8112732376d4cc033ee515a6531852ef42266929
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 EmailActivity}, {@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    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(Context context, long messageId) {
97        Controller.getInstance(context).deleteMessage(messageId, -1);
98        Utility.showToast(context,
99                context.getResources().getQuantityString(R.plurals.message_deleted_toast, 1));
100    }
101
102    public static void moveMessages(final Context context, final long newMailboxId,
103            final long[] messageIds) {
104        Controller.getInstance(context).moveMessages(messageIds, newMailboxId);
105        EmailAsyncTask.runAsyncSerial(new Runnable() {
106            @Override
107            public void run() {
108                String mailboxName = Mailbox.getDisplayName(context, newMailboxId);
109                if (mailboxName == null) {
110                    return; // Mailbox gone??
111                }
112                String message = context.getResources().getQuantityString(
113                        R.plurals.message_moved_toast, messageIds.length, messageIds.length ,
114                        mailboxName);
115                Utility.showToast(context, message);
116            }
117        });
118    }
119
120    /**
121     * If configured via debug flags, inhibit hardware graphics acceleration.  Must be called
122     * early in onCreate().
123     *
124     * NOTE: Currently, this only works if HW accel is *not* enabled via the manifest.
125     */
126    public static void debugSetWindowFlags(Activity activity) {
127        if (Email.sDebugInhibitGraphicsAcceleration) {
128            // Clear the flag in the activity's window
129            activity.getWindow().setFlags(0,
130                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
131        } else {
132            // Set the flag in the activity's window
133            activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
134                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
135        }
136    }
137
138    public static void updateRefreshMenuIcon(MenuItem item, boolean show, boolean animate) {
139        if (show) {
140            item.setVisible(true);
141            if (animate) {
142                item.setActionView(R.layout.action_bar_indeterminate_progress);
143            } else {
144                item.setActionView(null);
145            }
146        } else {
147            item.setVisible(false);
148        }
149    }
150}
151