Home | History | Annotate | Download | only in transaction
      1 /*
      2  * Copyright (C) 2007-2008 Esmertec AG.
      3  * Copyright (C) 2007-2008 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mms.transaction;
     19 
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.net.Uri;
     25 import android.provider.Telephony.Mms;
     26 import android.util.Log;
     27 
     28 import com.android.internal.telephony.Phone;
     29 import com.android.internal.telephony.TelephonyIntents;
     30 import com.android.mms.LogTag;
     31 import com.google.android.mms.util.PduCache;
     32 
     33 /**
     34  * MmsSystemEventReceiver receives the
     35  * {@link android.content.intent.ACTION_BOOT_COMPLETED},
     36  * {@link com.android.internal.telephony.TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED}
     37  * and performs a series of operations which may include:
     38  * <ul>
     39  * <li>Show/hide the icon in notification area which is used to indicate
     40  * whether there is new incoming message.</li>
     41  * <li>Resend the MM's in the outbox.</li>
     42  * </ul>
     43  */
     44 public class MmsSystemEventReceiver extends BroadcastReceiver {
     45     private static final String TAG = "MmsSystemEventReceiver";
     46     private static MmsSystemEventReceiver sMmsSystemEventReceiver;
     47 
     48     private static void wakeUpService(Context context) {
     49         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     50             Log.v(TAG, "wakeUpService: start transaction service ...");
     51         }
     52 
     53         context.startService(new Intent(context, TransactionService.class));
     54     }
     55 
     56     @Override
     57     public void onReceive(Context context, Intent intent) {
     58         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     59             Log.v(TAG, "Intent received: " + intent);
     60         }
     61 
     62         String action = intent.getAction();
     63         if (action.equals(Mms.Intents.CONTENT_CHANGED_ACTION)) {
     64             Uri changed = (Uri) intent.getParcelableExtra(Mms.Intents.DELETED_CONTENTS);
     65             PduCache.getInstance().purge(changed);
     66         } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
     67             String state = intent.getStringExtra(Phone.STATE_KEY);
     68 
     69             if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     70                 Log.v(TAG, "ANY_DATA_STATE event received: " + state);
     71             }
     72 
     73             if (state.equals("CONNECTED")) {
     74                 wakeUpService(context);
     75             }
     76         } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
     77             // We should check whether there are unread incoming
     78             // messages in the Inbox and then update the notification icon.
     79             // Called on the UI thread so don't block.
     80             MessagingNotification.nonBlockingUpdateNewMessageIndicator(context, false, false);
     81         }
     82     }
     83 
     84     public static void registerForConnectionStateChanges(Context context) {
     85         unRegisterForConnectionStateChanges(context);
     86 
     87         IntentFilter intentFilter = new IntentFilter();
     88         intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
     89         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     90             Log.v(TAG, "registerForConnectionStateChanges");
     91         }
     92         if (sMmsSystemEventReceiver == null) {
     93             sMmsSystemEventReceiver = new MmsSystemEventReceiver();
     94         }
     95 
     96         context.registerReceiver(sMmsSystemEventReceiver, intentFilter);
     97     }
     98 
     99     public static void unRegisterForConnectionStateChanges(Context context) {
    100         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
    101             Log.v(TAG, "unRegisterForConnectionStateChanges");
    102         }
    103         if (sMmsSystemEventReceiver != null) {
    104             try {
    105                 context.unregisterReceiver(sMmsSystemEventReceiver);
    106             } catch (IllegalArgumentException e) {
    107                 // Allow un-matched register-unregister calls
    108             }
    109         }
    110     }
    111 }
    112