Email.java revision faed6178b1858f0e6df388a409d14fb3f94afefa
1/*
2 * Copyright (C) 2008 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;
18
19import com.android.email.activity.AccountShortcutPicker;
20import com.android.email.activity.Debug;
21import com.android.email.activity.MessageCompose;
22import com.android.email.mail.internet.BinaryTempFileBody;
23import com.android.email.provider.EmailContent;
24import com.android.email.service.BootReceiver;
25import com.android.email.service.MailService;
26import com.android.exchange.Eas;
27
28import android.app.Application;
29import android.content.ComponentName;
30import android.content.Context;
31import android.content.pm.PackageManager;
32import android.database.Cursor;
33import android.text.format.DateUtils;
34import android.util.Log;
35
36import java.io.File;
37import java.util.HashMap;
38
39public class Email extends Application {
40    public static final String LOG_TAG = "Email";
41
42    public static File tempDirectory;
43
44    /**
45     * If this is enabled there will be additional logging information sent to
46     * Log.d, including protocol dumps.
47     *
48     * This should only be used for logs that are useful for debbuging user problems,
49     * not for internal/development logs.
50     *
51     * This can be enabled by typing "debug" in the AccountFolderList activity.
52     * Changing the value to 'true' here will likely have no effect at all!
53     *
54     * TODO: rename this to sUserDebug, and rename LOGD below to DEBUG.
55     */
56    public static boolean DEBUG = false;
57
58    /**
59     * If this is enabled than logging that normally hides sensitive information
60     * like passwords will show that information.
61     */
62    public static boolean DEBUG_SENSITIVE = false;
63
64    /**
65     * Set this to 'true' to enable as much Email logging as possible.
66     * Do not check-in with it set to 'true'!
67     */
68    public static final boolean LOGD = false;
69
70    /**
71     * The MIME type(s) of attachments we're willing to send via attachments.
72     *
73     * Any attachments may be added via Intents with Intent.ACTION_SEND or ACTION_SEND_MULTIPLE.
74     */
75    public static final String[] ACCEPTABLE_ATTACHMENT_SEND_INTENT_TYPES = new String[] {
76        "*/*",
77    };
78
79    /**
80     * The MIME type(s) of attachments we're willing to send from the internal UI.
81     *
82     * NOTE:  At the moment it is not possible to open a chooser with a list of filter types, so
83     * the chooser is only opened with the first item in the list.
84     */
85    public static final String[] ACCEPTABLE_ATTACHMENT_SEND_UI_TYPES = new String[] {
86        "image/*",
87        "video/*",
88    };
89
90    /**
91     * The MIME type(s) of attachments we're willing to view.
92     */
93    public static final String[] ACCEPTABLE_ATTACHMENT_VIEW_TYPES = new String[] {
94        "*/*",
95    };
96
97    /**
98     * The MIME type(s) of attachments we're not willing to view.
99     */
100    public static final String[] UNACCEPTABLE_ATTACHMENT_VIEW_TYPES = new String[] {
101    };
102
103    /**
104     * The MIME type(s) of attachments we're willing to download to SD.
105     */
106    public static final String[] ACCEPTABLE_ATTACHMENT_DOWNLOAD_TYPES = new String[] {
107        "image/*",
108    };
109
110    /**
111     * The MIME type(s) of attachments we're not willing to download to SD.
112     */
113    public static final String[] UNACCEPTABLE_ATTACHMENT_DOWNLOAD_TYPES = new String[] {
114    };
115
116    /**
117     * Specifies how many messages will be shown in a folder by default. This number is set
118     * on each new folder and can be incremented with "Load more messages..." by the
119     * VISIBLE_LIMIT_INCREMENT
120     */
121    public static final int VISIBLE_LIMIT_DEFAULT = 25;
122
123    /**
124     * Number of additional messages to load when a user selects "Load more messages..."
125     */
126    public static final int VISIBLE_LIMIT_INCREMENT = 25;
127
128    /**
129     * The maximum size of an attachment we're willing to download (either View or Save)
130     * Attachments that are base64 encoded (most) will be about 1.375x their actual size
131     * so we should probably factor that in. A 5MB attachment will generally be around
132     * 6.8MB downloaded but only 5MB saved.
133     */
134    public static final int MAX_ATTACHMENT_DOWNLOAD_SIZE = (5 * 1024 * 1024);
135
136    /**
137     * The maximum size of an attachment we're willing to upload (measured as stored on disk).
138     * Attachments that are base64 encoded (most) will be about 1.375x their actual size
139     * so we should probably factor that in. A 5MB attachment will generally be around
140     * 6.8MB uploaded.
141     */
142    public static final int MAX_ATTACHMENT_UPLOAD_SIZE = (5 * 1024 * 1024);
143
144    private static HashMap<Long, Long> sMailboxSyncTimes = new HashMap<Long, Long>();
145    private static final long UPDATE_INTERVAL = 5 * DateUtils.MINUTE_IN_MILLIS;
146
147    public static final String EXCHANGE_ACCOUNT_MANAGER_TYPE = "com.android.exchange";
148
149    /**
150     * Called throughout the application when the number of accounts has changed. This method
151     * enables or disables the Compose activity, the boot receiver and the service based on
152     * whether any accounts are configured.   Returns true if there are any accounts configured.
153     */
154    public static boolean setServicesEnabled(Context context) {
155        Cursor c = null;
156        try {
157            c = context.getContentResolver().query(
158                    EmailContent.Account.CONTENT_URI,
159                    EmailContent.Account.ID_PROJECTION,
160                    null, null, null);
161            boolean enable = c.getCount() > 0;
162            setServicesEnabled(context, enable);
163            return enable;
164        } finally {
165            if (c != null) {
166                c.close();
167            }
168        }
169    }
170
171    public static void setServicesEnabled(Context context, boolean enabled) {
172        PackageManager pm = context.getPackageManager();
173        if (!enabled && pm.getComponentEnabledSetting(new ComponentName(context, MailService.class)) ==
174                PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
175            /*
176             * If no accounts now exist but the service is still enabled we're about to disable it
177             * so we'll reschedule to kill off any existing alarms.
178             */
179            MailService.actionReschedule(context);
180        }
181        pm.setComponentEnabledSetting(
182                new ComponentName(context, MessageCompose.class),
183                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
184                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
185                PackageManager.DONT_KILL_APP);
186        pm.setComponentEnabledSetting(
187                new ComponentName(context, AccountShortcutPicker.class),
188                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
189                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
190                PackageManager.DONT_KILL_APP);
191        pm.setComponentEnabledSetting(
192                new ComponentName(context, BootReceiver.class),
193                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
194                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
195                PackageManager.DONT_KILL_APP);
196        pm.setComponentEnabledSetting(
197                new ComponentName(context, MailService.class),
198                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
199                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
200                PackageManager.DONT_KILL_APP);
201        if (enabled && pm.getComponentEnabledSetting(new ComponentName(context, MailService.class)) ==
202                PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
203            /*
204             * And now if accounts do exist then we've just enabled the service and we want to
205             * schedule alarms for the new accounts.
206             */
207            MailService.actionReschedule(context);
208        }
209    }
210
211    @Override
212    public void onCreate() {
213        super.onCreate();
214        Preferences prefs = Preferences.getPreferences(this);
215        DEBUG = prefs.getEnableDebugLogging();
216        DEBUG_SENSITIVE = prefs.getEnableSensitiveLogging();
217
218        // Reset all accounts to default visible window
219        Controller.getInstance(this).resetVisibleLimits();
220
221        /*
222         * We have to give MimeMessage a temp directory because File.createTempFile(String, String)
223         * doesn't work in Android and MimeMessage does not have access to a Context.
224         */
225        BinaryTempFileBody.setTempDirectory(getCacheDir());
226
227        // Enable logging in the EAS service, so it starts up as early as possible.
228        Debug.updateLoggingFlags(this);
229    }
230
231    /**
232     * Internal, utility method for logging.
233     * The calls to log() must be guarded with "if (Email.LOGD)" for performance reasons.
234     */
235    public static void log(String message) {
236        Log.d(LOG_TAG, message);
237    }
238
239    /**
240     * Update the time when the mailbox is refreshed
241     * @param mailboxId mailbox which need to be updated
242     */
243    public static void updateMailboxRefreshTime(long mailboxId) {
244        synchronized (sMailboxSyncTimes) {
245            sMailboxSyncTimes.put(mailboxId, System.currentTimeMillis());
246        }
247    }
248
249    /**
250     * Check if the mailbox is need to be refreshed
251     * @param mailboxId mailbox checked the need of refreshing
252     * @return the need of refreshing
253     */
254    public static boolean mailboxRequiresRefresh(long mailboxId) {
255        synchronized (sMailboxSyncTimes) {
256            return
257                !sMailboxSyncTimes.containsKey(mailboxId)
258                || (System.currentTimeMillis() - sMailboxSyncTimes.get(mailboxId)
259                        > UPDATE_INTERVAL);
260        }
261    }
262}
263