1 /* 2 * Copyright (C) 2011 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.exchange.service; 18 19 import android.accounts.AccountManager; 20 import android.app.IntentService; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.util.Log; 24 25 import com.android.emailcommon.Logging; 26 import com.android.exchange.Eas; 27 import com.android.exchange.ExchangeService; 28 29 /** 30 * The service that really handles broadcast intents on a worker thread. 31 * 32 * We make it a service, because: 33 * <ul> 34 * <li>So that it's less likely for the process to get killed. 35 * <li>Even if it does, the Intent that have started it will be re-delivered by the system, 36 * and we can start the process again. (Using {@link #setIntentRedelivery}). 37 * </ul> 38 */ 39 public class ExchangeBroadcastProcessorService extends IntentService { 40 // Action used for BroadcastReceiver entry point 41 private static final String ACTION_BROADCAST = "broadcast_receiver"; 42 ExchangeBroadcastProcessorService()43 public ExchangeBroadcastProcessorService() { 44 // Class name will be the thread name. 45 super(ExchangeBroadcastProcessorService.class.getName()); 46 // Intent should be redelivered if the process gets killed before completing the job. 47 setIntentRedelivery(true); 48 } 49 50 /** 51 * Entry point for {@link ExchangeBroadcastReceiver}. 52 */ processBroadcastIntent(Context context, Intent broadcastIntent)53 public static void processBroadcastIntent(Context context, Intent broadcastIntent) { 54 Intent i = new Intent(context, ExchangeBroadcastProcessorService.class); 55 i.setAction(ACTION_BROADCAST); 56 i.putExtra(Intent.EXTRA_INTENT, broadcastIntent); 57 context.startService(i); 58 } 59 60 @Override onHandleIntent(Intent intent)61 protected void onHandleIntent(Intent intent) { 62 // Dispatch from entry point 63 final String action = intent.getAction(); 64 if (ACTION_BROADCAST.equals(action)) { 65 final Intent broadcastIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT); 66 final String broadcastAction = broadcastIntent.getAction(); 67 68 if (Intent.ACTION_BOOT_COMPLETED.equals(broadcastAction)) { 69 onBootCompleted(); 70 } else if (AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION.equals(broadcastAction)) { 71 if (Eas.USER_LOG) { 72 Log.d(Logging.LOG_TAG, "Login accounts changed; reconciling..."); 73 } 74 ExchangeService.runAccountReconcilerSync(this); 75 } 76 } 77 } 78 79 /** 80 * Handles {@link Intent#ACTION_BOOT_COMPLETED}. Called on a worker thread. 81 */ onBootCompleted()82 private void onBootCompleted() { 83 startService(new Intent(this, ExchangeService.class)); 84 } 85 } 86