1/*
2 * Copyright (C) 2009 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.mms.transaction;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.provider.Settings;
26import android.provider.Telephony;
27
28import com.android.mms.R;
29import com.android.mms.ui.ConversationList;
30
31
32/**
33 * Receive Intent.SMS_REJECTED.  Handle notification that received SMS messages are being
34 * rejected. This can happen when the device is out of storage.
35 */
36public class SmsRejectedReceiver extends BroadcastReceiver {
37
38    public static final int SMS_REJECTED_NOTIFICATION_ID = 239;
39
40    @Override
41    public void onReceive(Context context, Intent intent) {
42        if (Settings.Global.getInt(context.getContentResolver(),
43                Settings.Global.DEVICE_PROVISIONED, 0) == 1 &&
44                Telephony.Sms.Intents.SMS_REJECTED_ACTION.equals(intent.getAction())) {
45
46            int reason = intent.getIntExtra("result", -1);
47            boolean outOfMemory = reason == Telephony.Sms.Intents.RESULT_SMS_OUT_OF_MEMORY;
48            if (!outOfMemory) {
49                // Right now, the only user-level rejection we show to the user is out-of-memory.
50                return;
51            }
52
53            NotificationManager nm = (NotificationManager)
54            context.getSystemService(Context.NOTIFICATION_SERVICE);
55
56            Intent viewConvIntent = new Intent(context, ConversationList.class);
57            viewConvIntent.setAction(Intent.ACTION_VIEW);
58            viewConvIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
59                    | Intent.FLAG_ACTIVITY_SINGLE_TOP
60                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
61            PendingIntent pendingIntent = PendingIntent.getActivity(
62                    context, 0, viewConvIntent, 0);
63
64            Notification notification = new Notification();
65
66            // TODO: need appropriate icons
67            notification.icon = R.drawable.stat_sys_no_sim;
68            int titleId;
69            int bodyId;
70            if (outOfMemory) {
71                titleId = R.string.sms_full_title;
72                bodyId = R.string.sms_full_body;
73            } else {
74                titleId = R.string.sms_rejected_title;
75                bodyId = R.string.sms_rejected_body;
76            }
77            notification.tickerText = context.getString(titleId);
78            notification.defaults = Notification.DEFAULT_ALL;
79
80            notification.setLatestEventInfo(
81                    context, context.getString(titleId),
82                    context.getString(bodyId),
83                    pendingIntent);
84            nm.notify(SMS_REJECTED_NOTIFICATION_ID, notification);
85        }
86    }
87
88}
89