replicant-packages_apps_Email/src/com/android/email/Email.java

369 lines
14 KiB
Java

/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email;
import com.android.email.activity.AccountShortcutPicker;
import com.android.email.activity.MessageCompose;
import com.android.email.service.AttachmentDownloadService;
import com.android.email.service.MailService;
import com.android.emailcommon.provider.EmailContent;
import com.android.exchange.Eas;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.util.Log;
import java.io.File;
import java.util.HashMap;
public class Email extends Application {
public static final String LOG_TAG = "Email";
/**
* If this is enabled there will be additional logging information sent to
* Log.d, including protocol dumps.
*
* This should only be used for logs that are useful for debbuging user problems,
* not for internal/development logs.
*
* This can be enabled by typing "debug" in the AccountFolderList activity.
* Changing the value to 'true' here will likely have no effect at all!
*
* TODO: rename this to sUserDebug, and rename LOGD below to DEBUG.
*/
public static boolean DEBUG = false;
/**
* If true, logging regarding activity/fragment lifecycle will be enabled.
* Do not check in as "true".
*/
public static final boolean DEBUG_LIFECYCLE = false;
/**
* If this is enabled then logging that normally hides sensitive information
* like passwords will show that information.
*/
public static final boolean DEBUG_SENSITIVE = false;
/**
* Set this to 'true' to enable as much Email logging as possible.
* Do not check-in with it set to 'true'!
*/
public static final boolean LOGD = false;
/**
* If true, inhibit hardware graphics acceleration in UI (for a/b testing)
*/
public static boolean sDebugInhibitGraphicsAcceleration = false;
/**
* The MIME type(s) of attachments we're willing to send via attachments.
*
* Any attachments may be added via Intents with Intent.ACTION_SEND or ACTION_SEND_MULTIPLE.
*/
public static final String[] ACCEPTABLE_ATTACHMENT_SEND_INTENT_TYPES = new String[] {
"*/*",
};
/**
* The MIME type(s) of attachments we're willing to send from the internal UI.
*
* NOTE: At the moment it is not possible to open a chooser with a list of filter types, so
* the chooser is only opened with the first item in the list.
*/
public static final String[] ACCEPTABLE_ATTACHMENT_SEND_UI_TYPES = new String[] {
"image/*",
"video/*",
};
/**
* The MIME type(s) of attachments we're willing to view.
*/
public static final String[] ACCEPTABLE_ATTACHMENT_VIEW_TYPES = new String[] {
"*/*",
};
/**
* The MIME type(s) of attachments we're not willing to view.
*/
public static final String[] UNACCEPTABLE_ATTACHMENT_VIEW_TYPES = new String[] {
};
/**
* The MIME type(s) of attachments we're willing to download to SD.
*/
public static final String[] ACCEPTABLE_ATTACHMENT_DOWNLOAD_TYPES = new String[] {
"*/*",
};
/**
* The MIME type(s) of attachments we're not willing to download to SD.
*/
public static final String[] UNACCEPTABLE_ATTACHMENT_DOWNLOAD_TYPES = new String[] {
};
/**
* Filename extensions of attachments we're never willing to download (potential malware).
* Entries in this list are compared to the end of the lower-cased filename, so they must
* be lower case, and should not include a "."
*/
public static final String[] UNACCEPTABLE_ATTACHMENT_EXTENSIONS = new String[] {
// File types that contain malware
"ade", "adp", "bat", "chm", "cmd", "com", "cpl", "dll", "exe",
"hta", "ins", "isp", "jse", "lib", "mde", "msc", "msp",
"mst", "pif", "scr", "sct", "shb", "sys", "vb", "vbe",
"vbs", "vxd", "wsc", "wsf", "wsh",
// File types of common compression/container formats (again, to avoid malware)
"zip", "gz", "z", "tar", "tgz", "bz2",
};
/**
* Filename extensions of attachments that can be installed.
* Entries in this list are compared to the end of the lower-cased filename, so they must
* be lower case, and should not include a "."
*/
public static final String[] INSTALLABLE_ATTACHMENT_EXTENSIONS = new String[] {
"apk",
};
/**
* Specifies how many messages will be shown in a folder by default. This number is set
* on each new folder and can be incremented with "Load more messages..." by the
* VISIBLE_LIMIT_INCREMENT
*/
public static final int VISIBLE_LIMIT_DEFAULT = 25;
/**
* Number of additional messages to load when a user selects "Load more messages..."
*/
public static final int VISIBLE_LIMIT_INCREMENT = 25;
/**
* The maximum size of an attachment we're willing to download (either View or Save)
* Attachments that are base64 encoded (most) will be about 1.375x their actual size
* so we should probably factor that in. A 5MB attachment will generally be around
* 6.8MB downloaded but only 5MB saved.
*/
public static final int MAX_ATTACHMENT_DOWNLOAD_SIZE = (5 * 1024 * 1024);
/**
* The maximum size of an attachment we're willing to upload (measured as stored on disk).
* Attachments that are base64 encoded (most) will be about 1.375x their actual size
* so we should probably factor that in. A 5MB attachment will generally be around
* 6.8MB uploaded.
*/
public static final int MAX_ATTACHMENT_UPLOAD_SIZE = (5 * 1024 * 1024);
/**
* This is used to force stacked UI to return to the "welcome" screen any time we change
* the accounts list (e.g. deleting accounts in the Account Manager preferences.)
*/
private static boolean sAccountsChangedNotification = false;
public static final String EXCHANGE_ACCOUNT_MANAGER_TYPE = "com.android.exchange";
public static final String POP_IMAP_ACCOUNT_MANAGER_TYPE = "com.android.email";
private static File sTempDirectory;
private static String sMessageDecodeErrorString;
private static Thread sUiThread;
public static void setTempDirectory(Context context) {
sTempDirectory = context.getCacheDir();
}
public static File getTempDirectory() {
if (sTempDirectory == null) {
throw new RuntimeException(
"TempDirectory not set. " +
"If in a unit test, call Email.setTempDirectory(context) in setUp().");
}
return sTempDirectory;
}
/**
* Asynchronous version of {@link #setServicesEnabledSync(Context)}. Use when calling from
* UI thread (or lifecycle entry points.)
*
* @param context
*/
public static void setServicesEnabledAsync(final Context context) {
Utility.runAsync(new Runnable() {
@Override
public void run() {
setServicesEnabledSync(context);
}
});
}
/**
* Called throughout the application when the number of accounts has changed. This method
* enables or disables the Compose activity, the boot receiver and the service based on
* whether any accounts are configured.
*
* Blocking call - do not call from UI/lifecycle threads.
*
* @param context
* @return true if there are any accounts configured.
*/
public static boolean setServicesEnabledSync(Context context) {
Cursor c = null;
try {
c = context.getContentResolver().query(
EmailContent.Account.CONTENT_URI,
EmailContent.Account.ID_PROJECTION,
null, null, null);
boolean enable = c.getCount() > 0;
setServicesEnabled(context, enable);
return enable;
} finally {
if (c != null) {
c.close();
}
}
}
private static void setServicesEnabled(Context context, boolean enabled) {
PackageManager pm = context.getPackageManager();
if (!enabled && pm.getComponentEnabledSetting(
new ComponentName(context, MailService.class)) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
/*
* If no accounts now exist but the service is still enabled we're about to disable it
* so we'll reschedule to kill off any existing alarms.
*/
MailService.actionReschedule(context);
}
pm.setComponentEnabledSetting(
new ComponentName(context, MessageCompose.class),
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(context, AccountShortcutPicker.class),
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(context, MailService.class),
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(context, AttachmentDownloadService.class),
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
if (enabled && pm.getComponentEnabledSetting(
new ComponentName(context, MailService.class)) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
/*
* And now if accounts do exist then we've just enabled the service and we want to
* schedule alarms for the new accounts.
*/
MailService.actionReschedule(context);
}
// Start/stop the AttachmentDownloadService, depending on whether there are any accounts
Intent intent = new Intent(context, AttachmentDownloadService.class);
if (enabled) {
context.startService(intent);
} else {
context.stopService(intent);
}
}
@Override
public void onCreate() {
super.onCreate();
sUiThread = Thread.currentThread();
Preferences prefs = Preferences.getPreferences(this);
DEBUG = prefs.getEnableDebugLogging();
sDebugInhibitGraphicsAcceleration = prefs.getInhibitGraphicsAcceleration();
setTempDirectory(this);
// Tie MailRefreshManager to the Controller.
RefreshManager.getInstance(this);
// Reset all accounts to default visible window
Controller.getInstance(this).resetVisibleLimits();
// Enable logging in the EAS service, so it starts up as early as possible.
updateLoggingFlags(this);
// Get a helper string used deep inside message decoders (which don't have context)
sMessageDecodeErrorString = getString(R.string.message_decode_error);
}
/**
* Load enabled debug flags from the preferences and update the EAS debug flag.
*/
public static void updateLoggingFlags(Context context) {
//EXCHANGE-REMOVE-SECTION-START
Preferences prefs = Preferences.getPreferences(context);
int debugLogging = prefs.getEnableDebugLogging() ? Eas.DEBUG_BIT : 0;
int exchangeLogging = prefs.getEnableExchangeLogging() ? Eas.DEBUG_EXCHANGE_BIT : 0;
int fileLogging = prefs.getEnableExchangeFileLogging() ? Eas.DEBUG_FILE_BIT : 0;
int debugBits = debugLogging | exchangeLogging | fileLogging;
Controller.getInstance(context).serviceLogging(debugBits);
//EXCHANGE-REMOVE-SECTION-END
}
/**
* Internal, utility method for logging.
* The calls to log() must be guarded with "if (Email.LOGD)" for performance reasons.
*/
public static void log(String message) {
Log.d(LOG_TAG, message);
}
/**
* Called by the accounts reconciler to notify that accounts have changed, or by "Welcome"
* to clear the flag.
* @param setFlag true to set the notification flag, false to clear it
*/
public static synchronized void setNotifyUiAccountsChanged(boolean setFlag) {
sAccountsChangedNotification = setFlag;
}
/**
* Called from activity onResume() functions to check for an accounts-changed condition, at
* which point they should finish() and jump to the Welcome activity.
*/
public static synchronized boolean getNotifyUiAccountsChanged() {
return sAccountsChangedNotification;
}
public static void warnIfUiThread() {
if (Thread.currentThread().equals(sUiThread)) {
Log.w(Email.LOG_TAG, "Method called on the UI thread", new Exception("STACK TRACE"));
}
}
/**
* Retrieve a simple string that can be used when message decoders encounter bad data.
* This is provided here because the protocol decoders typically don't have mContext.
*/
public static String getMessageDecodeErrorString() {
return sMessageDecodeErrorString != null ? sMessageDecodeErrorString : "";
}
}