SMSDispatcher.java revision 634a24f56f54b5d2911649288147c750b67c7ae4
10825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville/*
20825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * Copyright (C) 2006 The Android Open Source Project
30825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *
40825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * Licensed under the Apache License, Version 2.0 (the "License");
50825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * you may not use this file except in compliance with the License.
60825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * You may obtain a copy of the License at
70825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *
80825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *      http://www.apache.org/licenses/LICENSE-2.0
90825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *
100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * Unless required by applicable law or agreed to in writing, software
110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * distributed under the License is distributed on an "AS IS" BASIS,
120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * See the License for the specific language governing permissions and
140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * limitations under the License.
150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville */
160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
170825495a331bb44df395a0cdb79fab85e68db5d5Wink Savillepackage com.android.internal.telephony;
180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
190825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.app.Activity;
200825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.app.AlertDialog;
210825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.app.PendingIntent;
220825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.app.PendingIntent.CanceledException;
230825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.ContentResolver;
240825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.Context;
250825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.DialogInterface;
260825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.Intent;
270825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.pm.ApplicationInfo;
282029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condraimport android.content.pm.PackageInfo;
290825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.pm.PackageManager;
300825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.content.res.Resources;
31ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwaltimport android.database.ContentObserver;
320825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.AsyncResult;
330825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.Binder;
340825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.Handler;
350825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.Message;
360825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.SystemProperties;
37ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwaltimport android.provider.Settings;
380825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.telephony.PhoneNumberUtils;
390d4bcdf379842af4b6304809156971e926f374f0Jake Hambyimport android.telephony.Rlog;
400825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.telephony.ServiceState;
410825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.telephony.TelephonyManager;
420825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.text.Html;
430825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.text.Spanned;
442029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condraimport android.util.EventLog;
454658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hambyimport android.view.LayoutInflater;
464658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hambyimport android.view.View;
4778df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwaltimport android.view.ViewGroup;
480825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.view.WindowManager;
494658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hambyimport android.widget.Button;
504658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hambyimport android.widget.CheckBox;
514658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hambyimport android.widget.CompoundButton;
524658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hambyimport android.widget.TextView;
530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
540825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport com.android.internal.R;
550825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;
560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
570825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport java.util.ArrayList;
580825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport java.util.HashMap;
590825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport java.util.Random;
600d4bcdf379842af4b6304809156971e926f374f0Jake Hambyimport java.util.concurrent.atomic.AtomicInteger;
610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
620825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport static android.telephony.SmsManager.RESULT_ERROR_FDN_CHECK_FAILURE;
630825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport static android.telephony.SmsManager.RESULT_ERROR_GENERIC_FAILURE;
640825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport static android.telephony.SmsManager.RESULT_ERROR_LIMIT_EXCEEDED;
650825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport static android.telephony.SmsManager.RESULT_ERROR_NO_SERVICE;
660825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport static android.telephony.SmsManager.RESULT_ERROR_NULL_PDU;
670825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport static android.telephony.SmsManager.RESULT_ERROR_RADIO_OFF;
680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
690825495a331bb44df395a0cdb79fab85e68db5d5Wink Savillepublic abstract class SMSDispatcher extends Handler {
70cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    static final String TAG = "SMSDispatcher";    // accessed from inner class
71cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    static final boolean DBG = false;
720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final String SEND_NEXT_MSG_EXTRA = "SendNextMsg";
730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Permission required to send SMS to short codes without user confirmation. */
750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final String SEND_SMS_NO_CONFIRMATION_PERMISSION =
760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            "android.permission.SEND_SMS_NO_CONFIRMATION";
770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
78ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    private static final int PREMIUM_RULE_USE_SIM = 1;
79ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    private static final int PREMIUM_RULE_USE_NETWORK = 2;
80ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    private static final int PREMIUM_RULE_USE_BOTH = 3;
81ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    private final AtomicInteger mPremiumSmsRule = new AtomicInteger(PREMIUM_RULE_USE_SIM);
82ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    private final SettingsObserver mSettingsObserver;
83ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt
840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** SMS send complete. */
850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_SEND_SMS_COMPLETE = 2;
860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Retry sending a previously failed SMS message */
880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int EVENT_SEND_RETRY = 3;
890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Confirmation required for sending a large number of messages. */
910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int EVENT_SEND_LIMIT_REACHED_CONFIRMATION = 4;
920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Send the user confirmed SMS */
940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    static final int EVENT_SEND_CONFIRMED_SMS = 5;  // accessed from inner class
950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Don't send SMS (user did not confirm). */
970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    static final int EVENT_STOP_SENDING = 7;        // accessed from inner class
980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Confirmation required for third-party apps sending to an SMS short code. */
1000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int EVENT_CONFIRM_SEND_TO_POSSIBLE_PREMIUM_SHORT_CODE = 8;
1010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Confirmation required for third-party apps sending to an SMS short code. */
1030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int EVENT_CONFIRM_SEND_TO_PREMIUM_SHORT_CODE = 9;
1040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1050d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    /** Handle status report from {@code CdmaInboundSmsHandler}. */
1060d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    protected static final int EVENT_HANDLE_STATUS_REPORT = 10;
1070d4bcdf379842af4b6304809156971e926f374f0Jake Hamby
1080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected final Phone mPhone;
1090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected final Context mContext;
1100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected final ContentResolver mResolver;
11122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected final CommandsInterface mCi;
1120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected final TelephonyManager mTelephonyManager;
1130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Maximum number of times to retry sending a failed SMS. */
1150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int MAX_SEND_RETRIES = 3;
1160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Delay before next send attempt on a failed SMS, in milliseconds. */
1170825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int SEND_RETRY_DELAY = 2000;
1180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** single part SMS */
1190825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int SINGLE_PART_SMS = 1;
1200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Message sending queue limit */
1210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final int MO_MSG_QUEUE_LIMIT = 5;
1220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
1240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Message reference for a CONCATENATED_8_BIT_REFERENCE or
1250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * CONCATENATED_16_BIT_REFERENCE message set.  Should be
1260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * incremented for each set of concatenated messages.
1270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Static field shared by all dispatcher objects.
1280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
1290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static int sConcatenatedRef = new Random().nextInt(256);
1300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Outgoing message counter. Shared by all dispatchers. */
1320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private final SmsUsageMonitor mUsageMonitor;
1330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Number of outgoing SmsTrackers waiting for user confirmation. */
1350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private int mPendingTrackerCount;
1360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /* Flags indicating whether the current device allows sms service */
1380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected boolean mSmsCapable = true;
1390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected boolean mSmsSendDisabled;
1400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected int mRemainingMessages = -1;
1420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static int getNextConcatenatedRef() {
1440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        sConcatenatedRef += 1;
1450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        return sConcatenatedRef;
1460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
1470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
1490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Create a new SMS dispatcher.
1500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param phone the Phone to use
1510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param usageMonitor the SmsUsageMonitor to use
1520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
1530d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    protected SMSDispatcher(PhoneBase phone, SmsUsageMonitor usageMonitor) {
1540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mPhone = phone;
1550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mContext = phone.getContext();
1560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mResolver = mContext.getContentResolver();
15722d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mCi = phone.mCi;
1580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mUsageMonitor = usageMonitor;
1590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
160ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        mSettingsObserver = new SettingsObserver(this, mPremiumSmsRule, mContext);
161ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor(
162ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                Settings.Global.SMS_SHORT_CODE_RULE), false, mSettingsObserver);
1630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mSmsCapable = mContext.getResources().getBoolean(
1650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                com.android.internal.R.bool.config_sms_capable);
1660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mSmsSendDisabled = !SystemProperties.getBoolean(
1670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                                TelephonyProperties.PROPERTY_SMS_SEND, mSmsCapable);
168ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville        Rlog.d(TAG, "SMSDispatcher: ctor mSmsCapable=" + mSmsCapable + " format=" + getFormat()
1690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                + " mSmsSendDisabled=" + mSmsSendDisabled);
1700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
1710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
172ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    /**
173ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt     * Observe the secure setting for updated premium sms determination rules
174ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt     */
175ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    private static class SettingsObserver extends ContentObserver {
176ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        private final AtomicInteger mPremiumSmsRule;
177ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        private final Context mContext;
178ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        SettingsObserver(Handler handler, AtomicInteger premiumSmsRule, Context context) {
179ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            super(handler);
180ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            mPremiumSmsRule = premiumSmsRule;
181ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            mContext = context;
182ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            onChange(false); // load initial value;
183ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        }
184ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt
185ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        @Override
186ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        public void onChange(boolean selfChange) {
187ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            mPremiumSmsRule.set(Settings.Global.getInt(mContext.getContentResolver(),
188ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                    Settings.Global.SMS_SHORT_CODE_RULE, PREMIUM_RULE_USE_SIM));
189ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt        }
190ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt    }
191ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt
1920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Unregister for incoming SMS events. */
1930d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    public void dispose() {
1940d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        mContext.getContentResolver().unregisterContentObserver(mSettingsObserver);
1950d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    }
1960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
1980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * The format of the message PDU in the associated broadcast intent.
1990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * This will be either "3gpp" for GSM/UMTS/LTE messages in 3GPP format
2000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * or "3gpp2" for CDMA/LTE messages in 3GPP2 format.
2010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
2020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Note: All applications which handle incoming SMS messages by processing the
2030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * SMS_RECEIVED_ACTION broadcast intent MUST pass the "format" extra from the intent
2040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * into the new methods in {@link android.telephony.SmsMessage} which take an
2050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * extra format parameter. This is required in order to correctly decode the PDU on
2060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * devices which require support for both 3GPP and 3GPP2 formats at the same time,
2070825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * such as CDMA/LTE devices and GSM/CDMA world phones.
2080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
2090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return the format of the message PDU
2100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
2110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract String getFormat();
2120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2130d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    /**
2140d4bcdf379842af4b6304809156971e926f374f0Jake Hamby     * Pass the Message object to subclass to handle. Currently used to pass CDMA status reports
2150d4bcdf379842af4b6304809156971e926f374f0Jake Hamby     * from {@link com.android.internal.telephony.cdma.CdmaInboundSmsHandler}.
2160d4bcdf379842af4b6304809156971e926f374f0Jake Hamby     * @param o the SmsMessage containing the status report
2170d4bcdf379842af4b6304809156971e926f374f0Jake Hamby     */
2180d4bcdf379842af4b6304809156971e926f374f0Jake Hamby    protected void handleStatusReport(Object o) {
2190d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        Rlog.d(TAG, "handleStatusReport() called with no subclass.");
2200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
2210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /* TODO: Need to figure out how to keep track of status report routing in a
2230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *       persistent manner. If the phone process restarts (reboot or crash),
2240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *       we will lose this list and any status reports that come in after
2250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *       will be dropped.
2260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
2270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /** Sent messages awaiting a delivery status report. */
2280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected final ArrayList<SmsTracker> deliveryPendingList = new ArrayList<SmsTracker>();
2290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
2310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Handles events coming from the phone stack. Overridden from handler.
2320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
2330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param msg the message to handle
2340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
2350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    @Override
2360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public void handleMessage(Message msg) {
2370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        switch (msg.what) {
2380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_SEND_SMS_COMPLETE:
2390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // An outbound SMS has been successfully transferred, or failed.
2400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            handleSendComplete((AsyncResult) msg.obj);
2410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_SEND_RETRY:
2440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            sendSms((SmsTracker) msg.obj);
2450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_SEND_LIMIT_REACHED_CONFIRMATION:
2480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            handleReachSentLimit((SmsTracker)(msg.obj));
2490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_CONFIRM_SEND_TO_POSSIBLE_PREMIUM_SHORT_CODE:
2520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            handleConfirmShortCode(false, (SmsTracker)(msg.obj));
2530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_CONFIRM_SEND_TO_PREMIUM_SHORT_CODE:
2560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            handleConfirmShortCode(true, (SmsTracker)(msg.obj));
2570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_SEND_CONFIRMED_SMS:
2600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        {
2610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            SmsTracker tracker = (SmsTracker) msg.obj;
2620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (tracker.isMultipart()) {
2630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendMultipartSms(tracker);
2640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } else {
2650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendSms(tracker);
2660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
2670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mPendingTrackerCount--;
2680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
2700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        case EVENT_STOP_SENDING:
2720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        {
2730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            SmsTracker tracker = (SmsTracker) msg.obj;
2740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (tracker.mSentIntent != null) {
2750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                try {
2760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    tracker.mSentIntent.send(RESULT_ERROR_LIMIT_EXCEEDED);
2770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } catch (CanceledException ex) {
278ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.e(TAG, "failed to send RESULT_ERROR_LIMIT_EXCEEDED");
2790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                }
2800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
2810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mPendingTrackerCount--;
2820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            break;
2830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
2840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2850d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        case EVENT_HANDLE_STATUS_REPORT:
2860d4bcdf379842af4b6304809156971e926f374f0Jake Hamby            handleStatusReport(msg.obj);
2870d4bcdf379842af4b6304809156971e926f374f0Jake Hamby            break;
2880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2890d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        default:
2900d4bcdf379842af4b6304809156971e926f374f0Jake Hamby            Rlog.e(TAG, "handleMessage() ignoring message of unexpected type " + msg.what);
2910d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        }
292b8a13d300b245e0080aa01275e232f54d5d0e09cJake Hamby    }
293b8a13d300b245e0080aa01275e232f54d5d0e09cJake Hamby
294b8a13d300b245e0080aa01275e232f54d5d0e09cJake Hamby    /**
2950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Called when SMS send completes. Broadcasts a sentIntent on success.
2960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * On failure, either sets up retries or broadcasts a sentIntent with
2970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * the failure in the result code.
2980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
2990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param ar AsyncResult passed into the message handler.  ar.result should
3000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *           an SmsResponse instance if send was successful.  ar.userObj
3010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *           should be an SmsTracker instance.
3020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
3030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void handleSendComplete(AsyncResult ar) {
3040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        SmsTracker tracker = (SmsTracker) ar.userObj;
3050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        PendingIntent sentIntent = tracker.mSentIntent;
3060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
307454c5783e7695feec276325920550b2d4dcc3e16Rika Brooks        if (ar.result != null) {
3085a176f73dbb2bff2ca8b8f86af86a17be6863b87Wink Saville            tracker.mMessageRef = ((SmsResponse)ar.result).mMessageRef;
309454c5783e7695feec276325920550b2d4dcc3e16Rika Brooks        } else {
310454c5783e7695feec276325920550b2d4dcc3e16Rika Brooks            Rlog.d(TAG, "SmsResponse was null");
311454c5783e7695feec276325920550b2d4dcc3e16Rika Brooks        }
312454c5783e7695feec276325920550b2d4dcc3e16Rika Brooks
3130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (ar.exception == null) {
314cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) Rlog.d(TAG, "SMS send complete. Broadcasting intent: " + sentIntent);
3150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (tracker.mDeliveryIntent != null) {
3170825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                // Expecting a status report.  Add it to the list.
3180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                deliveryPendingList.add(tracker);
3190825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
3200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (sentIntent != null) {
3220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                try {
3230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    if (mRemainingMessages > -1) {
3240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        mRemainingMessages--;
3250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    }
3260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    if (mRemainingMessages == 0) {
3280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        Intent sendNext = new Intent();
3290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        sendNext.putExtra(SEND_NEXT_MSG_EXTRA, true);
3300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        sentIntent.send(mContext, Activity.RESULT_OK, sendNext);
3310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    } else {
3320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        sentIntent.send(Activity.RESULT_OK);
3330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    }
3340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } catch (CanceledException ex) {}
3350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
3360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        } else {
337cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) Rlog.d(TAG, "SMS send failed");
3380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            int ss = mPhone.getServiceState().getState();
3400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (ss != ServiceState.STATE_IN_SERVICE) {
3420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                handleNotInService(ss, tracker.mSentIntent);
3430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } else if ((((CommandException)(ar.exception)).getCommandError()
3440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    == CommandException.Error.SMS_FAIL_RETRY) &&
3450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                   tracker.mRetryCount < MAX_SEND_RETRIES) {
3460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                // Retry after a delay if needed.
3470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                // TODO: According to TS 23.040, 9.2.3.6, we should resend
3480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                //       with the same TP-MR as the failed message, and
3490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                //       TP-RD set to 1.  However, we don't have a means of
3500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                //       knowing the MR for the failed message (EF_SMSstatus
3510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                //       may or may not have the MR corresponding to this
3520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                //       message, depending on the failure).  Also, in some
3530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                //       implementations this retry is handled by the baseband.
3540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                tracker.mRetryCount++;
3550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                Message retryMsg = obtainMessage(EVENT_SEND_RETRY, tracker);
3560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendMessageDelayed(retryMsg, SEND_RETRY_DELAY);
3570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } else if (tracker.mSentIntent != null) {
3580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                int error = RESULT_ERROR_GENERIC_FAILURE;
3590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                if (((CommandException)(ar.exception)).getCommandError()
3610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        == CommandException.Error.FDN_CHECK_FAILURE) {
3620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    error = RESULT_ERROR_FDN_CHECK_FAILURE;
3630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                }
3640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                // Done retrying; return an error to the app.
3650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                try {
3660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    Intent fillIn = new Intent();
3670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    if (ar.result != null) {
36822d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                        fillIn.putExtra("errorCode", ((SmsResponse)ar.result).mErrorCode);
3690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    }
3700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    if (mRemainingMessages > -1) {
3710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        mRemainingMessages--;
3720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    }
3730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    if (mRemainingMessages == 0) {
3750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        fillIn.putExtra(SEND_NEXT_MSG_EXTRA, true);
3760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    }
3770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3780825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    tracker.mSentIntent.send(mContext, error, fillIn);
3790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } catch (CanceledException ex) {}
3800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
3810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
3820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
3830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
3840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
3850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Handles outbound message when the phone is not in service.
3860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
3870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param ss     Current service state.  Valid values are:
3880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *                  OUT_OF_SERVICE
3890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *                  EMERGENCY_ONLY
3900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *                  POWER_OFF
3910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param sentIntent the PendingIntent to send the error to
3920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
3930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static void handleNotInService(int ss, PendingIntent sentIntent) {
3940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (sentIntent != null) {
3950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            try {
3960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                if (ss == ServiceState.STATE_POWER_OFF) {
3970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent.send(RESULT_ERROR_RADIO_OFF);
3980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } else {
3990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent.send(RESULT_ERROR_NO_SERVICE);
4000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                }
4010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } catch (CanceledException ex) {}
4020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
4030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
4040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
4050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
4060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Send a data based SMS to a specific application port.
4070825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
4080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param destAddr the address to send the message to
4090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param scAddr is the service center address or null to use
4100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  the current default SMSC
4110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param destPort the port to deliver the message to
4120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param data the body of the message to send
4130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param sentIntent if not NULL this <code>PendingIntent</code> is
4140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  broadcast when the message is successfully sent, or failed.
4150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The result code will be <code>Activity.RESULT_OK<code> for success,
4160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  or one of these errors:<br>
4170825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_GENERIC_FAILURE</code><br>
4180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_RADIO_OFF</code><br>
4190825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_NULL_PDU</code><br>
4200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_NO_SERVICE</code><br>.
4210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  For <code>RESULT_ERROR_GENERIC_FAILURE</code> the sentIntent may include
4220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  the extra "errorCode" containing a radio technology specific value,
4230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  generally only useful for troubleshooting.<br>
4240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The per-application based SMS control checks sentIntent. If sentIntent
4250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  is NULL the caller will be checked against all unknown applications,
4260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  which cause smaller number of SMS to be sent in checking period.
4270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param deliveryIntent if not NULL this <code>PendingIntent</code> is
4280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  broadcast when the message is delivered to the recipient.  The
4290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  raw pdu of the status report is in the extended data ("pdu").
4300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
4310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract void sendData(String destAddr, String scAddr, int destPort,
4320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent);
4330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
4340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
4350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Send a text based SMS.
4360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
4370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param destAddr the address to send the message to
4380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param scAddr is the service center address or null to use
4390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  the current default SMSC
4400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param text the body of the message to send
4410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param sentIntent if not NULL this <code>PendingIntent</code> is
4420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  broadcast when the message is successfully sent, or failed.
4430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The result code will be <code>Activity.RESULT_OK<code> for success,
4440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  or one of these errors:<br>
4450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_GENERIC_FAILURE</code><br>
4460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_RADIO_OFF</code><br>
4470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_NULL_PDU</code><br>
4480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_NO_SERVICE</code><br>.
4490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  For <code>RESULT_ERROR_GENERIC_FAILURE</code> the sentIntent may include
4500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  the extra "errorCode" containing a radio technology specific value,
4510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  generally only useful for troubleshooting.<br>
4520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The per-application based SMS control checks sentIntent. If sentIntent
4530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  is NULL the caller will be checked against all unknown applications,
4540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  which cause smaller number of SMS to be sent in checking period.
4550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param deliveryIntent if not NULL this <code>PendingIntent</code> is
4560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  broadcast when the message is delivered to the recipient.  The
4570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  raw pdu of the status report is in the extended data ("pdu").
4580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
4590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract void sendText(String destAddr, String scAddr,
4600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            String text, PendingIntent sentIntent, PendingIntent deliveryIntent);
4610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
4620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
4630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Calculate the number of septets needed to encode the message.
4640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
4650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param messageBody the message to encode
4660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param use7bitOnly ignore (but still count) illegal characters if true
4670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return TextEncodingDetails
4680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
4690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract TextEncodingDetails calculateLength(CharSequence messageBody,
4700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            boolean use7bitOnly);
4710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
4720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
4730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Send a multi-part text based SMS.
4740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
4750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param destAddr the address to send the message to
4760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param scAddr is the service center address or null to use
4770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   the current default SMSC
4780825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param parts an <code>ArrayList</code> of strings that, in order,
4790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   comprise the original message
4800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param sentIntents if not null, an <code>ArrayList</code> of
4810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   <code>PendingIntent</code>s (one for each message part) that is
4820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   broadcast when the corresponding message part has been sent.
4830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   The result code will be <code>Activity.RESULT_OK<code> for success,
4840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   or one of these errors:
4850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   <code>RESULT_ERROR_GENERIC_FAILURE</code>
4860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   <code>RESULT_ERROR_RADIO_OFF</code>
4870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   <code>RESULT_ERROR_NULL_PDU</code>
4880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   <code>RESULT_ERROR_NO_SERVICE</code>.
4890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The per-application based SMS control checks sentIntent. If sentIntent
4900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  is NULL the caller will be checked against all unknown applications,
4910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  which cause smaller number of SMS to be sent in checking period.
4920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param deliveryIntents if not null, an <code>ArrayList</code> of
4930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   <code>PendingIntent</code>s (one for each message part) that is
4940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   broadcast when the corresponding message part has been delivered
4950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   to the recipient.  The raw pdu of the status report is in the
4960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *   extended data ("pdu").
4970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
4980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void sendMultipartText(String destAddr, String scAddr,
4990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            ArrayList<String> parts, ArrayList<PendingIntent> sentIntents,
5000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            ArrayList<PendingIntent> deliveryIntents) {
5010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        int refNumber = getNextConcatenatedRef() & 0x00FF;
5030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        int msgCount = parts.size();
5040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        int encoding = SmsConstants.ENCODING_UNKNOWN;
5050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mRemainingMessages = msgCount;
5070825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        TextEncodingDetails[] encodingForParts = new TextEncodingDetails[msgCount];
5090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        for (int i = 0; i < msgCount; i++) {
5100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            TextEncodingDetails details = calculateLength(parts.get(i), false);
5110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (encoding != details.codeUnitSize
5120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    && (encoding == SmsConstants.ENCODING_UNKNOWN
5130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                            || encoding == SmsConstants.ENCODING_7BIT)) {
5140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                encoding = details.codeUnitSize;
5150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
5160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            encodingForParts[i] = details;
5170825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
5180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5190825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        for (int i = 0; i < msgCount; i++) {
5200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef();
5210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            concatRef.refNumber = refNumber;
5220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            concatRef.seqNumber = i + 1;  // 1-based sequence
5230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            concatRef.msgCount = msgCount;
5240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // TODO: We currently set this to true since our messaging app will never
5250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // send more than 255 parts (it converts the message to MMS well before that).
5260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // However, we should support 3rd party messaging apps that might need 16-bit
5270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // references
5280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // Note:  It's not sufficient to just flip this bit to true; it will have
5290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // ripple effects (several calculations assume 8-bit ref).
5300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            concatRef.isEightBits = true;
5310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            SmsHeader smsHeader = new SmsHeader();
5320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            smsHeader.concatRef = concatRef;
5330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // Set the national language tables for 3GPP 7-bit encoding, if enabled.
5350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (encoding == SmsConstants.ENCODING_7BIT) {
5360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                smsHeader.languageTable = encodingForParts[i].languageTable;
5370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                smsHeader.languageShiftTable = encodingForParts[i].languageShiftTable;
5380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
5390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            PendingIntent sentIntent = null;
5410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (sentIntents != null && sentIntents.size() > i) {
5420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sentIntent = sentIntents.get(i);
5430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
5440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            PendingIntent deliveryIntent = null;
5460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (deliveryIntents != null && deliveryIntents.size() > i) {
5470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                deliveryIntent = deliveryIntents.get(i);
5480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
5490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            sendNewSubmitPdu(destAddr, scAddr, parts.get(i), smsHeader, encoding,
5510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent, deliveryIntent, (i == (msgCount - 1)));
5520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
5530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
5550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
5570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Create a new SubmitPdu and send it.
5580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
5590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract void sendNewSubmitPdu(String destinationAddress, String scAddress,
5600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            String message, SmsHeader smsHeader, int encoding,
5610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            PendingIntent sentIntent, PendingIntent deliveryIntent, boolean lastPart);
5620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
5640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Send a SMS
5650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
5660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param smsc the SMSC to send the message through, or NULL for the
5670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  default SMSC
5680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param pdu the raw PDU to send
5690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param sentIntent if not NULL this <code>Intent</code> is
5700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  broadcast when the message is successfully sent, or failed.
5710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The result code will be <code>Activity.RESULT_OK<code> for success,
5720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  or one of these errors:
5730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_GENERIC_FAILURE</code>
5740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_RADIO_OFF</code>
5750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_NULL_PDU</code>
5760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  <code>RESULT_ERROR_NO_SERVICE</code>.
5770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  The per-application based SMS control checks sentIntent. If sentIntent
5780825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  is NULL the caller will be checked against all unknown applications,
5790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  which cause smaller number of SMS to be sent in checking period.
5800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param deliveryIntent if not NULL this <code>Intent</code> is
5810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  broadcast when the message is delivered to the recipient.  The
5820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *  raw pdu of the status report is in the extended data ("pdu").
5830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param destAddr the destination phone number (for short code confirmation)
5840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
5850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void sendRawPdu(byte[] smsc, byte[] pdu, PendingIntent sentIntent,
5860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            PendingIntent deliveryIntent, String destAddr) {
5870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (mSmsSendDisabled) {
5880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (sentIntent != null) {
5890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                try {
5900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent.send(RESULT_ERROR_NO_SERVICE);
5910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } catch (CanceledException ex) {}
5920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
593ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville            Rlog.d(TAG, "Device does not support sending sms.");
5940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return;
5950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
5960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
5970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (pdu == null) {
5980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (sentIntent != null) {
5990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                try {
6000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent.send(RESULT_ERROR_NULL_PDU);
6010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } catch (CanceledException ex) {}
6020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
6030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return;
6040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
6050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        HashMap<String, Object> map = new HashMap<String, Object>();
6070825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        map.put("smsc", smsc);
6080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        map.put("pdu", pdu);
6090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // Get calling app package name via UID from Binder call
6110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        PackageManager pm = mContext.getPackageManager();
6120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        String[] packageNames = pm.getPackagesForUid(Binder.getCallingUid());
6130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (packageNames == null || packageNames.length == 0) {
6150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // Refuse to send SMS if we can't get the calling package name.
616ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville            Rlog.e(TAG, "Can't get calling app package name: refusing to send SMS");
6170825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (sentIntent != null) {
6180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                try {
6190825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent.send(RESULT_ERROR_GENERIC_FAILURE);
6200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                } catch (CanceledException ex) {
621ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.e(TAG, "failed to send error result");
6220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                }
6230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
6240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return;
6250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
6260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6272029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        // Get package info via packagemanager
6280d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        PackageInfo appInfo;
6292029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        try {
6302029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            // XXX this is lossy- apps can share a UID
6312029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            appInfo = pm.getPackageInfo(packageNames[0], PackageManager.GET_SIGNATURES);
6322029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        } catch (PackageManager.NameNotFoundException e) {
633ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville            Rlog.e(TAG, "Can't get calling app package info: refusing to send SMS");
6342029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            if (sentIntent != null) {
6352029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                try {
6362029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                    sentIntent.send(RESULT_ERROR_GENERIC_FAILURE);
6372029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                } catch (CanceledException ex) {
638ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.e(TAG, "failed to send error result");
6392029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                }
6402029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            }
6412029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            return;
6422029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        }
6430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // Strip non-digits from destination phone number before checking for short codes
6450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // and before displaying the number to the user if confirmation is required.
6462029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        SmsTracker tracker = new SmsTracker(map, sentIntent, deliveryIntent, appInfo,
6470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                PhoneNumberUtils.extractNetworkPortion(destAddr));
6480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // checkDestination() returns true if the destination is not a premium short code or the
6500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // sending app is approved to send to short codes. Otherwise, a message is sent to our
6510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // handler with the SmsTracker to request user confirmation before sending.
6520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (checkDestination(tracker)) {
6530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // check for excessive outgoing SMS usage by this app
6542029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            if (!mUsageMonitor.check(appInfo.packageName, SINGLE_PART_SMS)) {
6550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendMessage(obtainMessage(EVENT_SEND_LIMIT_REACHED_CONFIRMATION, tracker));
6560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                return;
6570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
6580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            int ss = mPhone.getServiceState().getState();
6600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (ss != ServiceState.STATE_IN_SERVICE) {
6620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                handleNotInService(ss, tracker.mSentIntent);
6630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } else {
6640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendSms(tracker);
6650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
6660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
6670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
6680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
6690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
6700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Check if destination is a potential premium short code and sender is not pre-approved to
6710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * send to short codes.
6720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
6730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param tracker the tracker for the SMS to send
6740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return true if the destination is approved; false if user confirmation event was sent
6750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
6760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    boolean checkDestination(SmsTracker tracker) {
6770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (mContext.checkCallingOrSelfPermission(SEND_SMS_NO_CONFIRMATION_PERMISSION)
6780825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                == PackageManager.PERMISSION_GRANTED) {
6790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return true;            // app is pre-approved to send to short codes
6800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        } else {
681ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            int rule = mPremiumSmsRule.get();
682ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            int smsCategory = SmsUsageMonitor.CATEGORY_NOT_SHORT_CODE;
683ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            if (rule == PREMIUM_RULE_USE_SIM || rule == PREMIUM_RULE_USE_BOTH) {
684ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                String simCountryIso = mTelephonyManager.getSimCountryIso();
685ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                if (simCountryIso == null || simCountryIso.length() != 2) {
686ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.e(TAG, "Can't get SIM country Iso: trying network country Iso");
687ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                    simCountryIso = mTelephonyManager.getNetworkCountryIso();
688ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                }
689ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt
690ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                smsCategory = mUsageMonitor.checkDestination(tracker.mDestAddress, simCountryIso);
691ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            }
692ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            if (rule == PREMIUM_RULE_USE_NETWORK || rule == PREMIUM_RULE_USE_BOTH) {
693ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                String networkCountryIso = mTelephonyManager.getNetworkCountryIso();
694ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                if (networkCountryIso == null || networkCountryIso.length() != 2) {
695ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.e(TAG, "Can't get Network country Iso: trying SIM country Iso");
696ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                    networkCountryIso = mTelephonyManager.getSimCountryIso();
697ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                }
698ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt
699cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                smsCategory = SmsUsageMonitor.mergeShortCodeCategories(smsCategory,
700ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                        mUsageMonitor.checkDestination(tracker.mDestAddress, networkCountryIso));
7010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
7020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
703ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt            if (smsCategory == SmsUsageMonitor.CATEGORY_NOT_SHORT_CODE
704ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                    || smsCategory == SmsUsageMonitor.CATEGORY_FREE_SHORT_CODE
705ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                    || smsCategory == SmsUsageMonitor.CATEGORY_STANDARD_SHORT_CODE) {
7064658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                return true;    // not a premium short code
7074658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            }
7084658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
7094658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            // Wait for user confirmation unless the user has set permission to always allow/deny
7104658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            int premiumSmsPermission = mUsageMonitor.getPremiumSmsPermission(
7114658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    tracker.mAppInfo.packageName);
7124658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            if (premiumSmsPermission == SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN) {
7134658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                // First time trying to send to premium SMS.
7144658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                premiumSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER;
7154658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            }
7160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7174658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            switch (premiumSmsPermission) {
7184658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW:
719ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.d(TAG, "User approved this app to send to premium SMS");
7204658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    return true;
7210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7224658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW:
723ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                    Rlog.w(TAG, "User denied this app from sending to premium SMS");
7244658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    sendMessage(obtainMessage(EVENT_STOP_SENDING, tracker));
7254658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    return false;   // reject this message
7264658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
7274658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER:
7280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                default:
7294658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    int event;
730ee7b6094d4f3d4c641e54246aec8f61349d6e9d0Robert Greenwalt                    if (smsCategory == SmsUsageMonitor.CATEGORY_POSSIBLE_PREMIUM_SHORT_CODE) {
7314658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                        event = EVENT_CONFIRM_SEND_TO_POSSIBLE_PREMIUM_SHORT_CODE;
7324658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    } else {
7334658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                        event = EVENT_CONFIRM_SEND_TO_PREMIUM_SHORT_CODE;
7344658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    }
7354658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    sendMessage(obtainMessage(event, tracker));
7364658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    return false;   // wait for user confirmation
7370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
7380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
7390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
7400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
7420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Deny sending an SMS if the outgoing queue limit is reached. Used when the message
7430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * must be confirmed by the user due to excessive usage or potential premium SMS detected.
7440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param tracker the SmsTracker for the message to send
7450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return true if the message was denied; false to continue with send confirmation
7460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
7470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private boolean denyIfQueueLimitReached(SmsTracker tracker) {
7480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (mPendingTrackerCount >= MO_MSG_QUEUE_LIMIT) {
7490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            // Deny sending message when the queue limit is reached.
7500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            try {
751a13eb8b86f22b0efcfb888e2491b007d3424ddddRekha Kumar                if (tracker.mSentIntent != null) {
752a13eb8b86f22b0efcfb888e2491b007d3424ddddRekha Kumar                    tracker.mSentIntent.send(RESULT_ERROR_LIMIT_EXCEEDED);
753a13eb8b86f22b0efcfb888e2491b007d3424ddddRekha Kumar                }
7540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } catch (CanceledException ex) {
755ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                Rlog.e(TAG, "failed to send back RESULT_ERROR_LIMIT_EXCEEDED");
7560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
7570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return true;
7580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
7590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        mPendingTrackerCount++;
7600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        return false;
7610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
7620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
7640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Returns the label for the specified app package name.
7650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param appPackage the package name of the app requesting to send an SMS
7660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return the label for the specified app, or the package name if getApplicationInfo() fails
7670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
7680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private CharSequence getAppLabel(String appPackage) {
7690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        PackageManager pm = mContext.getPackageManager();
7700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        try {
7710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            ApplicationInfo appInfo = pm.getApplicationInfo(appPackage, 0);
7720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return appInfo.loadLabel(pm);
7730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        } catch (PackageManager.NameNotFoundException e) {
774ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville            Rlog.e(TAG, "PackageManager Name Not Found for package " + appPackage);
7750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return appPackage;  // fall back to package name if we can't get app label
7760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
7770825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
7780825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
7800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Post an alert when SMS needs confirmation due to excessive usage.
7810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param tracker an SmsTracker for the current message.
7820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
7830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void handleReachSentLimit(SmsTracker tracker) {
7840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (denyIfQueueLimitReached(tracker)) {
7850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return;     // queue limit reached; error was returned to caller
7860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
7870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7882029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        CharSequence appLabel = getAppLabel(tracker.mAppInfo.packageName);
7890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        Resources r = Resources.getSystem();
7900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        Spanned messageText = Html.fromHtml(r.getString(R.string.sms_control_message, appLabel));
7910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
79278df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt        ConfirmDialogListener listener = new ConfirmDialogListener(tracker, null);
7930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
7940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        AlertDialog d = new AlertDialog.Builder(mContext)
7950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setTitle(R.string.sms_control_title)
7960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setIcon(R.drawable.stat_sys_warning)
7970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setMessage(messageText)
7980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setPositiveButton(r.getString(R.string.sms_control_yes), listener)
7990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setNegativeButton(r.getString(R.string.sms_control_no), listener)
8000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setOnCancelListener(listener)
8010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .create();
8020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
8040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        d.show();
8050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
8060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8070825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
8080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Post an alert for user confirmation when sending to a potential short code.
8090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param isPremium true if the destination is known to be a premium short code
8100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param tracker the SmsTracker for the current message.
8110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
8120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void handleConfirmShortCode(boolean isPremium, SmsTracker tracker) {
8130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (denyIfQueueLimitReached(tracker)) {
8140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return;     // queue limit reached; error was returned to caller
8150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
8160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8174658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        int detailsId;
8180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (isPremium) {
8194658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            detailsId = R.string.sms_premium_short_code_details;
8200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        } else {
8214658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            detailsId = R.string.sms_short_code_details;
8220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
8230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8242029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        CharSequence appLabel = getAppLabel(tracker.mAppInfo.packageName);
8250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        Resources r = Resources.getSystem();
8264658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        Spanned messageText = Html.fromHtml(r.getString(R.string.sms_short_code_confirm_message,
8274658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                appLabel, tracker.mDestAddress));
8280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8294658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
8304658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                Context.LAYOUT_INFLATER_SERVICE);
8314658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        View layout = inflater.inflate(R.layout.sms_short_code_confirmation_dialog, null);
8324658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
83378df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt        ConfirmDialogListener listener = new ConfirmDialogListener(tracker,
83478df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                (TextView)layout.findViewById(R.id.sms_short_code_remember_undo_instruction));
83578df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt
83678df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt
8374658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        TextView messageView = (TextView) layout.findViewById(R.id.sms_short_code_confirm_message);
8384658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        messageView.setText(messageText);
8394658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
84078df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt        ViewGroup detailsLayout = (ViewGroup) layout.findViewById(
8414658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                R.id.sms_short_code_detail_layout);
8424658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        TextView detailsView = (TextView) detailsLayout.findViewById(
8434658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                R.id.sms_short_code_detail_message);
8444658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        detailsView.setText(detailsId);
8454658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
8464658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        CheckBox rememberChoice = (CheckBox) layout.findViewById(
8474658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                R.id.sms_short_code_remember_choice_checkbox);
8484658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        rememberChoice.setOnCheckedChangeListener(listener);
8494658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
8500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        AlertDialog d = new AlertDialog.Builder(mContext)
8514658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                .setView(layout)
8520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setPositiveButton(r.getString(R.string.sms_short_code_confirm_allow), listener)
8530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setNegativeButton(r.getString(R.string.sms_short_code_confirm_deny), listener)
8540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .setOnCancelListener(listener)
8550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                .create();
8560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
8580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        d.show();
8594658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
8604658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        listener.setPositiveButton(d.getButton(DialogInterface.BUTTON_POSITIVE));
8614658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        listener.setNegativeButton(d.getButton(DialogInterface.BUTTON_NEGATIVE));
8624658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby    }
8634658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
8644658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby    /**
8654658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * Returns the premium SMS permission for the specified package. If the package has never
8664658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * been seen before, the default {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_ASK_USER}
8674658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * will be returned.
8684658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * @param packageName the name of the package to query permission
8694658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * @return one of {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_UNKNOWN},
8704658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     *  {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_ASK_USER},
8714658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     *  {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_NEVER_ALLOW}, or
8724658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     *  {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW}
8734658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     */
8744658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby    public int getPremiumSmsPermission(String packageName) {
8754658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        return mUsageMonitor.getPremiumSmsPermission(packageName);
8764658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby    }
8774658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
8784658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby    /**
8794658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * Sets the premium SMS permission for the specified package and save the value asynchronously
8804658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * to persistent storage.
8814658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * @param packageName the name of the package to set permission
8824658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     * @param permission one of {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_ASK_USER},
8834658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     *  {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_NEVER_ALLOW}, or
8844658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     *  {@link SmsUsageMonitor#PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW}
8854658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby     */
8864658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby    public void setPremiumSmsPermission(String packageName, int permission) {
8874658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        mUsageMonitor.setPremiumSmsPermission(packageName, permission);
8880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
8890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
8910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Send the message along to the radio.
8920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
8930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param tracker holds the SMS message to send
8940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
8950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract void sendSms(SmsTracker tracker);
8960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
8970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
8980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Send the multi-part SMS based on multipart Sms tracker
8990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
9000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @param tracker holds the multipart Sms tracker ready to be sent
9010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
9020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private void sendMultipartSms(SmsTracker tracker) {
9030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        ArrayList<String> parts;
9040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        ArrayList<PendingIntent> sentIntents;
9050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        ArrayList<PendingIntent> deliveryIntents;
9060825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9070825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        HashMap<String, Object> map = tracker.mData;
9080825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        String destinationAddress = (String) map.get("destination");
9100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        String scAddress = (String) map.get("scaddress");
9110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        parts = (ArrayList<String>) map.get("parts");
9130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        sentIntents = (ArrayList<PendingIntent>) map.get("sentIntents");
9140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        deliveryIntents = (ArrayList<PendingIntent>) map.get("deliveryIntents");
9150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // check if in service
9170825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        int ss = mPhone.getServiceState().getState();
9180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (ss != ServiceState.STATE_IN_SERVICE) {
9190825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            for (int i = 0, count = parts.size(); i < count; i++) {
9200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                PendingIntent sentIntent = null;
9210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                if (sentIntents != null && sentIntents.size() > i) {
9220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    sentIntent = sentIntents.get(i);
9230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                }
9240825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                handleNotInService(ss, sentIntent);
9250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
9260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return;
9270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
9280825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        sendMultipartText(destinationAddress, scAddress, parts, sentIntents, deliveryIntents);
9300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
9310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
9330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Keeps track of an SMS that has been sent to the RIL, until it has
9340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * successfully been sent, or we're done trying.
9350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
9360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final class SmsTracker {
9370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        // fields need to be public for derived SmsDispatchers
9380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public final HashMap<String, Object> mData;
9390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public int mRetryCount;
9400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public int mMessageRef;
9410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public final PendingIntent mSentIntent;
9430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public final PendingIntent mDeliveryIntent;
9440825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9452029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra        public final PackageInfo mAppInfo;
9460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public final String mDestAddress;
9470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public SmsTracker(HashMap<String, Object> data, PendingIntent sentIntent,
9492029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                PendingIntent deliveryIntent, PackageInfo appInfo, String destAddr) {
9500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mData = data;
9510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mSentIntent = sentIntent;
9520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mDeliveryIntent = deliveryIntent;
9530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mRetryCount = 0;
9542029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra            mAppInfo = appInfo;
9550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mDestAddress = destAddr;
9560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
9570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        /**
9590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville         * Returns whether this tracker holds a multi-part SMS.
9600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville         * @return true if the tracker holds a multi-part SMS; false otherwise
9610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville         */
9620d4bcdf379842af4b6304809156971e926f374f0Jake Hamby        boolean isMultipart() {
963cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            HashMap<String, Object> map = mData;
9640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            return map.containsKey("parts");
9650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
9660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
9670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
9690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Dialog listener for SMS confirmation dialog.
9700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
9710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private final class ConfirmDialogListener
9724658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener,
9734658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            CompoundButton.OnCheckedChangeListener {
9740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        private final SmsTracker mTracker;
9764658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        private Button mPositiveButton;
9774658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        private Button mNegativeButton;
9784658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        private boolean mRememberChoice;    // default is unchecked
97978df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt        private final TextView mRememberUndoInstruction;
9800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
98178df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt        ConfirmDialogListener(SmsTracker tracker, TextView textView) {
9820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            mTracker = tracker;
98378df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt            mRememberUndoInstruction = textView;
9840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
9850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
9864658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        void setPositiveButton(Button button) {
9874658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            mPositiveButton = button;
9884658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        }
9894658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
9904658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        void setNegativeButton(Button button) {
9914658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            mNegativeButton = button;
9924658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        }
9934658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
9940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        @Override
9950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public void onClick(DialogInterface dialog, int which) {
9964658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            // Always set the SMS permission so that Settings will show a permission setting
9974658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            // for the app (it won't be shown until after the app tries to send to a short code).
9984658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            int newSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER;
9994658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
10000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (which == DialogInterface.BUTTON_POSITIVE) {
1001ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                Rlog.d(TAG, "CONFIRM sending SMS");
10022029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                // XXX this is lossy- apps can have more than one signature
1003634a24f56f54b5d2911649288147c750b67c7ae4William Luh                EventLog.writeEvent(EventLogTags.EXP_DET_SMS_SENT_BY_USER,
10042029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                                    mTracker.mAppInfo.signatures[0].toCharsString());
10050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendMessage(obtainMessage(EVENT_SEND_CONFIRMED_SMS, mTracker));
10064658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                if (mRememberChoice) {
10074658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    newSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW;
10084658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                }
10090825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
1010ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville                Rlog.d(TAG, "DENY sending SMS");
10112029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                // XXX this is lossy- apps can have more than one signature
1012634a24f56f54b5d2911649288147c750b67c7ae4William Luh                EventLog.writeEvent(EventLogTags.EXP_DET_SMS_DENIED_BY_USER,
10132029d86b5aebd03f7acbd249a4e2e4f65ec37d47Geremy Condra                                    mTracker.mAppInfo.signatures[0].toCharsString());
10140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                sendMessage(obtainMessage(EVENT_STOP_SENDING, mTracker));
10154658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                if (mRememberChoice) {
10164658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                    newSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW;
10174658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                }
10180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
10194658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            setPremiumSmsPermission(mTracker.mAppInfo.packageName, newSmsPermission);
10200825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
10210825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
10220825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        @Override
10230825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        public void onCancel(DialogInterface dialog) {
1024ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville            Rlog.d(TAG, "dialog dismissed: don't send SMS");
10250825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            sendMessage(obtainMessage(EVENT_STOP_SENDING, mTracker));
10260825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
10274658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby
10284658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        @Override
10294658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1030ded9c0af7fa49504c047275ed34c2d3b22bf0c3aWink Saville            Rlog.d(TAG, "remember this choice: " + isChecked);
10314658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            mRememberChoice = isChecked;
10324658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            if (isChecked) {
10334658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                mPositiveButton.setText(R.string.sms_short_code_confirm_always_allow);
10344658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                mNegativeButton.setText(R.string.sms_short_code_confirm_never_allow);
103578df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                if (mRememberUndoInstruction != null) {
103678df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                    mRememberUndoInstruction.
103778df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                            setText(R.string.sms_short_code_remember_undo_instruction);
103878df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                    mRememberUndoInstruction.setPadding(0,0,0,32);
103978df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                }
10404658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            } else {
10414658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby                mPositiveButton.setText(R.string.sms_short_code_confirm_allow);
10425d875796122cdd8a7004e8e3964b20b5c65a31a1Robert Greenwalt                mNegativeButton.setText(R.string.sms_short_code_confirm_deny);
104378df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                if (mRememberUndoInstruction != null) {
104478df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                    mRememberUndoInstruction.setText("");
104578df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                    mRememberUndoInstruction.setPadding(0,0,0,0);
104678df2f47241a498bbd919c4c19a44b824e5dc2d6Robert Greenwalt                }
10474658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby            }
10484658a1a8c23111d5cc89feb040ce547a7b65dfb0Jake Hamby        }
10490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
10500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville}
1051