ActivityHelper.java revision bf3e65b667621f7f7ff82ab2945ae7911624a07b
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.Account;
23import com.android.emailcommon.provider.Mailbox;
24import com.android.emailcommon.utility.EmailAsyncTask;
25import com.android.emailcommon.utility.Utility;
26
27import android.app.Activity;
28import android.content.ActivityNotFoundException;
29import android.content.Context;
30import android.content.Intent;
31import android.net.Uri;
32import android.provider.Browser;
33import android.view.WindowManager;
34
35/**
36 * Various methods that are used by both 1-pane and 2-pane activities.
37 *
38 * Common code used by the activities and the fragments.
39 */
40public final class ActivityHelper {
41    private ActivityHelper() {
42    }
43
44    /**
45     * Open an URL in a message.
46     *
47     * This is intended to mirror the operation of the original
48     * (see android.webkit.CallbackProxy) with one addition of intent flags
49     * "FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET".  This improves behavior when sublaunching
50     * other apps via embedded URI's.
51     *
52     * We also use this hook to catch "mailto:" links and handle them locally.
53     *
54     * @param activity parent activity
55     * @param url URL to open
56     * @param senderAccountId if the URL is mailto:, we use this account as the sender.
57     *        TODO When MessageCompose implements the account selector, this won't be necessary.
58     *        Pass {@link Account#NO_ACCOUNT} to use the default account.
59     * @return true if the URI has successfully been opened.
60     */
61    public static boolean openUrlInMessage(Activity activity, String url, long senderAccountId) {
62        // hijack mailto: uri's and handle locally
63        if (url != null && url.toLowerCase().startsWith("mailto:")) {
64            return MessageCompose.actionCompose(activity, url, senderAccountId);
65        }
66
67        // Handle most uri's via intent launch
68        boolean result = false;
69        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
70        intent.addCategory(Intent.CATEGORY_BROWSABLE);
71        intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName());
72        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
73        try {
74            activity.startActivity(intent);
75            result = true;
76        } catch (ActivityNotFoundException ex) {
77            // No applications can handle it.  Ignore.
78        }
79        return result;
80    }
81
82    /**
83     * Open Calendar app with specific time
84     */
85    public static void openCalendar(Activity activity, long epochEventStartTime) {
86        Uri uri = Uri.parse("content://com.android.calendar/time/" + epochEventStartTime);
87        Intent intent = new Intent(Intent.ACTION_VIEW);
88        intent.setData(uri);
89        intent.putExtra("VIEW", "DAY");
90        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
91        activity.startActivity(intent);
92    }
93
94    public static void deleteMessage(Context context, long messageId) {
95        Controller.getInstance(context).deleteMessage(messageId);
96        Utility.showToast(context,
97                context.getResources().getQuantityString(R.plurals.message_deleted_toast, 1));
98    }
99
100    public static void moveMessages(final Context context, final long newMailboxId,
101            final long[] messageIds) {
102        Controller.getInstance(context).moveMessages(messageIds, newMailboxId);
103        EmailAsyncTask.runAsyncSerial(new Runnable() {
104            @Override
105            public void run() {
106                String mailboxName = Mailbox.getDisplayName(context, newMailboxId);
107                if (mailboxName == null) {
108                    return; // Mailbox gone??
109                }
110                String message = context.getResources().getQuantityString(
111                        R.plurals.message_moved_toast, messageIds.length, messageIds.length ,
112                        mailboxName);
113                Utility.showToast(context, message);
114            }
115        });
116    }
117
118    /**
119     * If configured via debug flags, inhibit hardware graphics acceleration.  Must be called
120     * early in onCreate().
121     *
122     * NOTE: Currently, this only works if HW accel is *not* enabled via the manifest.
123     */
124    public static void debugSetWindowFlags(Activity activity) {
125        // STOPSHIP - re-enable hw acceleration when b/4886133 is fixed.
126        /*
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}
139