Merge "Make NetworkSettings and NetworkQueryService multi-sim aware." into mnc-dev
diff --git a/res/xml/gsm_umts_options.xml b/res/xml/gsm_umts_options.xml
index 3f85ea8..86fe41d 100644
--- a/res/xml/gsm_umts_options.xml
+++ b/res/xml/gsm_umts_options.xml
@@ -31,11 +31,6 @@
         android:title="@string/networks"
         android:summary="@string/sum_carrier_select"
         android:persistent="false">
-
-        <intent android:action="android.intent.action.MAIN"
-            android:targetPackage="com.android.phone"
-            android:targetClass="com.android.phone.NetworkSetting" />
-
     </PreferenceScreen>
 
     <PreferenceScreen
diff --git a/src/com/android/phone/GsmUmtsOptions.java b/src/com/android/phone/GsmUmtsOptions.java
index d12bac9..2e9d88a 100644
--- a/src/com/android/phone/GsmUmtsOptions.java
+++ b/src/com/android/phone/GsmUmtsOptions.java
@@ -25,6 +25,7 @@
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
 import android.telephony.CarrierConfigManager;
+import android.content.ComponentName;
 
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.PhoneFactory;
@@ -41,6 +42,7 @@
     private static final String BUTTON_APN_EXPAND_KEY = "button_apn_key";
     private static final String BUTTON_OPERATOR_SELECTION_EXPAND_KEY = "button_carrier_sel_key";
     private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
+    public static final String EXTRA_SUB_ID = "sub_id";
     private PreferenceActivity mPrefActivity;
     private PreferenceScreen mPrefScreen;
     private int mSubId;
@@ -117,7 +119,21 @@
                             final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
                             // This will setup the Home and Search affordance
                             intent.putExtra(":settings:show_fragment_as_subsetting", true);
-                            intent.putExtra("sub_id", mSubId);
+                            intent.putExtra(EXTRA_SUB_ID, mSubId);
+                            mPrefActivity.startActivity(intent);
+                            return true;
+                        }
+            });
+        }
+        if (mPrefScreen.findPreference(BUTTON_OPERATOR_SELECTION_EXPAND_KEY) != null) {
+            mButtonOperatorSelectionExpand.setOnPreferenceClickListener(
+                    new Preference.OnPreferenceClickListener() {
+                        @Override
+                        public boolean onPreferenceClick(Preference preference) {
+                            final Intent intent = new Intent(Intent.ACTION_MAIN);
+                            intent.setComponent(new ComponentName("com.android.phone",
+                                    "com.android.phone.NetworkSetting"));
+                            intent.putExtra(EXTRA_SUB_ID, mSubId);
                             mPrefActivity.startActivity(intent);
                             return true;
                         }
diff --git a/src/com/android/phone/INetworkQueryService.aidl b/src/com/android/phone/INetworkQueryService.aidl
index 0733d73..81eb8e6 100644
--- a/src/com/android/phone/INetworkQueryService.aidl
+++ b/src/com/android/phone/INetworkQueryService.aidl
@@ -33,7 +33,7 @@
      * then just add the callback to the list of notifications
      * that will be sent upon query completion.
      */
-    void startNetworkQuery(in INetworkQueryServiceCallback cb);
+    void startNetworkQuery(in INetworkQueryServiceCallback cb, in int phoneId);
 
     /**
      * Tells the service that the requested query is to be ignored.
diff --git a/src/com/android/phone/NetworkQueryService.java b/src/com/android/phone/NetworkQueryService.java
index b38b110..1a497b4 100644
--- a/src/com/android/phone/NetworkQueryService.java
+++ b/src/com/android/phone/NetworkQueryService.java
@@ -57,9 +57,6 @@
     /** state of the query service */
     private int mState;
     
-    /** local handle to the phone object */
-    private Phone mPhone;
-    
     /**
      * Class for clients to access.  Because we know this service always
      * runs in the same process as its clients, we don't need to deal with
@@ -109,7 +106,7 @@
          * callback object in the queue to be notified upon request 
          * completion.
          */
-        public void startNetworkQuery(INetworkQueryServiceCallback cb) {
+        public void startNetworkQuery(INetworkQueryServiceCallback cb, int phoneId) {
             if (cb != null) {
                 // register the callback to the list of callbacks.
                 synchronized (mCallbacks) {
@@ -120,10 +117,17 @@
                         case QUERY_READY:
                             // TODO: we may want to install a timeout here in case we
                             // do not get a timely response from the RIL.
-                            mPhone.getAvailableNetworks(
-                                    mHandler.obtainMessage(EVENT_NETWORK_SCAN_COMPLETED));
-                            mState = QUERY_IS_RUNNING;
-                            if (DBG) log("starting new query");
+                            Phone phone = PhoneFactory.getPhone(phoneId);
+                            if (phone != null) {
+                                phone.getAvailableNetworks(
+                                        mHandler.obtainMessage(EVENT_NETWORK_SCAN_COMPLETED));
+                                mState = QUERY_IS_RUNNING;
+                                if (DBG) log("starting new query");
+                            } else {
+                                if (DBG) {
+                                    log("phone is null");
+                                }
+                            }
                             break;
                             
                         // do nothing if we're currently busy.
@@ -166,8 +170,6 @@
     @Override
     public void onCreate() {
         mState = QUERY_READY;        
-        mPhone = PhoneFactory.getPhone(
-                SubscriptionManager.getPhoneId(SubscriptionManager.getDefaultSubId()));
     }
 
     /**
diff --git a/src/com/android/phone/NetworkSetting.java b/src/com/android/phone/NetworkSetting.java
index 5925b0f..392db56 100644
--- a/src/com/android/phone/NetworkSetting.java
+++ b/src/com/android/phone/NetworkSetting.java
@@ -36,9 +36,11 @@
 import android.preference.PreferenceScreen;
 import android.text.TextUtils;
 import android.util.Log;
+import android.telephony.SubscriptionManager;
 
 import com.android.internal.telephony.CommandException;
 import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.OperatorInfo;
 
 import java.util.HashMap;
@@ -70,7 +72,7 @@
     //map of network controls to the network data.
     private HashMap<Preference, OperatorInfo> mNetworkMap;
 
-    Phone mPhone;
+    int mPhoneId = SubscriptionManager.INVALID_PHONE_INDEX;
     protected boolean mIsForeground = false;
 
     private UserManager mUm;
@@ -107,9 +109,6 @@
                         displayNetworkSelectionSucceeded();
                     }
 
-                    // update the phone in case replaced as part of selection
-                    mPhone = PhoneGlobals.getPhone();
-
                     break;
                 case EVENT_AUTO_SELECT_DONE:
                     if (DBG) log("hideProgressPanel");
@@ -135,9 +134,6 @@
                         displayNetworkSelectionSucceeded();
                     }
 
-                    // update the phone in case replaced as part of selection
-                    mPhone = PhoneGlobals.getPhone();
-
                     break;
             }
 
@@ -204,11 +200,16 @@
             if (DBG) log("selected network: " + networkStr);
 
             Message msg = mHandler.obtainMessage(EVENT_NETWORK_SELECTION_DONE);
-            mPhone.selectNetworkManually(mNetworkMap.get(selectedCarrier), msg);
+            Phone phone = PhoneFactory.getPhone(mPhoneId);
+            if (phone != null) {
+                phone.selectNetworkManually(mNetworkMap.get(selectedCarrier), msg);
+                displayNetworkSeletionInProgress(networkStr);
+                handled = true;
+            } else {
+                log("Error selecting network. phone is null.");
+            }
 
-            displayNetworkSeletionInProgress(networkStr);
 
-            handled = true;
         }
 
         return handled;
@@ -246,7 +247,14 @@
 
         addPreferencesFromResource(R.xml.carrier_select);
 
-        mPhone = PhoneGlobals.getPhone();
+        int subId;
+        Intent intent = getIntent();
+        if (intent != null && intent.getExtras() != null) {
+            subId = intent.getExtras().getInt(GsmUmtsOptions.EXTRA_SUB_ID);
+            if (SubscriptionManager.isValidSubscriptionId(subId)) {
+                mPhoneId = SubscriptionManager.getPhoneId(subId);
+            }
+        }
 
         mNetworkList = (PreferenceGroup) getPreferenceScreen().findPreference(LIST_NETWORKS_KEY);
         mNetworkMap = new HashMap<Preference, OperatorInfo>();
@@ -402,7 +410,7 @@
 
         // delegate query request to the service.
         try {
-            mNetworkQueryService.startNetworkQuery(mCallback);
+            mNetworkQueryService.startNetworkQuery(mCallback, mPhoneId);
         } catch (RemoteException e) {
             log("loadNetworksList: exception from startNetworkQuery " + e);
             if (mIsForeground) {
@@ -511,7 +519,10 @@
         }
 
         Message msg = mHandler.obtainMessage(EVENT_AUTO_SELECT_DONE);
-        mPhone.setNetworkSelectionModeAutomatic(msg);
+        Phone phone = PhoneFactory.getPhone(mPhoneId);
+        if (phone != null) {
+            phone.setNetworkSelectionModeAutomatic(msg);
+        }
     }
 
     private void log(String msg) {
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 885b797..c579fe0 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -573,6 +573,7 @@
         // Use NetworkSetting to handle the selection intent
         intent.setComponent(new ComponentName("com.android.phone",
                 "com.android.phone.NetworkSetting"));
+        intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
         PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
 
         List<UserInfo> users = mUserManager.getUsers(true);