Merge "Fix extra problem in embms testapps"
diff --git a/src/com/android/phone/NetworkOperators.java b/src/com/android/phone/NetworkOperators.java
index babd283..44af267 100644
--- a/src/com/android/phone/NetworkOperators.java
+++ b/src/com/android/phone/NetworkOperators.java
@@ -88,9 +88,9 @@
         mChooseNetwork = findPreference(BUTTON_CHOOSE_NETWORK_KEY);
         mNetworkSelect = (NetworkSelectListPreference) findPreference(BUTTON_NETWORK_SELECT_KEY);
         if (mEnableNewManualSelectNetworkUI) {
-            this.removePreference(mNetworkSelect);
+            removePreference(mNetworkSelect);
         } else {
-            this.removePreference(mChooseNetwork);
+            removePreference(mChooseNetwork);
         }
         mProgressDialog = new ProgressDialog(getContext());
     }
@@ -113,7 +113,7 @@
             if (mChooseNetwork != null) {
                 TelephonyManager telephonyManager = (TelephonyManager)
                         getContext().getSystemService(Context.TELEPHONY_SERVICE);
-                logd("data connection status " + telephonyManager.getDataState());
+                if (DBG) logd("data connection status " + telephonyManager.getDataState());
                 if (telephonyManager.getDataState() == telephonyManager.DATA_CONNECTED) {
                     mChooseNetwork.setSummary(telephonyManager.getNetworkOperatorName());
                 } else {
@@ -139,7 +139,7 @@
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         if (preference == mAutoSelect) {
             boolean autoSelect = (Boolean) newValue;
-            logd("onPreferenceChange autoSelect: " + String.valueOf(autoSelect));
+            if (DBG) logd("onPreferenceChange autoSelect: " + String.valueOf(autoSelect));
             selectNetworkAutomatic(autoSelect);
             MetricsLogger.action(getContext(),
                     MetricsEvent.ACTION_MOBILE_NETWORK_AUTO_SELECT_NETWORK_TOGGLE, autoSelect);
@@ -196,7 +196,6 @@
                         }
                     }
             }
-
             return;
         }
     };
@@ -241,7 +240,7 @@
     }
 
     private void selectNetworkAutomatic(boolean autoSelect) {
-        logd("selectNetworkAutomatic: " + String.valueOf(autoSelect));
+        if (DBG) logd("selectNetworkAutomatic: " + String.valueOf(autoSelect));
         if (mEnableNewManualSelectNetworkUI) {
             if (mChooseNetwork != null) {
                 mChooseNetwork.setEnabled(!autoSelect);
@@ -309,7 +308,7 @@
 
     protected boolean preferenceTreeClick(Preference preference) {
         if (mEnableNewManualSelectNetworkUI) {
-            logd("enable New AutoSelectNetwork UI");
+            if (DBG) logd("enable New AutoSelectNetwork UI");
             if (preference == mChooseNetwork) {
                 openChooseNetworkPage();
             }
diff --git a/src/com/android/phone/NetworkSelectListPreference.java b/src/com/android/phone/NetworkSelectListPreference.java
index a8f29c9..4bb03b0 100644
--- a/src/com/android/phone/NetworkSelectListPreference.java
+++ b/src/com/android/phone/NetworkSelectListPreference.java
@@ -33,11 +33,6 @@
 import android.telephony.CellInfoGsm;
 import android.telephony.CellInfoLte;
 import android.telephony.CellInfoWcdma;
-import android.telephony.CellSignalStrengthCdma;
-import android.telephony.CellSignalStrengthGsm;
-import android.telephony.CellSignalStrengthLte;
-import android.telephony.CellSignalStrengthWcdma;
-import android.telephony.NetworkScan;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.text.BidiFormatter;
@@ -70,8 +65,7 @@
 
     private static final int EVENT_NETWORK_SELECTION_DONE = 1;
     private static final int EVENT_NETWORK_SCAN_RESULTS = 2;
-    private static final int EVENT_NETWORK_SCAN_ERROR = 3;
-    private static final int EVENT_NETWORK_SCAN_COMPLETED = 4;
+    private static final int EVENT_NETWORK_SCAN_COMPLETED = 3;
 
     //dialog ids
     private static final int DIALOG_NETWORK_SELECTION = 100;
@@ -83,7 +77,6 @@
 
     private int mSubId;
     private NetworkOperators mNetworkOperators;
-    private boolean mNeedScanAgain;
 
     private ProgressDialog mProgressDialog;
     public NetworkSelectListPreference(Context context, AttributeSet attrs) {
@@ -98,8 +91,10 @@
     @Override
     protected void onClick() {
         sendMetricsEvent(null);
-        // Scan the network with setting the isIncrementalResult as true via TelephonyManager first.
-        loadNetworksList(true);
+        // Start the one-time network scan via {@link Phone#getAvailableNetworks()}.
+        // {@link NetworkQueryService will return a {@link onResults()} callback first with a list
+        // of CellInfo, and then will return a {@link onComplete} indicating the scan completed.
+        loadNetworksList();
     }
 
     private final Handler mHandler = new Handler() {
@@ -132,86 +127,22 @@
                 case EVENT_NETWORK_SCAN_RESULTS:
                     List<CellInfo> results = (List<CellInfo>) msg.obj;
                     results.removeIf(cellInfo -> cellInfo == null);
-                    if (results.size() > 0) {
-                        boolean isInvalidCellInfoList = true;
-                        // Regard the list as invalid only if all the elements in the list are
-                        // invalid.
-                        for (CellInfo cellInfo : results) {
-                            if (!isInvalidCellInfo(cellInfo)) {
-                                isInvalidCellInfoList = false;
-                                break;
-                            }
-                        }
-                        if (isInvalidCellInfoList) {
-                            mNeedScanAgain = true;
-                            if (DBG) {
-                                logd("Invalid cell info. Stop current network scan "
-                                        + "and start a new one via old API");
-                            }
-                            // Stop current network scan flow. This behavior will result in a
-                            // onComplete() callback, after which we will start a new network query
-                            // via Phone.getAvailableNetworks(). This behavior might also result in
-                            // a onError() callback if the modem did not stop network query
-                            // successfully. In this case we will display network query failed
-                            // instead of resending a new request.
-                            try {
-                                if (mNetworkQueryService != null) {
-                                    mNetworkQueryService.stopNetworkQuery();
-                                }
-                            } catch (RemoteException e) {
-                                loge("exception from stopNetworkQuery " + e);
-                            }
-                        } else {
-                            // TODO(b/70530820): Display the scan results incrementally after
-                            // finalizing the UI desing on Mobile Network Setting page. For now,
-                            // just update the CellInfo list when received the onResult callback,
-                            // and display the scan result when received the onComplete callback
-                            // in the end.
-                            mCellInfoList = new ArrayList<>(results);
-                            if (DBG) logd("CALLBACK_SCAN_RESULTS" + mCellInfoList.toString());
-                        }
-                    }
+                    mCellInfoList = new ArrayList<>(results);
+                    if (DBG) logd("CALLBACK_SCAN_RESULTS" + mCellInfoList.toString());
 
                     break;
 
-                case EVENT_NETWORK_SCAN_ERROR:
-                    int error = msg.arg1;
-                    if (DBG) logd("error while querying available networks " + error);
-                    if (error == NetworkScan.ERROR_UNSUPPORTED) {
-                        if (DBG) {
-                            logd("Modem does not support: try to scan network again via Phone");
-                        }
-                        loadNetworksList(false);
-                    } else {
-                        try {
-                            if (mNetworkQueryService != null) {
-                                mNetworkQueryService.unregisterCallback(mCallback);
-                            }
-                        } catch (RemoteException e) {
-                            loge("onError: exception from unregisterCallback " + e);
-                        }
-                        displayNetworkQueryFailed(error);
-                    }
-                    break;
-
                 case EVENT_NETWORK_SCAN_COMPLETED:
-                    if (mNeedScanAgain) {
-                        logd("CellInfo is invalid to display. Start a new scan via Phone. ");
-                        loadNetworksList(false);
-                        mNeedScanAgain = false;
-                    } else {
-                        try {
-                            if (mNetworkQueryService != null) {
-                                mNetworkQueryService.unregisterCallback(mCallback);
-                            }
-                        } catch (RemoteException e) {
-                            loge("onComplete: exception from unregisterCallback " + e);
+                    try {
+                        if (mNetworkQueryService != null) {
+                            mNetworkQueryService.unregisterCallback(mCallback);
                         }
-                        if (DBG) logd("scan complete, load the cellInfosList");
-                        // Modify UI to indicate users that the scan has completed.
-                        networksListLoaded();
+                    } catch (RemoteException e) {
+                        loge("onComplete: exception from unregisterCallback " + e);
                     }
-                    break;
+                    if (DBG) logd("scan complete, load the cellInfosList");
+                    // Modify UI to indicate users that the scan has completed.
+                    networksListLoaded();
             }
             return;
         }
@@ -224,7 +155,7 @@
      */
     private final INetworkQueryServiceCallback mCallback = new INetworkQueryServiceCallback.Stub() {
 
-        /** Returns the scan results to the user, this callback will be called at lease one time. */
+        /** Returns the scan results to the user, this callback will be called only one time. */
         public void onResults(List<CellInfo> results) {
             if (DBG) logd("get scan results.");
             Message msg = mHandler.obtainMessage(EVENT_NETWORK_SCAN_RESULTS, results);
@@ -244,16 +175,9 @@
         }
 
         /**
-         * Informs the user that there is some error about the scan.
-         *
-         * This callback will be called whenever there is any error about the scan, and the scan
-         * will be terminated. onComplete() will NOT be called.
+         * This callback will not be called, since the old Scan API won't send this callback.
          */
-        public void onError(int error) {
-            if (DBG) logd("get onError callback with error code: " + error);
-            Message msg = mHandler.obtainMessage(EVENT_NETWORK_SCAN_ERROR, error, 0 /* arg2 */);
-            msg.sendToTarget();
-        }
+        public void onError(int error) {}
     };
 
     @Override
@@ -298,7 +222,6 @@
         mNetworkOperators = networkOperators;
         // This preference should share the same progressDialog with networkOperators category.
         mProgressDialog = progressDialog;
-        mNeedScanAgain = false;
 
         if (SubscriptionManager.isValidSubscriptionId(mSubId)) {
             mPhoneId = SubscriptionManager.getPhoneId(mSubId);
@@ -361,17 +284,14 @@
                 NotificationMgr.NETWORK_SELECTION_NOTIFICATION, status);
     }
 
-    private void loadNetworksList(boolean isIncrementalResult) {
+    private void loadNetworksList() {
         if (DBG) logd("load networks list...");
 
-        if (!mNeedScanAgain) {
-            // Avoid blinking while showing the dialog again.
-            showProgressDialog(DIALOG_NETWORK_LIST_LOAD);
-        }
+        showProgressDialog(DIALOG_NETWORK_LIST_LOAD);
 
         try {
             if (mNetworkQueryService != null) {
-                mNetworkQueryService.startNetworkQuery(mCallback, mPhoneId, isIncrementalResult);
+                mNetworkQueryService.startNetworkQuery(mCallback, mPhoneId, false);
             } else {
                 displayNetworkQueryFailed(NetworkQueryService.QUERY_EXCEPTION);
             }
@@ -399,7 +319,6 @@
         }
 
         setEnabled(true);
-
         if (mCellInfoList != null) {
             // create a preference for each item in the list.
             // just use the operator name instead of the mildly
@@ -554,46 +473,6 @@
         return oi;
     }
 
-
-    /**
-     * Check if the CellInfo is valid to display. If a CellInfo has signal strength but does
-     * not have operator info, it is invalid to display.
-     */
-    private boolean isInvalidCellInfo(CellInfo cellInfo) {
-        if (DBG) logd("Check isInvalidCellInfo: " + cellInfo.toString());
-        CharSequence al = null;
-        CharSequence as = null;
-        boolean hasSignalStrength = false;
-        if (cellInfo instanceof CellInfoLte) {
-            CellInfoLte lte = (CellInfoLte) cellInfo;
-            al = lte.getCellIdentity().getOperatorAlphaLong();
-            as = lte.getCellIdentity().getOperatorAlphaShort();
-            hasSignalStrength = !lte.getCellSignalStrength().equals(new CellSignalStrengthLte());
-        } else if (cellInfo instanceof CellInfoWcdma) {
-            CellInfoWcdma wcdma = (CellInfoWcdma) cellInfo;
-            al = wcdma.getCellIdentity().getOperatorAlphaLong();
-            as = wcdma.getCellIdentity().getOperatorAlphaShort();
-            hasSignalStrength = !wcdma.getCellSignalStrength().equals(
-                    new CellSignalStrengthWcdma());
-        } else if (cellInfo instanceof CellInfoGsm) {
-            CellInfoGsm gsm = (CellInfoGsm) cellInfo;
-            al = gsm.getCellIdentity().getOperatorAlphaLong();
-            as = gsm.getCellIdentity().getOperatorAlphaShort();
-            hasSignalStrength = !gsm.getCellSignalStrength().equals(new CellSignalStrengthGsm());
-        } else if (cellInfo instanceof CellInfoCdma) {
-            CellInfoCdma cdma = (CellInfoCdma) cellInfo;
-            al = cdma.getCellIdentity().getOperatorAlphaLong();
-            as = cdma.getCellIdentity().getOperatorAlphaShort();
-            hasSignalStrength = !cdma.getCellSignalStrength().equals(new CellSignalStrengthCdma());
-        } else {
-            return true;
-        }
-        if (TextUtils.isEmpty(al) && TextUtils.isEmpty(as) && hasSignalStrength) {
-            return true;
-        }
-        return false;
-    }
-
     @Override
     protected Parcelable onSaveInstanceState() {
         final Parcelable superState = super.onSaveInstanceState();
diff --git a/src/com/android/phone/NetworkSelectSetting.java b/src/com/android/phone/NetworkSelectSetting.java
index 7ef3dea..ff918e2 100644
--- a/src/com/android/phone/NetworkSelectSetting.java
+++ b/src/com/android/phone/NetworkSelectSetting.java
@@ -15,6 +15,7 @@
  */
 package com.android.phone;
 
+import android.app.ActionBar;
 import android.app.Activity;
 import android.content.ComponentName;
 import android.content.Context;
@@ -60,7 +61,7 @@
 public class NetworkSelectSetting extends PreferenceFragment {
 
     private static final String TAG = "NetworkSelectSetting";
-    private static final boolean DBG = true;
+    private static final boolean DBG = false;
 
     private static final int EVENT_NETWORK_SELECTION_DONE = 1;
     private static final int EVENT_NETWORK_SCAN_RESULTS = 2;
@@ -132,6 +133,12 @@
         // Inflate progress bar
         final Activity activity = getActivity();
         if (activity != null) {
+            ActionBar actionBar = activity.getActionBar();
+            if (actionBar != null) {
+                // android.R.id.home will be triggered in
+                // {@link NetworkSelectSettingAcitivity#onOptionsItemSelected()}
+                actionBar.setDisplayHomeAsUpEnabled(true);
+            }
             mFrameLayout = activity.findViewById(R.id.choose_network_content);
             final LayoutInflater inflater = activity.getLayoutInflater();
             final View pinnedHeader =
@@ -256,13 +263,13 @@
 
                     ar = (AsyncResult) msg.obj;
                     if (ar.exception != null) {
-                        mNetworkOperators.displayNetworkSelectionFailed(ar.exception);
+                        if (DBG) logd("manual network selection: failed! ");
+                        updateNetworkSelection();
                         // Set summary as "Couldn't connect" to the selected network.
                         mSelectedNetworkOperatorPreference.setSummary(
                                 R.string.network_could_not_connect);
                     } else {
                         if (DBG) logd("manual network selection: succeeded! ");
-                        mNetworkOperators.displayNetworkSelectionSucceeded(msg.arg1);
                         // Set summary as "Connected" to the selected network.
                         mSelectedNetworkOperatorPreference.setSummary(R.string.network_connected);
                     }
@@ -290,7 +297,6 @@
                 case EVENT_NETWORK_SCAN_COMPLETED:
                     stopNetworkQuery();
                     if (DBG) logd("scan complete");
-                    setProgressBarVisible(false);
                     if (mCellInfoList == null) {
                         // In case the scan timeout before getting any results
                         addMessagePreference(R.string.empty_networks_list);
@@ -611,6 +617,22 @@
         }
     }
 
+    /**
+     * Call {@link NotificationMgr#updateNetworkSelection(int, int)} to send notification about
+     * no service of user selected operator
+     */
+    private void updateNetworkSelection() {
+        if (DBG) logd("Update notification about no service of user selected operator");
+        final PhoneGlobals app = PhoneGlobals.getInstance();
+        Phone phone = PhoneFactory.getPhone(mPhoneId);
+        if (phone != null) {
+            ServiceState ss = mTelephonyManager.getServiceStateForSubscriber(phone.getSubId());
+            if (ss != null) {
+                app.notificationMgr.updateNetworkSelection(ss.getState(), phone.getSubId());
+            }
+        }
+    }
+
     private void stopNetworkQuery() {
         // Stop the network query process
         try {
diff --git a/src/com/android/phone/NetworkSelectSettingActivity.java b/src/com/android/phone/NetworkSelectSettingActivity.java
index 2f730ad..a151c81 100644
--- a/src/com/android/phone/NetworkSelectSettingActivity.java
+++ b/src/com/android/phone/NetworkSelectSettingActivity.java
@@ -23,6 +23,7 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.MenuItem;
 
 
 /**
@@ -57,4 +58,14 @@
                     .commit();
         }
     }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        final int itemId = item.getItemId();
+        if (itemId == android.R.id.home) {
+            onBackPressed();
+            return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
 }