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