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