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 17 package com.android.email; 18 19 import android.app.Application; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.database.Cursor; 25 import android.util.Log; 26 27 import com.android.email.activity.MessageCompose; 28 import com.android.email.activity.ShortcutPicker; 29 import com.android.email.service.AttachmentDownloadService; 30 import com.android.email.service.MailService; 31 import com.android.email.widget.WidgetConfiguration; 32 import com.android.emailcommon.Logging; 33 import com.android.emailcommon.TempDirectory; 34 import com.android.emailcommon.provider.Account; 35 import com.android.emailcommon.service.EmailServiceProxy; 36 import com.android.emailcommon.utility.EmailAsyncTask; 37 import com.android.emailcommon.utility.Utility; 38 39 public 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 pm.setComponentEnabledSetting( 170 new ComponentName(context, WidgetConfiguration.class), 171 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 172 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 173 PackageManager.DONT_KILL_APP); 174 175 // Start/stop the various services depending on whether there are any accounts 176 startOrStopService(enabled, context, new Intent(context, AttachmentDownloadService.class)); 177 NotificationController.getInstance(context).watchForMessages(enabled); 178 } 179 180 /** 181 * Starts or stops the service as necessary. 182 * @param enabled If {@code true}, the service will be started. Otherwise, it will be stopped. 183 * @param context The context to manage the service with. 184 * @param intent The intent of the service to be managed. 185 */ 186 private static void startOrStopService(boolean enabled, Context context, Intent intent) { 187 if (enabled) { 188 context.startService(intent); 189 } else { 190 context.stopService(intent); 191 } 192 } 193 194 @Override 195 public void onCreate() { 196 super.onCreate(); 197 sUiThread = Thread.currentThread(); 198 Preferences prefs = Preferences.getPreferences(this); 199 DEBUG = prefs.getEnableDebugLogging(); 200 sDebugInhibitGraphicsAcceleration = prefs.getInhibitGraphicsAcceleration(); 201 enableStrictMode(prefs.getEnableStrictMode()); 202 TempDirectory.setTempDirectory(this); 203 204 // Tie MailRefreshManager to the Controller. 205 RefreshManager.getInstance(this); 206 // Reset all accounts to default visible window 207 Controller.getInstance(this).resetVisibleLimits(); 208 209 // Enable logging in the EAS service, so it starts up as early as possible. 210 updateLoggingFlags(this); 211 212 // Get a helper string used deep inside message decoders (which don't have context) 213 sMessageDecodeErrorString = getString(R.string.message_decode_error); 214 215 // Make sure all required services are running when the app is started (can prevent 216 // issues after an adb sync/install) 217 setServicesEnabledAsync(this); 218 } 219 220 /** 221 * Load enabled debug flags from the preferences and update the EAS debug flag. 222 */ 223 public static void updateLoggingFlags(Context context) { 224 Preferences prefs = Preferences.getPreferences(context); 225 int debugLogging = prefs.getEnableDebugLogging() ? EmailServiceProxy.DEBUG_BIT : 0; 226 int verboseLogging = 227 prefs.getEnableExchangeLogging() ? EmailServiceProxy.DEBUG_VERBOSE_BIT : 0; 228 int fileLogging = 229 prefs.getEnableExchangeFileLogging() ? EmailServiceProxy.DEBUG_FILE_BIT : 0; 230 int enableStrictMode = 231 prefs.getEnableStrictMode() ? EmailServiceProxy.DEBUG_ENABLE_STRICT_MODE : 0; 232 int debugBits = debugLogging | verboseLogging | fileLogging | enableStrictMode; 233 Controller.getInstance(context).serviceLogging(debugBits); 234 } 235 236 /** 237 * Internal, utility method for logging. 238 * The calls to log() must be guarded with "if (Email.LOGD)" for performance reasons. 239 */ 240 public static void log(String message) { 241 Log.d(Logging.LOG_TAG, message); 242 } 243 244 /** 245 * Called by the accounts reconciler to notify that accounts have changed, or by "Welcome" 246 * to clear the flag. 247 * @param setFlag true to set the notification flag, false to clear it 248 */ 249 public static synchronized void setNotifyUiAccountsChanged(boolean setFlag) { 250 sAccountsChangedNotification = setFlag; 251 } 252 253 /** 254 * Called from activity onResume() functions to check for an accounts-changed condition, at 255 * which point they should finish() and jump to the Welcome activity. 256 */ 257 public static synchronized boolean getNotifyUiAccountsChanged() { 258 return sAccountsChangedNotification; 259 } 260 261 public static void warnIfUiThread() { 262 if (Thread.currentThread().equals(sUiThread)) { 263 Log.w(Logging.LOG_TAG, "Method called on the UI thread", new Exception("STACK TRACE")); 264 } 265 } 266 267 /** 268 * Retrieve a simple string that can be used when message decoders encounter bad data. 269 * This is provided here because the protocol decoders typically don't have mContext. 270 */ 271 public static String getMessageDecodeErrorString() { 272 return sMessageDecodeErrorString != null ? sMessageDecodeErrorString : ""; 273 } 274 275 public static void enableStrictMode(boolean enabled) { 276 Utility.enableStrictMode(enabled); 277 } 278 } 279