Fix ArrayOutOfBounds bug, tighten parameter checking.

Also, make logging consistent.

Bug: 17424200
Change-Id: Ie5bef1f557bf88b01eeb97c117807a40689c28e4
diff --git a/src/java/com/android/internal/telephony/dataconnection/DctController.java b/src/java/com/android/internal/telephony/dataconnection/DctController.java
index e5631eb..4c2c8c7 100644
--- a/src/java/com/android/internal/telephony/dataconnection/DctController.java
+++ b/src/java/com/android/internal/telephony/dataconnection/DctController.java
@@ -143,11 +143,16 @@
 
                     long subId = intent.getLongExtra(PhoneConstants.SUBSCRIPTION_KEY, PhoneConstants.SUB1);
                     int phoneId = SubscriptionManager.getPhoneId(subId);
-                    logd("DataStateReceiver phoneId= " + phoneId);
+                    logd("DataStateReceiver: phoneId= " + phoneId);
 
-                    // for the case of network out of service when bootup
-                    if (subId == -1 || subId == -2) {
-                        logd("Network out of service and return");
+                    // for the case of network out of service when bootup (ignore dummy values too)
+                    if (!SubscriptionManager.isValidSubId(subId) || (subId < 0)) {
+                        // FIXME: Maybe add SM.isRealSubId(subId)??
+                        logd("DataStateReceiver: ignore invalid subId=" + subId);
+                        return;
+                    }
+                    if (!SubscriptionManager.isValidPhoneId(phoneId)) {
+                        logd("DataStateReceiver: ignore invalid phoneId=" + phoneId);
                         return;
                     }
 
@@ -157,31 +162,35 @@
                         switch (state) {
                             case ServiceState.STATE_POWER_OFF:
                                 mServicePowerOffFlag[phoneId] = true;
-                                logd("Recv STATE_POWER_OFF Intent from phoneId=" + phoneId);
+                                logd("DataStateReceiver: STATE_POWER_OFF Intent from phoneId="
+                                        + phoneId);
                                 break;
                             case ServiceState.STATE_IN_SERVICE:
                                 mServicePowerOffFlag[phoneId] = false;
-                                logd("Recv STATE_IN_SERVICE Intent from phoneId=" + phoneId);
+                                logd("DataStateReceiver: STATE_IN_SERVICE Intent from phoneId="
+                                        + phoneId);
                                 break;
                             case ServiceState.STATE_OUT_OF_SERVICE:
-                                logd("Recv STATE_OUT_OF_SERVICE Intent from phoneId=" + phoneId);
+                                logd("DataStateReceiver: STATE_OUT_OF_SERVICE Intent from phoneId="
+                                        + phoneId);
                                 if (mServicePowerOffFlag[phoneId]) {
                                     mServicePowerOffFlag[phoneId] = false;
                                 }
                                 break;
                             case ServiceState.STATE_EMERGENCY_ONLY:
-                                logd("Recv STATE_EMERGENCY_ONLY Intent from phoneId=" + phoneId);
+                                logd("DataStateReceiver: STATE_EMERGENCY_ONLY Intent from phoneId="
+                                        + phoneId);
                                 break;
                             default:
-                                logd("Recv SERVICE_STATE_CHANGED invalid state");
+                                logd("DataStateReceiver: SERVICE_STATE_CHANGED invalid state");
                                 break;
                         }
 
                         if (prevPowerOff && mServicePowerOffFlag[phoneId] == false &&
                                 mCurrentDataPhone == PHONE_NONE &&
                                 phoneId == getDataConnectionFromSetting()) {
-                            logd("Current Phone is none and default Phone is " +
-                                    phoneId + ", then enableApnType()");
+                            logd("DataStateReceiver: Current Phone is none and default phoneId="
+                                    + phoneId + ", then enableApnType()");
                             enableApnType(subId, PhoneConstants.APN_TYPE_DEFAULT);
                         }
                     }
@@ -210,7 +219,15 @@
     }
 
     private DctController(PhoneProxy[] phones) {
-        mPhoneNum = TelephonyManager.getDefault().getPhoneCount();
+        if (phones == null || phones.length == 0) {
+            if (phones == null) {
+                loge("DctController(phones): UNEXPECTED phones=null, ignore");
+            } else {
+                loge("DctController(phones): UNEXPECTED phones.length=0, ignore");
+            }
+            return;
+        }
+        mPhoneNum = phones.length;
         mServicePowerOffFlag = new boolean[mPhoneNum];
         mPhones = phones;
 
@@ -232,9 +249,9 @@
                 mDcSwitchStateHandler[i], mDcSwitchState[i].getHandler());
 
             if (status == AsyncChannel.STATUS_SUCCESSFUL) {
-                logd("Connect success: " + i);
+                logd("DctController(phones): Connect success: " + i);
             } else {
-                loge("Could not connect to " + i);
+                loge("DctController(phones): Could not connect to " + i);
             }
 
             mDcSwitchState[i].registerForIdle(mRspHander, EVENT_PHONE1_DETACH + i, null);