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.internal.telephony;
18 
19 import android.annotation.UnsupportedAppUsage;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.AsyncResult;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.os.PowerManager;
28 import android.provider.Telephony.Sms.Intents;
29 import android.telephony.Rlog;
30 import android.telephony.SubscriptionManager;
31 
32 /**
33  * Monitors the device and ICC storage, and sends the appropriate events.
34  *
35  * This code was formerly part of {@link SMSDispatcher}, and has been moved
36  * into a separate class to support instantiation of multiple SMSDispatchers on
37  * dual-mode devices that require support for both 3GPP and 3GPP2 format messages.
38  */
39 public class SmsStorageMonitor extends Handler {
40     private static final String TAG = "SmsStorageMonitor";
41 
42     /** SIM/RUIM storage is full */
43     private static final int EVENT_ICC_FULL = 1;
44 
45     /** Memory status reporting is acknowledged by RIL */
46     private static final int EVENT_REPORT_MEMORY_STATUS_DONE = 2;
47 
48     /** Radio is ON */
49     private static final int EVENT_RADIO_ON = 3;
50 
51     /** Context from phone object passed to constructor. */
52     private final Context mContext;
53 
54     /** Wake lock to ensure device stays awake while dispatching the SMS intent. */
55     private PowerManager.WakeLock mWakeLock;
56 
57     private boolean mReportMemoryStatusPending;
58 
59     /** it is use to put in to extra value for SIM_FULL_ACTION and SMS_REJECTED_ACTION */
60     Phone mPhone;
61 
62     @UnsupportedAppUsage
63     final CommandsInterface mCi;                            // accessed from inner class
64     boolean mStorageAvailable = true;                       // accessed from inner class
65 
66     /**
67      * Hold the wake lock for 5 seconds, which should be enough time for
68      * any receiver(s) to grab its own wake lock.
69      */
70     private static final int WAKE_LOCK_TIMEOUT = 5000;
71 
72     /**
73      * Creates an SmsStorageMonitor and registers for events.
74      * @param phone the Phone to use
75      */
SmsStorageMonitor(Phone phone)76     public SmsStorageMonitor(Phone phone) {
77         mPhone = phone;
78         mContext = phone.getContext();
79         mCi = phone.mCi;
80 
81         createWakelock();
82 
83         mCi.setOnIccSmsFull(this, EVENT_ICC_FULL, null);
84         mCi.registerForOn(this, EVENT_RADIO_ON, null);
85 
86         // Register for device storage intents.  Use these to notify the RIL
87         // that storage for SMS is or is not available.
88         IntentFilter filter = new IntentFilter();
89         filter.addAction(Intent.ACTION_DEVICE_STORAGE_FULL);
90         filter.addAction(Intent.ACTION_DEVICE_STORAGE_NOT_FULL);
91         mContext.registerReceiver(mResultReceiver, filter);
92     }
93 
dispose()94     public void dispose() {
95         mCi.unSetOnIccSmsFull(this);
96         mCi.unregisterForOn(this);
97         mContext.unregisterReceiver(mResultReceiver);
98     }
99 
100     /**
101      * Handles events coming from the phone stack. Overridden from handler.
102      * @param msg the message to handle
103      */
104     @Override
handleMessage(Message msg)105     public void handleMessage(Message msg) {
106         AsyncResult ar;
107 
108         switch (msg.what) {
109             case EVENT_ICC_FULL:
110                 handleIccFull();
111                 break;
112 
113             case EVENT_REPORT_MEMORY_STATUS_DONE:
114                 ar = (AsyncResult) msg.obj;
115                 if (ar.exception != null) {
116                     mReportMemoryStatusPending = true;
117                     Rlog.v(TAG, "Memory status report to modem pending : mStorageAvailable = "
118                             + mStorageAvailable);
119                 } else {
120                     mReportMemoryStatusPending = false;
121                 }
122                 break;
123 
124             case EVENT_RADIO_ON:
125                 if (mReportMemoryStatusPending) {
126                     Rlog.v(TAG, "Sending pending memory status report : mStorageAvailable = "
127                             + mStorageAvailable);
128                     mCi.reportSmsMemoryStatus(mStorageAvailable,
129                             obtainMessage(EVENT_REPORT_MEMORY_STATUS_DONE));
130                 }
131                 break;
132         }
133     }
134 
createWakelock()135     private void createWakelock() {
136         PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
137         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SmsStorageMonitor");
138         mWakeLock.setReferenceCounted(true);
139     }
140 
141     /**
142      * Called when SIM_FULL message is received from the RIL. Notifies the default SMS application
143      * that SIM storage for SMS messages is full.
144      */
handleIccFull()145     private void handleIccFull() {
146         // broadcast SIM_FULL intent
147         Intent intent = new Intent(Intents.SIM_FULL_ACTION);
148         intent.setComponent(SmsApplication.getDefaultSimFullApplication(mContext, false));
149         mWakeLock.acquire(WAKE_LOCK_TIMEOUT);
150         SubscriptionManager.putPhoneIdAndSubIdExtra(intent, mPhone.getPhoneId());
151         mContext.sendBroadcast(intent, android.Manifest.permission.RECEIVE_SMS);
152     }
153 
154     /** Returns whether or not there is storage available for an incoming SMS. */
isStorageAvailable()155     public boolean isStorageAvailable() {
156         return mStorageAvailable;
157     }
158 
159     private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
160         @Override
161         public void onReceive(Context context, Intent intent) {
162             if (intent.getAction().equals(Intent.ACTION_DEVICE_STORAGE_FULL)) {
163                 mStorageAvailable = false;
164                 mCi.reportSmsMemoryStatus(false, obtainMessage(EVENT_REPORT_MEMORY_STATUS_DONE));
165             } else if (intent.getAction().equals(Intent.ACTION_DEVICE_STORAGE_NOT_FULL)) {
166                 mStorageAvailable = true;
167                 mCi.reportSmsMemoryStatus(true, obtainMessage(EVENT_REPORT_MEMORY_STATUS_DONE));
168             }
169         }
170     };
171 }
172