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