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