On IMSI change and CDMA mode, set automatic network selection if it is manual.

Currently the mode is set to automiatic without checking the actual mode. This
causes the radio to not respond till it attaches to a network (as per the
definition in RIL).

Bug: 18233109
Change-Id: I543743298f888300af77d2d81f887bb61002524f
diff --git a/src/java/com/android/internal/telephony/Phone.java b/src/java/com/android/internal/telephony/Phone.java
index ccdd8f4..8003d0a 100644
--- a/src/java/com/android/internal/telephony/Phone.java
+++ b/src/java/com/android/internal/telephony/Phone.java
@@ -1093,6 +1093,15 @@
                             Message response);
 
     /**
+     * Query the radio for the current network selection mode.
+     *
+     * Return values:
+     *     0 - automatic.
+     *     1 - manual.
+     */
+    void getNetworkSelectionMode(Message response);
+
+    /**
      *  Requests to set the preferred network type for searching and registering
      * (CS/PS domain, RAT, and operation mode)
      * @param networkType one of  NT_*_TYPE
diff --git a/src/java/com/android/internal/telephony/PhoneBase.java b/src/java/com/android/internal/telephony/PhoneBase.java
index c2713a9..e0037ba 100644
--- a/src/java/com/android/internal/telephony/PhoneBase.java
+++ b/src/java/com/android/internal/telephony/PhoneBase.java
@@ -864,6 +864,11 @@
     }
 
     @Override
+    public void getNetworkSelectionMode(Message message) {
+        mCi.getNetworkSelectionMode(message);
+    }
+
+    @Override
     public void selectNetworkManually(OperatorInfo network, Message response) {
         // wrap the response message in our own message along with
         // the operator's id.
diff --git a/src/java/com/android/internal/telephony/PhoneProxy.java b/src/java/com/android/internal/telephony/PhoneProxy.java
index bdbb646..5abe3f2 100644
--- a/src/java/com/android/internal/telephony/PhoneProxy.java
+++ b/src/java/com/android/internal/telephony/PhoneProxy.java
@@ -867,6 +867,11 @@
     }
 
     @Override
+    public void getNetworkSelectionMode(Message response) {
+        mActivePhone.getNetworkSelectionMode(response);
+    }
+
+    @Override
     public void selectNetworkManually(OperatorInfo network, Message response) {
         mActivePhone.selectNetworkManually(network, response);
     }
diff --git a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
index cc7e3a1..28b1d5a 100644
--- a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
+++ b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
@@ -58,6 +58,7 @@
     private static final int PROJECT_SIM_NUM = TelephonyManager.getDefault().getPhoneCount();
     private static final int EVENT_OFFSET = 8;
     private static final int EVENT_QUERY_ICCID_DONE = 1;
+    private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 2;
     private static final String ICCID_STRING_FOR_NO_SIM = "";
     /**
      *  int[] sInsertSimState maintains all slots' SIM inserted status currently,
@@ -195,8 +196,9 @@
                             Settings.Global.putInt(sPhone[slotId].getContext().getContentResolver(),
                                     Settings.Global.PREFERRED_NETWORK_MODE, networkType);
 
-                            // Only support automatic selection mode on IMSI change
-                            sPhone[slotId].setNetworkSelectionModeAutomatic(null);
+                            // Only support automatic selection mode on IMSI change.
+                            sPhone[slotId].getNetworkSelectionMode(
+                                    obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE));
 
                             // Update stored IMSI
                             SharedPreferences.Editor editor = sp.edit();
@@ -290,6 +292,16 @@
                     updateSubscriptionInfoByIccId();
                 }
                 break;
+            case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
+                if (ar.exception == null && ar.result != null) {
+                    int[] modes = (int[])ar.result;
+                    if (modes[0] == 1) {  // Manual mode.
+                        sPhone[slotId].setNetworkSelectionModeAutomatic(null);
+                    }
+                } else {
+                    logd("EVENT_GET_NETWORK_SELECTION_MODE_DONE: error getting network mode.");
+                }
+                break;
             default:
                 logd("Unknown msg:" + msg.what);
         }
diff --git a/src/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java b/src/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java
index 61955d4..0e55c8c 100644
--- a/src/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java
+++ b/src/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java
@@ -312,7 +312,7 @@
             }
 
             // Only support automatic selection mode in CDMA.
-            // mPhone.setNetworkSelectionModeAutomatic(null);
+            mCi.getNetworkSelectionMode(obtainMessage(EVENT_POLL_STATE_NETWORK_SELECTION_MODE));
 
             mPhone.prepareEri();
             break;
@@ -321,7 +321,7 @@
             updatePhoneObject();
 
             // Only support automatic selection mode in CDMA.
-            // mPhone.setNetworkSelectionModeAutomatic(null);
+            mCi.getNetworkSelectionMode(obtainMessage(EVENT_POLL_STATE_NETWORK_SELECTION_MODE));
 
             // For Non-RUIM phones, the subscription information is stored in
             // Non Volatile. Here when Non-Volatile is ready, we can poll the CDMA
@@ -523,14 +523,25 @@
             setPowerStateToDesired();
             break;
 
+        case EVENT_POLL_STATE_NETWORK_SELECTION_MODE:
+            if (DBG) log("EVENT_POLL_STATE_NETWORK_SELECTION_MODE");
+            ar = (AsyncResult) msg.obj;
+            if (ar.exception == null && ar.result != null) {
+                ints = (int[])ar.result;
+                if (ints[0] == 1) {  // Manual selection.
+                    mPhone.setNetworkSelectionModeAutomatic(null);
+                }
+            } else {
+                log("Unable to getNetworkSelectionMode");
+            }
+            break;
+
         default:
             super.handleMessage(msg);
         break;
         }
     }
 
-    //***** Private Instance Methods
-
     private void handleCdmaSubscriptionSource(int newSubscriptionSource) {
         log("Subscription Source : " + newSubscriptionSource);
         mIsSubscriptionFromRuim =