package com.fsck.k9;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Debug;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.format.Time;
import android.util.Log;
import com.fsck.k9.Account.SortType;
import com.fsck.k9.activity.MessageCompose;
import com.fsck.k9.activity.UpgradeDatabases;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.store.LocalStore;
import com.fsck.k9.provider.UnreadWidgetProvider;
import com.fsck.k9.service.BootReceiver;
import com.fsck.k9.service.MailService;
import com.fsck.k9.service.ShutdownReceiver;
import com.fsck.k9.service.StorageGoneReceiver;
public class K9 extends Application {
public static final int THEME_LIGHT = 0;
public static final int THEME_DARK = 1;
/**
* Components that are interested in knowing when the K9 instance is
* available and ready (Android invokes Application.onCreate() after other
* components') should implement this interface and register using
* {@link K9#registerApplicationAware(ApplicationAware)}.
*/
public static interface ApplicationAware {
/**
* Called when the Application instance is available and ready.
*
* @param application
* The application instance. Never null
.
* @throws Exception
*/
void initializeComponent(K9 application);
}
public static Application app = null;
public static File tempDirectory;
public static final String LOG_TAG = "k9";
/**
* Name of the {@link SharedPreferences} file used to store the last known version of the
* accounts' databases.
*
*
* See {@link UpgradeDatabases} for a detailed explanation of the database upgrade process. *
*/ private static final String DATABASE_VERSION_CACHE = "database_version_cache"; /** * Key used to store the last known database version of the accounts' databases. * * @see #DATABASE_VERSION_CACHE */ private static final String KEY_LAST_ACCOUNT_DATABASE_VERSION = "last_account_database_version"; /** * Components that are interested in knowing when the K9 instance is * available and ready. * * @see ApplicationAware */ private static List
* If the stored version matches {@link LocalStore#DB_VERSION} we know that the databases are
* up to date.
* Using {@code SharedPreferences} should be a lot faster than opening all SQLite databases to
* get the current database version.
*
* See {@link UpgradeDatabases} for a detailed explanation of the database upgrade process. *
* * @see #areDatabasesUpToDate() */ public void checkCachedDatabaseVersion() { sDatabaseVersionCache = getSharedPreferences(DATABASE_VERSION_CACHE, MODE_PRIVATE); int cachedVersion = sDatabaseVersionCache.getInt(KEY_LAST_ACCOUNT_DATABASE_VERSION, 0); if (cachedVersion >= LocalStore.DB_VERSION) { K9.setDatabasesUpToDate(false); } } public static void loadPrefs(Preferences prefs) { SharedPreferences sprefs = prefs.getPreferences(); DEBUG = sprefs.getBoolean("enableDebugLogging", false); if (!DEBUG && Debug.isDebuggerConnected()) { // If the debugger is attached, we're probably (surprise surprise) debugging something. DEBUG = true; Log.i(K9.LOG_TAG, "Debugger attached; enabling debug logging."); } DEBUG_SENSITIVE = sprefs.getBoolean("enableSensitiveLogging", false); mAnimations = sprefs.getBoolean("animations", true); mGesturesEnabled = sprefs.getBoolean("gesturesEnabled", false); mUseVolumeKeysForNavigation = sprefs.getBoolean("useVolumeKeysForNavigation", false); mUseVolumeKeysForListNavigation = sprefs.getBoolean("useVolumeKeysForListNavigation", false); mStartIntegratedInbox = sprefs.getBoolean("startIntegratedInbox", false); mMeasureAccounts = sprefs.getBoolean("measureAccounts", true); mCountSearchMessages = sprefs.getBoolean("countSearchMessages", true); mHideSpecialAccounts = sprefs.getBoolean("hideSpecialAccounts", false); mMessageListSenderAboveSubject = sprefs.getBoolean("messageListSenderAboveSubject", false); mMessageListCheckboxes = sprefs.getBoolean("messageListCheckboxes", false); mMessageListPreviewLines = sprefs.getInt("messageListPreviewLines", 2); mMobileOptimizedLayout = sprefs.getBoolean("mobileOptimizedLayout", false); mQuietTimeEnabled = sprefs.getBoolean("quietTimeEnabled", false); mQuietTimeStarts = sprefs.getString("quietTimeStarts", "21:00"); mQuietTimeEnds = sprefs.getString("quietTimeEnds", "7:00"); mShowCorrespondentNames = sprefs.getBoolean("showCorrespondentNames", true); mShowContactName = sprefs.getBoolean("showContactName", false); mChangeContactNameColor = sprefs.getBoolean("changeRegisteredNameColor", false); mContactNameColor = sprefs.getInt("registeredNameColor", 0xff00008f); mMessageViewFixedWidthFont = sprefs.getBoolean("messageViewFixedWidthFont", false); mMessageViewReturnToList = sprefs.getBoolean("messageViewReturnToList", false); mMessageViewShowNext = sprefs.getBoolean("messageViewShowNext", false); mWrapFolderNames = sprefs.getBoolean("wrapFolderNames", false); mBatchButtonsMarkRead = sprefs.getBoolean("batchButtonsMarkRead", true); mBatchButtonsDelete = sprefs.getBoolean("batchButtonsDelete", true); mBatchButtonsArchive = sprefs.getBoolean("batchButtonsArchive", true); mBatchButtonsMove = sprefs.getBoolean("batchButtonsMove", true); mBatchButtonsFlag = sprefs.getBoolean("batchButtonsFlag", true); mBatchButtonsUnselect = sprefs.getBoolean("batchButtonsUnselect", true); useGalleryBugWorkaround = sprefs.getBoolean("useGalleryBugWorkaround", K9.isGalleryBuggy()); mConfirmDelete = sprefs.getBoolean("confirmDelete", false); mConfirmDeleteStarred = sprefs.getBoolean("confirmDeleteStarred", false); mConfirmSpam = sprefs.getBoolean("confirmSpam", false); mConfirmDeleteFromNotification = sprefs.getBoolean("confirmDeleteFromNotification", true); try { String value = sprefs.getString("sortTypeEnum", Account.DEFAULT_SORT_TYPE.name()); mSortType = SortType.valueOf(value); } catch (Exception e) { mSortType = Account.DEFAULT_SORT_TYPE; } boolean sortAscending = sprefs.getBoolean("sortAscending", Account.DEFAULT_SORT_ASCENDING); mSortAscending.put(mSortType, sortAscending); String notificationHideSubject = sprefs.getString("notificationHideSubject", null); if (notificationHideSubject == null) { // If the "notificationHideSubject" setting couldn't be found, the app was probably // updated. Look for the old "keyguardPrivacy" setting and map it to the new enum. sNotificationHideSubject = (sprefs.getBoolean("keyguardPrivacy", false)) ? NotificationHideSubject.WHEN_LOCKED : NotificationHideSubject.NEVER; } else { sNotificationHideSubject = NotificationHideSubject.valueOf(notificationHideSubject); } String notificationQuickDelete = sprefs.getString("notificationQuickDelete", null); if (notificationQuickDelete != null) { sNotificationQuickDelete = NotificationQuickDelete.valueOf(notificationQuickDelete); } String splitViewMode = sprefs.getString("splitViewMode", null); if (splitViewMode != null) { sSplitViewMode = SplitViewMode.valueOf(splitViewMode); } mAttachmentDefaultPath = sprefs.getString("attachmentdefaultpath", Environment.getExternalStorageDirectory().toString()); sUseBackgroundAsUnreadIndicator = sprefs.getBoolean("useBackgroundAsUnreadIndicator", true); sThreadedViewEnabled = sprefs.getBoolean("threadedView", true); fontSizes.load(sprefs); try { setBackgroundOps(BACKGROUND_OPS.valueOf(sprefs.getString("backgroundOperations", "WHEN_CHECKED"))); } catch (Exception e) { setBackgroundOps(BACKGROUND_OPS.WHEN_CHECKED); } K9.setK9Language(sprefs.getString("language", "")); int theme = sprefs.getInt("theme", THEME_LIGHT); // We used to save the resource ID of the theme. So convert that to the new format if // necessary. if (theme == THEME_DARK || theme == android.R.style.Theme) { theme = THEME_DARK; } else { theme = THEME_LIGHT; } K9.setK9Theme(theme); K9.setK9MessageViewTheme(sprefs.getInt("messageViewTheme", THEME_LIGHT)); } private void maybeSetupStrictMode() { if (!K9.DEVELOPER_MODE) return; try { Class> strictMode = Class.forName("android.os.StrictMode"); Method enableDefaults = strictMode.getMethod("enableDefaults"); enableDefaults.invoke(strictMode); } catch (Exception e) { // Discard , as it means we're not running on a device with strict mode Log.v(K9.LOG_TAG, "Failed to turn on strict mode", e); } } /** * since Android invokes Application.onCreate() only after invoking all * other components' onCreate(), here is a way to notify interested * component that the application is available and ready */ protected void notifyObservers() { for (final ApplicationAware aware : observers) { if (K9.DEBUG) { Log.v(K9.LOG_TAG, "Initializing observer: " + aware); } try { aware.initializeComponent(this); } catch (Exception e) { Log.w(K9.LOG_TAG, "Failure when notifying " + aware, e); } } } /** * Register a component to be notified when the {@link K9} instance is ready. * * @param component * Nevernull
.
*/
public static void registerApplicationAware(final ApplicationAware component) {
if (!observers.contains(component)) {
observers.add(component);
}
}
public static String getK9Language() {
return language;
}
public static void setK9Language(String nlanguage) {
language = nlanguage;
}
public static int getK9ThemeResourceId(int theme) {
return (theme == THEME_LIGHT) ? R.style.Theme_K9_Light : R.style.Theme_K9_Dark;
}
public static int getK9ThemeResourceId() {
return getK9ThemeResourceId(theme);
}
public static int getK9MessageViewTheme() {
return messageViewTheme;
}
public static int getK9Theme() {
return theme;
}
public static void setK9Theme(int ntheme) {
theme = ntheme;
}
public static void setK9MessageViewTheme(int nMessageViewTheme) {
messageViewTheme = nMessageViewTheme;
}
public static BACKGROUND_OPS getBackgroundOps() {
return backgroundOps;
}
public static boolean setBackgroundOps(BACKGROUND_OPS backgroundOps) {
BACKGROUND_OPS oldBackgroundOps = K9.backgroundOps;
K9.backgroundOps = backgroundOps;
return backgroundOps != oldBackgroundOps;
}
public static boolean setBackgroundOps(String nbackgroundOps) {
return setBackgroundOps(BACKGROUND_OPS.valueOf(nbackgroundOps));
}
public static boolean gesturesEnabled() {
return mGesturesEnabled;
}
public static void setGesturesEnabled(boolean gestures) {
mGesturesEnabled = gestures;
}
public static boolean useVolumeKeysForNavigationEnabled() {
return mUseVolumeKeysForNavigation;
}
public static void setUseVolumeKeysForNavigation(boolean volume) {
mUseVolumeKeysForNavigation = volume;
}
public static boolean useVolumeKeysForListNavigationEnabled() {
return mUseVolumeKeysForListNavigation;
}
public static void setUseVolumeKeysForListNavigation(boolean enabled) {
mUseVolumeKeysForListNavigation = enabled;
}
public static boolean mobileOptimizedLayout() {
return mMobileOptimizedLayout;
}
public static void setMobileOptimizedLayout(boolean mobileOptimizedLayout) {
mMobileOptimizedLayout = mobileOptimizedLayout;
}
public static boolean getQuietTimeEnabled() {
return mQuietTimeEnabled;
}
public static void setQuietTimeEnabled(boolean quietTimeEnabled) {
mQuietTimeEnabled = quietTimeEnabled;
}
public static String getQuietTimeStarts() {
return mQuietTimeStarts;
}
public static void setQuietTimeStarts(String quietTimeStarts) {
mQuietTimeStarts = quietTimeStarts;
}
public static String getQuietTimeEnds() {
return mQuietTimeEnds;
}
public static void setQuietTimeEnds(String quietTimeEnds) {
mQuietTimeEnds = quietTimeEnds;
}
public static boolean isQuietTime() {
if (!mQuietTimeEnabled) {
return false;
}
Time time = new Time();
time.setToNow();
Integer startHour = Integer.parseInt(mQuietTimeStarts.split(":")[0]);
Integer startMinute = Integer.parseInt(mQuietTimeStarts.split(":")[1]);
Integer endHour = Integer.parseInt(mQuietTimeEnds.split(":")[0]);
Integer endMinute = Integer.parseInt(mQuietTimeEnds.split(":")[1]);
Integer now = (time.hour * 60) + time.minute;
Integer quietStarts = startHour * 60 + startMinute;
Integer quietEnds = endHour * 60 + endMinute;
// If start and end times are the same, we're never quiet
if (quietStarts.equals(quietEnds)) {
return false;
}
// 21:00 - 05:00 means we want to be quiet if it's after 9 or before 5
if (quietStarts > quietEnds) {
// if it's 22:00 or 03:00 but not 8:00
if (now >= quietStarts || now <= quietEnds) {
return true;
}
}
// 01:00 - 05:00
else {
// if it' 2:00 or 4:00 but not 8:00 or 0:00
if (now >= quietStarts && now <= quietEnds) {
return true;
}
}
return false;
}
public static boolean startIntegratedInbox() {
return mStartIntegratedInbox;
}
public static void setStartIntegratedInbox(boolean startIntegratedInbox) {
mStartIntegratedInbox = startIntegratedInbox;
}
public static boolean showAnimations() {
return mAnimations;
}
public static void setAnimations(boolean animations) {
mAnimations = animations;
}
public static int messageListPreviewLines() {
return mMessageListPreviewLines;
}
public static void setMessageListPreviewLines(int lines) {
mMessageListPreviewLines = lines;
}
public static boolean messageListCheckboxes() {
return mMessageListCheckboxes;
}
public static void setMessageListCheckboxes(boolean checkboxes) {
mMessageListCheckboxes = checkboxes;
}
public static boolean showCorrespondentNames() {
return mShowCorrespondentNames;
}
public static boolean messageListSenderAboveSubject() {
return mMessageListSenderAboveSubject;
}
public static void setMessageListSenderAboveSubject(boolean sender) {
mMessageListSenderAboveSubject = sender;
}
public static void setShowCorrespondentNames(boolean showCorrespondentNames) {
mShowCorrespondentNames = showCorrespondentNames;
}
public static boolean showContactName() {
return mShowContactName;
}
public static void setShowContactName(boolean showContactName) {
mShowContactName = showContactName;
}
public static boolean changeContactNameColor() {
return mChangeContactNameColor;
}
public static void setChangeContactNameColor(boolean changeContactNameColor) {
mChangeContactNameColor = changeContactNameColor;
}
public static int getContactNameColor() {
return mContactNameColor;
}
public static void setContactNameColor(int contactNameColor) {
mContactNameColor = contactNameColor;
}
public static boolean messageViewFixedWidthFont() {
return mMessageViewFixedWidthFont;
}
public static void setMessageViewFixedWidthFont(boolean fixed) {
mMessageViewFixedWidthFont = fixed;
}
public static boolean messageViewReturnToList() {
return mMessageViewReturnToList;
}
public static void setMessageViewReturnToList(boolean messageViewReturnToList) {
mMessageViewReturnToList = messageViewReturnToList;
}
public static boolean messageViewShowNext() {
return mMessageViewShowNext;
}
public static void setMessageViewShowNext(boolean messageViewShowNext) {
mMessageViewShowNext = messageViewShowNext;
}
public static Method getMethod(Class> classObject, String methodName) {
try {
return classObject.getMethod(methodName, boolean.class);
} catch (NoSuchMethodException e) {
Log.i(K9.LOG_TAG, "Can't get method " +
classObject.toString() + "." + methodName);
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Error while using reflection to get method " +
classObject.toString() + "." + methodName, e);
}
return null;
}
public static FontSizes getFontSizes() {
return fontSizes;
}
public static boolean measureAccounts() {
return mMeasureAccounts;
}
public static void setMeasureAccounts(boolean measureAccounts) {
mMeasureAccounts = measureAccounts;
}
public static boolean countSearchMessages() {
return mCountSearchMessages;
}
public static void setCountSearchMessages(boolean countSearchMessages) {
mCountSearchMessages = countSearchMessages;
}
public static boolean isHideSpecialAccounts() {
return mHideSpecialAccounts;
}
public static void setHideSpecialAccounts(boolean hideSpecialAccounts) {
mHideSpecialAccounts = hideSpecialAccounts;
}
public static boolean useGalleryBugWorkaround() {
return useGalleryBugWorkaround;
}
public static void setUseGalleryBugWorkaround(boolean useGalleryBugWorkaround) {
K9.useGalleryBugWorkaround = useGalleryBugWorkaround;
}
public static boolean isGalleryBuggy() {
return galleryBuggy;
}
public static boolean confirmDelete() {
return mConfirmDelete;
}
public static void setConfirmDelete(final boolean confirm) {
mConfirmDelete = confirm;
}
public static boolean confirmDeleteStarred() {
return mConfirmDeleteStarred;
}
public static void setConfirmDeleteStarred(final boolean confirm) {
mConfirmDeleteStarred = confirm;
}
public static boolean confirmSpam() {
return mConfirmSpam;
}
public static void setConfirmSpam(final boolean confirm) {
mConfirmSpam = confirm;
}
public static boolean confirmDeleteFromNotification() {
return mConfirmDeleteFromNotification;
}
public static void setConfirmDeleteFromNotification(final boolean confirm) {
mConfirmDeleteFromNotification = confirm;
}
public static NotificationHideSubject getNotificationHideSubject() {
return sNotificationHideSubject;
}
public static void setNotificationHideSubject(final NotificationHideSubject mode) {
sNotificationHideSubject = mode;
}
public static NotificationQuickDelete getNotificationQuickDeleteBehaviour() {
return sNotificationQuickDelete;
}
public static void setNotificationQuickDeleteBehaviour(final NotificationQuickDelete mode) {
sNotificationQuickDelete = mode;
}
public static boolean batchButtonsMarkRead() {
return mBatchButtonsMarkRead;
}
public static void setBatchButtonsMarkRead(final boolean state) {
mBatchButtonsMarkRead = state;
}
public static boolean batchButtonsDelete() {
return mBatchButtonsDelete;
}
public static void setBatchButtonsDelete(final boolean state) {
mBatchButtonsDelete = state;
}
public static boolean batchButtonsArchive() {
return mBatchButtonsArchive;
}
public static void setBatchButtonsArchive(final boolean state) {
mBatchButtonsArchive = state;
}
public static boolean batchButtonsMove() {
return mBatchButtonsMove;
}
public static void setBatchButtonsMove(final boolean state) {
mBatchButtonsMove = state;
}
public static boolean batchButtonsFlag() {
return mBatchButtonsFlag;
}
public static void setBatchButtonsFlag(final boolean state) {
mBatchButtonsFlag = state;
}
public static boolean batchButtonsUnselect() {
return mBatchButtonsUnselect;
}
public static void setBatchButtonsUnselect(final boolean state) {
mBatchButtonsUnselect = state;
}
/**
* Check if this system contains a buggy Gallery 3D package.
*
* We have to work around the fact that those Gallery versions won't show
* any images or videos when the pick intent is used with a MIME type other
* than image/* or video/*. See issue 1186.
*
* @return true, if a buggy Gallery 3D package was found. False, otherwise.
*/
private boolean checkForBuggyGallery() {
try {
PackageInfo pi = getPackageManager().getPackageInfo("com.cooliris.media", 0);
return (pi.versionCode == 30682);
} catch (NameNotFoundException e) {
return false;
}
}
public static boolean wrapFolderNames() {
return mWrapFolderNames;
}
public static void setWrapFolderNames(final boolean state) {
mWrapFolderNames = state;
}
public static String getAttachmentDefaultPath() {
return mAttachmentDefaultPath;
}
public static void setAttachmentDefaultPath(String attachmentDefaultPath) {
K9.mAttachmentDefaultPath = attachmentDefaultPath;
}
public static synchronized SortType getSortType() {
return mSortType;
}
public static synchronized void setSortType(SortType sortType) {
mSortType = sortType;
}
public static synchronized boolean isSortAscending(SortType sortType) {
if (mSortAscending.get(sortType) == null) {
mSortAscending.put(sortType, sortType.isDefaultAscending());
}
return mSortAscending.get(sortType);
}
public static synchronized void setSortAscending(SortType sortType, boolean sortAscending) {
mSortAscending.put(sortType, sortAscending);
}
public static synchronized boolean useBackgroundAsUnreadIndicator() {
return sUseBackgroundAsUnreadIndicator;
}
public static synchronized void setUseBackgroundAsUnreadIndicator(boolean enabled) {
sUseBackgroundAsUnreadIndicator = enabled;
}
public static synchronized boolean isThreadedViewEnabled() {
return sThreadedViewEnabled;
}
public static synchronized void setThreadedViewEnabled(boolean enable) {
sThreadedViewEnabled = enable;
}
public static synchronized SplitViewMode getSplitViewMode() {
return sSplitViewMode;
}
public static synchronized void setSplitViewMode(SplitViewMode mode) {
sSplitViewMode = mode;
}
/**
* Check if we already know whether all databases are using the current database schema.
*
* * This method is only used for optimizations. If it returns {@code true} we can be certain that * getting a {@link LocalStore} instance won't trigger a schema upgrade. *
* * @return {@code true}, if we know that all databases are using the current database schema. * {@code false}, otherwise. */ public static synchronized boolean areDatabasesUpToDate() { return sDatabasesUpToDate; } /** * Remember that all account databases are using the most recent database schema. * * @param save * Whether or not to write the current database version to the * {@code SharedPreferences} {@link #DATABASE_VERSION_CACHE}. * * @see #areDatabasesUpToDate() */ public static synchronized void setDatabasesUpToDate(boolean save) { sDatabasesUpToDate = true; if (save) { Editor editor = sDatabaseVersionCache.edit(); editor.putInt(KEY_LAST_ACCOUNT_DATABASE_VERSION, LocalStore.DB_VERSION); editor.commit(); } } }