GsmSMSDispatcher.java revision 0217e2d6e60f7edd95407c7b385a627da9f1c1e3
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
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.internal.telephony.gsm;
19
20import android.app.Activity;
21import android.app.PendingIntent;
22import android.app.PendingIntent.CanceledException;
23import android.content.Intent;
24import android.os.AsyncResult;
25import android.os.Message;
26import android.provider.Telephony.Sms;
27import android.provider.Telephony.Sms.Intents;
28import android.telephony.Rlog;
29
30import com.android.internal.telephony.GsmAlphabet;
31import com.android.internal.telephony.PhoneBase;
32import com.android.internal.telephony.SMSDispatcher;
33import com.android.internal.telephony.SmsConstants;
34import com.android.internal.telephony.ImsSMSDispatcher;
35import com.android.internal.telephony.InboundSmsHandler;
36import com.android.internal.telephony.SmsHeader;
37import com.android.internal.telephony.SmsStorageMonitor;
38import com.android.internal.telephony.SmsUsageMonitor;
39import com.android.internal.telephony.TelephonyProperties;
40import com.android.internal.telephony.uicc.IccRecords;
41import com.android.internal.telephony.uicc.IccUtils;
42import com.android.internal.telephony.uicc.UiccCardApplication;
43import com.android.internal.telephony.uicc.UiccController;
44import com.android.internal.telephony.uicc.UsimServiceTable;
45import com.android.internal.telephony.gsm.GsmInboundSmsHandler;
46
47import java.util.HashMap;
48import java.util.Iterator;
49import java.util.concurrent.atomic.AtomicReference;
50
51public final class GsmSMSDispatcher extends SMSDispatcher {
52    private static final String TAG = "GsmSMSDispatcher";
53    private static final boolean VDBG = false;
54    private ImsSMSDispatcher mImsSMSDispatcher;
55    protected UiccController mUiccController = null;
56    private AtomicReference<IccRecords> mIccRecords = new AtomicReference<IccRecords>();
57    private AtomicReference<UiccCardApplication> mUiccApplication =
58            new AtomicReference<UiccCardApplication>();
59    private GsmInboundSmsHandler mGsmInboundSmsHandler;
60
61    /** Status report received */
62    private static final int EVENT_NEW_SMS_STATUS_REPORT = 100;
63
64    public GsmSMSDispatcher(PhoneBase phone, SmsStorageMonitor storageMonitor,
65            SmsUsageMonitor usageMonitor, ImsSMSDispatcher imsSMSDispatcher,
66            GsmInboundSmsHandler gsmInboundSmsHandler) {
67        super(phone, usageMonitor);
68        mCi.setOnSmsStatus(this, EVENT_NEW_SMS_STATUS_REPORT, null);
69        mImsSMSDispatcher = imsSMSDispatcher;
70        mGsmInboundSmsHandler = gsmInboundSmsHandler;
71        Rlog.d(TAG, "GsmSMSDispatcher created");
72    }
73
74    @Override
75    public void dispose() {
76        super.dispose();
77        mCi.unSetOnSmsStatus(this);
78    }
79
80    @Override
81    protected String getFormat() {
82        return SmsConstants.FORMAT_3GPP;
83    }
84
85    /**
86     * Handles 3GPP format-specific events coming from the phone stack.
87     * Other events are handled by {@link SMSDispatcher#handleMessage}.
88     *
89     * @param msg the message to handle
90     */
91    @Override
92    public void handleMessage(Message msg) {
93        switch (msg.what) {
94        case EVENT_NEW_SMS_STATUS_REPORT:
95            handleStatusReport((AsyncResult) msg.obj);
96            break;
97
98        case EVENT_NEW_ICC_SMS:
99        // pass to InboundSmsHandler to process
100        mGsmInboundSmsHandler.sendMessage(InboundSmsHandler.EVENT_NEW_SMS, msg.obj);
101        break;
102
103        case EVENT_ICC_CHANGED:
104            onUpdateIccAvailability();
105            break;
106
107        default:
108            super.handleMessage(msg);
109        }
110    }
111
112    /**
113     * Called when a status report is received.  This should correspond to
114     * a previously successful SEND.
115     *
116     * @param ar AsyncResult passed into the message handler.  ar.result should
117     *           be a String representing the status report PDU, as ASCII hex.
118     */
119    private void handleStatusReport(AsyncResult ar) {
120        String pduString = (String) ar.result;
121        SmsMessage sms = SmsMessage.newFromCDS(pduString);
122
123        if (sms != null) {
124            int tpStatus = sms.getStatus();
125            int messageRef = sms.mMessageRef;
126            for (int i = 0, count = deliveryPendingList.size(); i < count; i++) {
127                SmsTracker tracker = deliveryPendingList.get(i);
128                if (tracker.mMessageRef == messageRef) {
129                    // Found it.  Remove from list and broadcast.
130                    if(tpStatus >= Sms.STATUS_FAILED || tpStatus < Sms.STATUS_PENDING ) {
131                       deliveryPendingList.remove(i);
132                       // Update the message status (COMPLETE or FAILED)
133                       tracker.updateSentMessageStatus(mContext, tpStatus);
134                    }
135                    PendingIntent intent = tracker.mDeliveryIntent;
136                    Intent fillIn = new Intent();
137                    fillIn.putExtra("pdu", IccUtils.hexStringToBytes(pduString));
138                    fillIn.putExtra("format", getFormat());
139                    try {
140                        intent.send(mContext, Activity.RESULT_OK, fillIn);
141                    } catch (CanceledException ex) {}
142
143                    // Only expect to see one tracker matching this messageref
144                    break;
145                }
146            }
147        }
148        mCi.acknowledgeLastIncomingGsmSms(true, Intents.RESULT_SMS_HANDLED, null);
149    }
150
151    /** {@inheritDoc} */
152    @Override
153    protected void sendData(String destAddr, String scAddr, int destPort,
154            byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) {
155        SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu(
156                scAddr, destAddr, destPort, data, (deliveryIntent != null));
157        if (pdu != null) {
158            HashMap map =  SmsTrackerMapFactory(destAddr, scAddr, destPort, data, pdu);
159            SmsTracker tracker = SmsTrackerFactory(map, sentIntent, deliveryIntent,
160                    getFormat());
161            sendRawPdu(tracker);
162        } else {
163            Rlog.e(TAG, "GsmSMSDispatcher.sendData(): getSubmitPdu() returned null");
164        }
165    }
166
167    /** {@inheritDoc} */
168    @Override
169    protected void sendText(String destAddr, String scAddr, String text,
170            PendingIntent sentIntent, PendingIntent deliveryIntent) {
171        SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu(
172                scAddr, destAddr, text, (deliveryIntent != null));
173        if (pdu != null) {
174            HashMap map =  SmsTrackerMapFactory(destAddr, scAddr, text, pdu);
175            SmsTracker tracker = SmsTrackerFactory(map, sentIntent, deliveryIntent,
176                    getFormat());
177            sendRawPdu(tracker);
178        } else {
179            Rlog.e(TAG, "GsmSMSDispatcher.sendText(): getSubmitPdu() returned null");
180        }
181    }
182
183    /** {@inheritDoc} */
184    @Override
185    protected GsmAlphabet.TextEncodingDetails calculateLength(CharSequence messageBody,
186            boolean use7bitOnly) {
187        return SmsMessage.calculateLength(messageBody, use7bitOnly);
188    }
189
190    /** {@inheritDoc} */
191    @Override
192    protected void sendNewSubmitPdu(String destinationAddress, String scAddress,
193            String message, SmsHeader smsHeader, int encoding,
194            PendingIntent sentIntent, PendingIntent deliveryIntent, boolean lastPart) {
195        SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu(scAddress, destinationAddress,
196                message, deliveryIntent != null, SmsHeader.toByteArray(smsHeader),
197                encoding, smsHeader.languageTable, smsHeader.languageShiftTable);
198        if (pdu != null) {
199            HashMap map =  SmsTrackerMapFactory(destinationAddress, scAddress,
200                    message, pdu);
201            SmsTracker tracker = SmsTrackerFactory(map, sentIntent,
202                    deliveryIntent, getFormat());
203            sendRawPdu(tracker);
204        } else {
205            Rlog.e(TAG, "GsmSMSDispatcher.sendNewSubmitPdu(): getSubmitPdu() returned null");
206        }
207    }
208
209    /** {@inheritDoc} */
210    @Override
211    protected void sendSms(SmsTracker tracker) {
212        HashMap<String, Object> map = tracker.mData;
213
214        byte smsc[] = (byte[]) map.get("smsc");
215        byte pdu[] = (byte[]) map.get("pdu");
216
217        Message reply = obtainMessage(EVENT_SEND_SMS_COMPLETE, tracker);
218
219        if (tracker.mRetryCount > 0) {
220            Rlog.d(TAG, "sendSms: "
221                    + " mRetryCount=" + tracker.mRetryCount
222                    + " mMessageRef=" + tracker.mMessageRef
223                    + " SS=" + mPhone.getServiceState().getState());
224
225            // per TS 23.040 Section 9.2.3.6:  If TP-MTI SMS-SUBMIT (0x01) type
226            //   TP-RD (bit 2) is 1 for retry
227            //   and TP-MR is set to previously failed sms TP-MR
228            if (((0x01 & pdu[0]) == 0x01)) {
229                pdu[0] |= 0x04; // TP-RD
230                pdu[1] = (byte) tracker.mMessageRef; // TP-MR
231            }
232        }
233        Rlog.d(TAG, "sendSms: "
234                +" isIms()="+isIms()
235                +" mRetryCount="+tracker.mRetryCount
236                +" mImsRetry="+tracker.mImsRetry
237                +" mMessageRef="+tracker.mMessageRef
238                +" SS=" +mPhone.getServiceState().getState());
239
240        // sms over gsm is used:
241        //   if sms over IMS is not supported AND
242        //   this is not a retry case after sms over IMS failed
243        //     indicated by mImsRetry > 0
244        if (0 == tracker.mImsRetry && !isIms()) {
245            if (tracker.mRetryCount > 0) {
246                // per TS 23.040 Section 9.2.3.6:  If TP-MTI SMS-SUBMIT (0x01) type
247                //   TP-RD (bit 2) is 1 for retry
248                //   and TP-MR is set to previously failed sms TP-MR
249                if (((0x01 & pdu[0]) == 0x01)) {
250                    pdu[0] |= 0x04; // TP-RD
251                    pdu[1] = (byte) tracker.mMessageRef; // TP-MR
252                }
253            }
254            mCi.sendSMS(IccUtils.bytesToHexString(smsc),
255                    IccUtils.bytesToHexString(pdu), reply);
256        } else {
257            mCi.sendImsGsmSms(IccUtils.bytesToHexString(smsc),
258                    IccUtils.bytesToHexString(pdu), tracker.mImsRetry,
259                    tracker.mMessageRef, reply);
260            // increment it here, so in case of SMS_FAIL_RETRY over IMS
261            // next retry will be sent using IMS request again.
262            tracker.mImsRetry++;
263        }
264    }
265
266    @Override
267    public void sendRetrySms(SmsTracker tracker) {
268        //re-routing to ImsSMSDispatcher
269        mImsSMSDispatcher.sendRetrySms(tracker);
270    }
271
272    protected UiccCardApplication getUiccCardApplication() {
273        return mUiccController.getUiccCardApplication(UiccController.APP_FAM_3GPP);
274    }
275
276    private void onUpdateIccAvailability() {
277        if (mUiccController == null ) {
278            return;
279        }
280
281        UiccCardApplication newUiccApplication = getUiccCardApplication();
282
283        UiccCardApplication app = mUiccApplication.get();
284        if (app != newUiccApplication) {
285            if (app != null) {
286                Rlog.d(TAG, "Removing stale icc objects.");
287                if (mIccRecords.get() != null) {
288                    mIccRecords.get().unregisterForNewSms(this);
289                }
290                mIccRecords.set(null);
291                mUiccApplication.set(null);
292            }
293            if (newUiccApplication != null) {
294                Rlog.d(TAG, "New Uicc application found");
295                mUiccApplication.set(newUiccApplication);
296                mIccRecords.set(newUiccApplication.getIccRecords());
297                if (mIccRecords.get() != null) {
298                    mIccRecords.get().registerForNewSms(this, EVENT_NEW_ICC_SMS, null);
299                }
300            }
301        }
302    }
303
304    @Override
305    public boolean isIms() {
306        return mImsSMSDispatcher.isIms();
307    }
308
309    @Override
310    public String getImsSmsFormat() {
311        return mImsSMSDispatcher.getImsSmsFormat();
312    }
313}
314