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