Email.java revision 30003e582f03f98d656fe4f0b5feb40f9b2361ba
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 android.app.Application; 20import android.content.ComponentName; 21import android.content.Context; 22import android.content.Intent; 23import android.content.pm.PackageManager; 24import android.database.Cursor; 25import android.util.Log; 26 27import com.android.email.activity.MessageCompose; 28import com.android.email.activity.ShortcutPicker; 29import com.android.email.service.AttachmentDownloadService; 30import com.android.email.service.MailService; 31import com.android.email.widget.WidgetConfiguration; 32import com.android.emailcommon.Logging; 33import com.android.emailcommon.TempDirectory; 34import com.android.emailcommon.provider.Account; 35import com.android.emailcommon.service.EmailServiceProxy; 36import com.android.emailcommon.utility.EmailAsyncTask; 37import com.android.emailcommon.utility.Utility; 38 39public class Email extends Application { 40 /** 41 * If this is enabled there will be additional logging information sent to 42 * Log.d, including protocol dumps. 43 * 44 * This should only be used for logs that are useful for debbuging user problems, 45 * not for internal/development logs. 46 * 47 * This can be enabled by typing "debug" in the AccountFolderList activity. 48 * Changing the value to 'true' here will likely have no effect at all! 49 * 50 * TODO: rename this to sUserDebug, and rename LOGD below to DEBUG. 51 */ 52 public static boolean DEBUG; 53 54 // Exchange debugging flags (passed to Exchange, when available, via EmailServiceProxy) 55 public static boolean DEBUG_EXCHANGE; 56 public static boolean DEBUG_EXCHANGE_VERBOSE; 57 public static boolean DEBUG_EXCHANGE_FILE; 58 59 /** 60 * If true, inhibit hardware graphics acceleration in UI (for a/b testing) 61 */ 62 public static boolean sDebugInhibitGraphicsAcceleration = false; 63 64 /** 65 * Specifies how many messages will be shown in a folder by default. This number is set 66 * on each new folder and can be incremented with "Load more messages..." by the 67 * VISIBLE_LIMIT_INCREMENT 68 */ 69 public static final int VISIBLE_LIMIT_DEFAULT = 25; 70 71 /** 72 * Number of additional messages to load when a user selects "Load more messages..." 73 */ 74 public static final int VISIBLE_LIMIT_INCREMENT = 25; 75 76 /** 77 * This is used to force stacked UI to return to the "welcome" screen any time we change 78 * the accounts list (e.g. deleting accounts in the Account Manager preferences.) 79 */ 80 private static boolean sAccountsChangedNotification = false; 81 82 private static String sMessageDecodeErrorString; 83 84 private static Thread sUiThread; 85 86 /** 87 * Asynchronous version of {@link #setServicesEnabledSync(Context)}. Use when calling from 88 * UI thread (or lifecycle entry points.) 89 * 90 * @param context 91 */ 92 public static void setServicesEnabledAsync(final Context context) { 93 EmailAsyncTask.runAsyncParallel(new Runnable() { 94 @Override 95 public void run() { 96 setServicesEnabledSync(context); 97 } 98 }); 99 } 100 101 /** 102 * Called throughout the application when the number of accounts has changed. This method 103 * enables or disables the Compose activity, the boot receiver and the service based on 104 * whether any accounts are configured. 105 * 106 * Blocking call - do not call from UI/lifecycle threads. 107 * 108 * @param context 109 * @return true if there are any accounts configured. 110 */ 111 public static boolean setServicesEnabledSync(Context context) { 112 Cursor c = null; 113 try { 114 c = context.getContentResolver().query( 115 Account.CONTENT_URI, 116 Account.ID_PROJECTION, 117 null, null, null); 118 boolean enable = c.getCount() > 0; 119 setServicesEnabled(context, enable); 120 return enable; 121 } finally { 122 if (c != null) { 123 c.close(); 124 } 125 } 126 } 127 128 private static void setServicesEnabled(Context context, boolean enabled) { 129 PackageManager pm = context.getPackageManager(); 130 if (!enabled && pm.getComponentEnabledSetting( 131 new ComponentName(context, MailService.class)) == 132 PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { 133 /* 134 * If no accounts now exist but the service is still enabled we're about to disable it 135 * so we'll reschedule to kill off any existing alarms. 136 */ 137 MailService.actionReschedule(context); 138 } 139 pm.setComponentEnabledSetting( 140 new ComponentName(context, MessageCompose.class), 141 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 142 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 143 PackageManager.DONT_KILL_APP); 144 pm.setComponentEnabledSetting( 145 new ComponentName(context, ShortcutPicker.class), 146 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 147 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 148 PackageManager.DONT_KILL_APP); 149 pm.setComponentEnabledSetting( 150 new ComponentName(context, MailService.class), 151 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 152 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 153 PackageManager.DONT_KILL_APP); 154 pm.setComponentEnabledSetting( 155 new ComponentName(context, AttachmentDownloadService.class), 156 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 157 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 158 PackageManager.DONT_KILL_APP); 159 if (enabled && pm.getComponentEnabledSetting( 160 new ComponentName(context, MailService.class)) == 161 PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { 162 /* 163 * And now if accounts do exist then we've just enabled the service and we want to 164 * schedule alarms for the new accounts. 165 */ 166 MailService.actionReschedule(context); 167 } 168 169 // Start/stop the various services depending on whether there are any accounts 170 startOrStopService(enabled, context, new Intent(context, AttachmentDownloadService.class)); 171 NotificationController.getInstance(context).watchForMessages(enabled); 172 } 173 174 /** 175 * Starts or stops the service as necessary. 176 * @param enabled If {@code true}, the service will be started. Otherwise, it will be stopped. 177 * @param context The context to manage the service with. 178 * @param intent The intent of the service to be managed. 179 */ 180 private static void startOrStopService(boolean enabled, Context context, Intent intent) { 181 if (enabled) { 182 context.startService(intent); 183 } else { 184 context.stopService(intent); 185 } 186 } 187 188 @Override 189 public void onCreate() { 190 super.onCreate(); 191 sUiThread = Thread.currentThread(); 192 Preferences prefs = Preferences.getPreferences(this); 193 DEBUG = prefs.getEnableDebugLogging(); 194 sDebugInhibitGraphicsAcceleration = prefs.getInhibitGraphicsAcceleration(); 195 enableStrictMode(prefs.getEnableStrictMode()); 196 TempDirectory.setTempDirectory(this); 197 198 // Tie MailRefreshManager to the Controller. 199 RefreshManager.getInstance(this); 200 // Reset all accounts to default visible window 201 Controller.getInstance(this).resetVisibleLimits(); 202 203 // Enable logging in the EAS service, so it starts up as early as possible. 204 updateLoggingFlags(this); 205 206 // Get a helper string used deep inside message decoders (which don't have context) 207 sMessageDecodeErrorString = getString(R.string.message_decode_error); 208 209 // Make sure all required services are running when the app is started (can prevent 210 // issues after an adb sync/install) 211 setServicesEnabledAsync(this); 212 } 213 214 /** 215 * Load enabled debug flags from the preferences and update the EAS debug flag. 216 */ 217 public static void updateLoggingFlags(Context context) { 218 Preferences prefs = Preferences.getPreferences(context); 219 int debugLogging = prefs.getEnableDebugLogging() ? EmailServiceProxy.DEBUG_BIT : 0; 220 int verboseLogging = 221 prefs.getEnableExchangeLogging() ? EmailServiceProxy.DEBUG_VERBOSE_BIT : 0; 222 int fileLogging = 223 prefs.getEnableExchangeFileLogging() ? EmailServiceProxy.DEBUG_FILE_BIT : 0; 224 int enableStrictMode = 225 prefs.getEnableStrictMode() ? EmailServiceProxy.DEBUG_ENABLE_STRICT_MODE : 0; 226 int debugBits = debugLogging | verboseLogging | fileLogging | enableStrictMode; 227 Controller.getInstance(context).serviceLogging(debugBits); 228 } 229 230 /** 231 * Internal, utility method for logging. 232 * The calls to log() must be guarded with "if (Email.LOGD)" for performance reasons. 233 */ 234 public static void log(String message) { 235 Log.d(Logging.LOG_TAG, message); 236 } 237 238 /** 239 * Called by the accounts reconciler to notify that accounts have changed, or by "Welcome" 240 * to clear the flag. 241 * @param setFlag true to set the notification flag, false to clear it 242 */ 243 public static synchronized void setNotifyUiAccountsChanged(boolean setFlag) { 244 sAccountsChangedNotification = setFlag; 245 } 246 247 /** 248 * Called from activity onResume() functions to check for an accounts-changed condition, at 249 * which point they should finish() and jump to the Welcome activity. 250 */ 251 public static synchronized boolean getNotifyUiAccountsChanged() { 252 return sAccountsChangedNotification; 253 } 254 255 public static void warnIfUiThread() { 256 if (Thread.currentThread().equals(sUiThread)) { 257 Log.w(Logging.LOG_TAG, "Method called on the UI thread", new Exception("STACK TRACE")); 258 } 259 } 260 261 /** 262 * Retrieve a simple string that can be used when message decoders encounter bad data. 263 * This is provided here because the protocol decoders typically don't have mContext. 264 */ 265 public static String getMessageDecodeErrorString() { 266 return sMessageDecodeErrorString != null ? sMessageDecodeErrorString : ""; 267 } 268 269 public static void enableStrictMode(boolean enabled) { 270 Utility.enableStrictMode(enabled); 271 } 272} 273