2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2011-05-24 16:20:14 +00:00
|
|
|
import com.android.email.activity.ShortcutPicker;
|
2009-03-04 03:32:22 +00:00
|
|
|
import com.android.email.activity.MessageCompose;
|
2010-08-10 00:48:53 +00:00
|
|
|
import com.android.email.service.AttachmentDownloadService;
|
2009-03-04 03:32:22 +00:00
|
|
|
import com.android.email.service.MailService;
|
2011-05-11 00:22:08 +00:00
|
|
|
import com.android.email.service.NotificationService;
|
2011-06-01 23:20:43 +00:00
|
|
|
import com.android.email.widget.WidgetConfiguration;
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.Logging;
|
|
|
|
import com.android.emailcommon.TempDirectory;
|
2011-06-13 22:32:27 +00:00
|
|
|
import com.android.emailcommon.provider.Account;
|
2011-02-13 02:56:09 +00:00
|
|
|
import com.android.emailcommon.service.EmailServiceProxy;
|
2011-05-11 00:22:08 +00:00
|
|
|
import com.android.emailcommon.utility.EmailAsyncTask;
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.utility.Utility;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
2010-08-10 00:48:53 +00:00
|
|
|
import android.content.Intent;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2009-06-08 22:27:17 +00:00
|
|
|
import android.database.Cursor;
|
2009-07-28 17:41:59 +00:00
|
|
|
import android.util.Log;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
public class Email extends Application {
|
2011-05-12 23:01:30 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2009-07-16 21:09:15 +00:00
|
|
|
|
2011-02-13 02:56:09 +00:00
|
|
|
// Exchange debugging flags (passed to Exchange, when available, via EmailServiceProxy)
|
2011-05-12 23:01:30 +00:00
|
|
|
public static boolean DEBUG_EXCHANGE;
|
|
|
|
public static boolean DEBUG_EXCHANGE_VERBOSE;
|
|
|
|
public static boolean DEBUG_EXCHANGE_FILE;
|
2011-02-13 02:56:09 +00:00
|
|
|
|
2010-11-01 23:15:15 +00:00
|
|
|
/**
|
|
|
|
* If true, inhibit hardware graphics acceleration in UI (for a/b testing)
|
|
|
|
*/
|
|
|
|
public static boolean sDebugInhibitGraphicsAcceleration = false;
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2009-04-15 17:58:59 +00:00
|
|
|
public static final int VISIBLE_LIMIT_DEFAULT = 25;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
2009-04-15 17:58:59 +00:00
|
|
|
* Number of additional messages to load when a user selects "Load more messages..."
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
|
|
|
public static final int VISIBLE_LIMIT_INCREMENT = 25;
|
|
|
|
|
2010-04-14 16:28:04 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2011-01-25 02:06:53 +00:00
|
|
|
private static String sMessageDecodeErrorString;
|
|
|
|
|
2010-09-09 00:57:21 +00:00
|
|
|
private static Thread sUiThread;
|
|
|
|
|
2010-12-23 21:19:55 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2011-05-11 00:22:08 +00:00
|
|
|
EmailAsyncTask.runAsyncParallel(new Runnable() {
|
2010-12-23 21:19:55 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
setServicesEnabledSync(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2010-12-23 21:19:55 +00:00
|
|
|
* whether any accounts are configured.
|
|
|
|
*
|
|
|
|
* Blocking call - do not call from UI/lifecycle threads.
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return true if there are any accounts configured.
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
2010-12-23 21:19:55 +00:00
|
|
|
public static boolean setServicesEnabledSync(Context context) {
|
2009-06-08 22:27:17 +00:00
|
|
|
Cursor c = null;
|
|
|
|
try {
|
|
|
|
c = context.getContentResolver().query(
|
2011-06-13 22:32:27 +00:00
|
|
|
Account.CONTENT_URI,
|
|
|
|
Account.ID_PROJECTION,
|
2009-06-08 22:27:17 +00:00
|
|
|
null, null, null);
|
|
|
|
boolean enable = c.getCount() > 0;
|
2009-09-08 09:23:04 +00:00
|
|
|
setServicesEnabled(context, enable);
|
2009-09-23 22:06:19 +00:00
|
|
|
return enable;
|
2009-06-08 22:27:17 +00:00
|
|
|
} finally {
|
|
|
|
if (c != null) {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
2010-12-23 21:19:55 +00:00
|
|
|
private static void setServicesEnabled(Context context, boolean enabled) {
|
2009-03-04 03:32:22 +00:00
|
|
|
PackageManager pm = context.getPackageManager();
|
2010-08-10 00:48:53 +00:00
|
|
|
if (!enabled && pm.getComponentEnabledSetting(
|
|
|
|
new ComponentName(context, MailService.class)) ==
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
|
2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* 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(
|
2011-05-24 16:20:14 +00:00
|
|
|
new ComponentName(context, ShortcutPicker.class),
|
2009-03-04 03:32:22 +00:00
|
|
|
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
|
|
PackageManager.DONT_KILL_APP);
|
2011-06-01 23:20:43 +00:00
|
|
|
pm.setComponentEnabledSetting(
|
|
|
|
new ComponentName(context, WidgetConfiguration.class),
|
|
|
|
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
|
|
PackageManager.DONT_KILL_APP);
|
2009-03-04 03:32:22 +00:00
|
|
|
pm.setComponentEnabledSetting(
|
|
|
|
new ComponentName(context, MailService.class),
|
|
|
|
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
|
|
PackageManager.DONT_KILL_APP);
|
2010-08-10 00:48:53 +00:00
|
|
|
pm.setComponentEnabledSetting(
|
|
|
|
new ComponentName(context, AttachmentDownloadService.class),
|
|
|
|
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
|
|
PackageManager.DONT_KILL_APP);
|
2011-05-11 00:22:08 +00:00
|
|
|
pm.setComponentEnabledSetting(
|
|
|
|
new ComponentName(context, NotificationService.class),
|
|
|
|
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
2011-06-01 23:20:43 +00:00
|
|
|
PackageManager.DONT_KILL_APP);
|
2010-08-10 00:48:53 +00:00
|
|
|
if (enabled && pm.getComponentEnabledSetting(
|
|
|
|
new ComponentName(context, MailService.class)) ==
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
|
2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* And now if accounts do exist then we've just enabled the service and we want to
|
|
|
|
* schedule alarms for the new accounts.
|
|
|
|
*/
|
2010-08-24 01:48:25 +00:00
|
|
|
MailService.actionReschedule(context);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
2011-05-11 00:22:08 +00:00
|
|
|
|
|
|
|
// Start/stop the various services depending on whether there are any accounts
|
|
|
|
startOrStopService(enabled, context, new Intent(context, AttachmentDownloadService.class));
|
|
|
|
startOrStopService(enabled, context, new Intent(context, NotificationService.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts or stops the service as necessary.
|
|
|
|
* @param enabled If {@code true}, the service will be started. Otherwise, it will be stopped.
|
|
|
|
* @param context The context to manage the service with.
|
|
|
|
* @param intent The intent of the service to be managed.
|
|
|
|
*/
|
|
|
|
private static void startOrStopService(boolean enabled, Context context, Intent intent) {
|
2010-08-10 00:48:53 +00:00
|
|
|
if (enabled) {
|
|
|
|
context.startService(intent);
|
|
|
|
} else {
|
|
|
|
context.stopService(intent);
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2010-09-09 00:57:21 +00:00
|
|
|
sUiThread = Thread.currentThread();
|
2009-03-04 03:32:22 +00:00
|
|
|
Preferences prefs = Preferences.getPreferences(this);
|
2009-08-14 00:43:25 +00:00
|
|
|
DEBUG = prefs.getEnableDebugLogging();
|
2010-11-01 23:15:15 +00:00
|
|
|
sDebugInhibitGraphicsAcceleration = prefs.getInhibitGraphicsAcceleration();
|
2011-03-02 00:48:06 +00:00
|
|
|
enableStrictMode(prefs.getEnableStrictMode());
|
2011-02-11 23:05:17 +00:00
|
|
|
TempDirectory.setTempDirectory(this);
|
2010-08-24 01:48:25 +00:00
|
|
|
|
|
|
|
// 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.
|
2010-08-26 07:00:48 +00:00
|
|
|
updateLoggingFlags(this);
|
2011-01-25 02:06:53 +00:00
|
|
|
|
|
|
|
// Get a helper string used deep inside message decoders (which don't have context)
|
|
|
|
sMessageDecodeErrorString = getString(R.string.message_decode_error);
|
2011-03-09 00:44:08 +00:00
|
|
|
|
|
|
|
// Make sure all required services are running when the app is started (can prevent
|
|
|
|
// issues after an adb sync/install)
|
|
|
|
setServicesEnabledAsync(this);
|
2010-08-26 07:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load enabled debug flags from the preferences and update the EAS debug flag.
|
|
|
|
*/
|
|
|
|
public static void updateLoggingFlags(Context context) {
|
|
|
|
Preferences prefs = Preferences.getPreferences(context);
|
2011-02-13 02:56:09 +00:00
|
|
|
int debugLogging = prefs.getEnableDebugLogging() ? EmailServiceProxy.DEBUG_BIT : 0;
|
|
|
|
int verboseLogging =
|
|
|
|
prefs.getEnableExchangeLogging() ? EmailServiceProxy.DEBUG_VERBOSE_BIT : 0;
|
|
|
|
int fileLogging =
|
|
|
|
prefs.getEnableExchangeFileLogging() ? EmailServiceProxy.DEBUG_FILE_BIT : 0;
|
2011-03-02 00:48:06 +00:00
|
|
|
int enableStrictMode =
|
|
|
|
prefs.getEnableStrictMode() ? EmailServiceProxy.DEBUG_ENABLE_STRICT_MODE : 0;
|
|
|
|
int debugBits = debugLogging | verboseLogging | fileLogging | enableStrictMode;
|
2010-08-26 07:00:48 +00:00
|
|
|
Controller.getInstance(context).serviceLogging(debugBits);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 17:41:59 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2011-02-11 23:05:17 +00:00
|
|
|
Log.d(Logging.LOG_TAG, message);
|
2009-07-28 17:41:59 +00:00
|
|
|
}
|
2009-09-08 09:23:04 +00:00
|
|
|
|
2010-04-14 16:28:04 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2010-09-09 00:57:21 +00:00
|
|
|
|
|
|
|
public static void warnIfUiThread() {
|
|
|
|
if (Thread.currentThread().equals(sUiThread)) {
|
2011-02-11 23:05:17 +00:00
|
|
|
Log.w(Logging.LOG_TAG, "Method called on the UI thread", new Exception("STACK TRACE"));
|
2010-09-09 00:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-25 02:06:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 : "";
|
|
|
|
}
|
2011-03-02 00:48:06 +00:00
|
|
|
|
|
|
|
public static void enableStrictMode(boolean enabled) {
|
|
|
|
Utility.enableStrictMode(enabled);
|
|
|
|
}
|
2009-07-28 17:41:59 +00:00
|
|
|
}
|