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
18package com.android.mms.transaction;
19
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.net.Uri;
25import android.provider.Telephony.Mms;
26import android.util.Log;
27
28import com.android.internal.telephony.Phone;
29import com.android.internal.telephony.TelephonyIntents;
30import com.android.mms.LogTag;
31import com.android.mms.MmsApp;
32import com.google.android.mms.util.PduCache;
33
34/**
35 * MmsSystemEventReceiver receives the
36 * {@link android.content.intent.ACTION_BOOT_COMPLETED},
37 * {@link com.android.internal.telephony.TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED}
38 * and performs a series of operations which may include:
39 * <ul>
40 * <li>Show/hide the icon in notification area which is used to indicate
41 * whether there is new incoming message.</li>
42 * <li>Resend the MM's in the outbox.</li>
43 * </ul>
44 */
45public class MmsSystemEventReceiver extends BroadcastReceiver {
46    private static final String TAG = "MmsSystemEventReceiver";
47    private static MmsSystemEventReceiver sMmsSystemEventReceiver;
48
49    private static void wakeUpService(Context context) {
50        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
51            Log.v(TAG, "wakeUpService: start transaction service ...");
52        }
53
54        context.startService(new Intent(context, TransactionService.class));
55    }
56
57    @Override
58    public void onReceive(Context context, Intent intent) {
59        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
60            Log.v(TAG, "Intent received: " + intent);
61        }
62
63        String action = intent.getAction();
64        if (action.equals(Mms.Intents.CONTENT_CHANGED_ACTION)) {
65            Uri changed = (Uri) intent.getParcelableExtra(Mms.Intents.DELETED_CONTENTS);
66            MmsApp.getApplication().getPduLoaderManager().removePdu(changed);
67        } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
68            String state = intent.getStringExtra(Phone.STATE_KEY);
69
70            if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
71                Log.v(TAG, "ANY_DATA_STATE event received: " + state);
72            }
73
74            if (state.equals("CONNECTED")) {
75                wakeUpService(context);
76            }
77        } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
78            // We should check whether there are unread incoming
79            // messages in the Inbox and then update the notification icon.
80            // Called on the UI thread so don't block.
81            MessagingNotification.nonBlockingUpdateNewMessageIndicator(
82                    context, MessagingNotification.THREAD_NONE, false);
83        }
84    }
85
86    public static void registerForConnectionStateChanges(Context context) {
87        unRegisterForConnectionStateChanges(context);
88
89        IntentFilter intentFilter = new IntentFilter();
90        intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
91        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
92            Log.v(TAG, "registerForConnectionStateChanges");
93        }
94        if (sMmsSystemEventReceiver == null) {
95            sMmsSystemEventReceiver = new MmsSystemEventReceiver();
96        }
97
98        context.registerReceiver(sMmsSystemEventReceiver, intentFilter);
99    }
100
101    public static void unRegisterForConnectionStateChanges(Context context) {
102        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
103            Log.v(TAG, "unRegisterForConnectionStateChanges");
104        }
105        if (sMmsSystemEventReceiver != null) {
106            try {
107                context.unregisterReceiver(sMmsSystemEventReceiver);
108            } catch (IllegalArgumentException e) {
109                // Allow un-matched register-unregister calls
110            }
111        }
112    }
113}
114