EmailBroadcastProcessorService.java revision 2959a7e073c87e2fa5fab42ec543b352a91cf187
1/* 2 * Copyright (C) 2010 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.service; 18 19import com.android.email.Email; 20import com.android.email.ExchangeUtils; 21import com.android.email.Preferences; 22import com.android.email.VendorPolicyLoader; 23import com.android.email.activity.ActivityHelper; 24import com.android.email.activity.setup.AccountSettingsXL; 25 26import android.app.IntentService; 27import android.content.ComponentName; 28import android.content.Context; 29import android.content.Intent; 30import android.content.pm.PackageManager; 31import android.util.Config; 32import android.util.Log; 33 34/** 35 * The service that really handles broadcast intents on a worker thread. 36 * 37 * We make it a service, because: 38 * <ul> 39 * <li>So that it's less likely for the process to get killed. 40 * <li>Even if it does, the Intent that have started it will be re-delivered by the system, 41 * and we can start the process again. (Using {@link #setIntentRedelivery}). 42 * </ul> 43 */ 44public class EmailBroadcastProcessorService extends IntentService { 45 // Dialing "*#*#36245#*#*" to open the debug screen. "36245" = "email" 46 private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE"; 47 private static final String SECRET_CODE_HOST_DEBUG_SCREEN = "36245"; 48 49 public EmailBroadcastProcessorService() { 50 // Class name will be the thread name. 51 super(EmailBroadcastProcessorService.class.getName()); 52 53 // Intent should be redelivered if the process gets killed before completing the job. 54 setIntentRedelivery(true); 55 } 56 57 /** 58 * Entry point for {@link EmailBroadcastReceiver}. 59 */ 60 public static void processBroadcastIntent(Context context, Intent broadcastIntent) { 61 Intent i = new Intent(context, EmailBroadcastProcessorService.class); 62 i.putExtra(Intent.EXTRA_INTENT, broadcastIntent); 63 context.startService(i); 64 } 65 66 @Override 67 protected void onHandleIntent(Intent intent) { 68 // This method is called on a worker thread. 69 70 final Intent original = intent.getParcelableExtra(Intent.EXTRA_INTENT); 71 final String action = original.getAction(); 72 73 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 74 onBootCompleted(); 75 76 // TODO: Do a better job when we get ACTION_DEVICE_STORAGE_LOW. 77 // The code below came from very old code.... 78 } else if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) { 79 // Stop IMAP/POP3 poll. 80 MailService.actionCancel(this); 81 } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) { 82 enableComponentsIfNecessary(); 83 } else if (SECRET_CODE_ACTION.equals(action) 84 && SECRET_CODE_HOST_DEBUG_SCREEN.equals(original.getData().getHost())) { 85 AccountSettingsXL.actionSettingsWithDebug(this); 86 } 87 } 88 89 private void enableComponentsIfNecessary() { 90 if (Email.setServicesEnabledSync(this)) { 91 // At least one account exists. 92 // TODO probably we should check if it's a POP/IMAP account. 93 MailService.actionReschedule(this); 94 } 95 } 96 97 /** 98 * Handles {@link Intent#ACTION_BOOT_COMPLETED}. Called on a worker thread. 99 */ 100 private void onBootCompleted() { 101 performOneTimeInitialization(); 102 103 enableComponentsIfNecessary(); 104 105 // Starts the service for Exchange, if supported. 106 ExchangeUtils.startExchangeService(this); 107 } 108 109 private void performOneTimeInitialization() { 110 final Preferences pref = Preferences.getPreferences(this); 111 int progress = pref.getOneTimeInitializationProgress(); 112 final int initialProgress = progress; 113 114 if (progress < 1) { 115 Log.i(Email.LOG_TAG, "Onetime initialization: 1"); 116 progress = 1; 117 if (VendorPolicyLoader.getInstance(this).useAlternateExchangeStrings()) { 118 setComponentEnabled(EasAuthenticatorServiceAlternate.class, true); 119 setComponentEnabled(EasAuthenticatorService.class, false); 120 } 121 122 ExchangeUtils.enableEasCalendarSync(this); 123 } 124 125 // Add your initialization steps here. 126 // Use "progress" to skip the initializations that's already done before. 127 // Using this preference also makes it safe when a user skips an upgrade. (i.e. upgrading 128 // version N to version N+2) 129 130 if (progress != initialProgress) { 131 pref.setOneTimeInitializationProgress(progress); 132 Log.i(Email.LOG_TAG, "Onetime initialization: completed."); 133 } 134 } 135 136 private void setComponentEnabled(Class<?> clazz, boolean enabled) { 137 final ComponentName c = new ComponentName(this, clazz.getName()); 138 getPackageManager().setComponentEnabledSetting(c, 139 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 140 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 141 PackageManager.DONT_KILL_APP); 142 } 143} 144