[go: nahoru, domu]

1a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville/*
21e7aeace971d8b6d5e5b8cf2038cfce820bb7772Legler Wu* Copyright (C) 2014 The Android Open Source Project
3a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville*
4a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* Licensed under the Apache License, Version 2.0 (the "License");
5a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* you may not use this file except in compliance with the License.
6a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* You may obtain a copy of the License at
7a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville*
8a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville*      http://www.apache.org/licenses/LICENSE-2.0
9a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville*
10a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* Unless required by applicable law or agreed to in writing, software
11a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* distributed under the License is distributed on an "AS IS" BASIS,
12a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* See the License for the specific language governing permissions and
14a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville* limitations under the License.
15a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville*/
16a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
17a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savillepackage com.android.internal.telephony;
18a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
19058304574f5acd8b66e2e943e7da9824323a2050xinheimport android.app.ActivityManagerNative;
200cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidsonimport android.app.IUserSwitchObserver;
21a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.content.BroadcastReceiver;
22a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.content.ContentResolver;
23a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.content.ContentValues;
241573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yuimport android.content.Context;
25a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.content.Intent;
26a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.content.IntentFilter;
27a77b7504cf2c437548b89e73bdc0898feaa30be0Amit Mahajanimport android.content.SharedPreferences;
280cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidsonimport android.content.pm.IPackageManager;
29a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.os.AsyncResult;
30a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.os.Handler;
310cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidsonimport android.os.IRemoteCallback;
32a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.os.Message;
330cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidsonimport android.os.RemoteException;
340cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidsonimport android.os.ServiceManager;
35058304574f5acd8b66e2e943e7da9824323a2050xinheimport android.os.UserHandle;
362416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajanimport android.os.UserManager;
37a77b7504cf2c437548b89e73bdc0898feaa30be0Amit Mahajanimport android.preference.PreferenceManager;
38a77b7504cf2c437548b89e73bdc0898feaa30be0Amit Mahajanimport android.provider.Settings;
39a1c457765342cee69203188b32419c395f54829cJunda Liuimport android.telephony.CarrierConfigManager;
401573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yuimport android.telephony.Rlog;
41299dad50409e907993fa96c7401d4f10c58018b1Wink Savilleimport android.telephony.SubscriptionInfo;
421573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yuimport android.telephony.SubscriptionManager;
43a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport android.telephony.TelephonyManager;
441573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yuimport android.text.TextUtils;
451573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu
46cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawalimport com.android.internal.telephony.uicc.IccCardProxy;
47a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport com.android.internal.telephony.uicc.IccConstants;
48a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport com.android.internal.telephony.uicc.IccFileHandler;
49cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawalimport com.android.internal.telephony.uicc.IccRecords;
50a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport com.android.internal.telephony.uicc.IccUtils;
510cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
524a3d736dbf0b8262290d3720085779538ceaad72Zach Johnsonimport java.io.FileDescriptor;
534a3d736dbf0b8262290d3720085779538ceaad72Zach Johnsonimport java.io.PrintWriter;
542416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajanimport java.util.HashMap;
552416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajanimport java.util.Iterator;
56a8467dd0c524787104b1ccdddc5e8af10ba729edWink Savilleimport java.util.List;
572416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajanimport java.util.Map;
58a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
591573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yuimport static android.Manifest.permission.READ_PHONE_STATE;
602416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajanimport static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE;
611573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu
62a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville/**
63a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville *@hide
64a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville */
65299dad50409e907993fa96c7401d4f10c58018b1Wink Savillepublic class SubscriptionInfoUpdater extends Handler {
664b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private static final String LOG_TAG = "SubscriptionInfoUpdater";
67a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private static final int PROJECT_SIM_NUM = TelephonyManager.getDefault().getPhoneCount();
686095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
69cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    private static final int EVENT_SIM_LOCKED_QUERY_ICCID_DONE = 1;
703b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal    private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 2;
716095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal    private static final int EVENT_SIM_LOADED = 3;
726095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal    private static final int EVENT_SIM_ABSENT = 4;
73cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    private static final int EVENT_SIM_LOCKED = 5;
74a1c457765342cee69203188b32419c395f54829cJunda Liu    private static final int EVENT_SIM_IO_ERROR = 6;
75a1c457765342cee69203188b32419c395f54829cJunda Liu    private static final int EVENT_SIM_UNKNOWN = 7;
766095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
77a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private static final String ICCID_STRING_FOR_NO_SIM = "";
78a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    /**
79a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *  int[] sInsertSimState maintains all slots' SIM inserted status currently,
80a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *  it may contain 4 kinds of values:
81a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *    SIM_NOT_INSERT : no SIM inserted in slot i now
82a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *    SIM_CHANGED    : a valid SIM insert in slot i and is different SIM from last time
83a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *                     it will later become SIM_NEW or SIM_REPOSITION during update procedure
84a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *    SIM_NOT_CHANGE : a valid SIM insert in slot i and is the same SIM as last time
85a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *    SIM_NEW        : a valid SIM insert in slot i and is a new SIM
86a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *    SIM_REPOSITION : a valid SIM insert in slot i and is inserted in different slot last time
87a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     *    positive integer #: index to distinguish SIM cards with the same IccId
88a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville     */
89a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int SIM_NOT_CHANGE = 0;
90a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int SIM_CHANGED    = -1;
91a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int SIM_NEW        = -2;
92a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int SIM_REPOSITION = -3;
93a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int SIM_NOT_INSERT = -99;
94a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
95a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int STATUS_NO_SIM_INSERTED = 0x00;
96a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int STATUS_SIM1_INSERTED = 0x01;
97a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int STATUS_SIM2_INSERTED = 0x02;
98a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int STATUS_SIM3_INSERTED = 0x04;
99a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public static final int STATUS_SIM4_INSERTED = 0x08;
100a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
101a77b7504cf2c437548b89e73bdc0898feaa30be0Amit Mahajan    // Key used to read/write the current IMSI. Updated on SIM_STATE_CHANGED - LOADED.
10243677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan    public static final String CURR_SUBID = "curr_subid";
103a77b7504cf2c437548b89e73bdc0898feaa30be0Amit Mahajan
1044b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private static Phone[] mPhone;
1054b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private static Context mContext = null;
1064b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private static String mIccId[] = new String[PROJECT_SIM_NUM];
1074b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private static int[] mInsertSimState = new int[PROJECT_SIM_NUM];
1084b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private SubscriptionManager mSubscriptionManager = null;
1090cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson    private IPackageManager mPackageManager;
1102416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan    private UserManager mUserManager;
1112416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan    private Map<Integer, Intent> rebroadcastIntentsOnUnlock = new HashMap<>();
1122416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan
1130cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson    // The current foreground user ID.
1140cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson    private int mCurrentlyActiveUserId;
1154a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson    private CarrierServiceBindHelper mCarrierServiceBindHelper;
1164b09dff383ae7dfca595aeeea886a594a1947340Wink Saville
11758dd6858dc8013b680ea003d22063fd65ed5fe1cAmit Mahajan    public SubscriptionInfoUpdater(Context context, Phone[] phone, CommandsInterface[] ci) {
118a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        logd("Constructor invoked");
119a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
1204b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        mContext = context;
12158dd6858dc8013b680ea003d22063fd65ed5fe1cAmit Mahajan        mPhone = phone;
1224b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        mSubscriptionManager = SubscriptionManager.from(mContext);
1230cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        mPackageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
1242416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1254b09dff383ae7dfca595aeeea886a594a1947340Wink Saville
126a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        IntentFilter intentFilter = new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
127a1c457765342cee69203188b32419c395f54829cJunda Liu        intentFilter.addAction(IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED);
1282416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan        intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
129cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        mContext.registerReceiver(sReceiver, intentFilter);
1300cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
1314a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson        mCarrierServiceBindHelper = new CarrierServiceBindHelper(mContext);
1320cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        initializeCarrierApps();
1330cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson    }
1340cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
1350cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson    private void initializeCarrierApps() {
1360cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        // Initialize carrier apps:
1370cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        // -Now (on system startup)
1380cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        // -Whenever new carrier privilege rules might change (new SIM is loaded)
1390cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        // -Whenever we switch to a new user
1400cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        mCurrentlyActiveUserId = 0;
1410cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        try {
1420cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson            ActivityManagerNative.getDefault().registerUserSwitchObserver(
1430cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    new IUserSwitchObserver.Stub() {
1440cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                @Override
1450cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                public void onUserSwitching(int newUserId, IRemoteCallback reply)
1460cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                        throws RemoteException {
1470cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    mCurrentlyActiveUserId = newUserId;
1480cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(),
1490cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                            mPackageManager, TelephonyManager.getDefault(), mCurrentlyActiveUserId);
1500cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
1510cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    if (reply != null) {
1520cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                        try {
1530cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                            reply.sendResult(null);
1540cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                        } catch (RemoteException e) {
1550cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                        }
1560cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    }
1570cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                }
1580cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
1590cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                @Override
1600cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                public void onUserSwitchComplete(int newUserId) {
1610cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    // Ignore.
1620cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                }
1630cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
1640cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                @Override
1650cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                public void onForegroundProfileSwitch(int newProfileId) throws RemoteException {
1660cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                    // Ignore.
1670cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                }
1680cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson            });
1690cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson            mCurrentlyActiveUserId = ActivityManagerNative.getDefault().getCurrentUser().id;
1700cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        } catch (RemoteException e) {
1710cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson            logd("Couldn't get current user ID; guessing it's 0: " + e.getMessage());
1720cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        }
1730cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(),
1740cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                mPackageManager, TelephonyManager.getDefault(), mCurrentlyActiveUserId);
175a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
176a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
177a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private final BroadcastReceiver sReceiver = new  BroadcastReceiver() {
178440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville        @Override
179a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        public void onReceive(Context context, Intent intent) {
180a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            logd("[Receiver]+");
181a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            String action = intent.getAction();
182a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            logd("Action: " + action);
1836095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
1842416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan            if (action.equals(Intent.ACTION_USER_UNLOCKED)) {
1852416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                // broadcast pending intents
1862416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                Iterator iterator = rebroadcastIntentsOnUnlock.entrySet().iterator();
1872416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                while (iterator.hasNext()) {
1882416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                    Map.Entry pair = (Map.Entry) iterator.next();
1892416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                    Intent i = (Intent)pair.getValue();
1902416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                    iterator.remove();
1912416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                    logd("Broadcasting intent ACTION_SIM_STATE_CHANGED for mCardIndex: " +
1922416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                            pair.getKey());
1937d9297a8415f4515508c0bf9abbd650e5ce6aa1bAmit Mahajan                    ActivityManagerNative.broadcastStickyIntent(i, READ_PHONE_STATE,
1947d9297a8415f4515508c0bf9abbd650e5ce6aa1bAmit Mahajan                            UserHandle.USER_ALL);
1952416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                }
1962416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                rebroadcastIntentsOnUnlock = null;
1972416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                logd("[Receiver]-");
1982416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                return;
1992416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan            }
2002416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan
201cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            if (!action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED) &&
2022416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan                    !action.equals(IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED)) {
203cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                return;
204cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            }
205cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
206062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal            int slotId = intent.getIntExtra(PhoneConstants.PHONE_KEY,
207cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                    SubscriptionManager.INVALID_SIM_SLOT_INDEX);
208cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            logd("slotId: " + slotId);
209cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            if (slotId == SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
210cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                return;
211cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            }
212cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
213cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            String simStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
214cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            logd("simStatus: " + simStatus);
215cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
216a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
217cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(simStatus)) {
218cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                    sendMessage(obtainMessage(EVENT_SIM_ABSENT, slotId, -1));
219a1c457765342cee69203188b32419c395f54829cJunda Liu                } else if (IccCardConstants.INTENT_VALUE_ICC_UNKNOWN.equals(simStatus)) {
220a1c457765342cee69203188b32419c395f54829cJunda Liu                    sendMessage(obtainMessage(EVENT_SIM_UNKNOWN, slotId, -1));
221a1c457765342cee69203188b32419c395f54829cJunda Liu                } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(simStatus)) {
222a1c457765342cee69203188b32419c395f54829cJunda Liu                    sendMessage(obtainMessage(EVENT_SIM_IO_ERROR, slotId, -1));
223cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                } else {
224cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                    logd("Ignoring simStatus: " + simStatus);
225440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville                }
226cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            } else if (action.equals(IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED)) {
227cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(simStatus)) {
228cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                    String reason = intent.getStringExtra(
229cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                        IccCardConstants.INTENT_KEY_LOCKED_REASON);
230cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                    sendMessage(obtainMessage(EVENT_SIM_LOCKED, slotId, -1, reason));
231a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(simStatus)) {
232062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                    sendMessage(obtainMessage(EVENT_SIM_LOADED, slotId, -1));
233cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                } else {
234cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                    logd("Ignoring simStatus: " + simStatus);
235a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
236a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
237a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            logd("[Receiver]-");
238a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
239a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    };
240a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
241a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    private boolean isAllIccIdQueryDone() {
242a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
2434b09dff383ae7dfca595aeeea886a594a1947340Wink Saville            if (mIccId[i] == null) {
244a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                logd("Wait for SIM" + (i + 1) + " IccId");
245a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                return false;
246a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
247a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
248a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        logd("All IccIds query complete");
249a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
250a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        return true;
251a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
252a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
2534b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    public void setDisplayNameForNewSub(String newSubName, int subId, int newNameSource) {
2544b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
255a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        if (subInfo != null) {
256a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            // overwrite SIM display name if it is not assigned by user
2575f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott            int oldNameSource = subInfo.getNameSource();
2585f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott            CharSequence oldSubName = subInfo.getDisplayName();
2595f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott            logd("[setDisplayNameForNewSub] subId = " + subInfo.getSubscriptionId()
2605f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                    + ", oldSimName = " + oldSubName + ", oldNameSource = " + oldNameSource
2615f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                    + ", newSubName = " + newSubName + ", newNameSource = " + newNameSource);
262440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville            if (oldSubName == null ||
2634b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                (oldNameSource ==
2644b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    SubscriptionManager.NAME_SOURCE_DEFAULT_SOURCE && newSubName != null) ||
265440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville                (oldNameSource == SubscriptionManager.NAME_SOURCE_SIM_SOURCE && newSubName != null
266440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville                        && !newSubName.equals(oldSubName))) {
2674b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                mSubscriptionManager.setDisplayName(newSubName, subInfo.getSubscriptionId(),
2685f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                        newNameSource);
269a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
270a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        } else {
271a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            logd("SUB" + (subId + 1) + " SubInfo not created yet");
272a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
273a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
274a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
275440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville    @Override
276a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void handleMessage(Message msg) {
2776095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        switch (msg.what) {
278cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            case EVENT_SIM_LOCKED_QUERY_ICCID_DONE: {
279cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                AsyncResult ar = (AsyncResult)msg.obj;
280cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                QueryIccIdUserObj uObj = (QueryIccIdUserObj) ar.userObj;
281cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                int slotId = uObj.slotId;
282cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                logd("handleMessage : <EVENT_SIM_LOCKED_QUERY_ICCID_DONE> SIM" + (slotId + 1));
283a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                if (ar.exception == null) {
284a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    if (ar.result != null) {
285a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        byte[] data = (byte[])ar.result;
286525130aaed5ad8bfa22f64353d298b927b7ed3d0Amit Mahajan                        mIccId[slotId] = IccUtils.bcdToString(data, 0, data.length);
287a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    } else {
288a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        logd("Null ar");
2894b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                        mIccId[slotId] = ICCID_STRING_FOR_NO_SIM;
290a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    }
291a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                } else {
2924b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mIccId[slotId] = ICCID_STRING_FOR_NO_SIM;
293a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    logd("Query IccId fail: " + ar.exception);
294a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
2954b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                logd("sIccId[" + slotId + "] = " + mIccId[slotId]);
296cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                if (isAllIccIdQueryDone()) {
297299dad50409e907993fa96c7401d4f10c58018b1Wink Saville                    updateSubscriptionInfoByIccId();
298a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
299cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                broadcastSimStateChanged(slotId, IccCardConstants.INTENT_VALUE_ICC_LOCKED,
300cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                                         uObj.reason);
301a1c457765342cee69203188b32419c395f54829cJunda Liu                if (!ICCID_STRING_FOR_NO_SIM.equals(mIccId[slotId])) {
3024a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson                    updateCarrierServices(slotId, IccCardConstants.INTENT_VALUE_ICC_LOCKED);
303a1c457765342cee69203188b32419c395f54829cJunda Liu                }
304a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                break;
3056095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            }
3066095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
3076095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            case EVENT_GET_NETWORK_SELECTION_MODE_DONE: {
308cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                AsyncResult ar = (AsyncResult)msg.obj;
3096095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                Integer slotId = (Integer)ar.userObj;
3103b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                if (ar.exception == null && ar.result != null) {
3113b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                    int[] modes = (int[])ar.result;
3123b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                    if (modes[0] == 1) {  // Manual mode.
3134b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                        mPhone[slotId].setNetworkSelectionModeAutomatic(null);
3143b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                    }
3153b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                } else {
3163b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                    logd("EVENT_GET_NETWORK_SELECTION_MODE_DONE: error getting network mode.");
3173b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                }
3183b9b9d704121b1c7a9cd98626d7ca87d9cff5437Shishir Agrawal                break;
3196095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            }
3206095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
3216095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal           case EVENT_SIM_LOADED:
322062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                handleSimLoaded(msg.arg1);
3236095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                break;
3246095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
3256095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            case EVENT_SIM_ABSENT:
3266095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                handleSimAbsent(msg.arg1);
3276095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                break;
3286095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
329cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            case EVENT_SIM_LOCKED:
330cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                handleSimLocked(msg.arg1, (String) msg.obj);
3316095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                break;
3326095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
333a1c457765342cee69203188b32419c395f54829cJunda Liu            case EVENT_SIM_UNKNOWN:
3344a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson                updateCarrierServices(msg.arg1, IccCardConstants.INTENT_VALUE_ICC_UNKNOWN);
335a1c457765342cee69203188b32419c395f54829cJunda Liu                break;
336a1c457765342cee69203188b32419c395f54829cJunda Liu
337a1c457765342cee69203188b32419c395f54829cJunda Liu            case EVENT_SIM_IO_ERROR:
3384a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson                updateCarrierServices(msg.arg1, IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR);
339a1c457765342cee69203188b32419c395f54829cJunda Liu                break;
340a1c457765342cee69203188b32419c395f54829cJunda Liu
341a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            default:
342a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                logd("Unknown msg:" + msg.what);
343a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
344a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
345a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
346cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    private static class QueryIccIdUserObj {
347cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        public String reason;
348cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        public int slotId;
349cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
350cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        QueryIccIdUserObj(String reason, int slotId) {
351cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            this.reason = reason;
352cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            this.slotId = slotId;
353cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        }
354cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    };
355cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
356cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    private void handleSimLocked(int slotId, String reason) {
3576095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        if (mIccId[slotId] != null && mIccId[slotId].equals(ICCID_STRING_FOR_NO_SIM)) {
3586095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            logd("SIM" + (slotId + 1) + " hot plug in");
3596095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            mIccId[slotId] = null;
3606095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        }
3616095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
362cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
363cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        IccFileHandler fileHandler = mPhone[slotId].getIccCard() == null ? null :
364cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                mPhone[slotId].getIccCard().getIccFileHandler();
365cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
366cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        if (fileHandler != null) {
367cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            String iccId = mIccId[slotId];
368cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            if (iccId == null) {
369cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                logd("Querying IccId");
370cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                fileHandler.loadEFTransparent(IccConstants.EF_ICCID,
371cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                        obtainMessage(EVENT_SIM_LOCKED_QUERY_ICCID_DONE,
372cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                                new QueryIccIdUserObj(reason, slotId)));
373cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            } else {
374cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal                logd("NOT Querying IccId its already set sIccid[" + slotId + "]=" + iccId);
3754a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson                updateCarrierServices(slotId, IccCardConstants.INTENT_VALUE_ICC_LOCKED);
376bada592ba581460d2cf5a0ae12a0f73bc471a0d6duho.ro                broadcastSimStateChanged(slotId, IccCardConstants.INTENT_VALUE_ICC_LOCKED, reason);
377cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            }
378cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        } else {
379cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            logd("sFh[" + slotId + "] is null, ignore");
380cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        }
3816095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal    }
3826095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
383062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal    private void handleSimLoaded(int slotId) {
384062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal        logd("handleSimStateLoadedInternal: slotId: " + slotId);
385cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
386cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        // The SIM should be loaded at this state, but it is possible in cases such as SIM being
387cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        // removed or a refresh RESET that the IccRecords could be null. The right behavior is to
388cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        // not broadcast the SIM loaded.
389cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        IccRecords records = mPhone[slotId].getIccCard().getIccRecords();
390cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        if (records == null) {  // Possibly a race condition.
391cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            logd("onRecieve: IccRecords null");
392cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            return;
393cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        }
394525130aaed5ad8bfa22f64353d298b927b7ed3d0Amit Mahajan        if (records.getIccId() == null) {
395cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            logd("onRecieve: IccID null");
396cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            return;
397cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        }
398525130aaed5ad8bfa22f64353d298b927b7ed3d0Amit Mahajan        mIccId[slotId] = records.getIccId();
399cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
400cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        if (isAllIccIdQueryDone()) {
401cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal            updateSubscriptionInfoByIccId();
4026095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        }
4036095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
404062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal        int subId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
405062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal        int[] subIds = SubscriptionController.getInstance().getSubId(slotId);
406062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal        if (subIds != null) {   // Why an array?
407062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal            subId = subIds[0];
408062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal        }
409062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal
4106095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        if (SubscriptionManager.isValidSubscriptionId(subId)) {
4111573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu            TelephonyManager tm = TelephonyManager.getDefault();
4121573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu
4131573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu            String operator = tm.getSimOperatorNumericForPhone(slotId);
4141573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu
4157f14ab6328559e4456d969b735ab8f383a05d0e5Amit Mahajan            if (!TextUtils.isEmpty(operator)) {
416062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                if (subId == SubscriptionController.getInstance().getDefaultSubId()) {
417062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                    MccTable.updateMccMncConfiguration(mContext, operator, false);
418062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                }
4191573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu                SubscriptionController.getInstance().setMccMnc(operator, subId);
420062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal            } else {
421062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                logd("EVENT_RECORDS_LOADED Operator name is null");
422062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal            }
4231573c17cbeab5a41936d369cb05cb7c0b0299ca1Jack Yu
424960c250fc2afee078852df1f37a887cec848ec5dShishir Agrawal            String msisdn = tm.getLine1Number(subId);
4256095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            ContentResolver contentResolver = mContext.getContentResolver();
4266095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
4276095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            if (msisdn != null) {
4286095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                ContentValues number = new ContentValues(1);
4296095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                number.put(SubscriptionManager.NUMBER, msisdn);
4306095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                contentResolver.update(SubscriptionManager.CONTENT_URI, number,
4316095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                        SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "="
4326095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                        + Long.toString(subId), null);
4336095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            }
4346095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
435062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal            SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
4366095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            String nameToSet;
437960c250fc2afee078852df1f37a887cec848ec5dShishir Agrawal            String simCarrierName = tm.getSimOperatorName(subId);
4386095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            ContentValues name = new ContentValues(1);
4396095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
440062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal            if (subInfo != null && subInfo.getNameSource() !=
4416095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                    SubscriptionManager.NAME_SOURCE_USER_INPUT) {
4426095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                if (!TextUtils.isEmpty(simCarrierName)) {
4436095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                    nameToSet = simCarrierName;
4446095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                } else {
4456095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                    nameToSet = "CARD " + Integer.toString(slotId + 1);
4466095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                }
4476095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                name.put(SubscriptionManager.DISPLAY_NAME, nameToSet);
4486095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                logd("sim name = " + nameToSet);
449e9876ff49bded7a139ee80471408f9fa965c0e89Sanket Padawe                contentResolver.update(SubscriptionManager.CONTENT_URI, name,
450e9876ff49bded7a139ee80471408f9fa965c0e89Sanket Padawe                        SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID
451e9876ff49bded7a139ee80471408f9fa965c0e89Sanket Padawe                        + "=" + Long.toString(subId), null);
4526095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            }
4536095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
45443677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan            /* Update preferred network type and network selection mode on SIM change.
45543677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan             * Storing last subId in SharedPreference for now to detect SIM change. */
4566095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            SharedPreferences sp =
4576095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                    PreferenceManager.getDefaultSharedPreferences(mContext);
45843677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan            int storedSubId = sp.getInt(CURR_SUBID + slotId, -1);
4596095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
46043677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan            if (storedSubId != subId) {
4616095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                int networkType = RILConstants.PREFERRED_NETWORK_MODE;
4626095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
4636095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                // Set the modem network mode
4646095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                mPhone[slotId].setPreferredNetworkType(networkType, null);
4656095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                Settings.Global.putInt(mPhone[slotId].getContext().getContentResolver(),
466062a2a3838c8d8adf16f4d9fbde8d52450da0336Shishir Agrawal                        Settings.Global.PREFERRED_NETWORK_MODE + subId,
4676095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                        networkType);
4686095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
46943677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan                // Only support automatic selection mode on SIM change.
4706095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                mPhone[slotId].getNetworkSelectionMode(
4716095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                        obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, new Integer(slotId)));
4726095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
47343677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan                // Update stored subId
4746095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                SharedPreferences.Editor editor = sp.edit();
47543677a14279bf3820e0372ff4422ab0c1f8dbb2bAmit Mahajan                editor.putInt(CURR_SUBID + slotId, subId);
4766095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal                editor.apply();
4776095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            }
4786095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        } else {
4796095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            logd("Invalid subId, could not update ContentResolver");
4806095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        }
481cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
4820cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        // Update set of enabled carrier apps now that the privilege rules may have changed.
4830cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson        CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(),
4840cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson                mPackageManager, TelephonyManager.getDefault(), mCurrentlyActiveUserId);
4850cdc53a095b5e715b9730f78dbd80d160fdd665fJeff Davidson
486cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        broadcastSimStateChanged(slotId, IccCardConstants.INTENT_VALUE_ICC_LOADED, null);
4874a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson        updateCarrierServices(slotId, IccCardConstants.INTENT_VALUE_ICC_LOADED);
488a1c457765342cee69203188b32419c395f54829cJunda Liu    }
489a1c457765342cee69203188b32419c395f54829cJunda Liu
4904a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson    private void updateCarrierServices(int slotId, String simState) {
491a1c457765342cee69203188b32419c395f54829cJunda Liu        CarrierConfigManager configManager = (CarrierConfigManager)
492a1c457765342cee69203188b32419c395f54829cJunda Liu                mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
493a1c457765342cee69203188b32419c395f54829cJunda Liu        configManager.updateConfigForPhoneId(slotId, simState);
4944a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson        mCarrierServiceBindHelper.updateForPhoneId(slotId, simState);
4956095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal    }
4966095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
4976095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal    private void handleSimAbsent(int slotId) {
4986095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        if (mIccId[slotId] != null && !mIccId[slotId].equals(ICCID_STRING_FOR_NO_SIM)) {
4996095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            logd("SIM" + (slotId + 1) + " hot plug out");
5006095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        }
5016095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        mIccId[slotId] = ICCID_STRING_FOR_NO_SIM;
502cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        if (isAllIccIdQueryDone()) {
5036095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal            updateSubscriptionInfoByIccId();
5046095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal        }
5054a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson        updateCarrierServices(slotId, IccCardConstants.INTENT_VALUE_ICC_ABSENT);
5066095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal    }
5076095eb5ae2968051ee4b513372e4dc0aeb028a17Shishir Agrawal
508299dad50409e907993fa96c7401d4f10c58018b1Wink Saville    /**
509299dad50409e907993fa96c7401d4f10c58018b1Wink Saville     * TODO: Simplify more, as no one is interested in what happened
510299dad50409e907993fa96c7401d4f10c58018b1Wink Saville     * only what the current list contains.
511299dad50409e907993fa96c7401d4f10c58018b1Wink Saville     */
512299dad50409e907993fa96c7401d4f10c58018b1Wink Saville    synchronized private void updateSubscriptionInfoByIccId() {
513299dad50409e907993fa96c7401d4f10c58018b1Wink Saville        logd("updateSubscriptionInfoByIccId:+ Start");
514a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
5154b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        mSubscriptionManager.clearSubscriptionInfo();
516a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
517a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
5184b09dff383ae7dfca595aeeea886a594a1947340Wink Saville            mInsertSimState[i] = SIM_NOT_CHANGE;
519a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
520a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
521a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        int insertedSimCount = PROJECT_SIM_NUM;
522a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
5234b09dff383ae7dfca595aeeea886a594a1947340Wink Saville            if (ICCID_STRING_FOR_NO_SIM.equals(mIccId[i])) {
524a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                insertedSimCount--;
5254b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                mInsertSimState[i] = SIM_NOT_INSERT;
526a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
527a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
528a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        logd("insertedSimCount = " + insertedSimCount);
529a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
530a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        int index = 0;
531a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
5324b09dff383ae7dfca595aeeea886a594a1947340Wink Saville            if (mInsertSimState[i] == SIM_NOT_INSERT) {
533a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                continue;
534a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
535a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            index = 2;
536a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            for (int j = i + 1; j < PROJECT_SIM_NUM; j++) {
5374b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                if (mInsertSimState[j] == SIM_NOT_CHANGE && mIccId[i].equals(mIccId[j])) {
5384b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mInsertSimState[i] = 1;
5394b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mInsertSimState[j] = index;
540a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    index++;
541a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
542a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
543a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
544a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
5454b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        ContentResolver contentResolver = mContext.getContentResolver();
546a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        String[] oldIccId = new String[PROJECT_SIM_NUM];
547a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
548a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            oldIccId[i] = null;
549299dad50409e907993fa96c7401d4f10c58018b1Wink Saville            List<SubscriptionInfo> oldSubInfo =
550b8eadaa4e0496f89667e4357ca714033dac1f2a6Svet Ganov                    SubscriptionController.getInstance().getSubInfoUsingSlotIdWithCheck(i, false,
551b8eadaa4e0496f89667e4357ca714033dac1f2a6Svet Ganov                    mContext.getOpPackageName());
552a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            if (oldSubInfo != null) {
5535f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                oldIccId[i] = oldSubInfo.get(0).getIccId();
554299dad50409e907993fa96c7401d4f10c58018b1Wink Saville                logd("updateSubscriptionInfoByIccId: oldSubId = "
555299dad50409e907993fa96c7401d4f10c58018b1Wink Saville                        + oldSubInfo.get(0).getSubscriptionId());
5564b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                if (mInsertSimState[i] == SIM_NOT_CHANGE && !mIccId[i].equals(oldIccId[i])) {
5574b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mInsertSimState[i] = SIM_CHANGED;
558a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
5594b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                if (mInsertSimState[i] != SIM_NOT_CHANGE) {
560a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    ContentValues value = new ContentValues(1);
5614b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    value.put(SubscriptionManager.SIM_SLOT_INDEX,
5624b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                            SubscriptionManager.INVALID_SIM_SLOT_INDEX);
563a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    contentResolver.update(SubscriptionManager.CONTENT_URI, value,
5644b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                            SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "="
5655f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                            + Integer.toString(oldSubInfo.get(0).getSubscriptionId()), null);
566a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
567a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            } else {
5684b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                if (mInsertSimState[i] == SIM_NOT_CHANGE) {
569a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    // no SIM inserted last time, but there is one SIM inserted now
5704b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mInsertSimState[i] = SIM_CHANGED;
571a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
572a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                oldIccId[i] = ICCID_STRING_FOR_NO_SIM;
573299dad50409e907993fa96c7401d4f10c58018b1Wink Saville                logd("updateSubscriptionInfoByIccId: No SIM in slot " + i + " last time");
574a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
575a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
576a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
577a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
578299dad50409e907993fa96c7401d4f10c58018b1Wink Saville            logd("updateSubscriptionInfoByIccId: oldIccId[" + i + "] = " + oldIccId[i] +
5794b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    ", sIccId[" + i + "] = " + mIccId[i]);
580a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
581a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
582a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        //check if the inserted SIM is new SIM
583a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        int nNewCardCount = 0;
584a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        int nNewSimStatus = 0;
585a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
5864b09dff383ae7dfca595aeeea886a594a1947340Wink Saville            if (mInsertSimState[i] == SIM_NOT_INSERT) {
587299dad50409e907993fa96c7401d4f10c58018b1Wink Saville                logd("updateSubscriptionInfoByIccId: No SIM inserted in slot " + i + " this time");
588a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            } else {
5894b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                if (mInsertSimState[i] > 0) {
590a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    //some special SIMs may have the same IccIds, add suffix to distinguish them
591a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    //FIXME: addSubInfoRecord can return an error.
5924b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mSubscriptionManager.addSubscriptionInfoRecord(mIccId[i]
5934b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                            + Integer.toString(mInsertSimState[i]), i);
594a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    logd("SUB" + (i + 1) + " has invalid IccId");
595a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                } else /*if (sInsertSimState[i] != SIM_NOT_INSERT)*/ {
5964b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mSubscriptionManager.addSubscriptionInfoRecord(mIccId[i], i);
597a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
5984b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                if (isNewSim(mIccId[i], oldIccId)) {
599a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    nNewCardCount++;
600a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    switch (i) {
601a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        case PhoneConstants.SUB1:
602a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            nNewSimStatus |= STATUS_SIM1_INSERTED;
603a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            break;
604a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        case PhoneConstants.SUB2:
605a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            nNewSimStatus |= STATUS_SIM2_INSERTED;
606a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            break;
607a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        case PhoneConstants.SUB3:
608a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            nNewSimStatus |= STATUS_SIM3_INSERTED;
609a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                            break;
610a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        //case PhoneConstants.SUB3:
611a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        //    nNewSimStatus |= STATUS_SIM4_INSERTED;
612a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                        //    break;
613a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                    }
614a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
6154b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    mInsertSimState[i] = SIM_NEW;
616a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                }
617a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
618a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
619a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
620a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for (int i = 0; i < PROJECT_SIM_NUM; i++) {
6214b09dff383ae7dfca595aeeea886a594a1947340Wink Saville            if (mInsertSimState[i] == SIM_CHANGED) {
6224b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                mInsertSimState[i] = SIM_REPOSITION;
623a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
624299dad50409e907993fa96c7401d4f10c58018b1Wink Saville            logd("updateSubscriptionInfoByIccId: sInsertSimState[" + i + "] = "
6254b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                    + mInsertSimState[i]);
626a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
627a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
6284b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
629a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        int nSubCount = (subInfos == null) ? 0 : subInfos.size();
630299dad50409e907993fa96c7401d4f10c58018b1Wink Saville        logd("updateSubscriptionInfoByIccId: nSubCount = " + nSubCount);
6314b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        for (int i=0; i < nSubCount; i++) {
632299dad50409e907993fa96c7401d4f10c58018b1Wink Saville            SubscriptionInfo temp = subInfos.get(i);
633440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville
634960c250fc2afee078852df1f37a887cec848ec5dShishir Agrawal            String msisdn = TelephonyManager.getDefault().getLine1Number(
6355f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                    temp.getSubscriptionId());
636440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville
637440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville            if (msisdn != null) {
638440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville                ContentValues value = new ContentValues(1);
639440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville                value.put(SubscriptionManager.NUMBER, msisdn);
640440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville                contentResolver.update(SubscriptionManager.CONTENT_URI, value,
6414b09dff383ae7dfca595aeeea886a594a1947340Wink Saville                        SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "="
6425f208fd8c2c2ef5036dabffafc72b73b9eb3fc16Stuart Scott                        + Integer.toString(temp.getSubscriptionId()), null);
643440b44eb71b8d5afeeaa5fda8912f3a61b4c1588Wink Saville            }
644a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
645a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
646e50e6c1d03701f579fb558324425bc3d6f64c4eaStuart Scott        // Ensure the modems are mapped correctly
647960c250fc2afee078852df1f37a887cec848ec5dShishir Agrawal        mSubscriptionManager.setDefaultDataSubId(
648960c250fc2afee078852df1f37a887cec848ec5dShishir Agrawal                mSubscriptionManager.getDefaultDataSubscriptionId());
649e50e6c1d03701f579fb558324425bc3d6f64c4eaStuart Scott
650299dad50409e907993fa96c7401d4f10c58018b1Wink Saville        SubscriptionController.getInstance().notifySubscriptionInfoChanged();
651299dad50409e907993fa96c7401d4f10c58018b1Wink Saville        logd("updateSubscriptionInfoByIccId:- SsubscriptionInfo update complete");
652a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
653a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
6544b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private boolean isNewSim(String iccId, String[] oldIccId) {
655a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        boolean newSim = true;
656a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        for(int i = 0; i < PROJECT_SIM_NUM; i++) {
657a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            if(iccId.equals(oldIccId[i])) {
658a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                newSim = false;
659a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville                break;
660a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville            }
661a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        }
662a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        logd("newSim = " + newSim);
663a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
664a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        return newSim;
665a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
666a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
667cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    private void broadcastSimStateChanged(int slotId, String state, String reason) {
668cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        Intent i = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
669875357f84bde43d1b774123579a16afe689b4182Robert Greenwalt        // TODO - we'd like this intent to have a single snapshot of all sim state,
670875357f84bde43d1b774123579a16afe689b4182Robert Greenwalt        // but until then this should not use REPLACE_PENDING or we may lose
671875357f84bde43d1b774123579a16afe689b4182Robert Greenwalt        // information
672875357f84bde43d1b774123579a16afe689b4182Robert Greenwalt        // i.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
673875357f84bde43d1b774123579a16afe689b4182Robert Greenwalt        //         | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
674875357f84bde43d1b774123579a16afe689b4182Robert Greenwalt        i.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
675cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        i.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone");
676cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        i.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE, state);
677cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        i.putExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON, reason);
678cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal        SubscriptionManager.putPhoneIdAndSubIdExtra(i, slotId);
6792416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan        logd("Broadcasting intent ACTION_SIM_STATE_CHANGED " + state + " reason " + reason +
6802416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan             " for mCardIndex: " + slotId);
6817d9297a8415f4515508c0bf9abbd650e5ce6aa1bAmit Mahajan        ActivityManagerNative.broadcastStickyIntent(i, READ_PHONE_STATE, UserHandle.USER_ALL);
6822416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan        if (!mUserManager.isUserUnlocked()) {
6832416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan            rebroadcastIntentsOnUnlock.put(slotId, i);
6842416ed9dea025ad22c37d333ac1cbf4fae75c3abAmit Mahajan        }
685cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal    }
686cf53a4abdf13d04542be44e181d8a7f2983aeb7bShishir Agrawal
687a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    public void dispose() {
688a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville        logd("[dispose]");
6894b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        mContext.unregisterReceiver(sReceiver);
690a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
691a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville
6924b09dff383ae7dfca595aeeea886a594a1947340Wink Saville    private void logd(String message) {
6934b09dff383ae7dfca595aeeea886a594a1947340Wink Saville        Rlog.d(LOG_TAG, message);
694a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville    }
6954a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson
6964a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
6974a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson        pw.println("SubscriptionInfoUpdater:");
6984a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson        mCarrierServiceBindHelper.dump(fd, pw, args);
6994a3d736dbf0b8262290d3720085779538ceaad72Zach Johnson    }
700a8467dd0c524787104b1ccdddc5e8af10ba729edWink Saville}
701