DcTracker.java revision 76c5a9479d312139740dcaf7644172f2beb2f7e0
1c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/*
2c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Copyright (C) 2006 The Android Open Source Project
3c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
4c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Licensed under the Apache License, Version 2.0 (the "License");
5c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * you may not use this file except in compliance with the License.
6c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * You may obtain a copy of the License at
7c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
8c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *      http://www.apache.org/licenses/LICENSE-2.0
9c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
10c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Unless required by applicable law or agreed to in writing, software
11c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * distributed under the License is distributed on an "AS IS" BASIS,
12c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * See the License for the specific language governing permissions and
14c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * limitations under the License.
15c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
16c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
174918296afe1c667e9523cdfc799f558f7ebc2bfbWink Savillepackage com.android.internal.telephony.dataconnection;
18c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
19cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kallaimport android.app.AlarmManager;
20c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.app.PendingIntent;
212b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensenimport android.app.ProgressDialog;
22c9b81a0c05128694c617fcdd67e73821895822feWink Savilleimport android.content.ActivityNotFoundException;
23b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensenimport android.content.BroadcastReceiver;
24c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.content.ContentResolver;
25cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.content.ContentValues;
26c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.content.Context;
27c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.content.Intent;
28c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.content.IntentFilter;
291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.content.SharedPreferences;
30cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.content.res.Resources;
31c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.database.ContentObserver;
32cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.database.Cursor;
33cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.net.ConnectivityManager;
34c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.net.LinkProperties;
3596cce86cf08e37e0f09ed5057b1196e26b302743Robert Greenwaltimport android.net.NetworkCapabilities;
36cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.net.NetworkConfig;
371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.net.NetworkInfo;
38af5593594070f825032be46dced573cd195956e1Robert Greenwaltimport android.net.NetworkRequest;
39cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.net.NetworkUtils;
409c180aedfc9f0d20525c0128487d3500e6c0a715Jason Monkimport android.net.ProxyInfo;
411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.net.TrafficStats;
42cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.net.Uri;
431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.net.wifi.WifiManager;
44c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.AsyncResult;
453fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwaltimport android.os.Build;
46b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensenimport android.os.Bundle;
47a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.os.Handler;
481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.os.HandlerThread;
49c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.Message;
50a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.os.RegistrantList;
51b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensenimport android.os.ServiceManager;
52c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.SystemClock;
53c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.os.SystemProperties;
541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.preference.PreferenceManager;
55c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.provider.Settings;
561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.provider.Settings.SettingNotFoundException;
57cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.provider.Telephony;
58cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.telephony.CellLocation;
592e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwaltimport android.telephony.PcoData;
604c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxuimport android.telephony.Rlog;
610e776303ca82b5bec5db19bb44e0f13b0c7c6400Etan Cohenimport android.telephony.ServiceState;
62a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.telephony.SubscriptionManager;
631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
644c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxuimport android.telephony.TelephonyManager;
65cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.telephony.cdma.CdmaCellLocation;
66cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport android.telephony.gsm.GsmCellLocation;
67c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport android.text.TextUtils;
68cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kallaimport android.util.EventLog;
692dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwaltimport android.util.LocalLog;
70ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwaltimport android.util.Pair;
71af5593594070f825032be46dced573cd195956e1Robert Greenwaltimport android.util.SparseArray;
722b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensenimport android.view.WindowManager;
73c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport com.android.internal.R;
75af5593594070f825032be46dced573cd195956e1Robert Greenwaltimport com.android.internal.annotations.VisibleForTesting;
76cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport com.android.internal.telephony.DctConstants;
77cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport com.android.internal.telephony.EventLogTags;
784c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxuimport com.android.internal.telephony.GsmCdmaPhone;
79b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensenimport com.android.internal.telephony.ITelephony;
804c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxuimport com.android.internal.telephony.Phone;
814918296afe1c667e9523cdfc799f558f7ebc2bfbWink Savilleimport com.android.internal.telephony.PhoneConstants;
82cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Savilleimport com.android.internal.telephony.RILConstants;
834c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxuimport com.android.internal.telephony.ServiceStateTracker;
84a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxuimport com.android.internal.telephony.TelephonyIntents;
85d720945f2be5ea5fe0faf67e67d9ea0e184eba67Alex Yakavenkaimport com.android.internal.telephony.uicc.IccRecords;
86bb36adde615d3d85fa0fc23935197c6bc6a799edAlex Yakavenkaimport com.android.internal.telephony.uicc.UiccController;
8776f43316a5a6082d601bffd4b6898d0bd81e11fcramimport com.android.internal.util.ArrayUtils;
884c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxuimport com.android.internal.util.AsyncChannel;
89c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
90c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.io.FileDescriptor;
91c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.io.PrintWriter;
92c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleimport java.util.ArrayList;
9329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwaltimport java.util.Arrays;
941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.Comparator;
951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.HashMap;
9668f4f4a0bc8d4060b5775e7a24a97ea5b485989efionaxuimport java.util.HashSet;
971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.List;
981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.Map.Entry;
9929c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwaltimport java.util.Objects;
1001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.PriorityQueue;
1011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.Set;
1023d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.concurrent.ConcurrentHashMap;
1041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.concurrent.atomic.AtomicBoolean;
1051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.concurrent.atomic.AtomicInteger;
1061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yuimport java.util.concurrent.atomic.AtomicReference;
107c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/**
108c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * {@hide}
109c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
1100c3ec24396bb8c21b4d89f743b626c13dd35ba7bAmit Mahajanpublic class DcTracker extends Handler {
1111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String LOG_TAG = "DCT";
1121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final boolean DBG = true;
1131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final boolean VDBG = false; // STOPSHIP if true
1141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final boolean VDBG_STALL = false; // STOPSHIP if true
1151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final boolean RADIO_TESTS = false;
1161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1170e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu    public AtomicBoolean isCleanupRequired = new AtomicBoolean(false);
1181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final AlarmManager mAlarmManager;
1201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Currently requested APN type (TODO: This should probably be a parameter not a member) */
1221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private String mRequestedApnType = PhoneConstants.APN_TYPE_DEFAULT;
1231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
124a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    // All data enabling/disabling related settings
125a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    private final DataEnabledSettings mDataEnabledSettings = new DataEnabledSettings();
126a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu
1271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
1281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * After detecting a potential connection problem, this is the max number
1291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * of subsequent polls before attempting recovery.
1301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
1311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // 1 sec. default polling interval when screen is on.
1321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final int POLL_NETSTAT_MILLIS = 1000;
1331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // 10 min. default polling interval when screen is off.
1341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final int POLL_NETSTAT_SCREEN_OFF_MILLIS = 1000*60*10;
1351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Default sent packets without ack which triggers initial recovery steps
1361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final int NUMBER_SENT_PACKETS_OF_HANG = 10;
1371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Default for the data stall alarm while non-aggressive stall detection
1391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final int DATA_STALL_ALARM_NON_AGGRESSIVE_DELAY_IN_MS_DEFAULT = 1000 * 60 * 6;
1401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Default for the data stall alarm for aggressive stall detection
1411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final int DATA_STALL_ALARM_AGGRESSIVE_DELAY_IN_MS_DEFAULT = 1000 * 60;
1421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Tag for tracking stale alarms
1431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String DATA_STALL_ALARM_TAG_EXTRA = "data.stall.alram.tag";
1441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final boolean DATA_STALL_SUSPECTED = true;
1461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final boolean DATA_STALL_NOT_SUSPECTED = false;
1471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private String RADIO_RESET_PROPERTY = "gsm.radioreset";
1491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String INTENT_RECONNECT_ALARM =
1511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            "com.android.internal.telephony.data-reconnect";
1521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String INTENT_RECONNECT_ALARM_EXTRA_TYPE = "reconnect_alarm_extra_type";
1531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String INTENT_RECONNECT_ALARM_EXTRA_REASON =
1541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            "reconnect_alarm_extra_reason";
1551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String INTENT_DATA_STALL_ALARM =
1571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            "com.android.internal.telephony.data-stall";
1581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1593d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    @VisibleForTesting
1603d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    public static class DataAllowFailReason {
1613d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        private HashSet<DataAllowFailReasonType> mDataAllowFailReasonSet = new HashSet<>();
1623d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1633d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        public void addDataAllowFailReason(DataAllowFailReasonType type) {
1643d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            mDataAllowFailReasonSet.add(type);
1653d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
1663d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1673d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        public String getDataAllowFailReason() {
1683d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            StringBuilder failureReason = new StringBuilder();
1693d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.append("isDataAllowed: No");
1703d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            for(DataAllowFailReasonType reason : mDataAllowFailReasonSet) {
1713d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                failureReason.append(reason.mFailReasonStr);
1723d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            }
1733d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            return failureReason.toString();
1743d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
1753d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1763d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        public boolean isFailForSingleReason(DataAllowFailReasonType failReasonType) {
1773d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            return (mDataAllowFailReasonSet.size() == 1) &&
1783d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                    (mDataAllowFailReasonSet.contains(failReasonType));
1793d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
1803d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1813d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        public void clearAllReasons() {
1823d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            mDataAllowFailReasonSet.clear();
1833d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
1843d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1853d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        public boolean isFailed() {
1863d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            return mDataAllowFailReasonSet.size() > 0;
1873d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
1883d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    }
1893d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1903d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    @VisibleForTesting
1913d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    public enum DataAllowFailReasonType {
1923d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        NOT_ATTACHED(" - Not attached"),
1933d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        RECORD_NOT_LOADED(" - SIM not loaded"),
1943d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        ROAMING_DISABLED(" - Roaming and data roaming not enabled"),
1953d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        INVALID_PHONE_STATE(" - PhoneState is not idle"),
1963d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        CONCURRENT_VOICE_DATA_NOT_ALLOWED(" - Concurrent voice and data not allowed"),
1973d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        PS_RESTRICTED(" - mIsPsRestricted= true"),
1983d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        UNDESIRED_POWER_STATE(" - desiredPowerState= false"),
1993d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        INTERNAL_DATA_DISABLED(" - mInternalDataEnabled= false"),
2000d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu        DEFAULT_DATA_UNSELECTED(" - defaultDataSelected= false"),
2010d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu        RADIO_DISABLED_BY_CARRIER(" - powerStateFromCarrier= false");
2023d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
2033d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        public String mFailReasonStr;
2043d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
2053d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        DataAllowFailReasonType(String reason) {
2063d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            mFailReasonStr = reason;
2073d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
2083d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    }
2093d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
2101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private DcTesterFailBringUpAll mDcTesterFailBringUpAll;
2111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private DcController mDcc;
2121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** kept in sync with mApnContexts
2141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Higher numbers are higher priority and sorted so highest priority is first */
2151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final PriorityQueue<ApnContext>mPrioritySortedApnContexts =
2161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            new PriorityQueue<ApnContext>(5,
2171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            new Comparator<ApnContext>() {
2181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                public int compare(ApnContext c1, ApnContext c2) {
2191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    return c2.priority - c1.priority;
2201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
2211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } );
2221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** allApns holds all apns */
2241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private ArrayList<ApnSetting> mAllApnSettings = null;
2251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** preferred apn */
2271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private ApnSetting mPreferredApn = null;
2281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** Is packet service restricted by network */
2301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mIsPsRestricted = false;
2311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** emergency apn Setting*/
2331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private ApnSetting mEmergencyApn = null;
2341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Once disposed dont handle any messages */
2361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mIsDisposed = false;
2371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private ContentResolver mResolver;
2391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Set to true with CMD_ENABLE_MOBILE_PROVISIONING */
2411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mIsProvisioning = false;
2421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* The Url passed as object parameter in CMD_ENABLE_MOBILE_PROVISIONING */
2441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private String mProvisioningUrl = null;
2451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Intent for the provisioning apn alarm */
2471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String INTENT_PROVISIONING_APN_ALARM =
2481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            "com.android.internal.telephony.provisioning_apn_alarm";
2491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Tag for tracking stale alarms */
2511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String PROVISIONING_APN_ALARM_TAG_EXTRA = "provisioning.apn.alarm.tag";
2521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Debug property for overriding the PROVISIONING_APN_ALARM_DELAY_IN_MS */
2541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final String DEBUG_PROV_APN_ALARM = "persist.debug.prov_apn_alarm";
2551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Default for the provisioning apn alarm timeout */
2571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static final int PROVISIONING_APN_ALARM_DELAY_IN_MS_DEFAULT = 1000 * 60 * 15;
2581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* The provision apn alarm intent used to disable the provisioning apn */
2601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private PendingIntent mProvisioningApnAlarmIntent = null;
2611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /* Used to track stale provisioning apn alarms */
2631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int mProvisioningApnAlarmTag = (int) SystemClock.elapsedRealtime();
2641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private AsyncChannel mReplyAc = new AsyncChannel();
2661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver () {
2681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        @Override
2691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public void onReceive(Context context, Intent intent) {
2701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            String action = intent.getAction();
271c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu
2721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (action.equals(Intent.ACTION_SCREEN_ON)) {
273c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                if (DBG) log("screen on");
2741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mIsScreenOn = true;
2751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                stopNetStatPoll();
2761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                startNetStatPoll();
2771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                restartDataStallAlarm();
2781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
279c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                if (DBG) log("screen off");
2801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mIsScreenOn = false;
2811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                stopNetStatPoll();
2821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                startNetStatPoll();
2831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                restartDataStallAlarm();
2841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (action.startsWith(INTENT_RECONNECT_ALARM)) {
2851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("Reconnect alarm. Previous state was " + mState);
2861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onActionIntentReconnectAlarm(intent);
2871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (action.equals(INTENT_DATA_STALL_ALARM)) {
288c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                if (DBG) log("Data stall alarm");
2891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onActionIntentDataStallAlarm(intent);
2901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (action.equals(INTENT_PROVISIONING_APN_ALARM)) {
291c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                if (DBG) log("Provisioning apn alarm");
2921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onActionIntentProvisioningApnAlarm(intent);
2931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
2941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                final android.net.NetworkInfo networkInfo = (NetworkInfo)
2951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
2961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mIsWifiConnected = (networkInfo != null && networkInfo.isConnected());
2971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("NETWORK_STATE_CHANGED_ACTION: mIsWifiConnected=" + mIsWifiConnected);
2981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
299c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                if (DBG) log("Wifi state changed");
3001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                final boolean enabled = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
3011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_ENABLED;
3021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (!enabled) {
3031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    // when WiFi got disabled, the NETWORK_STATE_CHANGED_ACTION
3041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    // quit and won't report disconnected until next enabling.
3051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    mIsWifiConnected = false;
3061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
3071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) {
3081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("WIFI_STATE_CHANGED_ACTION: enabled=" + enabled
3091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            + " mIsWifiConnected=" + mIsWifiConnected);
3101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
311c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu            } else {
312c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                if (DBG) log("onReceive: Unknown action=" + action);
3131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
3141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
3151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    };
3161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final Runnable mPollNetStat = new Runnable() {
3181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        @Override
3191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public void run() {
3201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            updateDataActivity();
3211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (mIsScreenOn) {
3231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mNetStatPollPeriod = Settings.Global.getInt(mResolver,
3241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        Settings.Global.PDP_WATCHDOG_POLL_INTERVAL_MS, POLL_NETSTAT_MILLIS);
3251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
3261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mNetStatPollPeriod = Settings.Global.getInt(mResolver,
3271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        Settings.Global.PDP_WATCHDOG_LONG_POLL_INTERVAL_MS,
3281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        POLL_NETSTAT_SCREEN_OFF_MILLIS);
3291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
3301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (mNetStatPollEnabled) {
3321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mDataConnectionTracker.postDelayed(this, mNetStatPollPeriod);
3331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
3341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
3351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    };
3361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private SubscriptionManager mSubscriptionManager;
3381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final OnSubscriptionsChangedListener mOnSubscriptionsChangedListener =
3391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            new OnSubscriptionsChangedListener() {
3401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                public final AtomicInteger mPreviousSubId =
3411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        new AtomicInteger(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
3421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                /**
3441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                 * Callback invoked when there is any change to any SubscriptionInfo. Typically
3451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                 * this method invokes {@link SubscriptionManager#getActiveSubscriptionInfoList}
3461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                 */
3471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                @Override
3481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                public void onSubscriptionsChanged() {
3491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (DBG) log("SubscriptionListener.onSubscriptionInfoChanged");
3501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    // Set the network type, in case the radio does not restore it.
3511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    int subId = mPhone.getSubId();
3521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (SubscriptionManager.isValidSubscriptionId(subId)) {
353f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                        registerSettingsObserver();
3541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
3551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (mPreviousSubId.getAndSet(subId) != subId &&
3561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            SubscriptionManager.isValidSubscriptionId(subId)) {
3571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        onRecordsLoadedOrSubIdChanged();
3581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
3591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
3601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            };
3611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
362f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt    private static class SettingsObserver extends ContentObserver {
363f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        final private HashMap<Uri, Integer> mUriEventMap;
364f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        final private Context mContext;
365f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        final private Handler mHandler;
366f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        final private static String TAG = "DcTracker.SettingsObserver";
3671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
368f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        SettingsObserver(Context context, Handler handler) {
369f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            super(null);
370f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            mUriEventMap = new HashMap<Uri, Integer>();
371f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            mContext = context;
372f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            mHandler = handler;
3731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
3741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
375f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        void observe(Uri uri, int what) {
376f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            mUriEventMap.put(uri, what);
377f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            final ContentResolver resolver = mContext.getContentResolver();
378f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            resolver.registerContentObserver(uri, false, this);
3791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
3801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
381f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        void unobserve() {
382f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            final ContentResolver resolver = mContext.getContentResolver();
383f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            resolver.unregisterContentObserver(this);
3841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
3851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        @Override
3871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public void onChange(boolean selfChange) {
388f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            Rlog.e(TAG, "Should never be reached.");
389f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        }
390f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
391f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        @Override
392f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        public void onChange(boolean selfChange, Uri uri) {
393f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            final Integer what = mUriEventMap.get(uri);
394f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            if (what != null) {
395f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                mHandler.obtainMessage(what.intValue()).sendToTarget();
396f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            } else {
397f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                Rlog.e(TAG, "No matching event to send for URI=" + uri);
3981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
3991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
401f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
402f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt    private final SettingsObserver mSettingsObserver;
403f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
404f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt    private void registerSettingsObserver() {
405f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver.unobserve();
406f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        String simSuffix = "";
407f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu        if (TelephonyManager.getDefault().getSimCount() > 1) {
408f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            simSuffix = Integer.toString(mPhone.getSubId());
409f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        }
410f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu
411f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver.observe(
412f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                Settings.Global.getUriFor(Settings.Global.DATA_ROAMING + simSuffix),
413f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                DctConstants.EVENT_ROAMING_ON);
414f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver.observe(
415f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
416f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                DctConstants.EVENT_DEVICE_PROVISIONED_CHANGE);
417f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver.observe(
418f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED),
419f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                DctConstants.EVENT_DEVICE_PROVISIONED_CHANGE);
420f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt    }
4211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
4231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Maintain the sum of transmit and receive packets.
4241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     *
4251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * The packet counts are initialized and reset to -1 and
4261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * remain -1 until they can be updated.
4271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
4281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public static class TxRxSum {
4291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public long txPkts;
4301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public long rxPkts;
4311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public TxRxSum() {
4331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            reset();
4341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public TxRxSum(long txPkts, long rxPkts) {
4371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            this.txPkts = txPkts;
4381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            this.rxPkts = rxPkts;
4391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public TxRxSum(TxRxSum sum) {
4421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            txPkts = sum.txPkts;
4431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            rxPkts = sum.rxPkts;
4441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public void reset() {
4471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            txPkts = -1;
4481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            rxPkts = -1;
4491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        @Override
4521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public String toString() {
4531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            return "{txSum=" + txPkts + " rxSum=" + rxPkts + "}";
4541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public void updateTxRxSum() {
4571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            this.txPkts = TrafficStats.getMobileTcpTxPackets();
4581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            this.rxPkts = TrafficStats.getMobileTcpRxPackets();
4591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
4611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onActionIntentReconnectAlarm(Intent intent) {
4631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        String reason = intent.getStringExtra(INTENT_RECONNECT_ALARM_EXTRA_REASON);
4641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        String apnType = intent.getStringExtra(INTENT_RECONNECT_ALARM_EXTRA_TYPE);
4651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int phoneSubId = mPhone.getSubId();
4671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int currSubId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
4681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
4691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        log("onActionIntentReconnectAlarm: currSubId = " + currSubId + " phoneSubId=" + phoneSubId);
4701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // Stop reconnect if not current subId is not correct.
4721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // FIXME STOPSHIP - phoneSubId is coming up as -1 way after boot and failing this?
4731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (!SubscriptionManager.isValidSubscriptionId(currSubId) || (currSubId != phoneSubId)) {
4741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("receive ReconnectAlarm but subId incorrect, ignore");
4751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            return;
4761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnContext apnContext = mApnContexts.get(apnType);
4791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) {
4811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("onActionIntentReconnectAlarm: mState=" + mState + " reason=" + reason +
4821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " apnType=" + apnType + " apnContext=" + apnContext +
4831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " mDataConnectionAsyncChannels=" + mDataConnectionAcHashMap);
4841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
4851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if ((apnContext != null) && (apnContext.isEnabled())) {
4871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            apnContext.setReason(reason);
4881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            DctConstants.State apnContextState = apnContext.getState();
4891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
4901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("onActionIntentReconnectAlarm: apnContext state=" + apnContextState);
4911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
4921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if ((apnContextState == DctConstants.State.FAILED)
4931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    || (apnContextState == DctConstants.State.IDLE)) {
4941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) {
4951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("onActionIntentReconnectAlarm: state is FAILED|IDLE, disassociate");
4961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
4971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DcAsyncChannel dcac = apnContext.getDcAc();
4981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (dcac != null) {
4991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (DBG) {
5001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        log("onActionIntentReconnectAlarm: tearDown apnContext=" + apnContext);
5011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
5021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    dcac.tearDown(apnContext, "", null);
5031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
5041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                apnContext.setDataConnectionAc(null);
5051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                apnContext.setState(DctConstants.State.IDLE);
5061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
5071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("onActionIntentReconnectAlarm: keep associated");
5081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
5091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // TODO: IF already associated should we send the EVENT_TRY_SETUP_DATA???
5101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            sendMessage(obtainMessage(DctConstants.EVENT_TRY_SETUP_DATA, apnContext));
5111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            apnContext.setReconnectIntent(null);
5131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
5141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
5151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onActionIntentDataStallAlarm(Intent intent) {
5171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG_STALL) log("onActionIntentDataStallAlarm: action=" + intent.getAction());
5181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.EVENT_DATA_STALL_ALARM,
5191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                intent.getAction());
5201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = intent.getIntExtra(DATA_STALL_ALARM_TAG_EXTRA, 0);
5211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
5221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
5231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final ConnectivityManager mCm;
525c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
526b3a03455be44a2a0ffbeb757b193d860c1878599Naveen Kalla    /**
527a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     * List of messages that are waiting to be posted, when data call disconnect
528a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     * is complete
529a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     */
530a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private ArrayList<Message> mDisconnectAllCompleteMsgList = new ArrayList<Message>();
531a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
532a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private RegistrantList mAllDataDisconnectedRegistrants = new RegistrantList();
533a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
5341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // member variables
5351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final Phone mPhone;
5361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final UiccController mUiccController;
5371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final AtomicReference<IccRecords> mIccRecords = new AtomicReference<IccRecords>();
5381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private DctConstants.Activity mActivity = DctConstants.Activity.NONE;
5391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private DctConstants.State mState = DctConstants.State.IDLE;
5401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final Handler mDataConnectionTracker;
5411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private long mTxPkts;
5431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private long mRxPkts;
5441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int mNetStatPollPeriod;
5451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mNetStatPollEnabled = false;
5461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private TxRxSum mDataStallTxRxSum = new TxRxSum(0, 0);
5481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Used to track stale data stall alarms.
5491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int mDataStallAlarmTag = (int) SystemClock.elapsedRealtime();
5501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // The current data stall alarm intent
5511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private PendingIntent mDataStallAlarmIntent = null;
5521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Number of packets sent since the last received packet
5531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private long mSentSinceLastRecv;
5541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Controls when a simple recovery attempt it to be tried
5551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int mNoRecvPollCount = 0;
5560e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu    // Reference counter for enabling fail fast
5571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static int sEnableFailFastRefCounter = 0;
5581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // True if data stall detection is enabled
5591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private volatile boolean mDataStallDetectionEnabled = true;
5601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private volatile boolean mFailFast = false;
5621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // True when in voice call
5641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mInVoiceCall = false;
5651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // wifi connection status will be updated by sticky intent
5671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mIsWifiConnected = false;
5681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** Intent sent when the reconnect alarm fires. */
5701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private PendingIntent mReconnectIntent = null;
5711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // When false we will not auto attach and manually attaching is required.
5731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mAutoAttachOnCreationConfig = false;
5741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private AtomicBoolean mAutoAttachOnCreation = new AtomicBoolean(false);
5751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // State of screen
5771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // (TODO: Reconsider tying directly to screen, maybe this is
5781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    //        really a lower power mode")
5791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mIsScreenOn = true;
5801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Indicates if we found mvno-specific APNs in the full APN list.
5821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // used to determine if we can accept mno-specific APN for tethering.
5831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean mMvnoMatched = false;
5841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** Allows the generation of unique Id's for DataConnection objects */
5861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private AtomicInteger mUniqueIdGenerator = new AtomicInteger(0);
5871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** The data connections. */
5891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private HashMap<Integer, DataConnection> mDataConnections =
5901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            new HashMap<Integer, DataConnection>();
5911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** The data connection async channels */
5931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private HashMap<Integer, DcAsyncChannel> mDataConnectionAcHashMap =
5941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            new HashMap<Integer, DcAsyncChannel>();
5951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** Convert an ApnType string to Id (TODO: Use "enumeration" instead of String for ApnType) */
5971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private HashMap<String, Integer> mApnToDataConnectionId = new HashMap<String, Integer>();
5981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
5991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /** Phone.APN_TYPE_* ===> ApnContext */
6001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private final ConcurrentHashMap<String, ApnContext> mApnContexts =
6011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            new ConcurrentHashMap<String, ApnContext>();
6021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
603af5593594070f825032be46dced573cd195956e1Robert Greenwalt    private final SparseArray<ApnContext> mApnContextsById = new SparseArray<ApnContext>();
604af5593594070f825032be46dced573cd195956e1Robert Greenwalt
6051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int mDisconnectPendingCount = 0;
606a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
6074c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    /** Indicate if metered APNs are disabled.
6084c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu     *  set to block all the metered APNs from continuously sending requests, which causes
6094c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu     *  undesired network load */
6104c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    private boolean mMeteredApnDisabled = false;
61168f4f4a0bc8d4060b5775e7a24a97ea5b485989efionaxu
612a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    /**
613cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Handles changes to the APN db.
614b3a03455be44a2a0ffbeb757b193d860c1878599Naveen Kalla     */
615cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private class ApnChangeObserver extends ContentObserver {
616cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        public ApnChangeObserver () {
617cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            super(mDataConnectionTracker);
618cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
619c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
620cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        @Override
621cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        public void onChange(boolean selfChange) {
622cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            sendMessage(obtainMessage(DctConstants.EVENT_APN_CHANGED));
623cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
624cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
625c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
626cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    //***** Instance Variables
627c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
628cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private boolean mReregisterOnReconnectFailure = false;
629c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
630c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
631cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    //***** Constants
632c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
633ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    // Used by puppetmaster/*/radio_stress.py
634ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    private static final String PUPPET_MASTER_RADIO_STRESS_TEST = "gsm.defaultpdpcontext.active";
635c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
636ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    private static final int POLL_PDP_MILLIS = 5 * 1000;
637c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
6382b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen    private static final int PROVISIONING_SPINNER_TIMEOUT_MILLIS = 120 * 1000;
6392b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen
6406bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville    static final Uri PREFERAPN_NO_UPDATE_URI_USING_SUBID =
6416bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville                        Uri.parse("content://telephony/carriers/preferapn_no_update/subId/");
642cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    static final String APN_ID = "apn_id";
643cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
644ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    private boolean mCanSetPreferApn = false;
645c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
646187a39f896f88eb6c5e4306d9595546654825976Wink Saville    private AtomicBoolean mAttached = new AtomicBoolean(false);
647187a39f896f88eb6c5e4306d9595546654825976Wink Saville
648cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /** Watches for changes to the APN db. */
649cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private ApnChangeObserver mApnObserver;
650cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
651b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    private final String mProvisionActionName;
652b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    private BroadcastReceiver mProvisionBroadcastReceiver;
6532b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen    private ProgressDialog mProvisioningSpinner;
654b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
655a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public boolean mImsRegistrationState = false;
656a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
657cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    //***** Constructor
6581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public DcTracker(Phone phone) {
6591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        super();
6601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone = phone;
6611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("DCT.constructor");
6631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mResolver = mPhone.getContext().getContentResolver();
6651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mUiccController = UiccController.getInstance();
6661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mUiccController.registerForIccChanged(this, DctConstants.EVENT_ICC_CHANGED, null);
6671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mAlarmManager =
6681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                (AlarmManager) mPhone.getContext().getSystemService(Context.ALARM_SERVICE);
6691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mCm = (ConnectivityManager) mPhone.getContext().getSystemService(
6701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Context.CONNECTIVITY_SERVICE);
6711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        IntentFilter filter = new IntentFilter();
6741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        filter.addAction(Intent.ACTION_SCREEN_ON);
6751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        filter.addAction(Intent.ACTION_SCREEN_OFF);
6761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
6771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
6781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        filter.addAction(INTENT_DATA_STALL_ALARM);
6791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        filter.addAction(INTENT_PROVISIONING_APN_ALARM);
6801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // TODO - redundent with update call below?
682a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        mDataEnabledSettings.setUserDataEnabled(getDataEnabled());
6831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getContext().registerReceiver(mIntentReceiver, filter, null, mPhone);
6851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
6871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mAutoAttachOnCreation.set(sp.getBoolean(Phone.DATA_DISABLED_ON_BOOT_KEY, false));
6881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mSubscriptionManager = SubscriptionManager.from(mPhone.getContext());
6901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangedListener);
6911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
6921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        HandlerThread dcHandlerThread = new HandlerThread("DcHandlerThread");
6931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        dcHandlerThread.start();
6941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Handler dcHandler = new Handler(dcHandlerThread.getLooper());
6951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDcc = DcController.makeDcc(mPhone, this, dcHandler);
6961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDcTesterFailBringUpAll = new DcTesterFailBringUpAll(mPhone, dcHandler);
697cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
698cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mDataConnectionTracker = this;
699c374098c17a81f73f51e9d7df99eba574882949bYifan Bai        registerForAllEvents();
700a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        update();
701cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mApnObserver = new ApnChangeObserver();
7021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        phone.getContext().getContentResolver().registerContentObserver(
703cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                Telephony.Carriers.CONTENT_URI, true, mApnObserver);
704cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
705d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        initApnContexts();
706d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt
707d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        for (ApnContext apnContext : mApnContexts.values()) {
708d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            // Register the reconnect and restart actions.
7091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            filter = new IntentFilter();
710d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            filter.addAction(INTENT_RECONNECT_ALARM + '.' + apnContext.getApnType());
711d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            mPhone.getContext().registerReceiver(mIntentReceiver, filter, null, mPhone);
712d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        }
713d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt
71476f43316a5a6082d601bffd4b6898d0bd81e11fcram        // Add Emergency APN to APN setting list by default to support EPDN in sim absent cases
71576f43316a5a6082d601bffd4b6898d0bd81e11fcram        initEmergencyApnSetting();
71676f43316a5a6082d601bffd4b6898d0bd81e11fcram        addEmergencyApnSetting();
717b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
7181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mProvisionActionName = "com.android.internal.telephony.PROVISION" + phone.getPhoneId();
719f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
720f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver = new SettingsObserver(mPhone.getContext(), this);
721f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        registerSettingsObserver();
7221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
7231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
724af5593594070f825032be46dced573cd195956e1Robert Greenwalt    @VisibleForTesting
725af5593594070f825032be46dced573cd195956e1Robert Greenwalt    public DcTracker() {
726af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mAlarmManager = null;
727af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mCm = null;
728af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mPhone = null;
729af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mUiccController = null;
730af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mDataConnectionTracker = null;
731af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mProvisionActionName = null;
732f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver = new SettingsObserver(null, this);
733af5593594070f825032be46dced573cd195956e1Robert Greenwalt    }
734af5593594070f825032be46dced573cd195956e1Robert Greenwalt
7351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public void registerServiceStateTrackerEvents() {
7361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForDataConnectionAttached(this,
7371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null);
7381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForDataConnectionDetached(this,
7391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_DATA_CONNECTION_DETACHED, null);
7401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForDataRoamingOn(this,
7411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_ROAMING_ON, null);
7421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForDataRoamingOff(this,
7431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_ROAMING_OFF, null);
7441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForPsRestrictedEnabled(this,
7451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_PS_RESTRICT_ENABLED, null);
7461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForPsRestrictedDisabled(this,
7471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_PS_RESTRICT_DISABLED, null);
7481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().registerForDataRegStateOrRatChanged(this,
7491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_DATA_RAT_CHANGED, null);
750cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
751c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
7521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public void unregisterServiceStateTrackerEvents() {
7531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForDataConnectionAttached(this);
7541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForDataConnectionDetached(this);
7551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForDataRoamingOn(this);
7561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForDataRoamingOff(this);
7571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForPsRestrictedEnabled(this);
7581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForPsRestrictedDisabled(this);
7591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getServiceStateTracker().unregisterForDataRegStateOrRatChanged(this);
7601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
7611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
7621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void registerForAllEvents() {
763a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mPhone.mCi.registerForAvailable(this, DctConstants.EVENT_RADIO_AVAILABLE, null);
764a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mPhone.mCi.registerForOffOrNotAvailable(this,
7659c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu                DctConstants.EVENT_RADIO_OFF_OR_NOT_AVAILABLE, null);
766a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mPhone.mCi.registerForDataNetworkStateChanged(this,
7679c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu                DctConstants.EVENT_DATA_STATE_CHANGED, null);
7680710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // Note, this is fragile - the Phone is now presenting a merged picture
7690710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // of PS (volte) & CS and by diving into its internals you're just seeing
7700710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // the CS data.  This works well for the purposes this is currently used for
7710710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // but that may not always be the case.  Should probably be redesigned to
7720710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // accurately reflect what we're really interested in (registerForCSVoiceCallEnded).
7731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getCallTracker().registerForVoiceCallEnded(this,
7741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_VOICE_CALL_ENDED, null);
7751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getCallTracker().registerForVoiceCallStarted(this,
7761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DctConstants.EVENT_VOICE_CALL_STARTED, null);
7771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        registerServiceStateTrackerEvents();
778a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     //   SubscriptionManager.registerForDdsSwitch(this,
779a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     //          DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS, null);
7802e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        mPhone.mCi.registerForPcoData(this, DctConstants.EVENT_PCO_DATA_RECEIVED, null);
781a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
7821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
783cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public void dispose() {
7841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("DCT.dispose");
7854dfda5470a2582c0fb543ead6c79ccf598c580e0Robert Greenwalt
786b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        if (mProvisionBroadcastReceiver != null) {
787b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            mPhone.getContext().unregisterReceiver(mProvisionBroadcastReceiver);
788b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            mProvisionBroadcastReceiver = null;
789b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        }
7902b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen        if (mProvisioningSpinner != null) {
7912b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.dismiss();
7922b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner = null;
7932b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen        }
794b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
795cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        cleanUpAllConnections(true, null);
796cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
7971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        for (DcAsyncChannel dcac : mDataConnectionAcHashMap.values()) {
7981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            dcac.disconnect();
7991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
8001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDataConnectionAcHashMap.clear();
8011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mIsDisposed = true;
8021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.getContext().unregisterReceiver(mIntentReceiver);
8031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mUiccController.unregisterForIccChanged(this);
804f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        mSettingsObserver.unobserve();
805f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
8061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mSubscriptionManager
8071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                .removeOnSubscriptionsChangedListener(mOnSubscriptionsChangedListener);
8081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDcc.dispose();
8091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDcTesterFailBringUpAll.dispose();
810cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
811a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mPhone.getContext().getContentResolver().unregisterContentObserver(mApnObserver);
812a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mApnContexts.clear();
813af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mApnContextsById.clear();
814a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mPrioritySortedApnContexts.clear();
815c374098c17a81f73f51e9d7df99eba574882949bYifan Bai        unregisterForAllEvents();
816a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
817a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        destroyDataConnections();
818a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
8191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
8201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void unregisterForAllEvents() {
821a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville         //Unregister for all events
82222d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mPhone.mCi.unregisterForAvailable(this);
82322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mPhone.mCi.unregisterForOffOrNotAvailable(this);
824cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        IccRecords r = mIccRecords.get();
825a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (r != null) {
826a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            r.unregisterForRecordsLoaded(this);
827a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            mIccRecords.set(null);
828a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
82922d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mPhone.mCi.unregisterForDataNetworkStateChanged(this);
830cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mPhone.getCallTracker().unregisterForVoiceCallEnded(this);
831cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mPhone.getCallTracker().unregisterForVoiceCallStarted(this);
8321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        unregisterServiceStateTrackerEvents();
833a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        //SubscriptionManager.unregisterForDdsSwitch(this);
8342e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        mPhone.mCi.unregisterForPcoData(this);
835cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
836cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
8371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
8381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Called when EVENT_RESET_DONE is received so goto
8391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * IDLE state and send notifications to those interested.
8401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     *
8411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * TODO - currently unused.  Needs to be hooked into DataConnection cleanup
8421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * TODO - needs to pass some notion of which connection is reset..
8431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
8441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onResetDone(AsyncResult ar) {
8451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("EVENT_RESET_DONE");
8461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        String reason = null;
8471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (ar.userObj instanceof String) {
8481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            reason = (String) ar.userObj;
8491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
8501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        gotoIdleAndNotifyDataConnection(reason);
8511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
8521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
8531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
8541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Modify {@link android.provider.Settings.Global#MOBILE_DATA} value.
8551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
8561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public void setDataEnabled(boolean enable) {
8571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.CMD_SET_USER_DATA_ENABLE);
8581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = enable ? 1 : 0;
8591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("setDataEnabled: sendMessage: enable=" + enable);
8601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
8611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
8621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
8631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onSetUserDataEnabled(boolean enabled) {
864a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        synchronized (mDataEnabledSettings) {
865a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            if (mDataEnabledSettings.isUserDataEnabled() != enabled) {
866a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                mDataEnabledSettings.setUserDataEnabled(enabled);
8671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
868a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                //TODO: We should move the followings into DataEnabledSettings class.
8691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // For single SIM phones, this is a per phone property.
8701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (TelephonyManager.getDefault().getSimCount() == 1) {
8711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    Settings.Global.putInt(mResolver, Settings.Global.MOBILE_DATA, enabled ? 1 : 0);
8721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else {
8731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    int phoneSubId = mPhone.getSubId();
8741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    Settings.Global.putInt(mResolver, Settings.Global.MOBILE_DATA + phoneSubId,
8751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            enabled ? 1 : 0);
8761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
8771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (getDataOnRoamingEnabled() == false &&
8781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mPhone.getServiceState().getDataRoaming() == true) {
8791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (enabled) {
8801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        notifyOffApnsOfAvailability(Phone.REASON_ROAMING_ON);
8811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    } else {
8821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        notifyOffApnsOfAvailability(Phone.REASON_DATA_DISABLED);
8831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
8841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
8851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
886a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                // TODO: We should register for DataEnabledSetting's data enabled/disabled event and
887a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                // handle the rest from there.
8881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (enabled) {
8895292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                    teardownRestrictedMeteredConnections();
8901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    onTrySetupData(Phone.REASON_DATA_ENABLED);
8911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else {
8921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    onCleanUpAllConnections(Phone.REASON_DATA_SPECIFIC_DISABLED);
8931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
8941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
8951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
8961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
8971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
8985292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt    /**
8995292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     * Handle reverting restricted networks back to unrestricted.
9005292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     * If we're changing user data to enabled and this makes data
9015292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     * truely enabled (not disabled by other factors) we need to
9025292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     * tear down any metered apn type that was enabled anyway by
9035292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     * a privileged request.  This allows us to reconnect
9045292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     * to it in an unrestricted way.
9055292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt     */
9065292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt    private void teardownRestrictedMeteredConnections() {
907a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        if (mDataEnabledSettings.isDataEnabled(true)) {
9085292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt            for (ApnContext apnContext : mApnContexts.values()) {
9095292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                if (apnContext.isConnectedOrConnecting() &&
9105292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                        apnContext.getApnSetting().isMetered(mPhone.getContext(),
9115292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                        mPhone.getSubId(), mPhone.getServiceState().getDataRoaming())) {
91276c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt
91376c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                    final DcAsyncChannel dataConnectionAc = apnContext.getDcAc();
91476c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                    if (dataConnectionAc != null) {
91576c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                        final NetworkCapabilities nc =
91676c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                                dataConnectionAc.getNetworkCapabilitiesSync();
91776c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                        if (nc != null && nc.hasCapability(NetworkCapabilities.
91876c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                              NET_CAPABILITY_NOT_RESTRICTED)) {
91976c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                            if (DBG) log("not tearing down unrestricted metered net:" + apnContext);
92076c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                            continue;
92176c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                        }
92276c5a9479d312139740dcaf7644172f2beb2f7e0Robert Greenwalt                    }
9235292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                    if (DBG) log("tearing down restricted metered net: " + apnContext);
9245292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                    apnContext.setReason(Phone.REASON_DATA_ENABLED);
9255292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                    cleanUpConnection(true, apnContext);
9265292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                }
9275292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt            }
9285292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt        }
9295292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt    }
9305292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt
931f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt    private void onDeviceProvisionedChange() {
932f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        if (getDataEnabled()) {
933a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            mDataEnabledSettings.setUserDataEnabled(true);
9345292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt            teardownRestrictedMeteredConnections();
935f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            onTrySetupData(Phone.REASON_DATA_ENABLED);
936f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        } else {
937a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            mDataEnabledSettings.setUserDataEnabled(false);
938f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            onCleanUpAllConnections(Phone.REASON_DATA_SPECIFIC_DISABLED);
939f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        }
940f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt    }
9411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
9421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
9431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public long getSubId() {
9441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return mPhone.getSubId();
9451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
9461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
9471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public DctConstants.Activity getActivity() {
9481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return mActivity;
9491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
9501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
9511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void setActivity(DctConstants.Activity activity) {
9521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        log("setActivity = " + activity);
9531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mActivity = activity;
9541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mPhone.notifyDataActivity();
9551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
9561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
957af5593594070f825032be46dced573cd195956e1Robert Greenwalt    public void requestNetwork(NetworkRequest networkRequest, LocalLog log) {
958af5593594070f825032be46dced573cd195956e1Robert Greenwalt        final int apnId = ApnContext.apnIdForNetworkRequest(networkRequest);
959af5593594070f825032be46dced573cd195956e1Robert Greenwalt        final ApnContext apnContext = mApnContextsById.get(apnId);
960af5593594070f825032be46dced573cd195956e1Robert Greenwalt        log.log("DcTracker.requestNetwork for " + networkRequest + " found " + apnContext);
961692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt        if (apnContext != null) apnContext.requestNetwork(networkRequest, log);
962af5593594070f825032be46dced573cd195956e1Robert Greenwalt    }
963af5593594070f825032be46dced573cd195956e1Robert Greenwalt
964af5593594070f825032be46dced573cd195956e1Robert Greenwalt    public void releaseNetwork(NetworkRequest networkRequest, LocalLog log) {
965af5593594070f825032be46dced573cd195956e1Robert Greenwalt        final int apnId = ApnContext.apnIdForNetworkRequest(networkRequest);
966af5593594070f825032be46dced573cd195956e1Robert Greenwalt        final ApnContext apnContext = mApnContextsById.get(apnId);
967af5593594070f825032be46dced573cd195956e1Robert Greenwalt        log.log("DcTracker.releaseNetwork for " + networkRequest + " found " + apnContext);
968692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt        if (apnContext != null) apnContext.releaseNetwork(networkRequest, log);
969af5593594070f825032be46dced573cd195956e1Robert Greenwalt    }
970af5593594070f825032be46dced573cd195956e1Robert Greenwalt
971bda761320929f714951c328bfec6a51a1978db97Wink Saville    public boolean isApnSupported(String name) {
97291bce2abae052df918cb546b9c5d205706ede026Shishir Agrawal        if (name == null) {
97391bce2abae052df918cb546b9c5d205706ede026Shishir Agrawal            loge("isApnSupported: name=null");
97491bce2abae052df918cb546b9c5d205706ede026Shishir Agrawal            return false;
97591bce2abae052df918cb546b9c5d205706ede026Shishir Agrawal        }
976bda761320929f714951c328bfec6a51a1978db97Wink Saville        ApnContext apnContext = mApnContexts.get(name);
977bda761320929f714951c328bfec6a51a1978db97Wink Saville        if (apnContext == null) {
978bda761320929f714951c328bfec6a51a1978db97Wink Saville            loge("Request for unsupported mobile name: " + name);
979bda761320929f714951c328bfec6a51a1978db97Wink Saville            return false;
980071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt        }
981bda761320929f714951c328bfec6a51a1978db97Wink Saville        return true;
982bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
983071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt
984bda761320929f714951c328bfec6a51a1978db97Wink Saville    public int getApnPriority(String name) {
985071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt        ApnContext apnContext = mApnContexts.get(name);
986071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt        if (apnContext == null) {
987bda761320929f714951c328bfec6a51a1978db97Wink Saville            loge("Request for unsupported mobile name: " + name);
988071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt        }
989bda761320929f714951c328bfec6a51a1978db97Wink Saville        return apnContext.priority;
990071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt    }
991071b9f85ac559a35430ed37c03a66271977b9d17Robert Greenwalt
992b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    // Turn telephony radio on or off.
993b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    private void setRadio(boolean on) {
994b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
995b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        try {
996b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            phone.setRadio(on);
997b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        } catch (Exception e) {
998b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            // Ignore.
999b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        }
1000b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    }
1001b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
1002b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    // Class to handle Intent dispatched with user selects the "Sign-in to network"
1003b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    // notification.
1004b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    private class ProvisionNotificationBroadcastReceiver extends BroadcastReceiver {
10052b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen        private final String mNetworkOperator;
1006b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        // Mobile provisioning URL.  Valid while provisioning notification is up.
1007b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        // Set prior to notification being posted as URL contains ICCID which
1008b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        // disappears when radio is off (which is the case when notification is up).
1009b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        private final String mProvisionUrl;
1010b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
10112b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen        public ProvisionNotificationBroadcastReceiver(String provisionUrl, String networkOperator) {
10122b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mNetworkOperator = networkOperator;
1013b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            mProvisionUrl = provisionUrl;
1014b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        }
1015b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
1016b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        private void setEnableFailFastMobileData(int enabled) {
10176395443719ec3ee0257085945e753d02f603886bRobert Greenwalt            sendMessage(obtainMessage(DctConstants.CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA, enabled, 0));
1018b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        }
1019b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
1020b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        private void enableMobileProvisioning() {
1021b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            final Message msg = obtainMessage(DctConstants.CMD_ENABLE_MOBILE_PROVISIONING);
1022b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            msg.setData(Bundle.forPair(DctConstants.PROVISIONING_URL_KEY, mProvisionUrl));
1023b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            sendMessage(msg);
1024b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        }
1025b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
1026b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        @Override
1027b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        public void onReceive(Context context, Intent intent) {
10282b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            // Turning back on the radio can take time on the order of a minute, so show user a
10292b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            // spinner so they know something is going on.
10302b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner = new ProgressDialog(context);
10312b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.setTitle(mNetworkOperator);
10322b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.setMessage(
10332b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    // TODO: Don't borrow "Connecting..." i18n string; give Telephony a version.
10342b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    context.getText(com.android.internal.R.string.media_route_status_connecting));
10352b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.setIndeterminate(true);
10362b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.setCancelable(true);
10372b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            // Allow non-Activity Service Context to create a View.
10382b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.getWindow().setType(
10392b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
10402b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            mProvisioningSpinner.show();
10412b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            // After timeout, hide spinner so user can at least use their device.
10422b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            // TODO: Indicate to user that it is taking an unusually long time to connect?
10432b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            sendMessageDelayed(obtainMessage(DctConstants.CMD_CLEAR_PROVISIONING_SPINNER,
10442b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    mProvisioningSpinner), PROVISIONING_SPINNER_TIMEOUT_MILLIS);
1045b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            // This code is almost identical to the old
1046b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            // ConnectivityService.handleMobileProvisioningAction code.
1047b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            setRadio(true);
1048b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            setEnableFailFastMobileData(DctConstants.ENABLED);
1049b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen            enableMobileProvisioning();
1050b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen        }
1051b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen    }
1052b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen
1053cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public boolean isDataPossible(String apnType) {
1054cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ApnContext apnContext = mApnContexts.get(apnType);
1055cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext == null) {
1056cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return false;
1057cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1058cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean apnContextIsEnabled = apnContext.isEnabled();
1059cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        DctConstants.State apnContextState = apnContext.getState();
1060cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean apnTypePossible = !(apnContextIsEnabled &&
1061cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                (apnContextState == DctConstants.State.FAILED));
1062cf5205f70eb1eac497164124187a088ecb03fff5Ram        boolean isEmergencyApn = apnContext.getApnType().equals(PhoneConstants.APN_TYPE_EMERGENCY);
1063cf5205f70eb1eac497164124187a088ecb03fff5Ram        // Set the emergency APN availability status as TRUE irrespective of conditions checked in
1064cf5205f70eb1eac497164124187a088ecb03fff5Ram        // isDataAllowed() like IN_SERVICE, MOBILE DATA status etc.
10659c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu        boolean dataAllowed = isEmergencyApn || isDataAllowed(null);
1066cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean possible = dataAllowed && apnTypePossible;
1067cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
10680e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        if ((apnContext.getApnType().equals(PhoneConstants.APN_TYPE_DEFAULT)
10690e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh                    || apnContext.getApnType().equals(PhoneConstants.APN_TYPE_IA))
10700e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh                && (mPhone.getServiceState().getRilDataRadioTechnology()
10710e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh                == ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN)) {
10720e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh            log("Default data call activation not possible in iwlan.");
10730e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh            possible = false;
10740e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        }
10750e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh
1076ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (VDBG) {
1077cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log(String.format("isDataPossible(%s): possible=%b isDataAllowed=%b " +
1078a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu                            "apnTypePossible=%b apnContextisEnabled=%b apnContextState()=%s",
1079cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnType, possible, dataAllowed, apnTypePossible,
1080cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContextIsEnabled, apnContextState));
1081cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1082cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return possible;
1083cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1084cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1085cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    @Override
1086cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    protected void finalize() {
1087cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if(DBG) log("finalize");
1088cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1089cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
10904a9b3afeb2ec4d573eca335a3706392ecf9f281eWink Saville    private ApnContext addApnContext(String type, NetworkConfig networkConfig) {
10910e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        ApnContext apnContext = new ApnContext(mPhone, type, LOG_TAG, networkConfig, this);
1092cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mApnContexts.put(type, apnContext);
1093af5593594070f825032be46dced573cd195956e1Robert Greenwalt        mApnContextsById.put(ApnContext.apnIdForApnName(type), apnContext);
10943fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        mPrioritySortedApnContexts.add(apnContext);
1095bce3d2575122929bb27ec8a37d56e96da39a3ca2Robert Greenwalt        return apnContext;
1096cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1097c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
10981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void initApnContexts() {
1099d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        log("initApnContexts: E");
1100d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        // Load device network attributes from resources
1101d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        String[] networkConfigStrings = mPhone.getContext().getResources().getStringArray(
1102d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                com.android.internal.R.array.networkAttributes);
1103d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        for (String networkConfigString : networkConfigStrings) {
1104d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            NetworkConfig networkConfig = new NetworkConfig(networkConfigString);
1105d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            ApnContext apnContext = null;
1106d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt
1107d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            switch (networkConfig.type) {
1108d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE:
1109d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_DEFAULT, networkConfig);
1110d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1111d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_MMS:
1112d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_MMS, networkConfig);
1113d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1114d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_SUPL:
1115d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_SUPL, networkConfig);
1116d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1117d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_DUN:
1118d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_DUN, networkConfig);
1119d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1120d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_HIPRI:
1121d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_HIPRI, networkConfig);
1122d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1123d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_FOTA:
1124d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_FOTA, networkConfig);
1125d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1126d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_IMS:
1127d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_IMS, networkConfig);
1128d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1129d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_CBS:
1130d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_CBS, networkConfig);
1131d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1132d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            case ConnectivityManager.TYPE_MOBILE_IA:
1133d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                apnContext = addApnContext(PhoneConstants.APN_TYPE_IA, networkConfig);
1134d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                break;
1135cf5205f70eb1eac497164124187a088ecb03fff5Ram            case ConnectivityManager.TYPE_MOBILE_EMERGENCY:
1136cf5205f70eb1eac497164124187a088ecb03fff5Ram                apnContext = addApnContext(PhoneConstants.APN_TYPE_EMERGENCY, networkConfig);
1137cf5205f70eb1eac497164124187a088ecb03fff5Ram                break;
1138d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            default:
1139d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                log("initApnContexts: skipping unknown type=" + networkConfig.type);
1140d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt                continue;
1141d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            }
1142d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt            log("initApnContexts: apnContext=" + apnContext);
1143d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt        }
1144092e6bd60f1a4a3a55fb73ad0efca1122b8e15e2Jack Yu
1145092e6bd60f1a4a3a55fb73ad0efca1122b8e15e2Jack Yu        if (VDBG) log("initApnContexts: X mApnContexts=" + mApnContexts);
1146d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt    }
1147d32b58c8e7cea693c98f49b2291455e917cd9301Robert Greenwalt
1148cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public LinkProperties getLinkProperties(String apnType) {
1149cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ApnContext apnContext = mApnContexts.get(apnType);
1150cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext != null) {
1151454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville            DcAsyncChannel dcac = apnContext.getDcAc();
1152cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (dcac != null) {
1153cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("return link properites for " + apnType);
1154cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return dcac.getLinkPropertiesSync();
1155cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1156cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1157cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("return new LinkProperties");
1158cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return new LinkProperties();
1159cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1160cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
1161608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt    public NetworkCapabilities getNetworkCapabilities(String apnType) {
1162608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt        ApnContext apnContext = mApnContexts.get(apnType);
1163608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt        if (apnContext!=null) {
1164608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt            DcAsyncChannel dataConnectionAc = apnContext.getDcAc();
1165608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt            if (dataConnectionAc != null) {
1166608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt                if (DBG) {
1167608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt                    log("get active pdp is not null, return NetworkCapabilities for " + apnType);
1168608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt                }
1169608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt                return dataConnectionAc.getNetworkCapabilitiesSync();
1170608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt            }
1171608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt        }
1172608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt        if (DBG) log("return new NetworkCapabilities");
1173608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt        return new NetworkCapabilities();
1174608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt    }
1175cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1176cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    // Return all active apn types
1177cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public String[] getActiveApnTypes() {
1178cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("get all active apn types");
1179cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ArrayList<String> result = new ArrayList<String>();
1180cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
1181cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
1182187a39f896f88eb6c5e4306d9595546654825976Wink Saville            if (mAttached.get() && apnContext.isReady()) {
1183cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                result.add(apnContext.getApnType());
1184cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla            }
1185cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
1186c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1187cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return result.toArray(new String[0]);
1188cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1189cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1190cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    // Return active apn of specific apn type
1191cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public String getActiveApnString(String apnType) {
1192ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (VDBG) log( "get active apn string for type:" + apnType);
1193cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ApnContext apnContext = mApnContexts.get(apnType);
1194cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext != null) {
1195cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            ApnSetting apnSetting = apnContext.getApnSetting();
1196cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (apnSetting != null) {
1197cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return apnSetting.apn;
1198cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1199cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1200cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return null;
1201cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1202cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1203cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    // Return state of specific apn type
1204cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public DctConstants.State getState(String apnType) {
1205cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ApnContext apnContext = mApnContexts.get(apnType);
1206cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext != null) {
1207cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return apnContext.getState();
1208c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1209cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return DctConstants.State.FAILED;
1210cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1211c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1212c9b81a0c05128694c617fcdd67e73821895822feWink Saville    // Return if apn type is a provisioning apn.
12131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean isProvisioningApn(String apnType) {
1214c9b81a0c05128694c617fcdd67e73821895822feWink Saville        ApnContext apnContext = mApnContexts.get(apnType);
1215c9b81a0c05128694c617fcdd67e73821895822feWink Saville        if (apnContext != null) {
1216c9b81a0c05128694c617fcdd67e73821895822feWink Saville            return apnContext.isProvisioningApn();
1217c9b81a0c05128694c617fcdd67e73821895822feWink Saville        }
1218c9b81a0c05128694c617fcdd67e73821895822feWink Saville        return false;
1219c9b81a0c05128694c617fcdd67e73821895822feWink Saville    }
1220c9b81a0c05128694c617fcdd67e73821895822feWink Saville
1221cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    // Return state of overall
1222cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public DctConstants.State getOverallState() {
1223cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean isConnecting = false;
1224cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean isFailed = true; // All enabled Apns should be FAILED.
1225cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean isAnyEnabled = false;
1226cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1227cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
1228cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (apnContext.isEnabled()) {
1229cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                isAnyEnabled = true;
1230cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                switch (apnContext.getState()) {
1231cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                case CONNECTED:
1232cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                case DISCONNECTING:
12331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (VDBG) log("overall state is CONNECTED");
1234cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    return DctConstants.State.CONNECTED;
1235ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                case RETRYING:
1236cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                case CONNECTING:
1237cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    isConnecting = true;
1238cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    isFailed = false;
1239cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    break;
1240cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                case IDLE:
1241cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                case SCANNING:
1242cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    isFailed = false;
1243cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    break;
1244cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                default:
1245cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    isAnyEnabled = true;
1246cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    break;
1247cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
1248cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1249c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1250c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1251cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (!isAnyEnabled) { // Nothing enabled. return IDLE.
12521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG) log( "overall state is IDLE");
1253cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return DctConstants.State.IDLE;
1254c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1255c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1256cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (isConnecting) {
12571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG) log( "overall state is CONNECTING");
1258cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return DctConstants.State.CONNECTING;
1259cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else if (!isFailed) {
12601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG) log( "overall state is IDLE");
1261cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return DctConstants.State.IDLE;
1262cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
12631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG) log( "overall state is FAILED");
1264cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return DctConstants.State.FAILED;
1265c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1266c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1267c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1268cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /**
1269cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Report on whether data connectivity is enabled for any APN.
1270cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @return {@code false} if data connectivity has been explicitly disabled,
1271cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * {@code true} otherwise.
1272cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     */
1273cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public boolean getAnyDataEnabled() {
1274a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        if (!mDataEnabledSettings.isDataEnabled(true)) return false;
12753d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        DataAllowFailReason failureReason = new DataAllowFailReason();
12763d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (!isDataAllowed(failureReason)) {
12773d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if (DBG) log(failureReason.getDataAllowFailReason());
1278cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return false;
1279c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
12803d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        for (ApnContext apnContext : mApnContexts.values()) {
12813d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            // Make sure we don't have a context that is going down
12823d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            // and is explicitly disabled.
12833d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if (isDataAllowedForApn(apnContext)) {
12843d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                return true;
12853d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            }
12863d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
12873d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        return false;
1288c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1289c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
12900b03bcd1eccc833d5cac5ecf937cf0e037375561Jack Yu    @VisibleForTesting
12910b03bcd1eccc833d5cac5ecf937cf0e037375561Jack Yu    public boolean isDataEnabled(boolean checkUserDataEnabled) {
1292a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        return mDataEnabledSettings.isDataEnabled(checkUserDataEnabled);
1293a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
1294a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
12959c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu    private boolean isDataAllowedForApn(ApnContext apnContext) {
12960e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        //If RAT is iwlan then dont allow default/IA PDP at all.
12970e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        //Rest of APN types can be evaluated for remaining conditions.
12980e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        if ((apnContext.getApnType().equals(PhoneConstants.APN_TYPE_DEFAULT)
12990e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh                    || apnContext.getApnType().equals(PhoneConstants.APN_TYPE_IA))
13000e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh                && (mPhone.getServiceState().getRilDataRadioTechnology()
13010e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh                == ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN)) {
13020e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh            log("Default data call activation not allowed in iwlan.");
13030e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh            return false;
13040e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        }
13059c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu
13069c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu        return apnContext.isReady();
1307c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1308c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1309cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    //****** Called from ServiceStateTracker
1310c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
1311cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Invoked when ServiceStateTracker observes a transition from GPRS
1312cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * attach to detach.
1313c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
13141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onDataConnectionDetached() {
1315cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        /*
1316cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * We presently believe it is unnecessary to tear down the PDP context
1317cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * when GPRS detaches, but we should stop the network polling.
1318cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         */
1319cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log ("onDataConnectionDetached: stop polling and notify detached");
1320cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        stopNetStatPoll();
1321cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        stopDataStallAlarm();
1322cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        notifyDataConnection(Phone.REASON_DATA_DETACHED);
1323187a39f896f88eb6c5e4306d9595546654825976Wink Saville        mAttached.set(false);
1324cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1325c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1326cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private void onDataConnectionAttached() {
1327cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onDataConnectionAttached");
13287ab10e4710bdb54c6d9a5ee01cd443a42a2689f5Sungmin Choi        mAttached.set(true);
1329cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (getOverallState() == DctConstants.State.CONNECTED) {
1330cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("onDataConnectionAttached: start polling notify attached");
1331cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            startNetStatPoll();
1332cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
1333cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyDataConnection(Phone.REASON_DATA_ATTACHED);
1334cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
1335cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // update APN availability so that APN can be enabled.
1336cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(Phone.REASON_DATA_ATTACHED);
1337cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
133812fffcf0d8df6b8268806d9aa7cc7a662e73743bJing Zhao        if (mAutoAttachOnCreationConfig) {
1339aacc11b299ac047e73e1e712aa396ea0a6a80158Robert Greenwalt            mAutoAttachOnCreation.set(true);
134012fffcf0d8df6b8268806d9aa7cc7a662e73743bJing Zhao        }
1341ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        setupDataOnConnectableApns(Phone.REASON_DATA_ATTACHED);
1342cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1343c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
13443d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu    private boolean isDataAllowed(DataAllowFailReason failureReason) {
1345cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        final boolean internalDataEnabled;
1346a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        internalDataEnabled = mDataEnabledSettings.isInternalDataEnabled();
1347cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
13489894b3fb2f35e21d9cfd45f233ed093589e14c26sy.yun        boolean attachedState = mAttached.get();
1349cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean desiredPowerState = mPhone.getServiceStateTracker().getDesiredPowerState();
13500d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu        boolean radioStateFromCarrier = mPhone.getServiceStateTracker().getPowerStateFromCarrier();
13510e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
13520e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        if (radioTech == ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN) {
13530e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh            desiredPowerState = true;
13540d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu            radioStateFromCarrier = true;
13550e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh        }
13560e42864afb21261d6bd2e9b4aa97f6d01d039a25Yashdev Singh
1357cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        IccRecords r = mIccRecords.get();
13582b0f0da4f9fe8449c578632b491e2f158c201bd5Stuart Scott        boolean recordsLoaded = false;
13592b0f0da4f9fe8449c578632b491e2f158c201bd5Stuart Scott        if (r != null) {
13602b0f0da4f9fe8449c578632b491e2f158c201bd5Stuart Scott            recordsLoaded = r.getRecordsLoaded();
13619232dafa7ea833fc0b3a6024d6c7e23fc8e961eaRobert Greenwalt            if (DBG && !recordsLoaded) log("isDataAllowed getRecordsLoaded=" + recordsLoaded);
13622b0f0da4f9fe8449c578632b491e2f158c201bd5Stuart Scott        }
1363cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
136438ca51d0f643405df51e78fce6c546424e9f410dShishir Agrawal        int dataSub = SubscriptionManager.getDefaultDataSubscriptionId();
1365434fa420329b093f68d83862a637c7ded93a4dafCraig Lafayette        boolean defaultDataSelected = SubscriptionManager.isValidSubscriptionId(dataSub);
13663d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
1367b79f845a0451895b0f0b8a926a8571511d476ce8Libin.Tang@motorola.com        PhoneConstants.State state = PhoneConstants.State.IDLE;
13680710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // Note this is explicitly not using mPhone.getState.  See b/19090488.
13690710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // mPhone.getState reports the merge of CS and PS (volte) voice call state
13700710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // but we only care about CS calls here for data/voice concurrency issues.
13710710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // Calling getCallTracker currently gives you just the CS side where the
13720710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // ImsCallTracker is held internally where applicable.
13730710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // This should be redesigned to ask explicitly what we want:
13740710ff7ae80f217782f08528c5406f6d26a29f75Robert Greenwalt        // voiceCallStateAllowDataCall, or dataCallAllowed or something similar.
1375b79f845a0451895b0f0b8a926a8571511d476ce8Libin.Tang@motorola.com        if (mPhone.getCallTracker() != null) {
1376b79f845a0451895b0f0b8a926a8571511d476ce8Libin.Tang@motorola.com            state = mPhone.getCallTracker().getState();
1377b79f845a0451895b0f0b8a926a8571511d476ce8Libin.Tang@motorola.com        }
13781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
13793d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (failureReason != null) failureReason.clearAllReasons();
13803d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (!(attachedState || mAutoAttachOnCreation.get())) {
13813d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
13823d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.NOT_ATTACHED);
13833d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
13843d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (!recordsLoaded) {
13853d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
13863d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.RECORD_NOT_LOADED);
13873d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
13883d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (state != PhoneConstants.State.IDLE &&
13893d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                !mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
13903d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
13913d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.INVALID_PHONE_STATE);
13923d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(
13933d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                    DataAllowFailReasonType.CONCURRENT_VOICE_DATA_NOT_ALLOWED);
13943d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
13953d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (!internalDataEnabled) {
13963d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
13973d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.INTERNAL_DATA_DISABLED);
13983d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
13993d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (!defaultDataSelected) {
14003d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
14013d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(
14023d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                    DataAllowFailReasonType.DEFAULT_DATA_UNSELECTED);
14033d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
14043d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (mPhone.getServiceState().getDataRoaming() && !getDataOnRoamingEnabled()) {
14053d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
14063d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.ROAMING_DISABLED);
1407c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
14083d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (mIsPsRestricted) {
14093d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
14103d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.PS_RESTRICTED);
14113d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
14123d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        if (!desiredPowerState) {
14133d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if(failureReason == null) return false;
14143d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.UNDESIRED_POWER_STATE);
14153d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        }
14160d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu        if (!radioStateFromCarrier) {
14170d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu            if(failureReason == null) return false;
14180d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu            failureReason.addDataAllowFailReason(DataAllowFailReasonType.RADIO_DISABLED_BY_CARRIER);
14190d5e6cc0fdfb839d63abe1e7eb85eee1b5942108fionaxu        }
14203d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
14213d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        return failureReason == null || !failureReason.isFailed();
1422cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1423c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1424c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    // arg for setupDataOnConnectableApns
1425c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    private enum RetryFailures {
1426c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        // retry failed networks always (the old default)
1427c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        ALWAYS,
14280e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        // retry only when a substantial change has occurred.  Either:
1429c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        // 1) we were restricted by voice/data concurrency and aren't anymore
1430c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        // 2) our apn list has change
1431c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        ONLY_ON_CHANGE
1432c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    };
1433c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt
1434ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    private void setupDataOnConnectableApns(String reason) {
1435c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        setupDataOnConnectableApns(reason, RetryFailures.ALWAYS);
1436c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    }
1437c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt
1438c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    private void setupDataOnConnectableApns(String reason, RetryFailures retryFailures) {
14399c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu        if (VDBG) log("setupDataOnConnectableApns: " + reason);
14403fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
1441c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu        if (DBG && !VDBG) {
1442c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu            StringBuilder sb = new StringBuilder(120);
1443c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu            for (ApnContext apnContext : mPrioritySortedApnContexts) {
1444c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                sb.append(apnContext.getApnType());
1445c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                sb.append(":[state=");
1446c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                sb.append(apnContext.getState());
1447c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                sb.append(",enabled=");
1448c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                sb.append(apnContext.isEnabled());
1449c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu                sb.append("] ");
1450c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu            }
14519c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu            log("setupDataOnConnectableApns: " + reason + " " + sb);
1452c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu        }
1453c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu
14543fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        for (ApnContext apnContext : mPrioritySortedApnContexts) {
1455735bc2f4524d68155765351912ffae11306c3bd5Chris Manton            ArrayList<ApnSetting> waitingApns = null;
1456735bc2f4524d68155765351912ffae11306c3bd5Chris Manton
1457c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu            if (VDBG) log("setupDataOnConnectableApns: apnContext " + apnContext);
1458c1b229b7389e4cd682452c8d662afa1b3af14345Jack Yu
1459735bc2f4524d68155765351912ffae11306c3bd5Chris Manton            if (apnContext.getState() == DctConstants.State.FAILED
14600e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                    || apnContext.getState() == DctConstants.State.SCANNING) {
1461c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                if (retryFailures == RetryFailures.ALWAYS) {
1462ee665b78ad648abd98b019a9c9047f206ed22994Robert Greenwalt                    apnContext.releaseDataConnection(reason);
1463c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                } else if (apnContext.isConcurrentVoiceAndDataAllowed() == false &&
14640e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                        mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
1465c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    // RetryFailures.ONLY_ON_CHANGE - check if voice concurrency has changed
1466ee665b78ad648abd98b019a9c9047f206ed22994Robert Greenwalt                    apnContext.releaseDataConnection(reason);
1467c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                } else {
1468c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    // RetryFailures.ONLY_ON_CHANGE - check if the apns have changed
1469c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
14700e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                    ArrayList<ApnSetting> originalApns = apnContext.getWaitingApns();
1471c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    if (originalApns != null && originalApns.isEmpty() == false) {
1472c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                        waitingApns = buildWaitingApns(apnContext.getApnType(), radioTech);
1473c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                        if (originalApns.size() != waitingApns.size() ||
1474c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                                originalApns.containsAll(waitingApns) == false) {
1475ee665b78ad648abd98b019a9c9047f206ed22994Robert Greenwalt                            apnContext.releaseDataConnection(reason);
1476b290ce3d172215e5ab7cd43c8e7bbee9551ab29eRobert Greenwalt                        } else {
1477b290ce3d172215e5ab7cd43c8e7bbee9551ab29eRobert Greenwalt                            continue;
1478c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                        }
1479b290ce3d172215e5ab7cd43c8e7bbee9551ab29eRobert Greenwalt                    } else {
1480b290ce3d172215e5ab7cd43c8e7bbee9551ab29eRobert Greenwalt                        continue;
1481c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    }
1482c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                }
1483cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1484ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (apnContext.isConnectable()) {
14859c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu                log("isConnectable() call trySetupData");
1486ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                apnContext.setReason(reason);
1487c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                trySetupData(apnContext, waitingApns);
1488cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1489cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1490c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1491c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
14921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    boolean isEmergency() {
1493a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        final boolean result = mPhone.isInEcm() || mPhone.isInEmergencyCall();
14941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        log("isEmergency: result=" + result);
14951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return result;
14961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
14971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1498cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private boolean trySetupData(ApnContext apnContext) {
1499c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        return trySetupData(apnContext, null);
1500c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    }
1501c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt
1502c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt    private boolean trySetupData(ApnContext apnContext, ArrayList<ApnSetting> waitingApns) {
1503cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) {
1504cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("trySetupData for type:" + apnContext.getApnType() +
15059c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu                    " due to " + apnContext.getReason() + ", mIsPsRestricted=" + mIsPsRestricted);
1506cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
15072dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        apnContext.requestLog("trySetupData due to " + apnContext.getReason());
1508cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1509cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (mPhone.getSimulatedRadioControl() != null) {
1510cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // Assume data is connected on the simulator
1511cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // FIXME  this can be improved
1512cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext.setState(DctConstants.State.CONNECTED);
1513cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
1514cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1515cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("trySetupData: X We're on the simulator; assuming connected retValue=true");
1516cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return true;
1517cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1518cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1519cf5205f70eb1eac497164124187a088ecb03fff5Ram        // Allow SETUP_DATA request for E-APN to be completed during emergency call
1520cf5205f70eb1eac497164124187a088ecb03fff5Ram        // and MOBILE DATA On/Off cases as well.
1521cf5205f70eb1eac497164124187a088ecb03fff5Ram        boolean isEmergencyApn = apnContext.getApnType().equals(PhoneConstants.APN_TYPE_EMERGENCY);
1522c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt        final ServiceStateTracker sst = mPhone.getServiceStateTracker();
15230e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu
1524692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt        // set to false if apn type is non-metered or if we have a restricted (priveleged)
1525692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt        // request for the network.
1526692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt        // TODO - may want restricted requests to only apply to carrier-limited data access
1527692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt        //        rather than applying to user limited as well.
1528443b0ae99852b74898fa688f90dee303a80c1b89Robert Greenwalt        // Exclude DUN for the purposes of the override until we get finer grained
1529443b0ae99852b74898fa688f90dee303a80c1b89Robert Greenwalt        // intention in NetworkRequests
1530a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        boolean checkUserDataEnabled =
1531692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt                ApnSetting.isMeteredApnType(apnContext.getApnType(), mPhone.getContext(),
1532692640f429efa8e292c6261472b2c682e1079f8eRobert Greenwalt                        mPhone.getSubId(), mPhone.getServiceState().getDataRoaming()) &&
1533443b0ae99852b74898fa688f90dee303a80c1b89Robert Greenwalt                apnContext.hasNoRestrictedRequests(true /*exclude DUN */);
15343d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
15353d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        DataAllowFailReason failureReason = new DataAllowFailReason();
15363d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu
15373d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        // allow data if currently in roaming service, roaming setting disabled
15383d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        // and requested apn type is non-metered for roaming.
15393d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu        boolean isDataAllowed = isDataAllowed(failureReason) ||
15403d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                (failureReason.isFailForSingleReason(DataAllowFailReasonType.ROAMING_DISABLED) &&
15413d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                !(ApnSetting.isMeteredApnType(apnContext.getApnType(), mPhone.getContext(),
15423d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                mPhone.getSubId(), mPhone.getServiceState().getDataRoaming())));
1543cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1544cf5205f70eb1eac497164124187a088ecb03fff5Ram        if (apnContext.isConnectable() && (isEmergencyApn ||
15453d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                (isDataAllowed && isDataAllowedForApn(apnContext) &&
1546a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                        mDataEnabledSettings.isDataEnabled(checkUserDataEnabled) && !isEmergency()))) {
1547ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (apnContext.getState() == DctConstants.State.FAILED) {
15483d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                String str = "trySetupData: make a FAILED ApnContext IDLE so its reusable";
15492dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                if (DBG) log(str);
15502dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                apnContext.requestLog(str);
1551ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                apnContext.setState(DctConstants.State.IDLE);
1552ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            }
1553203e588e3c42a81aa8a56f595119c181a63b12caWink Saville            int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
1554c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt            apnContext.setConcurrentVoiceAndDataAllowed(sst.isConcurrentVoiceAndDataAllowed());
1555cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (apnContext.getState() == DctConstants.State.IDLE) {
1556c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                if (waitingApns == null) {
1557c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    waitingApns = buildWaitingApns(apnContext.getApnType(), radioTech);
1558c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                }
1559cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (waitingApns.isEmpty()) {
1560ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                    notifyNoData(DcFailCause.MISSING_UNKNOWN_APN, apnContext);
1561cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    notifyOffApnsOfAvailability(apnContext.getReason());
15622dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                    String str = "trySetupData: X No APN found retValue=false";
15632dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                    if (DBG) log(str);
15642dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                    apnContext.requestLog(str);
1565cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    return false;
1566cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
1567cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContext.setWaitingApns(waitingApns);
1568cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    if (DBG) {
1569ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                        log ("trySetupData: Create from mAllApnSettings : "
1570ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                    + apnListToString(mAllApnSettings));
1571cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
1572cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
1573cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1574cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1575203e588e3c42a81aa8a56f595119c181a63b12caWink Saville            boolean retValue = setupData(apnContext, radioTech);
1576cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(apnContext.getReason());
1577cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1578cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("trySetupData: X retValue=" + retValue);
1579cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return retValue;
1580cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
1581cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (!apnContext.getApnType().equals(PhoneConstants.APN_TYPE_DEFAULT)
1582ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                    && apnContext.isConnectable()) {
1583cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mPhone.notifyDataConnectionFailed(apnContext.getReason(), apnContext.getApnType());
1584ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            }
1585cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(apnContext.getReason());
15862e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu
15872e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            StringBuilder str = new StringBuilder();
15882e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu
15892e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            str.append("trySetupData failed. apnContext = [type=" + apnContext.getApnType() +
15902e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                    ", mState=" + apnContext.getState() + ", mDataEnabled=" +
15912e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                    apnContext.isEnabled() + ", mDependencyMet=" +
15922e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                    apnContext.getDependencyMet() + "] ");
15932e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu
15942e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            if (!apnContext.isConnectable()) {
15952e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                str.append("isConnectable = false. ");
15962dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt            }
15973d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if (!isDataAllowed) {
15983d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                str.append("data not allowed: " + failureReason.getDataAllowFailReason() + ". ");
15992e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            }
16002e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            if (!isDataAllowedForApn(apnContext)) {
16012e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                str.append("isDataAllowedForApn = false. RAT = " +
16022e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                        mPhone.getServiceState().getRilDataRadioTechnology());
16032e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            }
1604a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            if (!mDataEnabledSettings.isDataEnabled(checkUserDataEnabled)) {
16053d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                str.append("isDataEnabled(" + checkUserDataEnabled + ") = false. " +
1606a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                        "isInternalDataEnabled = " + mDataEnabledSettings.isInternalDataEnabled() +
1607a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                        ", userDataEnabled = " + mDataEnabledSettings.isUserDataEnabled() +
1608a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                        ", isPolicyDataEnabled = " + mDataEnabledSettings.isPolicyDataEnabled() +
1609a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                        ", isCarrierDataEnabled = " +
1610a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                        mDataEnabledSettings.isCarrierDataEnabled());
16112e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            }
16122e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            if (isEmergency()) {
16132e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu                str.append("emergency = true");
16142e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            }
16152e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu
16162e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            if (DBG) log(str.toString());
16172e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu            apnContext.requestLog(str.toString());
16182e3470722434dbc91dcc2095f5a410f6aaad620aJack Yu
1619cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return false;
1620cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1621c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1622c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
16230e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu    // Disabled apn's still need avail/unavail notifications - send them out
16241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void notifyOffApnsOfAvailability(String reason) {
16259c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu        if (DBG) {
16263d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            DataAllowFailReason failureReason = new DataAllowFailReason();
16273d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu            if (!isDataAllowed(failureReason)) {
16283d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                log(failureReason.getDataAllowFailReason());
16299c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu            }
16309c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu        }
1631cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
1632187a39f896f88eb6c5e4306d9595546654825976Wink Saville            if (!mAttached.get() || !apnContext.isReady()) {
1633ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                if (VDBG) log("notifyOffApnOfAvailability type:" + apnContext.getApnType());
1634cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mPhone.notifyDataConnection(reason != null ? reason : apnContext.getReason(),
1635cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                            apnContext.getApnType(),
1636cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                            PhoneConstants.DataState.DISCONNECTED);
1637cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else {
1638ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                if (VDBG) {
1639187a39f896f88eb6c5e4306d9595546654825976Wink Saville                    log("notifyOffApnsOfAvailability skipped apn due to attached && isReady " +
1640cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            apnContext.toString());
1641cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
1642c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
1643c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1644c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1645c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1646cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /**
1647cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * If tearDown is true, this only tears down a CONNECTED session. Presently,
1648cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * there is no mechanism for abandoning an CONNECTING session,
1649cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * but would likely involve cancelling pending async requests or
1650cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * setting a flag or new state to ignore them when they came in
1651cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param tearDown true if the underlying DataConnection should be
1652cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * disconnected.
1653cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param reason reason for the clean up.
16543fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * @return boolean - true if we did cleanup any connections, false if they
16553fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     *                   were already all disconnected.
1656cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     */
16571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean cleanUpAllConnections(boolean tearDown, String reason) {
1658cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("cleanUpAllConnections: tearDown=" + tearDown + " reason=" + reason);
16593fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        boolean didDisconnect = false;
16604c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu        boolean disableMeteredOnly = false;
1661a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
16624c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu        // reasons that only metered apn will be torn down
1663a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (!TextUtils.isEmpty(reason)) {
16644c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            disableMeteredOnly = reason.equals(Phone.REASON_DATA_SPECIFIC_DISABLED) ||
16654c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    reason.equals(Phone.REASON_ROAMING_ON) ||
16664c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    reason.equals(Phone.REASON_CARRIER_ACTION_DISABLE_METERED_APN);
1667a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
1668cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1669cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
16703fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            if (apnContext.isDisconnected() == false) didDisconnect = true;
16714c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            if (disableMeteredOnly) {
1672783061ca03572618c01ce244d70d82fa4328d45ffionaxu                // Use ApnSetting to decide metered or non-metered.
1673783061ca03572618c01ce244d70d82fa4328d45ffionaxu                // Tear down all metered data connections.
1674783061ca03572618c01ce244d70d82fa4328d45ffionaxu                ApnSetting apnSetting = apnContext.getApnSetting();
1675783061ca03572618c01ce244d70d82fa4328d45ffionaxu                if (apnSetting != null && apnSetting.isMetered(mPhone.getContext(),
16763d8c0f70a6fa7a53fda3c5d592de0ac3aa247e3cfionaxu                        mPhone.getSubId(), mPhone.getServiceState().getDataRoaming())) {
1677783061ca03572618c01ce244d70d82fa4328d45ffionaxu                    if (DBG) log("clean up metered ApnContext Type: " + apnContext.getApnType());
1678a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    apnContext.setReason(reason);
1679a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    cleanUpConnection(tearDown, apnContext);
1680a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
1681a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            } else {
1682a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                // TODO - only do cleanup if not disconnected
1683a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                apnContext.setReason(reason);
1684a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                cleanUpConnection(tearDown, apnContext);
1685a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
1686c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1687cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1688cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        stopNetStatPoll();
1689cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        stopDataStallAlarm();
1690cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1691cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // TODO: Do we need mRequestedApnType?
1692cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mRequestedApnType = PhoneConstants.APN_TYPE_DEFAULT;
1693a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
1694a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        log("cleanUpConnection: mDisconnectPendingCount = " + mDisconnectPendingCount);
1695a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (tearDown && mDisconnectPendingCount == 0) {
1696a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            notifyDataDisconnectComplete();
1697a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            notifyAllDataDisconnected();
1698a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
1699a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
17003fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        return didDisconnect;
1701cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1702cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1703cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /**
1704cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Cleanup all connections.
1705cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *
1706cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * TODO: Cleanup only a specified connection passed as a parameter.
1707cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *       Also, make sure when you clean up a conn, if it is last apply
1708cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *       logic as though it is cleanupAllConnections
1709cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *
1710cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param cause for the clean up.
1711cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     */
17121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onCleanUpAllConnections(String cause) {
1713cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        cleanUpAllConnections(true, cause);
1714cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1715cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
17161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    void sendCleanUpConnection(boolean tearDown, ApnContext apnContext) {
17171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("sendCleanUpConnection: tearDown=" + tearDown + " apnContext=" + apnContext);
17181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.EVENT_CLEAN_UP_CONNECTION);
17191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = tearDown ? 1 : 0;
17201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg2 = 0;
17211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.obj = apnContext;
17221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
17231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
1724cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
17251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void cleanUpConnection(boolean tearDown, ApnContext apnContext) {
1726cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext == null) {
1727cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("cleanUpConnection: apn context is null");
1728cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return;
1729cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1730cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1731454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville        DcAsyncChannel dcac = apnContext.getDcAc();
17322dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        String str = "cleanUpConnection: tearDown=" + tearDown + " reason=" +
17332dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                apnContext.getReason();
17349c32a1e9495f06905377c9e2b91c0ef9cdb0528fJack Yu        if (VDBG) log(str + " apnContext=" + apnContext);
17352dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        apnContext.requestLog(str);
1736cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (tearDown) {
1737cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (apnContext.isDisconnected()) {
1738cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // The request is tearDown and but ApnContext is not connected.
1739cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // If apnContext is not enabled anymore, break the linkage to the DCAC/DC.
1740cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                apnContext.setState(DctConstants.State.IDLE);
1741cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (!apnContext.isReady()) {
17424750c8c11836338b024e159f04f0cbd13c7444b9Wink Saville                    if (dcac != null) {
17430e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                        str = "cleanUpConnection: teardown, disconnected, !ready";
17442dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                        if (DBG) log(str + " apnContext=" + apnContext);
17452dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                        apnContext.requestLog(str);
17464750c8c11836338b024e159f04f0cbd13c7444b9Wink Saville                        dcac.tearDown(apnContext, "", null);
17474750c8c11836338b024e159f04f0cbd13c7444b9Wink Saville                    }
1748cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContext.setDataConnectionAc(null);
1749cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
1750cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else {
1751cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // Connection is still there. Try to clean up.
1752cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (dcac != null) {
1753cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    if (apnContext.getState() != DctConstants.State.DISCONNECTING) {
1754cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        boolean disconnectAll = false;
1755cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        if (PhoneConstants.APN_TYPE_DUN.equals(apnContext.getApnType())) {
1756a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            // CAF_MSIM is this below condition required.
1757a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            // if (PhoneConstants.APN_TYPE_DUN.equals(PhoneConstants.APN_TYPE_DEFAULT)) {
17581484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                            if (teardownForDun()) {
175945eaa2335e64a8ff1ad8d5e8224c580ef996f370Wink Saville                                if (DBG) {
176045eaa2335e64a8ff1ad8d5e8224c580ef996f370Wink Saville                                    log("cleanUpConnection: disconnectAll DUN connection");
176145eaa2335e64a8ff1ad8d5e8224c580ef996f370Wink Saville                                }
1762cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                // we need to tear it down - we brought it up just for dun and
1763cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                // other people are camped on it and now dun is done.  We need
1764cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                // to stop using it and let the normal apn list get used to find
1765cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                // connections for the remaining desired connections
1766cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                disconnectAll = true;
1767cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            }
1768cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        }
17691a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                        final int generation = apnContext.getConnectionGeneration();
17701a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                        str = "cleanUpConnection: tearing down" + (disconnectAll ? " all" : "") +
17711a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                                " using gen#" + generation;
17722dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                        if (DBG) log(str + "apnContext=" + apnContext);
17732dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                        apnContext.requestLog(str);
177437cacdfe7ed079d89fb9e80317b5dfd2acb975e5Robert Greenwalt                        Pair<ApnContext, Integer> pair =
17751a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                                new Pair<ApnContext, Integer>(apnContext, generation);
177637cacdfe7ed079d89fb9e80317b5dfd2acb975e5Robert Greenwalt                        Message msg = obtainMessage(DctConstants.EVENT_DISCONNECT_DONE, pair);
1777cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        if (disconnectAll) {
1778ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                            apnContext.getDcAc().tearDownAll(apnContext.getReason(), msg);
1779cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        } else {
1780ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                            apnContext.getDcAc()
1781cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                .tearDown(apnContext, apnContext.getReason(), msg);
1782cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        }
1783cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        apnContext.setState(DctConstants.State.DISCONNECTING);
1784a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        mDisconnectPendingCount++;
1785cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
1786cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
1787cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    // apn is connected but no reference to dcac.
1788cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    // Should not be happen, but reset the state in case.
1789cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContext.setState(DctConstants.State.IDLE);
17902dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                    apnContext.requestLog("cleanUpConnection: connected, bug no DCAC");
1791cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    mPhone.notifyDataConnection(apnContext.getReason(),
1792cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                                apnContext.getApnType());
1793cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
1794cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
1795cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
1796cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // force clean up the data connection.
1797ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (dcac != null) dcac.reqReset();
1798cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext.setState(DctConstants.State.IDLE);
1799cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
1800cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext.setDataConnectionAc(null);
1801cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1802cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1803ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        // Make sure reconnection alarm is cleaned up if there is no ApnContext
1804cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // associated to the connection.
1805cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (dcac != null) {
1806ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            cancelReconnectAlarm(apnContext);
1807c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
18082dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        str = "cleanUpConnection: X tearDown=" + tearDown + " reason=" + apnContext.getReason();
18092dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        if (DBG) log(str + " apnContext=" + apnContext + " dcac=" + apnContext.getDcAc());
18102dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        apnContext.requestLog(str);
1811cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
1812c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
18131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    ApnSetting fetchDunApn() {
18141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)) {
18151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("fetchDunApn: net.tethering.noprovisioning=true ret: null");
18161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            return null;
18171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
18181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int bearer = mPhone.getServiceState().getRilDataRadioTechnology();
18191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnSetting retDunSetting = null;
18201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        String apnData = Settings.Global.getString(mResolver, Settings.Global.TETHER_DUN_APN);
18211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        List<ApnSetting> dunSettings = ApnSetting.arrayFromString(apnData);
18221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        IccRecords r = mIccRecords.get();
18231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        for (ApnSetting dunSetting : dunSettings) {
18241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            String operator = (r != null) ? r.getOperatorNumeric() : "";
18251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (!ServiceState.bitmaskHasTech(dunSetting.bearerBitmask, bearer)) continue;
18261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (dunSetting.numeric.equals(operator)) {
18271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (dunSetting.hasMvnoParams()) {
18281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (r != null && ApnSetting.mvnoMatches(r, dunSetting.mvnoType,
18291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            dunSetting.mvnoMatchData)) {
18301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (VDBG) {
18311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            log("fetchDunApn: global TETHER_DUN_APN dunSetting=" + dunSetting);
18321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        }
18331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        return dunSetting;
18341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
18351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else if (mMvnoMatched == false) {
18361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (VDBG) log("fetchDunApn: global TETHER_DUN_APN dunSetting=" + dunSetting);
18371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    return dunSetting;
18381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
18391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
18401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
18411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
18421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Context c = mPhone.getContext();
18431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        String[] apnArrayData = c.getResources().getStringArray(R.array.config_tether_apndata);
18441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        for (String apn : apnArrayData) {
18451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            ApnSetting dunSetting = ApnSetting.fromString(apn);
18461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (dunSetting != null) {
18471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (!ServiceState.bitmaskHasTech(dunSetting.bearerBitmask, bearer)) continue;
18481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (dunSetting.hasMvnoParams()) {
18491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (r != null && ApnSetting.mvnoMatches(r, dunSetting.mvnoType,
18501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            dunSetting.mvnoMatchData)) {
18511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (VDBG) {
18521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            log("fetchDunApn: config_tether_apndata mvno dunSetting=" + dunSetting);
18531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        }
18541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        return dunSetting;
18551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
18561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else if (mMvnoMatched == false) {
18571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    retDunSetting = dunSetting;
18581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
18591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
18601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
18611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
18621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG) log("fetchDunApn: config_tether_apndata dunSetting=" + retDunSetting);
18631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return retDunSetting;
18641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
18651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
18661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public boolean hasMatchedTetherApnSetting() {
18671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnSetting matched = fetchDunApn();
18681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        log("hasMatchedTetherApnSetting: APN=" + matched);
18691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return matched != null;
18701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
18711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
1872cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /**
18731484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt     * Determine if DUN connection is special and we need to teardown on start/stop
18741484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt     */
18751484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt    private boolean teardownForDun() {
18761484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        // CDMA always needs to do this the profile id is correct
18771484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        final int rilRat = mPhone.getServiceState().getRilDataRadioTechnology();
18781484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        if (ServiceState.isCdma(rilRat)) return true;
18791484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt
18801484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        return (fetchDunApn() != null);
18811484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt    }
18821484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt
18831484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt    /**
1884ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * Cancels the alarm associated with apnContext.
1885cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *
1886ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * @param apnContext on which the alarm should be stopped.
1887cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     */
1888ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    private void cancelReconnectAlarm(ApnContext apnContext) {
1889ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (apnContext == null) return;
1890cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1891ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        PendingIntent intent = apnContext.getReconnectIntent();
1892cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
1893cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (intent != null) {
1894cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                AlarmManager am =
1895cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    (AlarmManager) mPhone.getContext().getSystemService(Context.ALARM_SERVICE);
1896cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                am.cancel(intent);
1897ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                apnContext.setReconnectIntent(null);
1898cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
1899c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1900c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1901cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /**
1902cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param types comma delimited list of APN types
1903cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @return array of APN types
1904cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     */
1905cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private String[] parseTypes(String types) {
1906c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        String[] result;
1907cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // If unset, set to DEFAULT.
1908cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (types == null || types.equals("")) {
1909c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            result = new String[1];
1910cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            result[0] = PhoneConstants.APN_TYPE_ALL;
1911cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
1912cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            result = types.split(",");
1913c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
1914c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return result;
1915c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1916c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
19171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    boolean isPermanentFail(DcFailCause dcFailCause) {
1918796d3c22f21041116110735c92d7e2c3a7c8f60dAmit Mahajan        return (dcFailCause.isPermanentFail() &&
1919796d3c22f21041116110735c92d7e2c3a7c8f60dAmit Mahajan                (mAttached.get() == false || dcFailCause != DcFailCause.SIGNAL_LOST));
1920796d3c22f21041116110735c92d7e2c3a7c8f60dAmit Mahajan    }
1921796d3c22f21041116110735c92d7e2c3a7c8f60dAmit Mahajan
1922fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi    private ApnSetting makeApnSetting(Cursor cursor) {
1923fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi        String[] types = parseTypes(
1924fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.TYPE)));
1925fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi        ApnSetting apn = new ApnSetting(
1926fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers._ID)),
1927fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NUMERIC)),
1928fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NAME)),
1929fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.APN)),
1930fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                NetworkUtils.trimV4AddrZeros(
1931fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        cursor.getString(
1932fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        cursor.getColumnIndexOrThrow(Telephony.Carriers.PROXY))),
1933fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PORT)),
1934fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                NetworkUtils.trimV4AddrZeros(
1935fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        cursor.getString(
1936fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSC))),
1937fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                NetworkUtils.trimV4AddrZeros(
1938fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        cursor.getString(
1939fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPROXY))),
1940fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPORT)),
1941fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.USER)),
1942fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PASSWORD)),
19431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.AUTH_TYPE)),
1944fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                types,
1945fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROTOCOL)),
1946fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getString(cursor.getColumnIndexOrThrow(
1947fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        Telephony.Carriers.ROAMING_PROTOCOL)),
1948fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                cursor.getInt(cursor.getColumnIndexOrThrow(
1949fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                        Telephony.Carriers.CARRIER_ENABLED)) == 1,
19509d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.BEARER)),
1951aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.BEARER_BITMASK)),
19529d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROFILE_ID)),
19539d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                cursor.getInt(cursor.getColumnIndexOrThrow(
19549d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                        Telephony.Carriers.MODEM_COGNITIVE)) == 1,
19559d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MAX_CONNS)),
19569d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                cursor.getInt(cursor.getColumnIndexOrThrow(
19579d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan                        Telephony.Carriers.WAIT_TIME)),
1958e9701717e43cc5aacbcf624f77a53be92350662cw                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MAX_CONNS_TIME)),
19593262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MTU)),
19603262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MVNO_TYPE)),
19613262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MVNO_MATCH_DATA)));
1962fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi        return apn;
1963fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi    }
1964fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi
1965cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private ArrayList<ApnSetting> createApnList(Cursor cursor) {
19663262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal        ArrayList<ApnSetting> mnoApns = new ArrayList<ApnSetting>();
19673262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal        ArrayList<ApnSetting> mvnoApns = new ArrayList<ApnSetting>();
1968fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi        IccRecords r = mIccRecords.get();
1969fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi
1970cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (cursor.moveToFirst()) {
1971cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            do {
19723262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                ApnSetting apn = makeApnSetting(cursor);
19733262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                if (apn == null) {
19743262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                    continue;
19753262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                }
19763262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal
19773262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                if (apn.hasMvnoParams()) {
197863913dc903872c45bab7d2483d633d845dd9c5d6Amit Mahajan                    if (r != null && ApnSetting.mvnoMatches(r, apn.mvnoType, apn.mvnoMatchData)) {
19793262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                        mvnoApns.add(apn);
1980fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                    }
1981fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                } else {
19823262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal                    mnoApns.add(apn);
1983fd5de4dae153c57e13bf8979d6f7a8aa86e3dedeSungmin Choi                }
1984cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } while (cursor.moveToNext());
1985cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
19863262c21feb1eede2917450cfdfd3faa86ee70cabShishir Agrawal
19871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ArrayList<ApnSetting> result;
19881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mvnoApns.isEmpty()) {
19891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            result = mnoApns;
19901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mMvnoMatched = false;
19911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
19921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            result = mvnoApns;
19931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mMvnoMatched = true;
19941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
1995cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("createApnList: X result=" + result);
1996c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        return result;
1997c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
1998c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
1999454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville    private boolean dataConnectionNotInUse(DcAsyncChannel dcac) {
2000ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (DBG) log("dataConnectionNotInUse: check if dcac is inuse dcac=" + dcac);
2001cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
2002ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (apnContext.getDcAc() == dcac) {
2003cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("dataConnectionNotInUse: in use by apnContext=" + apnContext);
2004cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return false;
2005cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2006cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2007cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // TODO: Fix retry handling so free DataConnections have empty apnlists.
2008cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // Probably move retry handling into DataConnections and reduce complexity
2009cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // of DCT.
2010cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("dataConnectionNotInUse: tearDownAll");
2011ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        dcac.tearDownAll("No connection", null);
2012cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("dataConnectionNotInUse: not in use return true");
2013cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return true;
2014cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2015cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2016454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville    private DcAsyncChannel findFreeDataConnection() {
2017454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville        for (DcAsyncChannel dcac : mDataConnectionAcHashMap.values()) {
2018cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (dcac.isInactiveSync() && dataConnectionNotInUse(dcac)) {
2019cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) {
2020cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    log("findFreeDataConnection: found free DataConnection=" +
2021ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                        " dcac=" + dcac);
2022cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2023ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                return dcac;
2024cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2025cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2026cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        log("findFreeDataConnection: NO free DataConnection");
2027cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return null;
2028cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2029cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2030203e588e3c42a81aa8a56f595119c181a63b12caWink Saville    private boolean setupData(ApnContext apnContext, int radioTech) {
2031cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("setupData: apnContext=" + apnContext);
20322dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        apnContext.requestLog("setupData");
2033ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        ApnSetting apnSetting;
20341484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        DcAsyncChannel dcac = null;
2035cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
20360e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        apnSetting = apnContext.getNextApnSetting();
20370e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu
2038ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (apnSetting == null) {
2039cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("setupData: return for no apn found!");
2040cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return false;
2041cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2042cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2043231c3c6271a27b5f03444f4d24b52c41c46566abEtan Cohen        int profileId = apnSetting.profileId;
2044231c3c6271a27b5f03444f4d24b52c41c46566abEtan Cohen        if (profileId == 0) {
2045231c3c6271a27b5f03444f4d24b52c41c46566abEtan Cohen            profileId = getApnProfileID(apnContext.getApnType());
2046231c3c6271a27b5f03444f4d24b52c41c46566abEtan Cohen        }
2047231c3c6271a27b5f03444f4d24b52c41c46566abEtan Cohen
20481484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        // On CDMA, if we're explicitly asking for DUN, we need have
20491484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        // a dun-profiled connection so we can't share an existing one
20501484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        // On GSM/LTE we can share existing apn connections provided they support
20511484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        // this type.
20521484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt        if (apnContext.getApnType() != PhoneConstants.APN_TYPE_DUN ||
20531484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                teardownForDun() == false) {
20541484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt            dcac = checkForCompatibleConnectedApnContext(apnContext);
20551484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt            if (dcac != null) {
20561484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                // Get the dcacApnSetting for the connection we want to share.
20571484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                ApnSetting dcacApnSetting = dcac.getApnSettingSync();
20581484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                if (dcacApnSetting != null) {
20591484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                    // Setting is good, so use it.
20601484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                    apnSetting = dcacApnSetting;
20611484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                }
2062ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            }
2063ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        }
2064ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (dcac == null) {
20653fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            if (isOnlySingleDcAllowed(radioTech)) {
20663fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                if (isHigherPriorityApnContextActive(apnContext)) {
20673fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    if (DBG) {
20683fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                        log("setupData: Higher priority ApnContext active.  Ignoring call");
20693fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    }
20703fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    return false;
20713fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                }
20723fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
20733fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                // Only lower priority calls left.  Disconnect them all in this single PDP case
20743fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                // so that we can bring up the requested higher priority call (once we receive
20750e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                // response for deactivate request for the calls we are about to disconnect
20763fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                if (cleanUpAllConnections(true, Phone.REASON_SINGLE_PDN_ARBITRATION)) {
20773fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    // If any call actually requested to be disconnected, means we can't
20783fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    // bring up this connection yet as we need to wait for those data calls
20793fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    // to be disconnected.
20803fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    if (DBG) log("setupData: Some calls are disconnecting first.  Wait and retry");
20813fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    return false;
20823fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                }
20833fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
20843fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                // No other calls are active, so proceed
20853fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                if (DBG) log("setupData: Single pdp. Continue setting up data call.");
20863fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            }
20873fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
2088ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            dcac = findFreeDataConnection();
2089cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2090ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (dcac == null) {
2091ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                dcac = createDataConnection();
2092cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2093cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2094ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (dcac == null) {
2095ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                if (DBG) log("setupData: No free DataConnection and couldn't create one, WEIRD");
2096cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return false;
2097cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2098cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
20991a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt        final int generation = apnContext.incAndGetConnectionGeneration();
21001a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt        if (DBG) {
21011a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt            log("setupData: dcac=" + dcac + " apnSetting=" + apnSetting + " gen#=" + generation);
21021a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt        }
2103cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2104cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        apnContext.setDataConnectionAc(dcac);
2105ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        apnContext.setApnSetting(apnSetting);
2106cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        apnContext.setState(DctConstants.State.CONNECTING);
2107cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
2108cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2109cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        Message msg = obtainMessage();
2110cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        msg.what = DctConstants.EVENT_DATA_SETUP_COMPLETE;
21111a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt        msg.obj = new Pair<ApnContext, Integer>(apnContext, generation);
21120e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        dcac.bringUp(apnContext, profileId, radioTech, msg, generation);
2113cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2114cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("setupData: initing!");
2115cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return true;
2116cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2117cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
21181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void setInitialAttachApn() {
21191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnSetting iaApnSetting = null;
21201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnSetting defaultApnSetting = null;
21211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnSetting firstApnSetting = null;
21221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        log("setInitialApn: E mPreferredApn=" + mPreferredApn);
21241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mAllApnSettings != null && !mAllApnSettings.isEmpty()) {
21261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            firstApnSetting = mAllApnSettings.get(0);
21271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("setInitialApn: firstApnSetting=" + firstApnSetting);
21281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // Search for Initial APN setting and the first apn that can handle default
21301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            for (ApnSetting apn : mAllApnSettings) {
21311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // Can't use apn.canHandleType(), as that returns true for APNs that have no type.
21321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (ArrayUtils.contains(apn.types, PhoneConstants.APN_TYPE_IA) &&
21331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        apn.carrierEnabled) {
21341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    // The Initial Attach APN is highest priority so use it if there is one
21351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("setInitialApn: iaApnSetting=" + apn);
21361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    iaApnSetting = apn;
21371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    break;
21381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else if ((defaultApnSetting == null)
21391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        && (apn.canHandleType(PhoneConstants.APN_TYPE_DEFAULT))) {
21401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    // Use the first default apn if no better choice
21411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("setInitialApn: defaultApnSetting=" + apn);
21421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    defaultApnSetting = apn;
21431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
21441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
21451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
21461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // The priority of apn candidates from highest to lowest is:
21480e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        //   1) APN_TYPE_IA (Initial Attach)
21491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        //   2) mPreferredApn, i.e. the current preferred apn
21501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        //   3) The first apn that than handle APN_TYPE_DEFAULT
21511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        //   4) The first APN we can find.
21521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ApnSetting initialAttachApnSetting = null;
21541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (iaApnSetting != null) {
21551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("setInitialAttachApn: using iaApnSetting");
21561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            initialAttachApnSetting = iaApnSetting;
21571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else if (mPreferredApn != null) {
21581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("setInitialAttachApn: using mPreferredApn");
21591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            initialAttachApnSetting = mPreferredApn;
21601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else if (defaultApnSetting != null) {
21611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("setInitialAttachApn: using defaultApnSetting");
21621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            initialAttachApnSetting = defaultApnSetting;
21631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else if (firstApnSetting != null) {
21641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("setInitialAttachApn: using firstApnSetting");
21651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            initialAttachApnSetting = firstApnSetting;
21661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
21671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (initialAttachApnSetting == null) {
21691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("setInitialAttachApn: X There in no available apn");
21701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
21711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("setInitialAttachApn: X selected Apn=" + initialAttachApnSetting);
21721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
21731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mPhone.mCi.setInitialAttachApn(initialAttachApnSetting.apn,
21741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    initialAttachApnSetting.protocol, initialAttachApnSetting.authType,
21751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    initialAttachApnSetting.user, initialAttachApnSetting.password, null);
21761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
21771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
21781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2179c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
2180cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Handles changes to the APN database.
2181c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
2182cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private void onApnChanged() {
2183cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        DctConstants.State overallState = getOverallState();
2184cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean isDisconnected = (overallState == DctConstants.State.IDLE ||
2185cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                overallState == DctConstants.State.FAILED);
2186cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
21871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mPhone instanceof GsmCdmaPhone) {
2188cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // The "current" may no longer be valid.  MMS depends on this to send properly. TBD
21891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            ((GsmCdmaPhone)mPhone).updateCurrentCarrierInProvider();
2190cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2191cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2192cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // TODO: It'd be nice to only do this if the changed entrie(s)
2193cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // match the current operator.
2194cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onApnChanged: createAllApnList and cleanUpAllConnections");
2195cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        createAllApnList();
21965d5eea6ed231163c225144316b0d1913d48678a4Sungmin Choi        setInitialAttachApn();
21979a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        cleanUpConnectionsOnUpdatedApns(!isDisconnected);
2198bda761320929f714951c328bfec6a51a1978db97Wink Saville
21998f6f52e4f7598e44cea1f9e5f4781291f9060d1dWink Saville        // FIXME: See bug 17426028 maybe no conditional is needed.
220038ca51d0f643405df51e78fce6c546424e9f410dShishir Agrawal        if (mPhone.getSubId() == SubscriptionManager.getDefaultDataSubscriptionId()) {
2201ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            setupDataOnConnectableApns(Phone.REASON_APN_CHANGED);
2202c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
2203c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
2204c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2205c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
2206cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param cid Connection id provided from RIL.
2207cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @return DataConnectionAc associated with specified cid.
2208c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
2209454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville    private DcAsyncChannel findDataConnectionAcByCid(int cid) {
2210454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville        for (DcAsyncChannel dcac : mDataConnectionAcHashMap.values()) {
2211cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (dcac.getCidSync() == cid) {
2212cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return dcac;
2213cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2214c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
2215cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return null;
2216c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
2217c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2218cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    // TODO: For multiple Active APNs not exactly sure how to do this.
22191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void gotoIdleAndNotifyDataConnection(String reason) {
2220cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("gotoIdleAndNotifyDataConnection: reason=" + reason);
2221cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        notifyDataConnection(reason);
2222cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2223cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
22243fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    /**
22253fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * "Active" here means ApnContext isEnabled() and not in FAILED state
22263fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * @param apnContext to compare with
22273fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * @return true if higher priority active apn found
22283fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     */
22293fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    private boolean isHigherPriorityApnContextActive(ApnContext apnContext) {
22303fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        for (ApnContext otherContext : mPrioritySortedApnContexts) {
22313fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            if (apnContext.getApnType().equalsIgnoreCase(otherContext.getApnType())) return false;
22323fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            if (otherContext.isEnabled() && otherContext.getState() != DctConstants.State.FAILED) {
22333fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                return true;
22343fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            }
22353fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        }
22363fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        return false;
22373fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    }
22383fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
22393fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    /**
22403fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * Reports if we support multiple connections or not.
22413fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * This is a combination of factors, based on carrier and RAT.
22423fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * @param rilRadioTech the RIL Radio Tech currently in use
22433fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     * @return true if only single DataConnection is allowed
22443fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt     */
22453fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    private boolean isOnlySingleDcAllowed(int rilRadioTech) {
22463fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        int[] singleDcRats = mPhone.getContext().getResources().getIntArray(
22473fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                com.android.internal.R.array.config_onlySingleDcAllowed);
22483fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        boolean onlySingleDcAllowed = false;
22493fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        if (Build.IS_DEBUGGABLE &&
22503fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                SystemProperties.getBoolean("persist.telephony.test.singleDc", false)) {
22513fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            onlySingleDcAllowed = true;
22523fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        }
22533fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        if (singleDcRats != null) {
22543fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            for (int i=0; i < singleDcRats.length && onlySingleDcAllowed == false; i++) {
22553fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                if (rilRadioTech == singleDcRats[i]) onlySingleDcAllowed = true;
22563fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            }
22573fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        }
22583fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
22593fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        if (DBG) log("isOnlySingleDcAllowed(" + rilRadioTech + "): " + onlySingleDcAllowed);
22603fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        return onlySingleDcAllowed;
22613fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    }
22623fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt
22631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    void sendRestartRadio() {
22641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG)log("sendRestartRadio:");
22651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.EVENT_RESTART_RADIO);
22661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
22671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
22681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
22691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void restartRadio() {
2270cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("restartRadio: ************TURN OFF RADIO**************");
2271cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        cleanUpAllConnections(true, Phone.REASON_RADIO_TURNED_OFF);
2272cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mPhone.getServiceStateTracker().powerOffRadioSafely(this);
2273cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        /* Note: no need to call setRadioPower(true).  Assuming the desired
2274cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * radio power state is still ON (as tracked by ServiceStateTracker),
2275cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * ServiceStateTracker will call setRadioPower when it receives the
2276cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * RADIO_STATE_CHANGED notification for the power off.  And if the
2277cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * desired power state has changed in the interim, we don't want to
2278cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         * override it with an unconditional power on.
2279cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville         */
2280cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2281cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        int reset = Integer.parseInt(SystemProperties.get("net.ppp.reset-by-timeout", "0"));
22820e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        SystemProperties.set("net.ppp.reset-by-timeout", String.valueOf(reset + 1));
2283cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2284cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2285cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    /**
2286cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Return true if data connection need to be setup after disconnected due to
2287cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * reason.
2288cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *
22890e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu     * @param apnContext APN context
2290cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @return true if try setup data connection is need for this reason
2291cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     */
22923fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt    private boolean retryAfterDisconnected(ApnContext apnContext) {
2293cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean retry = true;
22943fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        String reason = apnContext.getReason();
2295cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
22963fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        if ( Phone.REASON_RADIO_TURNED_OFF.equals(reason) ||
22973fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                (isOnlySingleDcAllowed(mPhone.getServiceState().getRilDataRadioTechnology())
22983fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                 && isHigherPriorityApnContextActive(apnContext))) {
2299cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            retry = false;
2300cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2301cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return retry;
2302cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2303cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
23040e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu    private void startAlarmForReconnect(long delay, ApnContext apnContext) {
2305cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        String apnType = apnContext.getApnType();
230674672e8ee972f12406b72551261b4cc7e0651933Wink Saville
230774672e8ee972f12406b72551261b4cc7e0651933Wink Saville        Intent intent = new Intent(INTENT_RECONNECT_ALARM + "." + apnType);
230874672e8ee972f12406b72551261b4cc7e0651933Wink Saville        intent.putExtra(INTENT_RECONNECT_ALARM_EXTRA_REASON, apnContext.getReason());
2309cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        intent.putExtra(INTENT_RECONNECT_ALARM_EXTRA_TYPE, apnType);
231067d43cfed4b996c20780bfec8fde1ae8c1391779Junda Liu        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
2311cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2312a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        // Get current sub id.
231338ca51d0f643405df51e78fce6c546424e9f410dShishir Agrawal        int subId = SubscriptionManager.getDefaultDataSubscriptionId();
2314a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
2315a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
231674672e8ee972f12406b72551261b4cc7e0651933Wink Saville        if (DBG) {
231774672e8ee972f12406b72551261b4cc7e0651933Wink Saville            log("startAlarmForReconnect: delay=" + delay + " action=" + intent.getAction()
231874672e8ee972f12406b72551261b4cc7e0651933Wink Saville                    + " apn=" + apnContext);
231974672e8ee972f12406b72551261b4cc7e0651933Wink Saville        }
232074672e8ee972f12406b72551261b4cc7e0651933Wink Saville
23210e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        PendingIntent alarmIntent = PendingIntent.getBroadcast(mPhone.getContext(), 0,
2322cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
2323ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        apnContext.setReconnectIntent(alarmIntent);
23247d6d7d6cb68ee37b4bee33588ba2594e9cf0c197Jack Yu
23257d6d7d6cb68ee37b4bee33588ba2594e9cf0c197Jack Yu        // Use the exact timer instead of the inexact one to provide better user experience.
23267d6d7d6cb68ee37b4bee33588ba2594e9cf0c197Jack Yu        // In some extreme cases, we saw the retry was delayed for few minutes.
23270852a954be5937a1b0bca94df0c2007d7ee3c0c7Jack Yu        // Note that if the stated trigger time is in the past, the alarm will be triggered
23280852a954be5937a1b0bca94df0c2007d7ee3c0c7Jack Yu        // immediately.
23297d6d7d6cb68ee37b4bee33588ba2594e9cf0c197Jack Yu        mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
2330cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                SystemClock.elapsedRealtime() + delay, alarmIntent);
2331cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2332cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2333ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    private void notifyNoData(DcFailCause lastFailCauseCode,
2334cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                              ApnContext apnContext) {
2335cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log( "notifyNoData: type=" + apnContext.getApnType());
2336796d3c22f21041116110735c92d7e2c3a7c8f60dAmit Mahajan        if (isPermanentFail(lastFailCauseCode)
2337cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            && (!apnContext.getApnType().equals(PhoneConstants.APN_TYPE_DEFAULT))) {
2338cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            mPhone.notifyDataConnectionFailed(apnContext.getReason(), apnContext.getApnType());
2339cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2340cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2341cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
23421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public boolean getAutoAttachOnCreation() {
23431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return mAutoAttachOnCreation.get();
23441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
23451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
23461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onRecordsLoadedOrSubIdChanged() {
23471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("onRecordsLoadedOrSubIdChanged: createAllApnList");
234812fffcf0d8df6b8268806d9aa7cc7a662e73743bJing Zhao        mAutoAttachOnCreationConfig = mPhone.getContext().getResources()
234912fffcf0d8df6b8268806d9aa7cc7a662e73743bJing Zhao                .getBoolean(com.android.internal.R.bool.config_auto_attach_data_on_creation);
2350bda761320929f714951c328bfec6a51a1978db97Wink Saville
2351cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        createAllApnList();
23525d5eea6ed231163c225144316b0d1913d48678a4Sungmin Choi        setInitialAttachApn();
235322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (mPhone.mCi.getRadioState().isOn()) {
23541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("onRecordsLoadedOrSubIdChanged: notifying data availability");
2355cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(Phone.REASON_SIM_LOADED);
2356cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2357bda761320929f714951c328bfec6a51a1978db97Wink Saville        setupDataOnConnectableApns(Phone.REASON_SIM_LOADED);
2358cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2359cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2360a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    public void setApnsEnabledByCarrier(boolean enabled) {
2361a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        Message msg = obtainMessage(DctConstants.EVENT_SET_CARRIER_DATA_ENABLED);
2362a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        msg.arg1 = (enabled ? DctConstants.ENABLED : DctConstants.DISABLED);
2363a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        sendMessage(msg);
2364a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    }
2365a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu
23664c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    /**
23674c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu     * Action set from carrier signalling broadcast receivers to enable/disable metered apns.
23684c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu     */
2369a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    private void onSetCarrierDataEnabled(boolean enabled) {
2370a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        synchronized (mDataEnabledSettings) {
2371a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            if (enabled != mDataEnabledSettings.isCarrierDataEnabled()) {
2372a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                if (DBG) {
2373a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    log("carrier Action: set metered apns enabled: " + enabled);
2374a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                }
2375a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu
2376a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                // Disable/enable all metered apns
2377a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                mDataEnabledSettings.setCarrierDataEnabled(enabled);
2378a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu
2379a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                if (!enabled) {
2380a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    // Send otasp_sim_unprovisioned so that SuW is able to proceed and notify users
2381a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    mPhone.notifyOtaspChanged(ServiceStateTracker.OTASP_SIM_UNPROVISIONED);
2382a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    // Tear down all metered apns
2383a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    cleanUpAllConnections(true, Phone.REASON_CARRIER_ACTION_DISABLE_METERED_APN);
2384a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                } else {
2385a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    teardownRestrictedMeteredConnections();
2386a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    setupDataOnConnectableApns(Phone.REASON_DATA_ENABLED);
2387a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                }
238868f4f4a0bc8d4060b5775e7a24a97ea5b485989efionaxu            }
2389a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu        }
2390a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu    }
2391a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu
23924c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    /**
23934c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu     * Action set from carrier signalling broadcast receivers to enable/disable radio
23944c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu     */
23954c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    public void carrierActionSetRadioEnabled(boolean enabled) {
23964c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu        if (DBG) {
23974c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            log("carrier Action: set radio enabled: " + enabled);
23984c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu        }
23994c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu        final ServiceStateTracker sst = mPhone.getServiceStateTracker();
24004c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu        sst.setRadioPowerFromCarrier(enabled);
24014c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    }
24024c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu
24030469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal    private void onSimNotReady() {
24040469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal        if (DBG) log("onSimNotReady");
24050469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal
24060469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal        cleanUpAllConnections(true, Phone.REASON_SIM_NOT_READY);
24070469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal        mAllApnSettings = null;
24080469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal        mAutoAttachOnCreationConfig = false;
24090469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal    }
24100469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal
24111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onSetDependencyMet(String apnType, boolean met) {
2412cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // don't allow users to tweak hipri to work around default dependency not met
2413cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (PhoneConstants.APN_TYPE_HIPRI.equals(apnType)) return;
2414cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2415cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ApnContext apnContext = mApnContexts.get(apnType);
2416cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext == null) {
2417cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            loge("onSetDependencyMet: ApnContext not found in onSetDependencyMet(" +
2418cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnType + ", " + met + ")");
2419cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return;
2420cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2421cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        applyNewState(apnContext, apnContext.isEnabled(), met);
2422cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (PhoneConstants.APN_TYPE_DEFAULT.equals(apnType)) {
2423cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // tie actions on default to similar actions on HIPRI regarding dependencyMet
2424cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext = mApnContexts.get(PhoneConstants.APN_TYPE_HIPRI);
2425cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (apnContext != null) applyNewState(apnContext, apnContext.isEnabled(), met);
2426cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2427cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2428c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2429a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    public void setPolicyDataEnabled(boolean enabled) {
2430a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        if (DBG) log("setPolicyDataEnabled: " + enabled);
2431a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        Message msg = obtainMessage(DctConstants.CMD_SET_POLICY_DATA_ENABLE);
2432a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        msg.arg1 = (enabled ? DctConstants.ENABLED : DctConstants.DISABLED);
2433a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        sendMessage(msg);
2434a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    }
2435a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu
24361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onSetPolicyDataEnabled(boolean enabled) {
2437a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        synchronized (mDataEnabledSettings) {
24381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            final boolean prevEnabled = getAnyDataEnabled();
2439a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            if (mDataEnabledSettings.isPolicyDataEnabled() != enabled) {
2440a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                mDataEnabledSettings.setPolicyDataEnabled(enabled);
2441a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                // TODO: We should register for DataEnabledSetting's data enabled/disabled event and
2442a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                // handle the rest from there.
24431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (prevEnabled != getAnyDataEnabled()) {
24441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (!prevEnabled) {
24455292d0e24a73aed20bb3ed8591c0ac6005a0e3c9Robert Greenwalt                        teardownRestrictedMeteredConnections();
24461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        onTrySetupData(Phone.REASON_DATA_ENABLED);
24471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    } else {
24481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        onCleanUpAllConnections(Phone.REASON_DATA_SPECIFIC_DISABLED);
24491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
24501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
24511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
24521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
24531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
24541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
2455cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private void applyNewState(ApnContext apnContext, boolean enabled, boolean met) {
2456cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean cleanup = false;
2457cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean trySetup = false;
24582dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        String str ="applyNewState(" + apnContext.getApnType() + ", " + enabled +
24592dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                "(" + apnContext.isEnabled() + "), " + met + "(" +
24602dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                apnContext.getDependencyMet() +"))";
24612dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        if (DBG) log(str);
24622dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt        apnContext.requestLog(str);
24632dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt
2464cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext.isReady()) {
2465305122cd621385652826cf7d8cd4e651dc6b5e9fRobert Greenwalt            cleanup = true;
24662428693913ae731d4ace3414429f5e91af24ea36Wink Saville            if (enabled && met) {
24672428693913ae731d4ace3414429f5e91af24ea36Wink Saville                DctConstants.State state = apnContext.getState();
24682428693913ae731d4ace3414429f5e91af24ea36Wink Saville                switch(state) {
24692428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case CONNECTING:
24702428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case SCANNING:
24712428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case CONNECTED:
24722428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case DISCONNECTING:
24732428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        // We're "READY" and active so just return
24742428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        if (DBG) log("applyNewState: 'ready' so return");
24752dde8b1659b1eb231e667bc19307ef294b627bebRobert Greenwalt                        apnContext.requestLog("applyNewState state=" + state + ", so return");
24762428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        return;
24772428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case IDLE:
24782428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        // fall through: this is unexpected but if it happens cleanup and try setup
24792428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case FAILED:
24802428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    case RETRYING: {
24812428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        // We're "READY" but not active so disconnect (cleanup = true) and
24822428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        // connect (trySetup = true) to be sure we retry the connection.
24832428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        trySetup = true;
24842428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        apnContext.setReason(Phone.REASON_DATA_ENABLED);
24852428693913ae731d4ace3414429f5e91af24ea36Wink Saville                        break;
24862428693913ae731d4ace3414429f5e91af24ea36Wink Saville                    }
24872428693913ae731d4ace3414429f5e91af24ea36Wink Saville                }
2488305122cd621385652826cf7d8cd4e651dc6b5e9fRobert Greenwalt            } else if (met) {
2489cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                apnContext.setReason(Phone.REASON_DATA_DISABLED);
2490305122cd621385652826cf7d8cd4e651dc6b5e9fRobert Greenwalt                // If ConnectivityService has disabled this network, stop trying to bring
2491305122cd621385652826cf7d8cd4e651dc6b5e9fRobert Greenwalt                // it up, but do not tear it down - ConnectivityService will do that
2492305122cd621385652826cf7d8cd4e651dc6b5e9fRobert Greenwalt                // directly by talking with the DataConnection.
24931484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                //
24941484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                // This doesn't apply to DUN, however.  Those connections have special
24951484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                // requirements from carriers and we need stop using them when the dun
24961484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                // request goes away.  This applies to both CDMA and GSM because they both
24971484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                // can declare the DUN APN sharable by default traffic, thus still satisfying
24981484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                // those requests and not torn down organically.
24991484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                if (apnContext.getApnType() == PhoneConstants.APN_TYPE_DUN && teardownForDun()) {
25001484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                    cleanup = true;
25011484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                } else {
25021484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                    cleanup = false;
25031484bfe9c58cfd2ddf59a5b8dad7373a36de946dRobert Greenwalt                }
2504cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else {
2505cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_UNMET);
2506cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2507cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
2508cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (enabled && met) {
2509cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (apnContext.isEnabled()) {
2510cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_MET);
2511cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
2512cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContext.setReason(Phone.REASON_DATA_ENABLED);
2513c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                }
2514cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (apnContext.getState() == DctConstants.State.FAILED) {
2515cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnContext.setState(DctConstants.State.IDLE);
2516cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2517cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                trySetup = true;
2518cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2519cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2520cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        apnContext.setEnabled(enabled);
2521cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        apnContext.setDependencyMet(met);
2522cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (cleanup) cleanUpConnection(true, apnContext);
25230d2abb5518d6a86619d2c2db04867c338b2092d4Robert Greenwalt        if (trySetup) {
25240d2abb5518d6a86619d2c2db04867c338b2092d4Robert Greenwalt            apnContext.resetErrorCodeRetries();
25250d2abb5518d6a86619d2c2db04867c338b2092d4Robert Greenwalt            trySetupData(apnContext);
25260d2abb5518d6a86619d2c2db04867c338b2092d4Robert Greenwalt        }
2527cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2528c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2529454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville    private DcAsyncChannel checkForCompatibleConnectedApnContext(ApnContext apnContext) {
2530cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        String apnType = apnContext.getApnType();
2531cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ApnSetting dunSetting = null;
2532cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2533cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (PhoneConstants.APN_TYPE_DUN.equals(apnType)) {
2534cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            dunSetting = fetchDunApn();
2535cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2536ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (DBG) {
2537ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            log("checkForCompatibleConnectedApnContext: apnContext=" + apnContext );
2538ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        }
2539cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2540454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville        DcAsyncChannel potentialDcac = null;
2541ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        ApnContext potentialApnCtx = null;
2542ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        for (ApnContext curApnCtx : mApnContexts.values()) {
2543454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville            DcAsyncChannel curDcac = curApnCtx.getDcAc();
2544ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (curDcac != null) {
2545ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                ApnSetting apnSetting = curApnCtx.getApnSetting();
2546a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                log("apnSetting: " + apnSetting);
2547cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (dunSetting != null) {
2548cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    if (dunSetting.equals(apnSetting)) {
2549ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                        switch (curApnCtx.getState()) {
2550cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            case CONNECTED:
2551cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                if (DBG) {
2552ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                    log("checkForCompatibleConnectedApnContext:"
2553ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                            + " found dun conn=" + curDcac
2554ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                            + " curApnCtx=" + curApnCtx);
2555cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                }
2556ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                return curDcac;
2557ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                            case RETRYING:
2558cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            case CONNECTING:
2559ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                potentialDcac = curDcac;
2560ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                potentialApnCtx = curApnCtx;
2561cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            default:
2562cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                // Not connected, potential unchanged
2563cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                break;
2564cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        }
2565cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
2566cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else if (apnSetting != null && apnSetting.canHandleType(apnType)) {
2567ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                    switch (curApnCtx.getState()) {
2568cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        case CONNECTED:
2569cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            if (DBG) {
2570ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                log("checkForCompatibleConnectedApnContext:"
2571ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                        + " found canHandle conn=" + curDcac
2572ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                        + " curApnCtx=" + curApnCtx);
2573cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            }
2574ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                            return curDcac;
2575ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                        case RETRYING:
2576cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        case CONNECTING:
2577ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                            potentialDcac = curDcac;
2578ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                            potentialApnCtx = curApnCtx;
2579cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        default:
2580cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            // Not connected, potential unchanged
2581cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            break;
2582cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
2583cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2584ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            } else {
2585ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                if (VDBG) {
2586ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                    log("checkForCompatibleConnectedApnContext: not conn curApnCtx=" + curApnCtx);
2587ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                }
2588cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2589cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2590ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (potentialDcac != null) {
2591cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) {
2592ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                log("checkForCompatibleConnectedApnContext: found potential conn=" + potentialDcac
2593ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                        + " curApnCtx=" + potentialApnCtx);
2594cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
2595ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            return potentialDcac;
2596cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2597c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2598ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (DBG) log("checkForCompatibleConnectedApnContext: NO conn apnContext=" + apnContext);
2599cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return null;
2600cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2601c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
260227b650c406018355a88a41528db7859e232728a0Jack Yu    public void setEnabled(int id, boolean enable) {
26031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.EVENT_ENABLE_NEW_APN);
26041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = id;
26051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg2 = (enable ? DctConstants.ENABLED : DctConstants.DISABLED);
26061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
26071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
26081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
26091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onEnableApn(int apnId, int enabled) {
2610af5593594070f825032be46dced573cd195956e1Robert Greenwalt        ApnContext apnContext = mApnContextsById.get(apnId);
2611cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext == null) {
2612cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            loge("onEnableApn(" + apnId + ", " + enabled + "): NO ApnContext");
2613cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return;
2614cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2615cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // TODO change our retry manager to use the appropriate numbers for the new APN
2616cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onEnableApn: apnContext=" + apnContext + " call applyNewState");
2617cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        applyNewState(apnContext, enabled == DctConstants.ENABLED, apnContext.getDependencyMet());
2618cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2619c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2620cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    // TODO: We shouldnt need this.
26211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean onTrySetupData(String reason) {
2622cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onTrySetupData: reason=" + reason);
2623ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        setupDataOnConnectableApns(reason);
2624cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return true;
2625cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2626c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
26271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean onTrySetupData(ApnContext apnContext) {
2628cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onTrySetupData: apnContext=" + apnContext);
2629cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return trySetupData(apnContext);
2630cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2631c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
26321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
26331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Return current {@link android.provider.Settings.Global#MOBILE_DATA} value.
26341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
2635a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    //TODO: Merge this into DataSettings. And probably should rename to getUserDataEnabled().
26361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public boolean getDataEnabled() {
2637f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        final int device_provisioned =
2638f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                Settings.Global.getInt(mResolver, Settings.Global.DEVICE_PROVISIONED, 0);
2639f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
26401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        boolean retVal = "true".equalsIgnoreCase(SystemProperties.get(
26411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                "ro.com.android.mobiledata", "true"));
2642f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        if (TelephonyManager.getDefault().getSimCount() == 1) {
2643f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            retVal = Settings.Global.getInt(mResolver, Settings.Global.MOBILE_DATA,
2644f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                    retVal ? 1 : 0) != 0;
2645f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        } else {
2646f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            int phoneSubId = mPhone.getSubId();
2647f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            try {
2648f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                retVal = TelephonyManager.getIntWithSubId(mResolver,
2649f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                        Settings.Global.MOBILE_DATA, phoneSubId) != 0;
2650f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            } catch (SettingNotFoundException e) {
2651f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                // use existing retVal
2652f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            }
2653f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        }
2654f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        if (VDBG) log("getDataEnabled: retVal=" + retVal);
2655f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt        if (device_provisioned == 0) {
2656f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            // device is still getting provisioned - use whatever setting they
2657f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            // want during this process
2658f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            //
2659f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            // use the normal data_enabled setting (retVal, determined above)
2660f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            // as the default if nothing else is set
2661f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            final String prov_property = SystemProperties.get("ro.com.android.prov_mobiledata",
2662f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                  retVal ? "true" : "false");
2663f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            retVal = "true".equalsIgnoreCase(prov_property);
2664f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
2665f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            final int prov_mobile_data = Settings.Global.getInt(mResolver,
2666f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                    Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED,
2667f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                    retVal ? 1 : 0);
2668f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            retVal = prov_mobile_data != 0;
2669f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            log("getDataEnabled during provisioning retVal=" + retVal + " - (" + prov_property +
2670f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                    ", " + prov_mobile_data + ")");
26711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
2672f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
26731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return retVal;
26741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
26751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
26761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
26771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Modify {@link android.provider.Settings.Global#DATA_ROAMING} value.
26781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
26791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public void setDataOnRoamingEnabled(boolean enabled) {
26801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        final int phoneSubId = mPhone.getSubId();
26811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (getDataOnRoamingEnabled() != enabled) {
26821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            int roaming = enabled ? 1 : 0;
26831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
26841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // For single SIM phones, this is a per phone property.
26851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (TelephonyManager.getDefault().getSimCount() == 1) {
26861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Settings.Global.putInt(mResolver, Settings.Global.DATA_ROAMING, roaming);
26871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
26881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Settings.Global.putInt(mResolver, Settings.Global.DATA_ROAMING +
26891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                         phoneSubId, roaming);
26901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
26911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
26921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mSubscriptionManager.setDataRoaming(roaming, phoneSubId);
26931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // will trigger handleDataOnRoamingChange() through observer
26941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
26951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu               log("setDataOnRoamingEnabled: set phoneSubId=" + phoneSubId
26961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                       + " isRoaming=" + enabled);
26971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
26981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
26991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
27001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("setDataOnRoamingEnabled: unchanged phoneSubId=" + phoneSubId
27011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        + " isRoaming=" + enabled);
27021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu             }
27031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
27041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
27051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
27061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
27071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Return current {@link android.provider.Settings.Global#DATA_ROAMING} value.
27081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
27091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public boolean getDataOnRoamingEnabled() {
27101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        boolean isDataRoamingEnabled = "true".equalsIgnoreCase(SystemProperties.get(
27111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                "ro.com.android.dataroaming", "false"));
27121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        final int phoneSubId = mPhone.getSubId();
27131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
27141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        try {
27151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // For single SIM phones, this is a per phone property.
27161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (TelephonyManager.getDefault().getSimCount() == 1) {
27171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                isDataRoamingEnabled = Settings.Global.getInt(mResolver,
27181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        Settings.Global.DATA_ROAMING, isDataRoamingEnabled ? 1 : 0) != 0;
27191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
27201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                isDataRoamingEnabled = TelephonyManager.getIntWithSubId(mResolver,
27211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        Settings.Global.DATA_ROAMING, phoneSubId) != 0;
27221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
27231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } catch (SettingNotFoundException snfe) {
27241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("getDataOnRoamingEnabled: SettingNofFoundException snfe=" + snfe);
27251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
27261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG) {
27271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("getDataOnRoamingEnabled: phoneSubId=" + phoneSubId +
27281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " isDataRoamingEnabled=" + isDataRoamingEnabled);
27291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
27301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return isDataRoamingEnabled;
27311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
27321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
27331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onRoamingOff() {
2734cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onRoamingOff");
2735c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2736a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        if (!mDataEnabledSettings.isUserDataEnabled()) return;
2737c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
2738bda761320929f714951c328bfec6a51a1978db97Wink Saville        if (getDataOnRoamingEnabled() == false) {
2739cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(Phone.REASON_ROAMING_OFF);
2740bda761320929f714951c328bfec6a51a1978db97Wink Saville            setupDataOnConnectableApns(Phone.REASON_ROAMING_OFF);
2741cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
2742cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyDataConnection(Phone.REASON_ROAMING_OFF);
2743cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2744cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2745cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
27461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onRoamingOn() {
27476bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        if (DBG) log("onRoamingOn");
2748bda761320929f714951c328bfec6a51a1978db97Wink Saville
2749a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        if (!mDataEnabledSettings.isUserDataEnabled()) {
2750f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu            if (DBG) log("data not enabled by user");
2751f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu            return;
2752f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu        }
2753f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu
2754f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu        // Check if the device is actually data roaming
2755f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu        if (!mPhone.getServiceState().getDataRoaming()) {
2756f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu            if (DBG) log("device is not roaming. ignored the request.");
2757f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu            return;
2758f629e7f1a1e36215d9bbed40df310a54b772f9e9Jack Yu        }
2759cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2760bda761320929f714951c328bfec6a51a1978db97Wink Saville        if (getDataOnRoamingEnabled()) {
2761cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("onRoamingOn: setup data on roaming");
2762ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            setupDataOnConnectableApns(Phone.REASON_ROAMING_ON);
2763cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyDataConnection(Phone.REASON_ROAMING_ON);
2764cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
2765cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("onRoamingOn: Tear down data connection on roaming.");
2766cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            cleanUpAllConnections(true, Phone.REASON_ROAMING_ON);
2767cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(Phone.REASON_ROAMING_ON);
2768cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2769cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2770cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
27711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onRadioAvailable() {
2772cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onRadioAvailable");
2773cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (mPhone.getSimulatedRadioControl() != null) {
2774cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // Assume data is connected on the simulator
2775cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // FIXME  this can be improved
2776cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // setState(DctConstants.State.CONNECTED);
2777cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyDataConnection(null);
2778cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2779cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("onRadioAvailable: We're on the simulator; assuming data is connected");
2780cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2781cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2782cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        IccRecords r = mIccRecords.get();
2783cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (r != null && r.getRecordsLoaded()) {
2784cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyOffApnsOfAvailability(null);
2785cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2786cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2787cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (getOverallState() != DctConstants.State.IDLE) {
2788cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            cleanUpConnection(true, null);
2789cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2790cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2791cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
27921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onRadioOffOrNotAvailable() {
2793cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // Make sure our reconnect delay starts at the initial value
2794cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // next time the radio comes on
2795cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2796cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mReregisterOnReconnectFailure = false;
2797cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2798cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (mPhone.getSimulatedRadioControl() != null) {
2799cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // Assume data is connected on the simulator
2800cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // FIXME  this can be improved
2801cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("We're on the simulator; assuming radio off is meaningless");
2802cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
2803cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("onRadioOffOrNotAvailable: is off and clean up all connections");
2804cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            cleanUpAllConnections(false, Phone.REASON_RADIO_TURNED_OFF);
2805cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
2806cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        notifyOffApnsOfAvailability(null);
2807cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
2808cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
28091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void completeConnection(ApnContext apnContext) {
2810c9b81a0c05128694c617fcdd67e73821895822feWink Saville
2811c9b81a0c05128694c617fcdd67e73821895822feWink Saville        if (DBG) log("completeConnection: successful, notify the world apnContext=" + apnContext);
2812c9b81a0c05128694c617fcdd67e73821895822feWink Saville
2813c9b81a0c05128694c617fcdd67e73821895822feWink Saville        if (mIsProvisioning && !TextUtils.isEmpty(mProvisioningUrl)) {
2814c9b81a0c05128694c617fcdd67e73821895822feWink Saville            if (DBG) {
2815c9b81a0c05128694c617fcdd67e73821895822feWink Saville                log("completeConnection: MOBILE_PROVISIONING_ACTION url="
2816c9b81a0c05128694c617fcdd67e73821895822feWink Saville                        + mProvisioningUrl);
2817c9b81a0c05128694c617fcdd67e73821895822feWink Saville            }
2818c8dc0c8244aac9f3985a53bc94b8ec2e295db430Robert Greenwalt            Intent newIntent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
2819c8dc0c8244aac9f3985a53bc94b8ec2e295db430Robert Greenwalt                    Intent.CATEGORY_APP_BROWSER);
2820c8dc0c8244aac9f3985a53bc94b8ec2e295db430Robert Greenwalt            newIntent.setData(Uri.parse(mProvisioningUrl));
2821c9b81a0c05128694c617fcdd67e73821895822feWink Saville            newIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
2822c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    Intent.FLAG_ACTIVITY_NEW_TASK);
2823c9b81a0c05128694c617fcdd67e73821895822feWink Saville            try {
2824c9b81a0c05128694c617fcdd67e73821895822feWink Saville                mPhone.getContext().startActivity(newIntent);
2825c9b81a0c05128694c617fcdd67e73821895822feWink Saville            } catch (ActivityNotFoundException e) {
2826c9b81a0c05128694c617fcdd67e73821895822feWink Saville                loge("completeConnection: startActivityAsUser failed" + e);
2827c9b81a0c05128694c617fcdd67e73821895822feWink Saville            }
2828c9b81a0c05128694c617fcdd67e73821895822feWink Saville        }
2829c9b81a0c05128694c617fcdd67e73821895822feWink Saville        mIsProvisioning = false;
2830c9b81a0c05128694c617fcdd67e73821895822feWink Saville        mProvisioningUrl = null;
28312b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen        if (mProvisioningSpinner != null) {
28322b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            sendMessage(obtainMessage(DctConstants.CMD_CLEAR_PROVISIONING_SPINNER,
28332b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    mProvisioningSpinner));
28342b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen        }
2835c9b81a0c05128694c617fcdd67e73821895822feWink Saville
2836c9b81a0c05128694c617fcdd67e73821895822feWink Saville        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
2837c9b81a0c05128694c617fcdd67e73821895822feWink Saville        startNetStatPoll();
2838c9b81a0c05128694c617fcdd67e73821895822feWink Saville        startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
2839c9b81a0c05128694c617fcdd67e73821895822feWink Saville    }
2840c9b81a0c05128694c617fcdd67e73821895822feWink Saville
2841ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    /**
2842ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * A SETUP (aka bringUp) has completed, possibly with an error. If
2843ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * there is an error this method will call {@link #onDataSetupCompleteError}.
2844ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     */
28451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onDataSetupComplete(AsyncResult ar) {
2846608588e9af271f0f5640236809f27c626f9d98e9Robert Greenwalt
2847ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        DcFailCause cause = DcFailCause.UNKNOWN;
2848cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean handleError = false;
2849ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        ApnContext apnContext = getValidApnContext(ar, "onDataSetupComplete");
2850cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2851ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        if (apnContext == null) return;
2852cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2853cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (ar.exception == null) {
2854454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville            DcAsyncChannel dcac = apnContext.getDcAc();
2855cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2856cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (RADIO_TESTS) {
2857cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // Note: To change radio.test.onDSC.null.dcac from command line you need to
2858cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // adb root and adb remount and from the command line you can only change the
2859cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // value to 1 once. To change it a second time you can reboot or execute
2860cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // adb shell stop and then adb shell start. The command line to set the value is:
2861ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                // adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db "insert into system (name,value) values ('radio.test.onDSC.null.dcac', '1');"
2862cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                ContentResolver cr = mPhone.getContext().getContentResolver();
2863cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                String radioTestProperty = "radio.test.onDSC.null.dcac";
2864cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (Settings.System.getInt(cr, radioTestProperty, 0) == 1) {
2865cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    log("onDataSetupComplete: " + radioTestProperty +
2866cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            " is true, set dcac to null and reset property to false");
2867cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    dcac = null;
2868cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    Settings.System.putInt(cr, radioTestProperty, 0);
2869cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    log("onDataSetupComplete: " + radioTestProperty + "=" +
2870cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            Settings.System.getInt(mPhone.getContext().getContentResolver(),
2871cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                    radioTestProperty, -1));
2872cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2873c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
2874cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (dcac == null) {
2875cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                log("onDataSetupComplete: no connection to DC, handle as error");
2876ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                cause = DcFailCause.CONNECTION_TO_DATACONNECTIONAC_BROKEN;
2877cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                handleError = true;
2878cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else {
2879cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                ApnSetting apn = apnContext.getApnSetting();
2880cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) {
2881cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    log("onDataSetupComplete: success apn=" + (apn == null ? "unknown" : apn.apn));
2882cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2883cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (apn != null && apn.proxy != null && apn.proxy.length() != 0) {
2884cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    try {
2885cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        String port = apn.port;
2886cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        if (TextUtils.isEmpty(port)) port = "8080";
28879c180aedfc9f0d20525c0128487d3500e6c0a715Jason Monk                        ProxyInfo proxy = new ProxyInfo(apn.proxy,
2888cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                Integer.parseInt(port), null);
2889cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        dcac.setLinkPropertiesHttpProxySync(proxy);
2890cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    } catch (NumberFormatException e) {
2891cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        loge("onDataSetupComplete: NumberFormatException making ProxyProperties (" +
2892cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                                apn.port + "): " + e);
2893cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
2894cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2895cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
2896cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // everything is setup
2897cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if(TextUtils.equals(apnContext.getApnType(),PhoneConstants.APN_TYPE_DEFAULT)) {
289827b650c406018355a88a41528db7859e232728a0Jack Yu                    try {
289927b650c406018355a88a41528db7859e232728a0Jack Yu                        SystemProperties.set(PUPPET_MASTER_RADIO_STRESS_TEST, "true");
290027b650c406018355a88a41528db7859e232728a0Jack Yu                    } catch (RuntimeException ex) {
290127b650c406018355a88a41528db7859e232728a0Jack Yu                        log("Failed to set PUPPET_MASTER_RADIO_STRESS_TEST to true");
290227b650c406018355a88a41528db7859e232728a0Jack Yu                    }
290322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                    if (mCanSetPreferApn && mPreferredApn == null) {
29040e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                        if (DBG) log("onDataSetupComplete: PREFERRED APN is null");
2905cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        mPreferredApn = apn;
2906cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        if (mPreferredApn != null) {
2907cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            setPreferredApn(mPreferredApn.id);
2908cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        }
2909cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
2910cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
291127b650c406018355a88a41528db7859e232728a0Jack Yu                    try {
291227b650c406018355a88a41528db7859e232728a0Jack Yu                        SystemProperties.set(PUPPET_MASTER_RADIO_STRESS_TEST, "false");
291327b650c406018355a88a41528db7859e232728a0Jack Yu                    } catch (RuntimeException ex) {
291427b650c406018355a88a41528db7859e232728a0Jack Yu                        log("Failed to set PUPPET_MASTER_RADIO_STRESS_TEST to false");
291527b650c406018355a88a41528db7859e232728a0Jack Yu                    }
2916cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
2917c9b81a0c05128694c617fcdd67e73821895822feWink Saville
2918c9b81a0c05128694c617fcdd67e73821895822feWink Saville                // A connection is setup
2919c9b81a0c05128694c617fcdd67e73821895822feWink Saville                apnContext.setState(DctConstants.State.CONNECTED);
29200e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu
2921c9b81a0c05128694c617fcdd67e73821895822feWink Saville                boolean isProvApn = apnContext.isProvisioningApn();
2922b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                final ConnectivityManager cm = ConnectivityManager.from(mPhone.getContext());
2923b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                if (mProvisionBroadcastReceiver != null) {
2924b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    mPhone.getContext().unregisterReceiver(mProvisionBroadcastReceiver);
2925b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    mProvisionBroadcastReceiver = null;
2926b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                }
2927c9b81a0c05128694c617fcdd67e73821895822feWink Saville                if ((!isProvApn) || mIsProvisioning) {
2928b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    // Hide any provisioning notification.
2929b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    cm.setProvisioningNotificationVisible(false, ConnectivityManager.TYPE_MOBILE,
2930b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                            mProvisionActionName);
2931c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // Complete the connection normally notifying the world we're connected.
2932c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // We do this if this isn't a special provisioning apn or if we've been
2933c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // told its time to provision.
2934c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    completeConnection(apnContext);
2935c9b81a0c05128694c617fcdd67e73821895822feWink Saville                } else {
2936c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // This is a provisioning APN that we're reporting as connected. Later
2937c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // when the user desires to upgrade this to a "default" connection,
2938c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // mIsProvisioning == true, we'll go through the code path above.
2939c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // mIsProvisioning becomes true when CMD_ENABLE_MOBILE_PROVISIONING
2940c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    // is sent to the DCT.
2941c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    if (DBG) {
2942c9b81a0c05128694c617fcdd67e73821895822feWink Saville                        log("onDataSetupComplete: successful, BUT send connected to prov apn as"
2943c9b81a0c05128694c617fcdd67e73821895822feWink Saville                                + " mIsProvisioning:" + mIsProvisioning + " == false"
2944c9b81a0c05128694c617fcdd67e73821895822feWink Saville                                + " && (isProvisioningApn:" + isProvApn + " == true");
2945c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    }
2946c9b81a0c05128694c617fcdd67e73821895822feWink Saville
2947b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    // While radio is up, grab provisioning URL.  The URL contains ICCID which
2948b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    // disappears when radio is off.
2949b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    mProvisionBroadcastReceiver = new ProvisionNotificationBroadcastReceiver(
29502b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                            cm.getMobileProvisioningUrl(),
29512b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                            TelephonyManager.getDefault().getNetworkOperatorName());
2952b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    mPhone.getContext().registerReceiver(mProvisionBroadcastReceiver,
2953b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                            new IntentFilter(mProvisionActionName));
2954b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    // Put up user notification that sign-in is required.
2955b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    cm.setProvisioningNotificationVisible(true, ConnectivityManager.TYPE_MOBILE,
2956b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                            mProvisionActionName);
2957b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    // Turn off radio to save battery and avoid wasting carrier resources.
2958b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    // The network isn't usable and network validation will just fail anyhow.
2959b449dc75ef4b9cb996c34a11e758f7e6ca193240Paul Jensen                    setRadio(false);
2960c9b81a0c05128694c617fcdd67e73821895822feWink Saville                }
2961c9b81a0c05128694c617fcdd67e73821895822feWink Saville                if (DBG) {
2962c9b81a0c05128694c617fcdd67e73821895822feWink Saville                    log("onDataSetupComplete: SETUP complete type=" + apnContext.getApnType()
2963c9b81a0c05128694c617fcdd67e73821895822feWink Saville                        + ", reason:" + apnContext.getReason());
2964c9b81a0c05128694c617fcdd67e73821895822feWink Saville                }
29654c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                if (Build.IS_DEBUGGABLE) {
29664c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    // adb shell setprop persist.radio.test.pco [pco_val]
29674c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    String radioTestProperty = "persist.radio.test.pco";
2968cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    int pcoVal = SystemProperties.getInt(radioTestProperty, -1);
2969cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    if (pcoVal != -1) {
2970cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        log("PCO testing: read pco value from persist.radio.test.pco " + pcoVal);
2971cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        final byte[] value = new byte[1];
2972cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        value[0] = (byte) pcoVal;
2973cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        final Intent intent =
2974cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                                new Intent(TelephonyIntents.ACTION_CARRIER_SIGNAL_PCO_VALUE);
2975cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        intent.putExtra(TelephonyIntents.EXTRA_APN_TYPE_KEY, "default");
2976cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        intent.putExtra(TelephonyIntents.EXTRA_APN_PROTO_KEY, "IPV4V6");
2977cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        intent.putExtra(TelephonyIntents.EXTRA_PCO_ID_KEY, 0xFF00);
2978cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        intent.putExtra(TelephonyIntents.EXTRA_PCO_VALUE_KEY, value);
2979cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        mPhone.getCarrierSignalAgent().notifyCarrierSignalReceivers(intent);
2980cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    }
29814c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                }
2982c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
2983cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
2984ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            cause = (DcFailCause) (ar.result);
2985cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) {
2986cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                ApnSetting apn = apnContext.getApnSetting();
2987cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                log(String.format("onDataSetupComplete: error apn=%s cause=%s",
2988cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        (apn == null ? "unknown" : apn.apn), cause));
2989c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
2990cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (cause.isEventLoggable()) {
2991cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // Log this failure to the Event Logs.
2992cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                int cid = getCellLocationId();
2993cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                EventLog.writeEvent(EventLogTags.PDP_SETUP_FAIL,
2994cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        cause.ordinal(), cid, TelephonyManager.getDefault().getNetworkType());
2995c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
29960742246233c3f03f864d23e52d89e1845d994701Antonio Marín Cerezuela            ApnSetting apn = apnContext.getApnSetting();
29970742246233c3f03f864d23e52d89e1845d994701Antonio Marín Cerezuela            mPhone.notifyPreciseDataConnectionFailed(apnContext.getReason(),
29980742246233c3f03f864d23e52d89e1845d994701Antonio Marín Cerezuela                    apnContext.getApnType(), apn != null ? apn.apn : "unknown", cause.toString());
2999cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
30004c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            // Compose broadcast intent send to the specific carrier signaling receivers
30014c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            Intent intent = new Intent(TelephonyIntents
30024c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    .ACTION_CARRIER_SIGNAL_REQUEST_NETWORK_FAILED);
30034c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            intent.putExtra(TelephonyIntents.EXTRA_ERROR_CODE_KEY, cause.getErrorCode());
30044c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            intent.putExtra(TelephonyIntents.EXTRA_APN_TYPE_KEY, apnContext.getApnType());
30054c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            mPhone.getCarrierSignalAgent().notifyCarrierSignalReceivers(intent);
3006a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu
30070e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            if (cause.isRestartRadioFail() || apnContext.restartOnError(cause.getErrorCode())) {
30080e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                if (DBG) log("Modem restarted.");
30090e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                sendRestartRadio();
30100e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            }
3011cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
30120e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            // If the data call failure cause is a permanent failure, we mark the APN as permanent
30130e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            // failed.
30140e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            if (isPermanentFail(cause)) {
30150e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                log("cause = " + cause + ", mark apn as permanent failed. apn = " + apn);
30160e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                apnContext.markApnPermanentFailed(apn);
3017c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
30180e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu
3019cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            handleError = true;
3020cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
3021cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3022cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (handleError) {
3023ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            onDataSetupCompleteError(ar);
3024ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        }
3025a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3026a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        /* If flag is set to false after SETUP_DATA_CALL is invoked, we need
3027a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville         * to clean data connections.
3028a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville         */
3029a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        if (!mDataEnabledSettings.isInternalDataEnabled()) {
30304fdc57b9bf223c908474c4545cc6f63456117a3bSanket Padawe            cleanUpAllConnections(Phone.REASON_DATA_DISABLED);
3031a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
3032a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3033ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    }
3034cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3035ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    /**
3036ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt     * check for obsolete messages.  Return ApnContext if valid, null if not
3037ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt     */
3038ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt    private ApnContext getValidApnContext(AsyncResult ar, String logString) {
3039ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        if (ar != null && ar.userObj instanceof Pair) {
3040ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt            Pair<ApnContext, Integer>pair = (Pair<ApnContext, Integer>)ar.userObj;
3041ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt            ApnContext apnContext = pair.first;
3042ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt            if (apnContext != null) {
30431a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                final int generation = apnContext.getConnectionGeneration();
30441a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                if (DBG) {
30451a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                    log("getValidApnContext (" + logString + ") on " + apnContext + " got " +
30461a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                            generation + " vs " + pair.second);
30471a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                }
30481a1cc3f5759471edcf51294439f0e0b71922d6a0Robert Greenwalt                if (generation == pair.second) {
3049ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt                    return apnContext;
3050ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt                } else {
3051ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt                    log("ignoring obsolete " + logString);
3052ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt                    return null;
3053ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt                }
3054ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt            }
3055ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        }
3056ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        throw new RuntimeException(logString + ": No apnContext");
3057ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt    }
3058ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt
3059ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt    /**
3060ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * Error has occurred during the SETUP {aka bringUP} request and the DCT
3061ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * should either try the next waiting APN or start over from the
3062ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * beginning if the list is empty. Between each SETUP request there will
3063ec107cb8a36181fa26e61ab51f423ba862deb0faWink Saville     * be a delay defined by {@link #getApnDelay()}.
3064ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     */
30651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onDataSetupCompleteError(AsyncResult ar) {
30660e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu
3067ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        ApnContext apnContext = getValidApnContext(ar, "onDataSetupCompleteError");
3068ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
3069ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        if (apnContext == null) return;
3070ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
30710e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        long delay = apnContext.getDelayForNextApn(mFailFast);
3072ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
30730e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        // Check if we need to retry or not.
30740852a954be5937a1b0bca94df0c2007d7ee3c0c7Jack Yu        if (delay >= 0) {
30750e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            if (DBG) log("onDataSetupCompleteError: Try next APN. delay = " + delay);
3076ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            apnContext.setState(DctConstants.State.SCANNING);
3077ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            // Wait a bit before trying the next APN, so that
3078ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            // we're not tying up the RIL command channel
30790e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            startAlarmForReconnect(delay, apnContext);
30800e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        } else {
30810e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            // If we are not going to retry any APN, set this APN context to failed state.
30820e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            // This would be the final state of a data connection.
30830e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            apnContext.setState(DctConstants.State.FAILED);
30840e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            mPhone.notifyDataConnection(Phone.REASON_APN_FAILED, apnContext.getApnType());
30850e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            apnContext.setDataConnectionAc(null);
30860e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            log("onDataSetupCompleteError: Stop retrying APNs.");
3087c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3088c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3089c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3090c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
3091a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu     * Called when EVENT_REDIRECTION_DETECTED is received.
3092a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu     */
30934c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu    private void onDataConnectionRedirected(String redirectUrl) {
3094a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu        if (!TextUtils.isEmpty(redirectUrl)) {
30954c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            Intent intent = new Intent(TelephonyIntents.ACTION_CARRIER_SIGNAL_REDIRECTED);
30964c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            intent.putExtra(TelephonyIntents.EXTRA_REDIRECTION_URL_KEY, redirectUrl);
30974c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu            if(mPhone.getCarrierSignalAgent().notifyCarrierSignalReceivers(intent)) {
30984c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                log("Notify carrier signal receivers with redirectUrl: " + redirectUrl);
3099a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu            }
3100a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu        }
3101a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu    }
3102a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu
3103a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu    /**
3104cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Called when EVENT_DISCONNECT_DONE is received.
3105c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
31061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onDisconnectDone(AsyncResult ar) {
3107ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        ApnContext apnContext = getValidApnContext(ar, "onDisconnectDone");
3108ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        if (apnContext == null) return;
3109c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3110cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if(DBG) log("onDisconnectDone: EVENT_DISCONNECT_DONE apnContext=" + apnContext);
3111cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        apnContext.setState(DctConstants.State.IDLE);
3112cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3113cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
3114cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3115cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // if all data connection are gone, check whether Airplane mode request was
3116cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // pending.
3117cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (isDisconnected()) {
3118cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (mPhone.getServiceStateTracker().processPendingRadioPowerOffAfterDataOff()) {
3119a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu                if (DBG) log("onDisconnectDone: radio will be turned off, no retries");
3120cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // Radio will be turned off. No need to retry data setup
3121cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                apnContext.setApnSetting(null);
3122cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                apnContext.setDataConnectionAc(null);
3123a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3124a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                // Need to notify disconnect as well, in the case of switching Airplane mode.
3125a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                // Otherwise, it would cause 30s delayed to turn on Airplane mode.
31260e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                if (mDisconnectPendingCount > 0) {
3127a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    mDisconnectPendingCount--;
31280e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                }
3129a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3130a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                if (mDisconnectPendingCount == 0) {
3131a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    notifyDataDisconnectComplete();
3132a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    notifyAllDataDisconnected();
3133a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
3134cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return;
3135cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
3136c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3137cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // If APN is still enabled, try to bring it back up automatically
31383fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt        if (mAttached.get() && apnContext.isReady() && retryAfterDisconnected(apnContext)) {
313927b650c406018355a88a41528db7859e232728a0Jack Yu            try {
314027b650c406018355a88a41528db7859e232728a0Jack Yu                SystemProperties.set(PUPPET_MASTER_RADIO_STRESS_TEST, "false");
314127b650c406018355a88a41528db7859e232728a0Jack Yu            } catch (RuntimeException ex) {
314227b650c406018355a88a41528db7859e232728a0Jack Yu                log("Failed to set PUPPET_MASTER_RADIO_STRESS_TEST to false");
314327b650c406018355a88a41528db7859e232728a0Jack Yu            }
3144cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // Wait a bit before trying the next APN, so that
3145cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // we're not tying up the RIL command channel.
3146cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // This also helps in any external dependency to turn off the context.
31470e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            if (DBG) log("onDisconnectDone: attached, ready and retry after disconnect");
31480e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            long delay = apnContext.getInterApnDelay(mFailFast);
31490e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            if (delay > 0) {
31500e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                // Data connection is in IDLE state, so when we reconnect later, we'll rebuild
31510e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                // the waiting APN list, which will also reset/reconfigure the retry manager.
31520e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu                startAlarmForReconnect(delay, apnContext);
31530e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            }
3154c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else {
3155449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville            boolean restartRadioAfterProvisioning = mPhone.getContext().getResources().getBoolean(
3156449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville                    com.android.internal.R.bool.config_restartRadioAfterProvisioning);
3157449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville
3158449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville            if (apnContext.isProvisioningApn() && restartRadioAfterProvisioning) {
3159449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville                log("onDisconnectDone: restartRadio after provisioning");
3160449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville                restartRadio();
3161449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville            }
3162cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext.setApnSetting(null);
3163cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext.setDataConnectionAc(null);
31643fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            if (isOnlySingleDcAllowed(mPhone.getServiceState().getRilDataRadioTechnology())) {
3165449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville                if(DBG) log("onDisconnectDone: isOnlySigneDcAllowed true so setup single apn");
31663fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                setupDataOnConnectableApns(Phone.REASON_SINGLE_PDN_ARBITRATION);
3167449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville            } else {
3168449cbf85380bd1d6716b7ab002da1efb9493bb02Wink Saville                if(DBG) log("onDisconnectDone: not retrying");
31693fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt            }
3170c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3171a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3172a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (mDisconnectPendingCount > 0)
3173a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            mDisconnectPendingCount--;
3174a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3175a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (mDisconnectPendingCount == 0) {
3176c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt            apnContext.setConcurrentVoiceAndDataAllowed(
3177c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                    mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed());
3178a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            notifyDataDisconnectComplete();
3179a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            notifyAllDataDisconnected();
3180a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
3181a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
3182c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3183c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3184ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    /**
3185ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     * Called when EVENT_DISCONNECT_DC_RETRYING is received.
3186ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville     */
31871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onDisconnectDcRetrying(AsyncResult ar) {
3188ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        // We could just do this in DC!!!
3189ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        ApnContext apnContext = getValidApnContext(ar, "onDisconnectDcRetrying");
3190ea763d93b231bccf7e476b625fa028bef7874bf8Robert Greenwalt        if (apnContext == null) return;
3191ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
3192ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        apnContext.setState(DctConstants.State.RETRYING);
3193ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if(DBG) log("onDisconnectDcRetrying: apnContext=" + apnContext);
3194ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
3195ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
3196ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    }
3197ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
31981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onVoiceCallStarted() {
3199cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onVoiceCallStarted");
3200ec107cb8a36181fa26e61ab51f423ba862deb0faWink Saville        mInVoiceCall = true;
3201cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (isConnected() && ! mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
3202cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("onVoiceCallStarted stop polling");
3203cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            stopNetStatPoll();
3204cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            stopDataStallAlarm();
3205cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            notifyDataConnection(Phone.REASON_VOICE_CALL_STARTED);
3206c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3207c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3208c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
32091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onVoiceCallEnded() {
3210cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onVoiceCallEnded");
3211ec107cb8a36181fa26e61ab51f423ba862deb0faWink Saville        mInVoiceCall = false;
3212cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (isConnected()) {
3213cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (!mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
3214cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                startNetStatPoll();
3215cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
3216cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                notifyDataConnection(Phone.REASON_VOICE_CALL_ENDED);
3217cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else {
3218cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // clean slate after call end.
3219cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                resetPollStats();
3220c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
3221c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3222bda761320929f714951c328bfec6a51a1978db97Wink Saville        // reset reconnect timer
3223bda761320929f714951c328bfec6a51a1978db97Wink Saville        setupDataOnConnectableApns(Phone.REASON_VOICE_CALL_ENDED);
3224c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3225c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
32261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onCleanUpConnection(boolean tearDown, int apnId, String reason) {
3227cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("onCleanUpConnection");
3228af5593594070f825032be46dced573cd195956e1Robert Greenwalt        ApnContext apnContext = mApnContextsById.get(apnId);
3229cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (apnContext != null) {
3230cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            apnContext.setReason(reason);
3231cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            cleanUpConnection(tearDown, apnContext);
3232c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3233c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3234c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
32351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private boolean isConnected() {
3236cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
3237ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (apnContext.getState() == DctConstants.State.CONNECTED) {
3238cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // At least one context is connected, return true
3239cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return true;
3240c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
3241c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3242cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // There are not any contexts connected, return false
3243cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return false;
3244c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3245c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3246cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public boolean isDisconnected() {
3247cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
3248cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (!apnContext.isDisconnected()) {
3249cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                // At least one context was not disconnected return false
3250cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return false;
3251cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
3252c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3253cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // All contexts were disconnected so return true
3254cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return true;
3255c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3256c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
32571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void notifyDataConnection(String reason) {
3258cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("notifyDataConnection: reason=" + reason);
3259cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (ApnContext apnContext : mApnContexts.values()) {
3260187a39f896f88eb6c5e4306d9595546654825976Wink Saville            if (mAttached.get() && apnContext.isReady()) {
3261187a39f896f88eb6c5e4306d9595546654825976Wink Saville                if (DBG) log("notifyDataConnection: type:" + apnContext.getApnType());
3262cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mPhone.notifyDataConnection(reason != null ? reason : apnContext.getReason(),
3263cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        apnContext.getApnType());
3264cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
3265c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3266cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        notifyOffApnsOfAvailability(reason);
3267c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3268c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
32691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void setDataProfilesAsNeeded() {
32701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("setDataProfilesAsNeeded");
32711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mAllApnSettings != null && !mAllApnSettings.isEmpty()) {
32721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            ArrayList<DataProfile> dps = new ArrayList<DataProfile>();
32731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            for (ApnSetting apn : mAllApnSettings) {
32741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (apn.modemCognitive) {
32751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    DataProfile dp = new DataProfile(apn,
32761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            mPhone.getServiceState().getDataRoaming());
32771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    boolean isDup = false;
32781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    for(DataProfile dpIn : dps) {
32791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (dp.equals(dpIn)) {
32801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            isDup = true;
32811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            break;
32821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        }
32831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
32841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (!isDup) {
32851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        dps.add(dp);
32861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
32871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
32881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
32891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if(dps.size() > 0) {
32901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mPhone.mCi.setDataProfile(dps.toArray(new DataProfile[0]), null);
32911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
32921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
32931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
32941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3295c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
3296cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Based on the sim operator numeric, create a list for all possible
3297cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Data Connections and setup the preferredApn.
3298c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
3299cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private void createAllApnList() {
33001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mMvnoMatched = false;
3301ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        mAllApnSettings = new ArrayList<ApnSetting>();
3302cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        IccRecords r = mIccRecords.get();
3303cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        String operator = (r != null) ? r.getOperatorNumeric() : "";
3304cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (operator != null) {
3305cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            String selection = "numeric = '" + operator + "'";
330660bc489803a3557526e1f95e34c237e70f28bc50Sungmin Choi            String orderBy = "_id";
3307cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // query only enabled apn.
3308cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            // carrier_enabled : 1 means enabled apn, 0 disabled apn.
33099d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan            // selection += " and carrier_enabled = 1";
3310cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("createAllApnList: selection=" + selection);
3311cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3312cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            Cursor cursor = mPhone.getContext().getContentResolver().query(
331360bc489803a3557526e1f95e34c237e70f28bc50Sungmin Choi                    Telephony.Carriers.CONTENT_URI, null, selection, null, orderBy);
3314cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3315cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (cursor != null) {
3316cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (cursor.getCount() > 0) {
3317ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                    mAllApnSettings = createApnList(cursor);
3318cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
3319cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                cursor.close();
3320cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
3321c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3322c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
332376f43316a5a6082d601bffd4b6898d0bd81e11fcram        addEmergencyApnSetting();
332476f43316a5a6082d601bffd4b6898d0bd81e11fcram
332529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        dedupeApnSettings();
332629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
3327ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (mAllApnSettings.isEmpty()) {
3328cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("createAllApnList: No APN found for carrier: " + operator);
3329cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            mPreferredApn = null;
3330ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            // TODO: What is the right behavior?
3331cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            //notifyNoData(DataConnection.FailCause.MISSING_UNKNOWN_APN);
3332c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else {
3333cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            mPreferredApn = getPreferredApn();
3334cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (mPreferredApn != null && !mPreferredApn.numeric.equals(operator)) {
3335cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mPreferredApn = null;
3336cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                setPreferredApn(-1);
3337cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
3338cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("createAllApnList: mPreferredApn=" + mPreferredApn);
3339c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3340ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (DBG) log("createAllApnList: X mAllApnSettings=" + mAllApnSettings);
33419d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan
33429d4ec7d45061f1da05f16cd244eb0a798e7f36bbAmit Mahajan        setDataProfilesAsNeeded();
3343c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3344c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
334529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    private void dedupeApnSettings() {
334629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        ArrayList<ApnSetting> resultApns = new ArrayList<ApnSetting>();
334729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
334829c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        // coalesce APNs if they are similar enough to prevent
334929c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        // us from bringing up two data calls with the same interface
335029c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        int i = 0;
335129c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        while (i < mAllApnSettings.size() - 1) {
335229c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            ApnSetting first = mAllApnSettings.get(i);
335329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            ApnSetting second = null;
335429c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            int j = i + 1;
335529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            while (j < mAllApnSettings.size()) {
335629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                second = mAllApnSettings.get(j);
335729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                if (apnsSimilar(first, second)) {
335829c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                    ApnSetting newApn = mergeApns(first, second);
335929c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                    mAllApnSettings.set(i, newApn);
336029c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                    first = newApn;
336129c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                    mAllApnSettings.remove(j);
336229c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                } else {
336329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                    j++;
336429c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                }
336529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            }
336629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            i++;
336729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        }
336829c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    }
336929c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
3370d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe    //check whether the types of two APN same (even only one type of each APN is same)
3371d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe    private boolean apnTypeSameAny(ApnSetting first, ApnSetting second) {
3372d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe        if(VDBG) {
3373d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            StringBuilder apnType1 = new StringBuilder(first.apn + ": ");
3374d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            for(int index1 = 0; index1 < first.types.length; index1++) {
3375d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                apnType1.append(first.types[index1]);
3376d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                apnType1.append(",");
3377d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            }
3378d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe
3379d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            StringBuilder apnType2 = new StringBuilder(second.apn + ": ");
3380d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            for(int index1 = 0; index1 < second.types.length; index1++) {
3381d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                apnType2.append(second.types[index1]);
3382d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                apnType2.append(",");
3383d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            }
3384d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            log("APN1: is " + apnType1);
3385d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            log("APN2: is " + apnType2);
3386d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe        }
3387d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe
3388d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe        for(int index1 = 0; index1 < first.types.length; index1++) {
3389d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            for(int index2 = 0; index2 < second.types.length; index2++) {
3390d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                if(first.types[index1].equals(PhoneConstants.APN_TYPE_ALL) ||
3391d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                        second.types[index2].equals(PhoneConstants.APN_TYPE_ALL) ||
3392d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                        first.types[index1].equals(second.types[index2])) {
3393d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                    if(VDBG)log("apnTypeSameAny: return true");
3394d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                    return true;
3395d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                }
3396d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe            }
3397d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe        }
3398d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe
3399d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe        if(VDBG)log("apnTypeSameAny: return false");
3400d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe        return false;
3401d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe    }
3402d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe
340329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    // Check if neither mention DUN and are substantially similar
340429c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    private boolean apnsSimilar(ApnSetting first, ApnSetting second) {
340529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        return (first.canHandleType(PhoneConstants.APN_TYPE_DUN) == false &&
340629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                second.canHandleType(PhoneConstants.APN_TYPE_DUN) == false &&
340729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                Objects.equals(first.apn, second.apn) &&
3408d86df358b8fe07160caa12147b6e4ad34d378ce6xinhe                !apnTypeSameAny(first, second) &&
340961cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan                xorEquals(first.proxy, second.proxy) &&
341061cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan                xorEquals(first.port, second.port) &&
341129c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                first.carrierEnabled == second.carrierEnabled &&
3412aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                first.bearerBitmask == second.bearerBitmask &&
341329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                first.profileId == second.profileId &&
341429c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                Objects.equals(first.mvnoType, second.mvnoType) &&
341529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                Objects.equals(first.mvnoMatchData, second.mvnoMatchData) &&
341629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                xorEquals(first.mmsc, second.mmsc) &&
341729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                xorEquals(first.mmsProxy, second.mmsProxy) &&
341829c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                xorEquals(first.mmsPort, second.mmsPort));
341929c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    }
342029c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
342129c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    // equal or one is not specified
342229c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    private boolean xorEquals(String first, String second) {
342329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        return (Objects.equals(first, second) ||
342429c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                TextUtils.isEmpty(first) ||
342529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                TextUtils.isEmpty(second));
342629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    }
342729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
342829c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    private ApnSetting mergeApns(ApnSetting dest, ApnSetting src) {
3429bca51fc3a191d3ca30df627b75374db0941571c5Sungmin Choi        int id = dest.id;
343029c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        ArrayList<String> resultTypes = new ArrayList<String>();
343129c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        resultTypes.addAll(Arrays.asList(dest.types));
343229c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        for (String srcType : src.types) {
343329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt            if (resultTypes.contains(srcType) == false) resultTypes.add(srcType);
3434bca51fc3a191d3ca30df627b75374db0941571c5Sungmin Choi            if (srcType.equals(PhoneConstants.APN_TYPE_DEFAULT)) id = src.id;
343529c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        }
343629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        String mmsc = (TextUtils.isEmpty(dest.mmsc) ? src.mmsc : dest.mmsc);
343729c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        String mmsProxy = (TextUtils.isEmpty(dest.mmsProxy) ? src.mmsProxy : dest.mmsProxy);
343829c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt        String mmsPort = (TextUtils.isEmpty(dest.mmsPort) ? src.mmsPort : dest.mmsPort);
343961cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan        String proxy = (TextUtils.isEmpty(dest.proxy) ? src.proxy : dest.proxy);
344061cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan        String port = (TextUtils.isEmpty(dest.port) ? src.port : dest.port);
344161cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan        String protocol = src.protocol.equals("IPV4V6") ? src.protocol : dest.protocol;
344261cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan        String roamingProtocol = src.roamingProtocol.equals("IPV4V6") ? src.roamingProtocol :
344361cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan                dest.roamingProtocol;
3444aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan        int bearerBitmask = (dest.bearerBitmask == 0 || src.bearerBitmask == 0) ?
3445aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                0 : (dest.bearerBitmask | src.bearerBitmask);
344629c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
3447bca51fc3a191d3ca30df627b75374db0941571c5Sungmin Choi        return new ApnSetting(id, dest.numeric, dest.carrier, dest.apn,
344861cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan                proxy, port, mmsc, mmsProxy, mmsPort, dest.user, dest.password,
344961cdbcf2de392d043de0e4a533fbc615fc423000Amit Mahajan                dest.authType, resultTypes.toArray(new String[0]), protocol,
3450aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                roamingProtocol, dest.carrierEnabled, 0, bearerBitmask, dest.profileId,
345129c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                (dest.modemCognitive || src.modemCognitive), dest.maxConns, dest.waitTime,
345229c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt                dest.maxConnsTime, dest.mtu, dest.mvnoType, dest.mvnoMatchData);
345329c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt    }
345429c6659c8767212d23d417f2b7f032b6c0d82119Robert Greenwalt
3455ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville    /** Return the DC AsyncChannel for the new data connection */
3456454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville    private DcAsyncChannel createDataConnection() {
3457cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("createDataConnection E");
3458cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3459cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        int id = mUniqueIdGenerator.getAndIncrement();
3460ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        DataConnection conn = DataConnection.makeDataConnection(mPhone, id,
3461ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                                                this, mDcTesterFailBringUpAll, mDcc);
3462cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        mDataConnections.put(id, conn);
3463454b1dfd508844b42eb775e4ab2359be74d3672bWink Saville        DcAsyncChannel dcac = new DcAsyncChannel(conn, LOG_TAG);
3464cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        int status = dcac.fullyConnectSync(mPhone.getContext(), this, conn.getHandler());
3465cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (status == AsyncChannel.STATUS_SUCCESSFUL) {
3466ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            mDataConnectionAcHashMap.put(dcac.getDataConnectionIdSync(), dcac);
3467c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } else {
3468ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            loge("createDataConnection: Could not connect to dcac=" + dcac + " status=" + status);
3469c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3470cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3471cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("createDataConnection() X id=" + id + " dc=" + conn);
3472ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        return dcac;
3473c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3474c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3475cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private void destroyDataConnections() {
3476cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if(mDataConnections != null) {
3477cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("destroyDataConnections: clear mDataConnectionList");
3478cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            mDataConnections.clear();
3479cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
3480cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("destroyDataConnections: mDataConnecitonList is empty, ignore");
3481c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3482c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3483c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3484c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /**
3485cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * Build a list of APNs to be used to create PDP's.
3486c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     *
3487cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param requestedApnType
3488cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @return waitingApns list to be used to create PDP
3489cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     *          error when waitingApns.isEmpty()
3490c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville     */
3491203e588e3c42a81aa8a56f595119c181a63b12caWink Saville    private ArrayList<ApnSetting> buildWaitingApns(String requestedApnType, int radioTech) {
3492cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) log("buildWaitingApns: E requestedApnType=" + requestedApnType);
3493cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ArrayList<ApnSetting> apnList = new ArrayList<ApnSetting>();
3494cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3495cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (requestedApnType.equals(PhoneConstants.APN_TYPE_DUN)) {
3496cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            ApnSetting dun = fetchDunApn();
3497cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (dun != null) {
3498cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                apnList.add(dun);
3499cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("buildWaitingApns: X added APN_TYPE_DUN apnList=" + apnList);
3500cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                return apnList;
3501c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
3502c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3503c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3504cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        IccRecords r = mIccRecords.get();
3505cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        String operator = (r != null) ? r.getOperatorNumeric() : "";
3506c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3507cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // This is a workaround for a bug (7305641) where we don't failover to other
3508cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // suitable APNs if our preferred APN fails.  On prepaid ATT sims we need to
3509cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // failover to a provisioning APN, but once we've used their default data
3510cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // connection we are locked to it for life.  This change allows ATT devices
3511cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        // to say they don't want to use preferred at all.
3512cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        boolean usePreferred = true;
3513cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        try {
3514cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            usePreferred = ! mPhone.getContext().getResources().getBoolean(com.android.
3515cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    internal.R.bool.config_dontPreferApn);
3516cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } catch (Resources.NotFoundException e) {
3517cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) log("buildWaitingApns: usePreferred NotFoundException set to true");
3518cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            usePreferred = true;
3519cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
3520bf660a4647db8151609cdfa0ecc4c96e1518947fSungmin Choi        if (usePreferred) {
3521bf660a4647db8151609cdfa0ecc4c96e1518947fSungmin Choi            mPreferredApn = getPreferredApn();
3522bf660a4647db8151609cdfa0ecc4c96e1518947fSungmin Choi        }
3523cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (DBG) {
3524cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("buildWaitingApns: usePreferred=" + usePreferred
352522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                    + " canSetPreferApn=" + mCanSetPreferApn
3526cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    + " mPreferredApn=" + mPreferredApn
3527cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    + " operator=" + operator + " radioTech=" + radioTech
3528cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    + " IccRecords r=" + r);
3529cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
3530c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
353122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (usePreferred && mCanSetPreferApn && mPreferredApn != null &&
3532cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mPreferredApn.canHandleType(requestedApnType)) {
3533cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (DBG) {
3534cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                log("buildWaitingApns: Preferred APN:" + operator + ":"
3535cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        + mPreferredApn.numeric + ":" + mPreferredApn);
3536cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            }
3537cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (mPreferredApn.numeric.equals(operator)) {
3538aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                if (ServiceState.bitmaskHasTech(mPreferredApn.bearerBitmask, radioTech)) {
3539cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    apnList.add(mPreferredApn);
3540cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    if (DBG) log("buildWaitingApns: X added preferred apnList=" + apnList);
3541cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    return apnList;
3542cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
3543cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    if (DBG) log("buildWaitingApns: no preferred APN");
3544cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    setPreferredApn(-1);
3545cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    mPreferredApn = null;
3546c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                }
3547cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else {
3548cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("buildWaitingApns: no preferred APN");
3549cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                setPreferredApn(-1);
3550cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mPreferredApn = null;
3551c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
3552c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3553ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        if (mAllApnSettings != null) {
3554ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            if (DBG) log("buildWaitingApns: mAllApnSettings=" + mAllApnSettings);
3555ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            for (ApnSetting apn : mAllApnSettings) {
3556cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (apn.canHandleType(requestedApnType)) {
3557aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                    if (ServiceState.bitmaskHasTech(apn.bearerBitmask, radioTech)) {
35589232dafa7ea833fc0b3a6024d6c7e23fc8e961eaRobert Greenwalt                        if (DBG) log("buildWaitingApns: adding apn=" + apn);
3559cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        apnList.add(apn);
3560c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    } else {
3561cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        if (DBG) {
3562aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                            log("buildWaitingApns: bearerBitmask:" + apn.bearerBitmask + " does " +
3563aaf0e8d623b1e94004557573aef9235177ca19e3Amit Mahajan                                    "not include radioTech:" + radioTech);
3564cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        }
3565c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                    }
35669232dafa7ea833fc0b3a6024d6c7e23fc8e961eaRobert Greenwalt                } else if (DBG) {
356727b650c406018355a88a41528db7859e232728a0Jack Yu                    log("buildWaitingApns: couldn't handle requested ApnType="
3568cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                            + requestedApnType);
3569c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville                }
3570c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            }
3571cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
35724bd0ae43eb0cb9969dee4f30cddc18a71da68190Jack Yu            loge("mAllApnSettings is null!");
3573c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
35740e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu        if (DBG) log("buildWaitingApns: " + apnList.size() + " APNs in the list: " + apnList);
3575cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return apnList;
3576c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3577c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3578cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private String apnListToString (ArrayList<ApnSetting> apns) {
3579cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        StringBuilder result = new StringBuilder();
3580cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        for (int i = 0, size = apns.size(); i < size; i++) {
3581cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            result.append('[')
3582cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                  .append(apns.get(i).toString())
3583cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                  .append(']');
3584c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        }
3585cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return result.toString();
3586c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
3587c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
3588cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private void setPreferredApn(int pos) {
358922d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (!mCanSetPreferApn) {
3590cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("setPreferredApn: X !canSEtPreferApn");
3591cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return;
3592cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
3593cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
35946bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        String subId = Long.toString(mPhone.getSubId());
35956bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        Uri uri = Uri.withAppendedPath(PREFERAPN_NO_UPDATE_URI_USING_SUBID, subId);
3596cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        log("setPreferredApn: delete");
3597cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        ContentResolver resolver = mPhone.getContext().getContentResolver();
35986bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        resolver.delete(uri, null, null);
3599cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3600cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (pos >= 0) {
3601cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            log("setPreferredApn: insert");
3602cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            ContentValues values = new ContentValues();
3603cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            values.put(APN_ID, pos);
36046bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville            resolver.insert(uri, values);
3605cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
3606cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
3607cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3608cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private ApnSetting getPreferredApn() {
36094bd0ae43eb0cb9969dee4f30cddc18a71da68190Jack Yu        if (mAllApnSettings == null || mAllApnSettings.isEmpty()) {
36104bd0ae43eb0cb9969dee4f30cddc18a71da68190Jack Yu            log("getPreferredApn: mAllApnSettings is " + ((mAllApnSettings == null)?"null":"empty"));
3611cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return null;
3612cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
3613cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
36146bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        String subId = Long.toString(mPhone.getSubId());
36156bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        Uri uri = Uri.withAppendedPath(PREFERAPN_NO_UPDATE_URI_USING_SUBID, subId);
3616cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        Cursor cursor = mPhone.getContext().getContentResolver().query(
36176bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville                uri, new String[] { "_id", "name", "apn" },
3618cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
3619cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3620cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (cursor != null) {
362122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mCanSetPreferApn = true;
3622cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
362322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mCanSetPreferApn = false;
3624cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
3625cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        log("getPreferredApn: mRequestedApnType=" + mRequestedApnType + " cursor=" + cursor
3626cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                + " cursor.count=" + ((cursor != null) ? cursor.getCount() : 0));
3627cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
362822d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        if (mCanSetPreferApn && cursor.getCount() > 0) {
3629cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            int pos;
3630cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            cursor.moveToFirst();
3631cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            pos = cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers._ID));
3632ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville            for(ApnSetting p : mAllApnSettings) {
3633cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                log("getPreferredApn: apnSetting=" + p);
3634cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (p.id == pos && p.canHandleType(mRequestedApnType)) {
3635cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    log("getPreferredApn: X found apnSetting" + p);
3636cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    cursor.close();
3637cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    return p;
3638cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
3639cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla            }
3640cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        }
3641cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3642cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (cursor != null) {
3643cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            cursor.close();
3644cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
3645cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3646cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        log("getPreferredApn: X not found");
3647cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return null;
3648cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla    }
3649cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3650cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    @Override
3651cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    public void handleMessage (Message msg) {
36521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG) log("handleMessage msg=" + msg);
3653cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3654cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        switch (msg.what) {
3655cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_RECORDS_LOADED:
36561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // If onRecordsLoadedOrSubIdChanged() is not called here, it should be called on
36571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // onSubscriptionsChanged() when a valid subId is available.
36581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                int subId = mPhone.getSubId();
36591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (SubscriptionManager.isValidSubscriptionId(subId)) {
36601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    onRecordsLoadedOrSubIdChanged();
36611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else {
36621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("Ignoring EVENT_RECORDS_LOADED as subId is not valid: " + subId);
36631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
3664cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                break;
3665cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3666cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_DATA_CONNECTION_DETACHED:
3667cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                onDataConnectionDetached();
3668cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                break;
3669cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
3670cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_DATA_CONNECTION_ATTACHED:
3671bda761320929f714951c328bfec6a51a1978db97Wink Saville                onDataConnectionAttached();
3672cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                break;
3673cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3674cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_DO_RECOVERY:
3675cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                doRecovery();
3676cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                break;
3677cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3678cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_APN_CHANGED:
3679cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                onApnChanged();
3680cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                break;
3681cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3682cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_PS_RESTRICT_ENABLED:
3683cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                /**
3684cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 * We don't need to explicitly to tear down the PDP context
3685cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 * when PS restricted is enabled. The base band will deactive
3686cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 * PDP context and notify us with PDP_CONTEXT_CHANGED.
3687cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 * But we should stop the network polling and prevent reset PDP.
3688cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 */
3689cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("EVENT_PS_RESTRICT_ENABLED " + mIsPsRestricted);
3690cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                stopNetStatPoll();
3691cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                stopDataStallAlarm();
3692cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mIsPsRestricted = true;
3693cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla                break;
3694cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3695cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_PS_RESTRICT_DISABLED:
3696cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                /**
3697cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 * When PS restrict is removed, we need setup PDP connection if
3698cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 * PDP connection is down.
3699cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                 */
3700cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("EVENT_PS_RESTRICT_DISABLED " + mIsPsRestricted);
3701cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mIsPsRestricted  = false;
3702cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (isConnected()) {
3703cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    startNetStatPoll();
3704cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
3705cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
3706cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    // TODO: Should all PDN states be checked to fail?
3707ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville                    if (mState == DctConstants.State.FAILED) {
3708cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        cleanUpAllConnections(false, Phone.REASON_PS_RESTRICT_ENABLED);
3709cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                        mReregisterOnReconnectFailure = false;
3710cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    }
3711af5593594070f825032be46dced573cd195956e1Robert Greenwalt                    ApnContext apnContext = mApnContextsById.get(DctConstants.APN_DEFAULT_ID);
37123fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    if (apnContext != null) {
37133fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                        apnContext.setReason(Phone.REASON_PS_RESTRICT_ENABLED);
37143fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                        trySetupData(apnContext);
37153fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    } else {
37163fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                        loge("**** Default ApnContext not found ****");
37173fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                        if (Build.IS_DEBUGGABLE) {
37183fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                            throw new RuntimeException("Default ApnContext not found");
37193fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                        }
37203fad7daba62dcb7aafc4adc7f8cc123726ed5a7cRobert Greenwalt                    }
3721cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
3722cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla                break;
3723ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
3724cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_TRY_SETUP_DATA:
3725cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (msg.obj instanceof ApnContext) {
3726cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    onTrySetupData((ApnContext)msg.obj);
3727cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else if (msg.obj instanceof String) {
3728cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    onTrySetupData((String)msg.obj);
3729cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
3730cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    loge("EVENT_TRY_SETUP request w/o apnContext or String");
3731cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
3732cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla                break;
3733cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville
3734cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            case DctConstants.EVENT_CLEAN_UP_CONNECTION:
3735cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                boolean tearDown = (msg.arg1 == 0) ? false : true;
3736cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (DBG) log("EVENT_CLEAN_UP_CONNECTION tearDown=" + tearDown);
3737cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                if (msg.obj instanceof ApnContext) {
3738cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                    cleanUpConnection(tearDown, (ApnContext)msg.obj);
3739cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                } else {
37401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    onCleanUpConnection(tearDown, msg.arg2, (String) msg.obj);
3741cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                }
3742cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla                break;
37431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_SET_INTERNAL_DATA_ENABLE: {
37441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                final boolean enabled = (msg.arg1 == DctConstants.ENABLED) ? true : false;
3745a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                onSetInternalDataEnabled(enabled, (Message) msg.obj);
3746a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                break;
37471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
3748a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            case DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS:
37491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if ((msg.obj != null) && (msg.obj instanceof String == false)) {
37501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    msg.obj = null;
3751a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
37521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onCleanUpAllConnections((String) msg.obj);
3753a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                break;
3754ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville
3755220d43e0611edd0f3a2eb7a33bb38a008fff0868Wink Saville            case DctConstants.EVENT_DATA_RAT_CHANGED:
3756220d43e0611edd0f3a2eb7a33bb38a008fff0868Wink Saville                //May new Network allow setupData, so try it here
3757c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                setupDataOnConnectableApns(Phone.REASON_NW_TYPE_CHANGED,
3758c2d1d6b2725b4611360d2725624a0d8905d75694Robert Greenwalt                        RetryFailures.ONLY_ON_CHANGE);
3759220d43e0611edd0f3a2eb7a33bb38a008fff0868Wink Saville                break;
3760220d43e0611edd0f3a2eb7a33bb38a008fff0868Wink Saville
37612b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen            case DctConstants.CMD_CLEAR_PROVISIONING_SPINNER:
37622b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                // Check message sender intended to clear the current spinner.
37632b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                if (mProvisioningSpinner == msg.obj) {
37642b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    mProvisioningSpinner.dismiss();
37652b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                    mProvisioningSpinner = null;
37662b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                }
37672b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen                break;
37681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
37691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("DISCONNECTED_CONNECTED: msg=" + msg);
37701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                DcAsyncChannel dcac = (DcAsyncChannel) msg.obj;
37711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mDataConnectionAcHashMap.remove(dcac.getDataConnectionIdSync());
37721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                dcac.disconnected();
37731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
37741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
37751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_ENABLE_NEW_APN:
37761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onEnableApn(msg.arg1, msg.arg2);
37771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
37781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
37791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_DATA_STALL_ALARM:
37801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onDataStallAlarm(msg.arg1);
37811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
37821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
37831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_ROAMING_OFF:
37841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onRoamingOff();
37851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
37861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
37871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_ROAMING_ON:
37881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onRoamingOn();
37891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
37901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3791f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt            case DctConstants.EVENT_DEVICE_PROVISIONED_CHANGE:
3792f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                onDeviceProvisionedChange();
3793f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt                break;
3794f299322eabb18e1234c81fe1e356b550b6687772Robert Greenwalt
3795a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu            case DctConstants.EVENT_REDIRECTION_DETECTED:
37964c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                String url = (String) msg.obj;
379768f4f4a0bc8d4060b5775e7a24a97ea5b485989efionaxu                log("dataConnectionTracker.handleMessage: EVENT_REDIRECTION_DETECTED=" + url);
37984c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                onDataConnectionRedirected(url);
3799a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu
38001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_RADIO_AVAILABLE:
38011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onRadioAvailable();
38021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_RADIO_OFF_OR_NOT_AVAILABLE:
38051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onRadioOffOrNotAvailable();
38061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_DATA_SETUP_COMPLETE:
38091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onDataSetupComplete((AsyncResult) msg.obj);
38101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_DATA_SETUP_COMPLETE_ERROR:
38131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onDataSetupCompleteError((AsyncResult) msg.obj);
38141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
3815a02b6fd88953d783c32e6d7f84b7eddbc0d1faf1fionaxu
38161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_DISCONNECT_DONE:
38171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("DataConnectionTracker.handleMessage: EVENT_DISCONNECT_DONE msg=" + msg);
38181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onDisconnectDone((AsyncResult) msg.obj);
38191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_DISCONNECT_DC_RETRYING:
38221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("DataConnectionTracker.handleMessage: EVENT_DISCONNECT_DC_RETRYING msg=" + msg);
38231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onDisconnectDcRetrying((AsyncResult) msg.obj);
38241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_VOICE_CALL_STARTED:
38271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onVoiceCallStarted();
38281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_VOICE_CALL_ENDED:
38311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onVoiceCallEnded();
38321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
38341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_RESET_DONE: {
38351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("EVENT_RESET_DONE");
38361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onResetDone((AsyncResult) msg.obj);
38371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
38391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_SET_USER_DATA_ENABLE: {
38401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                final boolean enabled = (msg.arg1 == DctConstants.ENABLED) ? true : false;
38411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("CMD_SET_USER_DATA_ENABLE enabled=" + enabled);
38421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onSetUserDataEnabled(enabled);
38431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
38451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // TODO - remove
38461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_SET_DEPENDENCY_MET: {
38471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                boolean met = (msg.arg1 == DctConstants.ENABLED) ? true : false;
38481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("CMD_SET_DEPENDENCY_MET met=" + met);
38491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Bundle bundle = msg.getData();
38501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (bundle != null) {
38511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    String apnType = (String)bundle.get(DctConstants.APN_TYPE_KEY);
38521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (apnType != null) {
38531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        onSetDependencyMet(apnType, met);
38541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
38551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
38561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
38581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_SET_POLICY_DATA_ENABLE: {
38591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                final boolean enabled = (msg.arg1 == DctConstants.ENABLED) ? true : false;
38601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onSetPolicyDataEnabled(enabled);
38611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
38631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA: {
38641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                sEnableFailFastRefCounter += (msg.arg1 == DctConstants.ENABLED) ? 1 : -1;
38651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) {
38661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA: "
38671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            + " sEnableFailFastRefCounter=" + sEnableFailFastRefCounter);
38681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
38691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (sEnableFailFastRefCounter < 0) {
38701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    final String s = "CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA: "
38711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            + "sEnableFailFastRefCounter:" + sEnableFailFastRefCounter + " < 0";
38721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    loge(s);
38731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    sEnableFailFastRefCounter = 0;
38741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
38751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                final boolean enabled = sEnableFailFastRefCounter > 0;
38761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) {
38771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA: enabled=" + enabled
38781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            + " sEnableFailFastRefCounter=" + sEnableFailFastRefCounter);
38791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
38801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (mFailFast != enabled) {
38811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    mFailFast = enabled;
38820e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu
38831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    mDataStallDetectionEnabled = !enabled;
38841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (mDataStallDetectionEnabled
38851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            && (getOverallState() == DctConstants.State.CONNECTED)
38861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            && (!mInVoiceCall ||
38871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                                    mPhone.getServiceStateTracker()
38881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                                        .isConcurrentVoiceAndDataAllowed())) {
38891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (DBG) log("CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA: start data stall");
38901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        stopDataStallAlarm();
38911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
38921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    } else {
38931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (DBG) log("CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA: stop data stall");
38941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        stopDataStallAlarm();
38951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
38961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
38972b7b6016c7a5f2c3ce9c7e623ea10a9fe9239dc2Paul Jensen
38981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
38991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_ENABLE_MOBILE_PROVISIONING: {
39011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Bundle bundle = msg.getData();
39021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (bundle != null) {
39031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    try {
39041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mProvisioningUrl = (String)bundle.get(DctConstants.PROVISIONING_URL_KEY);
39051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    } catch(ClassCastException e) {
39061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        loge("CMD_ENABLE_MOBILE_PROVISIONING: provisioning url not a string" + e);
39071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mProvisioningUrl = null;
39081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
39091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
39101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (TextUtils.isEmpty(mProvisioningUrl)) {
39111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    loge("CMD_ENABLE_MOBILE_PROVISIONING: provisioning url is empty, ignoring");
39121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    mIsProvisioning = false;
39131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    mProvisioningUrl = null;
39141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else {
39151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    loge("CMD_ENABLE_MOBILE_PROVISIONING: provisioningUrl=" + mProvisioningUrl);
39161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    mIsProvisioning = true;
39171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    startProvisioningApnAlarm();
39181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
39191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_PROVISIONING_APN_ALARM: {
39221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("EVENT_PROVISIONING_APN_ALARM");
3923af5593594070f825032be46dced573cd195956e1Robert Greenwalt                ApnContext apnCtx = mApnContextsById.get(DctConstants.APN_DEFAULT_ID);
39241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (apnCtx.isProvisioningApn() && apnCtx.isConnectedOrConnecting()) {
39251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (mProvisioningApnAlarmTag == msg.arg1) {
39261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (DBG) log("EVENT_PROVISIONING_APN_ALARM: Disconnecting");
39271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mIsProvisioning = false;
39281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mProvisioningUrl = null;
39291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        stopProvisioningApnAlarm();
39301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        sendCleanUpConnection(true, apnCtx);
39311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    } else {
39321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        if (DBG) {
39331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                            log("EVENT_PROVISIONING_APN_ALARM: ignore stale tag,"
39341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                                    + " mProvisioningApnAlarmTag:" + mProvisioningApnAlarmTag
39351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                                    + " != arg1:" + msg.arg1);
39361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        }
39371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
39381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else {
39391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (DBG) log("EVENT_PROVISIONING_APN_ALARM: Not connected ignore");
39401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
39411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_IS_PROVISIONING_APN: {
39441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("CMD_IS_PROVISIONING_APN");
39451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                boolean isProvApn;
39461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                try {
39471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    String apnType = null;
39481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    Bundle bundle = msg.getData();
39491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (bundle != null) {
39501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        apnType = (String)bundle.get(DctConstants.APN_TYPE_KEY);
39511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
39521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    if (TextUtils.isEmpty(apnType)) {
39531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        loge("CMD_IS_PROVISIONING_APN: apnType is empty");
39541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        isProvApn = false;
39551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    } else {
39561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        isProvApn = isProvisioningApn(apnType);
39571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    }
39581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } catch (ClassCastException e) {
39591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    loge("CMD_IS_PROVISIONING_APN: NO provisioning url ignoring");
39601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    isProvApn = false;
39611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
39621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("CMD_IS_PROVISIONING_APN: ret=" + isProvApn);
39631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mReplyAc.replyToMessage(msg, DctConstants.CMD_IS_PROVISIONING_APN,
39641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        isProvApn ? DctConstants.ENABLED : DctConstants.DISABLED);
39651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_ICC_CHANGED: {
39681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                onUpdateIcc();
39691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_RESTART_RADIO: {
39721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                restartRadio();
39731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.CMD_NET_STAT_POLL: {
39761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (msg.arg1 == DctConstants.ENABLED) {
39771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    handleStartNetStatPoll((DctConstants.Activity)msg.obj);
39781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } else if (msg.arg1 == DctConstants.DISABLED) {
39791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    handleStopNetStatPoll((DctConstants.Activity)msg.obj);
39801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                }
39811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case DctConstants.EVENT_DATA_STATE_CHANGED: {
39841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // no longer do anything, but still registered - clean up log
39851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // TODO - why are we still registering?
39861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
39871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
39882e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt            case DctConstants.EVENT_PCO_DATA_RECEIVED: {
39892e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt                handlePcoData((AsyncResult)msg.obj);
39902e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt                break;
39912e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt            }
3992a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            case DctConstants.EVENT_SET_CARRIER_DATA_ENABLED:
3993a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                onSetCarrierDataEnabled(msg.arg1 == DctConstants.ENABLED);
3994a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                break;
3995cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla            default:
39961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Rlog.e("DcTracker", "Unhandled event=" + msg);
3997cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                break;
39981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
3999cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
4000cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla    }
4001cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
40021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int getApnProfileID(String apnType) {
4003cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_IMS)) {
4004cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return RILConstants.DATA_PROFILE_IMS;
4005cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_FOTA)) {
4006cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return RILConstants.DATA_PROFILE_FOTA;
4007cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_CBS)) {
4008cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return RILConstants.DATA_PROFILE_CBS;
40091b5fe200e47f40f82f0e28502a5f40bce64a82e6Wink Saville        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_IA)) {
40101b5fe200e47f40f82f0e28502a5f40bce64a82e6Wink Saville            return RILConstants.DATA_PROFILE_DEFAULT; // DEFAULT for now
401145df26444864daad60afdd4d121ab4043da3834bSungmin Choi        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_DUN)) {
401245df26444864daad60afdd4d121ab4043da3834bSungmin Choi            return RILConstants.DATA_PROFILE_TETHERED;
4013cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        } else {
4014cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            return RILConstants.DATA_PROFILE_DEFAULT;
4015cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
4016cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    }
4017cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4018cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    private int getCellLocationId() {
4019cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        int cid = -1;
4020cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        CellLocation loc = mPhone.getCellLocation();
4021cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4022cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (loc != null) {
4023cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (loc instanceof GsmCellLocation) {
4024cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                cid = ((GsmCellLocation)loc).getCid();
4025cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            } else if (loc instanceof CdmaCellLocation) {
4026cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                cid = ((CdmaCellLocation)loc).getBaseStationId();
4027cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla            }
4028cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
4029cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        return cid;
4030cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla    }
4031cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4032a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private IccRecords getUiccRecords(int appFamily) {
4033a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        return mUiccController.getIccRecords(mPhone.getPhoneId(), appFamily);
4034a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4035a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4036a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
40371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onUpdateIcc() {
4038cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (mUiccController == null ) {
4039cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla            return;
4040cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla        }
4041cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4042a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        IccRecords newIccRecords = getUiccRecords(UiccController.APP_FAM_3GPP);
4043cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4044cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        IccRecords r = mIccRecords.get();
4045cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        if (r != newIccRecords) {
4046cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (r != null) {
4047cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                log("Removing stale icc objects.");
4048cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                r.unregisterForRecordsLoaded(this);
4049cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville                mIccRecords.set(null);
40509aaa228cbb37657d12a87b5058676e449866f2acJeevaka Badrappan            }
4051cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville            if (newIccRecords != null) {
40521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (SubscriptionManager.isValidSubscriptionId(mPhone.getSubId())) {
4053aa863054476b152fe9323defd197fa946a47033fSungmin Choi                    log("New records found.");
4054aa863054476b152fe9323defd197fa946a47033fSungmin Choi                    mIccRecords.set(newIccRecords);
4055aa863054476b152fe9323defd197fa946a47033fSungmin Choi                    newIccRecords.registerForRecordsLoaded(
4056aa863054476b152fe9323defd197fa946a47033fSungmin Choi                            this, DctConstants.EVENT_RECORDS_LOADED, null);
40574c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    // reset carrier actions on sim loaded
40584c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    final ServiceStateTracker sst = mPhone.getServiceStateTracker();
40594c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    sst.setRadioPowerFromCarrier(true);
4060a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    mDataEnabledSettings.setCarrierDataEnabled(true);
40614c31e4c0d2db31fe84081aa6f1e2bcc08b31f9d3fionaxu                    mPhone.getCarrierSignalAgent().reset();
4062aa863054476b152fe9323defd197fa946a47033fSungmin Choi                }
40630469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal            } else {
40640469925aace7a2254e0cef90bd7398fbd72622faShishir Agrawal                onSimNotReady();
40659aaa228cbb37657d12a87b5058676e449866f2acJeevaka Badrappan            }
40669aaa228cbb37657d12a87b5058676e449866f2acJeevaka Badrappan        }
4067cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla    }
4068cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4069a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void update() {
4070a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        log("update sub = " + mPhone.getSubId());
4071bda761320929f714951c328bfec6a51a1978db97Wink Saville        log("update(): Active DDS, register for all events now!");
4072bda761320929f714951c328bfec6a51a1978db97Wink Saville        onUpdateIcc();
4073a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4074a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        mDataEnabledSettings.setUserDataEnabled(getDataEnabled());
40750979b71e48405cab10bdf1d1b4170cfce72838a7Jack Yu        mAutoAttachOnCreation.set(false);
4076bda761320929f714951c328bfec6a51a1978db97Wink Saville
40771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ((GsmCdmaPhone)mPhone).updateCurrentCarrierInProvider();
4078a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4079a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4080a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void cleanUpAllConnections(String cause) {
4081a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        cleanUpAllConnections(cause, null);
4082a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4083a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4084a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void updateRecords() {
4085bda761320929f714951c328bfec6a51a1978db97Wink Saville        onUpdateIcc();
4086a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4087a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4088a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void cleanUpAllConnections(String cause, Message disconnectAllCompleteMsg) {
4089a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        log("cleanUpAllConnections");
4090a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (disconnectAllCompleteMsg != null) {
4091a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            mDisconnectAllCompleteMsgList.add(disconnectAllCompleteMsg);
4092a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
4093a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4094a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        Message msg = obtainMessage(DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS);
4095a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        msg.obj = cause;
4096a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        sendMessage(msg);
4097a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4098a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
40991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void notifyDataDisconnectComplete() {
4100a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        log("notifyDataDisconnectComplete");
4101a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (Message m: mDisconnectAllCompleteMsgList) {
4102a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            m.sendToTarget();
4103a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
4104a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mDisconnectAllCompleteMsgList.clear();
4105a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4106a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4107a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
41081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void notifyAllDataDisconnected() {
4109a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        sEnableFailFastRefCounter = 0;
4110a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mFailFast = false;
4111a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mAllDataDisconnectedRegistrants.notifyRegistrants();
4112a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4113a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4114a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void registerForAllDataDisconnected(Handler h, int what, Object obj) {
4115a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mAllDataDisconnectedRegistrants.addUnique(h, what, obj);
4116a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4117a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (isDisconnected()) {
4118a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            log("notify All Data Disconnected");
4119a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            notifyAllDataDisconnected();
4120a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
4121a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4122a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4123a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void unregisterForAllDataDisconnected(Handler h) {
4124a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        mAllDataDisconnectedRegistrants.remove(h);
4125a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4126a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4127a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    public void registerForDataEnabledChanged(Handler h, int what, Object obj) {
4128a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        mDataEnabledSettings.registerForDataEnabledChanged(h, what, obj);
4129a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    }
4130a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu
4131a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    public void unregisterForDataEnabledChanged(Handler h) {
4132a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        mDataEnabledSettings.unregisterForDataEnabledChanged(h);
4133a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu    }
4134a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
41351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onSetInternalDataEnabled(boolean enabled, Message onCompleteMsg) {
4136a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        synchronized (mDataEnabledSettings) {
4137a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            if (DBG) log("onSetInternalDataEnabled: enabled=" + enabled);
4138a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            boolean sendOnComplete = true;
4139a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4140a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            mDataEnabledSettings.setInternalDataEnabled(enabled);
4141a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            if (enabled) {
4142a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                log("onSetInternalDataEnabled: changed to enabled, try to setup data call");
4143a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                onTrySetupData(Phone.REASON_DATA_ENABLED);
4144a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            } else {
4145a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                sendOnComplete = false;
4146a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                log("onSetInternalDataEnabled: changed to disabled, cleanUpAllConnections");
41474fdc57b9bf223c908474c4545cc6f63456117a3bSanket Padawe                cleanUpAllConnections(Phone.REASON_DATA_DISABLED, onCompleteMsg);
4148a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
4149a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4150a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu            if (sendOnComplete) {
4151a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                if (onCompleteMsg != null) {
4152a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                    onCompleteMsg.sendToTarget();
4153a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu                }
4154a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
4155a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
4156a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4157a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4158a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public boolean setInternalDataEnabled(boolean enable) {
4159a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        return setInternalDataEnabled(enable, null);
4160a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4161a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4162a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public boolean setInternalDataEnabled(boolean enable, Message onCompleteMsg) {
41636bc4098827f3070a44b5e51508b455d7c7be9c07Wink Saville        if (DBG) log("setInternalDataEnabled(" + enable + ")");
4164a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4165a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        Message msg = obtainMessage(DctConstants.EVENT_SET_INTERNAL_DATA_ENABLE, onCompleteMsg);
4166a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        msg.arg1 = (enable ? DctConstants.ENABLED : DctConstants.DISABLED);
4167a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        sendMessage(msg);
4168a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        return true;
4169a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4170a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
41711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void log(String s) {
4172a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        Rlog.d(LOG_TAG, "[" + mPhone.getPhoneId() + "]" + s);
4173cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla    }
4174cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
41751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void loge(String s) {
4176a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        Rlog.e(LOG_TAG, "[" + mPhone.getPhoneId() + "]" + s);
4177cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla    }
4178cebb2cc576c652dd642d7f419532ec04e0f59d7dNaveen Kalla
4179c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
41801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println("DcTracker:");
41811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" RADIO_TESTS=" + RADIO_TESTS);
4182a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        pw.println(" isInternalDataEnabled=" + mDataEnabledSettings.isInternalDataEnabled());
4183a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        pw.println(" isUserDataEnabled=" + mDataEnabledSettings.isUserDataEnabled());
4184a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        pw.println(" isPolicyDataEnabled=" + mDataEnabledSettings.isPolicyDataEnabled());
41851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.flush();
41861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mRequestedApnType=" + mRequestedApnType);
41871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mPhone=" + mPhone.getPhoneName());
41881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mActivity=" + mActivity);
41891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mState=" + mState);
41901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mTxPkts=" + mTxPkts);
41911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mRxPkts=" + mRxPkts);
41921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mNetStatPollPeriod=" + mNetStatPollPeriod);
41931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mNetStatPollEnabled=" + mNetStatPollEnabled);
41941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mDataStallTxRxSum=" + mDataStallTxRxSum);
41951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mDataStallAlarmTag=" + mDataStallAlarmTag);
4196a8f5a859f8e4a15902d729af5d2edc9a9433ba41Jack Yu        pw.println(" mDataStallDetectionEnabled=" + mDataStallDetectionEnabled);
41971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mSentSinceLastRecv=" + mSentSinceLastRecv);
41981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mNoRecvPollCount=" + mNoRecvPollCount);
41991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mResolver=" + mResolver);
42001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mIsWifiConnected=" + mIsWifiConnected);
42011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mReconnectIntent=" + mReconnectIntent);
42021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mAutoAttachOnCreation=" + mAutoAttachOnCreation.get());
42031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mIsScreenOn=" + mIsScreenOn);
42041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mUniqueIdGenerator=" + mUniqueIdGenerator);
42051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.flush();
42061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" ***************************************");
42071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        DcController dcc = mDcc;
42081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (dcc != null) {
42091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            dcc.dump(fd, pw, args);
42101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
42111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mDcc=null");
42121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
42131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" ***************************************");
42141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        HashMap<Integer, DataConnection> dcs = mDataConnections;
42151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (dcs != null) {
42161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            Set<Entry<Integer, DataConnection> > mDcSet = mDataConnections.entrySet();
42171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mDataConnections: count=" + mDcSet.size());
42181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            for (Entry<Integer, DataConnection> entry : mDcSet) {
42191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                pw.printf(" *** mDataConnection[%d] \n", entry.getKey());
42201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                entry.getValue().dump(fd, pw, args);
42211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
42221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
42231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println("mDataConnections=null");
42241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
42251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" ***************************************");
42261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.flush();
42271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        HashMap<String, Integer> apnToDcId = mApnToDataConnectionId;
42281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (apnToDcId != null) {
42291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            Set<Entry<String, Integer>> apnToDcIdSet = apnToDcId.entrySet();
42301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mApnToDataConnectonId size=" + apnToDcIdSet.size());
42311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            for (Entry<String, Integer> entry : apnToDcIdSet) {
42321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                pw.printf(" mApnToDataConnectonId[%s]=%d\n", entry.getKey(), entry.getValue());
42331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
42341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
42351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println("mApnToDataConnectionId=null");
42361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
42371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" ***************************************");
42381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.flush();
42391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ConcurrentHashMap<String, ApnContext> apnCtxs = mApnContexts;
42401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (apnCtxs != null) {
42411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            Set<Entry<String, ApnContext>> apnCtxsSet = apnCtxs.entrySet();
42421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mApnContexts size=" + apnCtxsSet.size());
42431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            for (Entry<String, ApnContext> entry : apnCtxsSet) {
42441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                entry.getValue().dump(fd, pw, args);
42451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
42461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" ***************************************");
42471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
42481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mApnContexts=null");
42491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
42501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.flush();
42511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        ArrayList<ApnSetting> apnSettings = mAllApnSettings;
42521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (apnSettings != null) {
42531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mAllApnSettings size=" + apnSettings.size());
42541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            for (int i=0; i < apnSettings.size(); i++) {
42551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                pw.printf(" mAllApnSettings[%d]: %s\n", i, apnSettings.get(i));
42561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
42571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.flush();
42581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
42591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            pw.println(" mAllApnSettings=null");
42601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
42611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mPreferredApn=" + mPreferredApn);
42621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mIsPsRestricted=" + mIsPsRestricted);
42631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mIsDisposed=" + mIsDisposed);
42641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.println(" mIntentReceiver=" + mIntentReceiver);
4265cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        pw.println(" mReregisterOnReconnectFailure=" + mReregisterOnReconnectFailure);
426622d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        pw.println(" canSetPreferApn=" + mCanSetPreferApn);
4267cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        pw.println(" mApnObserver=" + mApnObserver);
4268cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville        pw.println(" getOverallState=" + getOverallState());
4269ff4e317d24f0d23bdc0f306d53ddc51f2f1ecf6aWink Saville        pw.println(" mDataConnectionAsyncChannels=%s\n" + mDataConnectionAcHashMap);
4270187a39f896f88eb6c5e4306d9595546654825976Wink Saville        pw.println(" mAttached=" + mAttached.get());
42711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        pw.flush();
4272c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    }
4273a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4274bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram    public String[] getPcscfAddress(String apnType) {
4275a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        log("getPcscfAddress()");
4276bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        ApnContext apnContext = null;
4277bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram
4278bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        if(apnType == null){
4279bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram            log("apnType is null, return null");
4280bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram            return null;
4281bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        }
4282a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4283bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_EMERGENCY)) {
4284af5593594070f825032be46dced573cd195956e1Robert Greenwalt            apnContext = mApnContextsById.get(DctConstants.APN_EMERGENCY_ID);
4285bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_IMS)) {
4286af5593594070f825032be46dced573cd195956e1Robert Greenwalt            apnContext = mApnContextsById.get(DctConstants.APN_IMS_ID);
4287bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        } else {
4288bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram            log("apnType is invalid, return null");
4289bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram            return null;
4290bc78e2f9988f380a3b88d4cb4a9c0b80b8f44beeram        }
4291a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4292a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (apnContext == null) {
4293a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            log("apnContext is null, return null");
4294a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            return null;
4295a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
4296a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4297a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        DcAsyncChannel dcac = apnContext.getDcAc();
4298a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        String[] result = null;
4299a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4300a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (dcac != null) {
4301a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            result = dcac.getPcscfAddr();
4302a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
4303a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            for (int i = 0; i < result.length; i++) {
4304a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                log("Pcscf[" + i + "]: " + result[i]);
4305a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
4306a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            return result;
4307a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
4308a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        return null;
4309a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
4310a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
431176f43316a5a6082d601bffd4b6898d0bd81e11fcram    /**
431276f43316a5a6082d601bffd4b6898d0bd81e11fcram     * Read APN configuration from Telephony.db for Emergency APN
431376f43316a5a6082d601bffd4b6898d0bd81e11fcram     * All opertors recognize the connection request for EPDN based on APN type
431476f43316a5a6082d601bffd4b6898d0bd81e11fcram     * PLMN name,APN name are not mandatory parameters
431576f43316a5a6082d601bffd4b6898d0bd81e11fcram     */
431676f43316a5a6082d601bffd4b6898d0bd81e11fcram    private void initEmergencyApnSetting() {
431776f43316a5a6082d601bffd4b6898d0bd81e11fcram        // Operator Numeric is not available when sim records are not loaded.
431876f43316a5a6082d601bffd4b6898d0bd81e11fcram        // Query Telephony.db with APN type as EPDN request does not
431976f43316a5a6082d601bffd4b6898d0bd81e11fcram        // require APN name, plmn and all operators support same APN config.
432076f43316a5a6082d601bffd4b6898d0bd81e11fcram        // DB will contain only one entry for Emergency APN
432176f43316a5a6082d601bffd4b6898d0bd81e11fcram        String selection = "type=\"emergency\"";
432276f43316a5a6082d601bffd4b6898d0bd81e11fcram        Cursor cursor = mPhone.getContext().getContentResolver().query(
432376f43316a5a6082d601bffd4b6898d0bd81e11fcram                Telephony.Carriers.CONTENT_URI, null, selection, null, null);
432476f43316a5a6082d601bffd4b6898d0bd81e11fcram
432576f43316a5a6082d601bffd4b6898d0bd81e11fcram        if (cursor != null) {
432676f43316a5a6082d601bffd4b6898d0bd81e11fcram            if (cursor.getCount() > 0) {
432776f43316a5a6082d601bffd4b6898d0bd81e11fcram                if (cursor.moveToFirst()) {
432876f43316a5a6082d601bffd4b6898d0bd81e11fcram                    mEmergencyApn = makeApnSetting(cursor);
432976f43316a5a6082d601bffd4b6898d0bd81e11fcram                }
433076f43316a5a6082d601bffd4b6898d0bd81e11fcram            }
433176f43316a5a6082d601bffd4b6898d0bd81e11fcram            cursor.close();
433276f43316a5a6082d601bffd4b6898d0bd81e11fcram        }
433376f43316a5a6082d601bffd4b6898d0bd81e11fcram    }
433476f43316a5a6082d601bffd4b6898d0bd81e11fcram
433576f43316a5a6082d601bffd4b6898d0bd81e11fcram    /**
433676f43316a5a6082d601bffd4b6898d0bd81e11fcram     * Add the Emergency APN settings to APN settings list
433776f43316a5a6082d601bffd4b6898d0bd81e11fcram     */
433876f43316a5a6082d601bffd4b6898d0bd81e11fcram    private void addEmergencyApnSetting() {
433976f43316a5a6082d601bffd4b6898d0bd81e11fcram        if(mEmergencyApn != null) {
434076f43316a5a6082d601bffd4b6898d0bd81e11fcram            if(mAllApnSettings == null) {
434176f43316a5a6082d601bffd4b6898d0bd81e11fcram                mAllApnSettings = new ArrayList<ApnSetting>();
434276f43316a5a6082d601bffd4b6898d0bd81e11fcram            } else {
434376f43316a5a6082d601bffd4b6898d0bd81e11fcram                boolean hasEmergencyApn = false;
434476f43316a5a6082d601bffd4b6898d0bd81e11fcram                for (ApnSetting apn : mAllApnSettings) {
434576f43316a5a6082d601bffd4b6898d0bd81e11fcram                    if (ArrayUtils.contains(apn.types, PhoneConstants.APN_TYPE_EMERGENCY)) {
434676f43316a5a6082d601bffd4b6898d0bd81e11fcram                        hasEmergencyApn = true;
434776f43316a5a6082d601bffd4b6898d0bd81e11fcram                        break;
434876f43316a5a6082d601bffd4b6898d0bd81e11fcram                    }
434976f43316a5a6082d601bffd4b6898d0bd81e11fcram                }
435076f43316a5a6082d601bffd4b6898d0bd81e11fcram
435176f43316a5a6082d601bffd4b6898d0bd81e11fcram                if(hasEmergencyApn == false) {
435276f43316a5a6082d601bffd4b6898d0bd81e11fcram                    mAllApnSettings.add(mEmergencyApn);
435376f43316a5a6082d601bffd4b6898d0bd81e11fcram                } else {
435476f43316a5a6082d601bffd4b6898d0bd81e11fcram                    log("addEmergencyApnSetting - E-APN setting is already present");
435576f43316a5a6082d601bffd4b6898d0bd81e11fcram                }
435676f43316a5a6082d601bffd4b6898d0bd81e11fcram            }
435776f43316a5a6082d601bffd4b6898d0bd81e11fcram        }
435876f43316a5a6082d601bffd4b6898d0bd81e11fcram    }
43599a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
43609a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang    private void cleanUpConnectionsOnUpdatedApns(boolean tearDown) {
43619a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        if (DBG) log("cleanUpConnectionsOnUpdatedApns: tearDown=" + tearDown);
43629a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        if (mAllApnSettings.isEmpty()) {
43639a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            cleanUpAllConnections(tearDown, Phone.REASON_APN_CHANGED);
43649a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        } else {
43659a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            for (ApnContext apnContext : mApnContexts.values()) {
43669a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                if (VDBG) log("cleanUpConnectionsOnUpdatedApns for "+ apnContext);
43679a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
43689a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                boolean cleanUpApn = true;
43699a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                ArrayList<ApnSetting> currentWaitingApns = apnContext.getWaitingApns();
43709a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
43719a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                if ((currentWaitingApns != null) && (!apnContext.isDisconnected())) {
43729a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
43739a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    ArrayList<ApnSetting> waitingApns = buildWaitingApns(
43749a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                            apnContext.getApnType(), radioTech);
43759a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    if (VDBG) log("new waitingApns:" + waitingApns);
43769a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    if (waitingApns.size() == currentWaitingApns.size()) {
43779a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                        cleanUpApn = false;
43789a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                        for (int i = 0; i < waitingApns.size(); i++) {
43799a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                            if (!currentWaitingApns.get(i).equals(waitingApns.get(i))) {
43809a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                                if (VDBG) log("new waiting apn is different at " + i);
43819a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                                cleanUpApn = true;
43829a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                                apnContext.setWaitingApns(waitingApns);
43839a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                                break;
43849a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                            }
43859a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                        }
43869a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    }
43879a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                }
43889a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
43899a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                if (cleanUpApn) {
43909a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    apnContext.setReason(Phone.REASON_APN_CHANGED);
43919a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                    cleanUpConnection(true, apnContext);
43929a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang                }
43939a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            }
43949a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        }
43959a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
43969a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        if (!isConnected()) {
43979a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            stopNetStatPoll();
43989a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            stopDataStallAlarm();
43999a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        }
44009a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
44019a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        mRequestedApnType = PhoneConstants.APN_TYPE_DEFAULT;
44029a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang
44039a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        if (DBG) log("mDisconnectPendingCount = " + mDisconnectPendingCount);
44049a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        if (tearDown && mDisconnectPendingCount == 0) {
44059a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            notifyDataDisconnectComplete();
44069a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang            notifyAllDataDisconnected();
44079a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang        }
44089a90f1dde8fa4bd7b1606742909c2f2f28a28a2fHui Wang    }
44091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
44111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Polling stuff
44121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
44131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void resetPollStats() {
44141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mTxPkts = -1;
44151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mRxPkts = -1;
44161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mNetStatPollPeriod = POLL_NETSTAT_MILLIS;
44171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void startNetStatPoll() {
44201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (getOverallState() == DctConstants.State.CONNECTED
44211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                && mNetStatPollEnabled == false) {
44221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
44231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("startNetStatPoll");
44241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
44251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            resetPollStats();
44261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mNetStatPollEnabled = true;
44271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mPollNetStat.run();
44281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
44291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mPhone != null) {
44301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mPhone.notifyDataActivity();
44311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
44321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void stopNetStatPoll() {
44351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mNetStatPollEnabled = false;
44361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        removeCallbacks(mPollNetStat);
44371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) {
44381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("stopNetStatPoll");
44391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
44401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // To sync data activity icon in the case of switching data connection to send MMS.
44421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mPhone != null) {
44431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mPhone.notifyDataActivity();
44441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
44451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public void sendStartNetStatPoll(DctConstants.Activity activity) {
44481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.CMD_NET_STAT_POLL);
44491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = DctConstants.ENABLED;
44501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.obj = activity;
44511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
44521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void handleStartNetStatPoll(DctConstants.Activity activity) {
44551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        startNetStatPoll();
44561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
44571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        setActivity(activity);
44581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    public void sendStopNetStatPoll(DctConstants.Activity activity) {
44611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.CMD_NET_STAT_POLL);
44621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = DctConstants.DISABLED;
44631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.obj = activity;
44641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
44651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void handleStopNetStatPoll(DctConstants.Activity activity) {
44681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        stopNetStatPoll();
44691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        stopDataStallAlarm();
44701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        setActivity(activity);
44711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
44721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void updateDataActivity() {
44741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        long sent, received;
44751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        DctConstants.Activity newActivity;
44771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        TxRxSum preTxRxSum = new TxRxSum(mTxPkts, mRxPkts);
44791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        TxRxSum curTxRxSum = new TxRxSum();
44801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        curTxRxSum.updateTxRxSum();
44811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mTxPkts = curTxRxSum.txPkts;
44821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mRxPkts = curTxRxSum.rxPkts;
44831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG) {
44851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("updateDataActivity: curTxRxSum=" + curTxRxSum + " preTxRxSum=" + preTxRxSum);
44861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
44871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mNetStatPollEnabled && (preTxRxSum.txPkts > 0 || preTxRxSum.rxPkts > 0)) {
44891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            sent = mTxPkts - preTxRxSum.txPkts;
44901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            received = mRxPkts - preTxRxSum.rxPkts;
44911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
44921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG)
44931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("updateDataActivity: sent=" + sent + " received=" + received);
44941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (sent > 0 && received > 0) {
44951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                newActivity = DctConstants.Activity.DATAINANDOUT;
44961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (sent > 0 && received == 0) {
44971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                newActivity = DctConstants.Activity.DATAOUT;
44981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else if (sent == 0 && received > 0) {
44991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                newActivity = DctConstants.Activity.DATAIN;
45001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
45011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                newActivity = (mActivity == DctConstants.Activity.DORMANT) ?
45021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mActivity : DctConstants.Activity.NONE;
45031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
45041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
45051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (mActivity != newActivity && mIsScreenOn) {
45061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (VDBG)
45071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    log("updateDataActivity: newActivity=" + newActivity);
45081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mActivity = newActivity;
45091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mPhone.notifyDataActivity();
45101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
45111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
45121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
45131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
45142e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt    private void handlePcoData(AsyncResult ar) {
45152e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        if (ar.exception != null) {
45162e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt            Rlog.e(LOG_TAG, "PCO_DATA exception: " + ar.exception);
45172e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt            return;
45182e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        }
45192e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        PcoData pcoData = (PcoData)(ar.result);
4520cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        ArrayList<DataConnection> dcList = new ArrayList<>();
4521cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        DataConnection temp = mDcc.getActiveDcByCid(pcoData.cid);
4522cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        if (temp != null) {
4523cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            dcList.add(temp);
4524cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        }
4525cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        if (dcList.size() == 0) {
4526cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            Rlog.e(LOG_TAG, "PCO_DATA for unknown cid: " + pcoData.cid + ", inferring");
4527cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            for (DataConnection dc : mDataConnections.values()) {
4528cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                final int cid = dc.getCid();
4529cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                if (cid == pcoData.cid) {
4530cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    if (VDBG) Rlog.d(LOG_TAG, "  found " + dc);
4531cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    dcList.clear();
4532cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    dcList.add(dc);
4533cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    break;
4534cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                }
4535cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                // check if this dc is still connecting
4536cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                if (cid == -1) {
4537cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    for (ApnContext apnContext : dc.mApnContexts.keySet()) {
4538cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        if (apnContext.getState() == DctConstants.State.CONNECTING) {
4539cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                            if (VDBG) Rlog.d(LOG_TAG, "  found potential " + dc);
4540cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                            dcList.add(dc);
4541cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                            break;
4542cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                        }
4543cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                    }
4544cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                }
4545cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            }
4546cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        }
4547cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        if (dcList.size() == 0) {
4548cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            Rlog.e(LOG_TAG, "PCO_DATA - couldn't infer cid");
45492e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt            return;
45502e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        }
4551cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt        for (DataConnection dc : dcList) {
4552cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            if (dc.mApnContexts.size() == 0) {
4553cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                break;
4554cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            }
4555cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            // send one out for each apn type in play
4556cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            for (ApnContext apnContext : dc.mApnContexts.keySet()) {
4557cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                String apnType = apnContext.getApnType();
4558cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt
4559cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                final Intent intent = new Intent(TelephonyIntents.ACTION_CARRIER_SIGNAL_PCO_VALUE);
4560cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                intent.putExtra(TelephonyIntents.EXTRA_APN_TYPE_KEY, apnType);
4561cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                intent.putExtra(TelephonyIntents.EXTRA_APN_PROTO_KEY, pcoData.bearerProto);
4562cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                intent.putExtra(TelephonyIntents.EXTRA_PCO_ID_KEY, pcoData.pcoId);
4563cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                intent.putExtra(TelephonyIntents.EXTRA_PCO_VALUE_KEY, pcoData.contents);
4564cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt                mPhone.getCarrierSignalAgent().notifyCarrierSignalReceivers(intent);
4565cda5b321ceddda71d47ec2fe619d6624dd2ea3daRobert Greenwalt            }
45662e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt        }
45672e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt    }
45682e2ec7d6844b8441df95b9ff38129e1e4a2d18f3Robert Greenwalt
45691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
45701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Data-Stall
45711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
45721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    // Recovery action taken in case of data stall
45731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private static class RecoveryAction {
45741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public static final int GET_DATA_CALL_LIST      = 0;
45751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public static final int CLEANUP                 = 1;
45761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public static final int REREGISTER              = 2;
45771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public static final int RADIO_RESTART           = 3;
45781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        public static final int RADIO_RESTART_WITH_PROP = 4;
45791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
45801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        private static boolean isAggressiveRecovery(int value) {
45811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            return ((value == RecoveryAction.CLEANUP) ||
45821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    (value == RecoveryAction.REREGISTER) ||
45831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    (value == RecoveryAction.RADIO_RESTART) ||
45841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    (value == RecoveryAction.RADIO_RESTART_WITH_PROP));
45851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
45861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
45871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
45881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private int getRecoveryAction() {
45891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int action = Settings.System.getInt(mResolver,
45901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                "radio.data.stall.recovery.action", RecoveryAction.GET_DATA_CALL_LIST);
45911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG_STALL) log("getRecoveryAction: " + action);
45921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        return action;
45931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
45941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
45951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void putRecoveryAction(int action) {
45961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Settings.System.putInt(mResolver, "radio.data.stall.recovery.action", action);
45971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG_STALL) log("putRecoveryAction: " + action);
45981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
45991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
46001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void doRecovery() {
46011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (getOverallState() == DctConstants.State.CONNECTED) {
46021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // Go through a series of recovery steps, each action transitions to the next action
46031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            int recoveryAction = getRecoveryAction();
46041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            switch (recoveryAction) {
46051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case RecoveryAction.GET_DATA_CALL_LIST:
46061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                EventLog.writeEvent(EventLogTags.DATA_STALL_RECOVERY_GET_DATA_CALL_LIST,
46071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mSentSinceLastRecv);
46081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("doRecovery() get data call list");
46091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mPhone.mCi.getDataCallList(obtainMessage(DctConstants.EVENT_DATA_STATE_CHANGED));
46101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                putRecoveryAction(RecoveryAction.CLEANUP);
46111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
46121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case RecoveryAction.CLEANUP:
46131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                EventLog.writeEvent(EventLogTags.DATA_STALL_RECOVERY_CLEANUP, mSentSinceLastRecv);
46141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("doRecovery() cleanup all connections");
46151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                cleanUpAllConnections(Phone.REASON_PDP_RESET);
46161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                putRecoveryAction(RecoveryAction.REREGISTER);
46171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
46181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case RecoveryAction.REREGISTER:
46191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                EventLog.writeEvent(EventLogTags.DATA_STALL_RECOVERY_REREGISTER,
46201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mSentSinceLastRecv);
46211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("doRecovery() re-register");
46221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mPhone.getServiceStateTracker().reRegisterNetwork(null);
46231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                putRecoveryAction(RecoveryAction.RADIO_RESTART);
46241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
46251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case RecoveryAction.RADIO_RESTART:
46261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                EventLog.writeEvent(EventLogTags.DATA_STALL_RECOVERY_RADIO_RESTART,
46271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        mSentSinceLastRecv);
46281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("restarting radio");
46291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                putRecoveryAction(RecoveryAction.RADIO_RESTART_WITH_PROP);
46301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                restartRadio();
46311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
46321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            case RecoveryAction.RADIO_RESTART_WITH_PROP:
46331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // This is in case radio restart has not recovered the data.
46341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // It will set an additional "gsm.radioreset" property to tell
46351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // RIL or system to take further action.
46361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // The implementation of hard reset recovery action is up to OEM product.
46371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // Once RADIO_RESET property is consumed, it is expected to set back
46381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // to false by RIL.
46391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                EventLog.writeEvent(EventLogTags.DATA_STALL_RECOVERY_RADIO_RESTART_WITH_PROP, -1);
46401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                if (DBG) log("restarting radio with gsm.radioreset to true");
46411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                SystemProperties.set(RADIO_RESET_PROPERTY, "true");
46421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                // give 1 sec so property change can be notified.
46431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                try {
46441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    Thread.sleep(1000);
46451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                } catch (InterruptedException e) {}
46461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                restartRadio();
46471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                putRecoveryAction(RecoveryAction.GET_DATA_CALL_LIST);
46481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                break;
46491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            default:
46501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                throw new RuntimeException("doRecovery: Invalid recoveryAction=" +
46511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    recoveryAction);
46521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
46531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mSentSinceLastRecv = 0;
46541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
46551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
46561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
46571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void updateDataStallInfo() {
46581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        long sent, received;
46591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
46601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        TxRxSum preTxRxSum = new TxRxSum(mDataStallTxRxSum);
46611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDataStallTxRxSum.updateTxRxSum();
46621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
46631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG_STALL) {
46641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("updateDataStallInfo: mDataStallTxRxSum=" + mDataStallTxRxSum +
46651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " preTxRxSum=" + preTxRxSum);
46661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
46671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
46681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sent = mDataStallTxRxSum.txPkts - preTxRxSum.txPkts;
46691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        received = mDataStallTxRxSum.rxPkts - preTxRxSum.rxPkts;
46701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
46711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (RADIO_TESTS) {
46721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (SystemProperties.getBoolean("radio.test.data.stall", false)) {
46731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("updateDataStallInfo: radio.test.data.stall true received = 0;");
46741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                received = 0;
46751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
46761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
46771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if ( sent > 0 && received > 0 ) {
46781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG_STALL) log("updateDataStallInfo: IN/OUT");
46791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mSentSinceLastRecv = 0;
46801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            putRecoveryAction(RecoveryAction.GET_DATA_CALL_LIST);
46811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else if (sent > 0 && received == 0) {
46821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (mPhone.getState() == PhoneConstants.State.IDLE) {
46831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mSentSinceLastRecv += sent;
46841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
46851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                mSentSinceLastRecv = 0;
46861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
46871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
46881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("updateDataStallInfo: OUT sent=" + sent +
46891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        " mSentSinceLastRecv=" + mSentSinceLastRecv);
46901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
46911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else if (sent == 0 && received > 0) {
46921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG_STALL) log("updateDataStallInfo: IN");
46931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mSentSinceLastRecv = 0;
46941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            putRecoveryAction(RecoveryAction.GET_DATA_CALL_LIST);
46951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
46961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG_STALL) log("updateDataStallInfo: NONE");
46971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
46981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
46991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onDataStallAlarm(int tag) {
47011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mDataStallAlarmTag != tag) {
47021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
47031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("onDataStallAlarm: ignore, tag=" + tag + " expecting " + mDataStallAlarmTag);
47041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
47051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            return;
47061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
47071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        updateDataStallInfo();
47081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int hangWatchdogTrigger = Settings.Global.getInt(mResolver,
47101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                Settings.Global.PDP_WATCHDOG_TRIGGER_PACKET_COUNT,
47111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                NUMBER_SENT_PACKETS_OF_HANG);
47121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        boolean suspectedStall = DATA_STALL_NOT_SUSPECTED;
47141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mSentSinceLastRecv >= hangWatchdogTrigger) {
47151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) {
47161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("onDataStallAlarm: tag=" + tag + " do recovery action=" + getRecoveryAction());
47171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
47181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            suspectedStall = DATA_STALL_SUSPECTED;
47191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            sendMessage(obtainMessage(DctConstants.EVENT_DO_RECOVERY));
47201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
47211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG_STALL) {
47221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("onDataStallAlarm: tag=" + tag + " Sent " + String.valueOf(mSentSinceLastRecv) +
47231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " pkts since last received, < watchdogTrigger=" + hangWatchdogTrigger);
47241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
47251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
47261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        startDataStallAlarm(suspectedStall);
47271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
47281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void startDataStallAlarm(boolean suspectedStall) {
47301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int nextAction = getRecoveryAction();
47311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int delayInMs;
47321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mDataStallDetectionEnabled && getOverallState() == DctConstants.State.CONNECTED) {
47341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // If screen is on or data stall is currently suspected, set the alarm
47350e664d8c9ff3a1f2ab03d2bef4268615335ea1a2Jack Yu            // with an aggressive timeout.
47361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (mIsScreenOn || suspectedStall || RecoveryAction.isAggressiveRecovery(nextAction)) {
47371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                delayInMs = Settings.Global.getInt(mResolver,
47381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        Settings.Global.DATA_STALL_ALARM_AGGRESSIVE_DELAY_IN_MS,
47391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        DATA_STALL_ALARM_AGGRESSIVE_DELAY_IN_MS_DEFAULT);
47401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } else {
47411a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                delayInMs = Settings.Global.getInt(mResolver,
47421a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        Settings.Global.DATA_STALL_ALARM_NON_AGGRESSIVE_DELAY_IN_MS,
47431a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        DATA_STALL_ALARM_NON_AGGRESSIVE_DELAY_IN_MS_DEFAULT);
47441a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
47451a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47461a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mDataStallAlarmTag += 1;
47471a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG_STALL) {
47481a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("startDataStallAlarm: tag=" + mDataStallAlarmTag +
47491a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                        " delay=" + (delayInMs / 1000) + "s");
47501a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
47511a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            Intent intent = new Intent(INTENT_DATA_STALL_ALARM);
47521a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            intent.putExtra(DATA_STALL_ALARM_TAG_EXTRA, mDataStallAlarmTag);
47531a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mDataStallAlarmIntent = PendingIntent.getBroadcast(mPhone.getContext(), 0, intent,
47541a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    PendingIntent.FLAG_UPDATE_CURRENT);
47551a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
47561a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    SystemClock.elapsedRealtime() + delayInMs, mDataStallAlarmIntent);
47571a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        } else {
47581a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (VDBG_STALL) {
47591a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                log("startDataStallAlarm: NOT started, no connection tag=" + mDataStallAlarmTag);
47601a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
47611a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
47621a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
47631a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47641a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void stopDataStallAlarm() {
47651a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG_STALL) {
47661a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("stopDataStallAlarm: current tag=" + mDataStallAlarmTag +
47671a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " mDataStallAlarmIntent=" + mDataStallAlarmIntent);
47681a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
47691a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mDataStallAlarmTag += 1;
47701a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mDataStallAlarmIntent != null) {
47711a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mAlarmManager.cancel(mDataStallAlarmIntent);
47721a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mDataStallAlarmIntent = null;
47731a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
47741a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
47751a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47761a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void restartDataStallAlarm() {
47771a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (isConnected() == false) return;
47781a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // To be called on screen status change.
47791a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        // Do not cancel the alarm if it is set with aggressive timeout.
47801a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int nextAction = getRecoveryAction();
47811a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47821a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (RecoveryAction.isAggressiveRecovery(nextAction)) {
47831a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            if (DBG) log("restartDataStallAlarm: action is pending. not resetting the alarm.");
47841a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            return;
47851a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
47861a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (VDBG_STALL) log("restartDataStallAlarm: stop then start.");
47871a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        stopDataStallAlarm();
47881a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
47891a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
47901a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
47911a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    /**
47921a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     * Provisioning APN
47931a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu     */
47941a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void onActionIntentProvisioningApnAlarm(Intent intent) {
47951a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) log("onActionIntentProvisioningApnAlarm: action=" + intent.getAction());
47961a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Message msg = obtainMessage(DctConstants.EVENT_PROVISIONING_APN_ALARM,
47971a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                intent.getAction());
47981a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        msg.arg1 = intent.getIntExtra(PROVISIONING_APN_ALARM_TAG_EXTRA, 0);
47991a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        sendMessage(msg);
48001a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
48011a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
48021a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void startProvisioningApnAlarm() {
48031a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        int delayInMs = Settings.Global.getInt(mResolver,
48041a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                                Settings.Global.PROVISIONING_APN_ALARM_DELAY_IN_MS,
48051a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                                PROVISIONING_APN_ALARM_DELAY_IN_MS_DEFAULT);
48061a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (Build.IS_DEBUGGABLE) {
48071a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            // Allow debug code to use a system property to provide another value
48081a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            String delayInMsStrg = Integer.toString(delayInMs);
48091a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            delayInMsStrg = System.getProperty(DEBUG_PROV_APN_ALARM, delayInMsStrg);
48101a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            try {
48111a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                delayInMs = Integer.parseInt(delayInMsStrg);
48121a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            } catch (NumberFormatException e) {
48131a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                loge("startProvisioningApnAlarm: e=" + e);
48141a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            }
48151a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
48161a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mProvisioningApnAlarmTag += 1;
48171a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) {
48181a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("startProvisioningApnAlarm: tag=" + mProvisioningApnAlarmTag +
48191a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " delay=" + (delayInMs / 1000) + "s");
48201a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
48211a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        Intent intent = new Intent(INTENT_PROVISIONING_APN_ALARM);
48221a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        intent.putExtra(PROVISIONING_APN_ALARM_TAG_EXTRA, mProvisioningApnAlarmTag);
48231a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mProvisioningApnAlarmIntent = PendingIntent.getBroadcast(mPhone.getContext(), 0, intent,
48241a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                PendingIntent.FLAG_UPDATE_CURRENT);
48251a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
48261a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                SystemClock.elapsedRealtime() + delayInMs, mProvisioningApnAlarmIntent);
48271a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
48281a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
48291a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    private void stopProvisioningApnAlarm() {
48301a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (DBG) {
48311a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            log("stopProvisioningApnAlarm: current tag=" + mProvisioningApnAlarmTag +
48321a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu                    " mProvsioningApnAlarmIntent=" + mProvisioningApnAlarmIntent);
48331a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
48341a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        mProvisioningApnAlarmTag += 1;
48351a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        if (mProvisioningApnAlarmIntent != null) {
48361a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mAlarmManager.cancel(mProvisioningApnAlarmIntent);
48371a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu            mProvisioningApnAlarmIntent = null;
48381a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu        }
48391a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu    }
48401a87ab3d7170d618f048c4f5af8c7504a587aaa5Jack Yu
4841c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville}
4842