Merge "Update network select page"
diff --git a/src/com/android/settings/network/telephony/NetworkOperatorPreference.java b/src/com/android/settings/network/telephony/NetworkOperatorPreference.java
index ad0285f..fd57bf5 100644
--- a/src/com/android/settings/network/telephony/NetworkOperatorPreference.java
+++ b/src/com/android/settings/network/telephony/NetworkOperatorPreference.java
@@ -116,14 +116,9 @@
     private void updateIcon(int level) {
         if (level < 0 || level >= NUMBER_OF_LEVELS) return;
         Context context = getContext();
-        // Make the signal strength drawable
-        int iconId = 0;
-        if (DBG) Log.d(TAG, "updateIcon level: " + String.valueOf(level));
-        iconId = SignalDrawable.getState(level, NUMBER_OF_LEVELS, false /* cutOut */);
-
         SignalDrawable signalDrawable = new SignalDrawable(getContext());
-        signalDrawable.setLevel(iconId);
-        signalDrawable.setDarkIntensity(0);
+        signalDrawable.setLevel(
+                SignalDrawable.getState(level, NUMBER_OF_LEVELS, false /* cutOut */));
 
         // Make the network type drawable
         int iconType = getIconIdForCell(mCellInfo);
@@ -134,7 +129,7 @@
                                 .getResources().getDrawable(iconType, getContext().getTheme());
 
         // Overlay the two drawables
-        Drawable[] layers = {networkDrawable, signalDrawable};
+        final Drawable[] layers = {networkDrawable, signalDrawable};
         final int iconSize =
                 context.getResources().getDimensionPixelSize(R.dimen.signal_strength_icon_size);
 
diff --git a/src/com/android/settings/network/telephony/NetworkSelectSettings.java b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
index dbab674..9df9d1e 100644
--- a/src/com/android/settings/network/telephony/NetworkSelectSettings.java
+++ b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
@@ -44,6 +44,7 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
+import com.android.settingslib.utils.ThreadUtils;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -60,6 +61,7 @@
 
     private static final String TAG = "NetworkSelectSettings";
 
+    private static final int EVENT_SET_NETWORK_SELECTION_MANUALLY_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;
@@ -135,13 +137,10 @@
         mForbiddenPlmns = Arrays.asList(mTelephonyManager.getForbiddenPlmns());
         setProgressBarVisible(true);
 
-        if (mUseNewApi) {
-            mNetworkScanHelper.startNetworkScan(
-                    NetworkScanHelper.NETWORK_SCAN_TYPE_INCREMENTAL_RESULTS);
-        } else {
-            mNetworkScanHelper.startNetworkScan(
-                    NetworkScanHelper.NETWORK_SCAN_TYPE_WAIT_FOR_ALL_RESULTS);
-        }
+        mNetworkScanHelper.startNetworkScan(
+                mUseNewApi
+                        ? NetworkScanHelper.NETWORK_SCAN_TYPE_INCREMENTAL_RESULTS
+                        : NetworkScanHelper.NETWORK_SCAN_TYPE_WAIT_FOR_ALL_RESULTS);
     }
 
     @Override
@@ -154,7 +153,6 @@
     public boolean onPreferenceTreeClick(Preference preference) {
         if (preference != mSelectedPreference) {
             stopNetworkQuery();
-            setProgressBarVisible(false);
             // Refresh the last selected item in case users reselect network.
             if (mSelectedPreference != null) {
                 mSelectedPreference.setSummary(null);
@@ -162,6 +160,7 @@
 
             mSelectedPreference = (NetworkOperatorPreference) preference;
             CellInfo cellInfo = mSelectedPreference.getCellInfo();
+            mSelectedPreference.setSummary(R.string.network_connecting);
 
             mMetricsFeatureProvider.action(getContext(),
                     MetricsEvent.ACTION_MOBILE_NETWORK_MANUAL_SELECT_NETWORK);
@@ -176,11 +175,17 @@
                 }
             }
 
+            setProgressBarVisible(true);
+            // Disable the screen until network is manually set
+            getPreferenceScreen().setEnabled(false);
+
             final OperatorInfo operatorInfo = CellInfoUtil.getOperatorInfoFromCellInfo(cellInfo);
-            final boolean isSuccess = mTelephonyManager.setNetworkSelectionModeManual(
-                    operatorInfo, true /* persistSelection */);
-            mSelectedPreference.setSummary(
-                    isSuccess ? R.string.network_connected : R.string.network_could_not_connect);
+            ThreadUtils.postOnBackgroundThread(() -> {
+                Message msg = mHandler.obtainMessage(EVENT_SET_NETWORK_SELECTION_MANUALLY_DONE);
+                msg.obj = mTelephonyManager.setNetworkSelectionModeManual(
+                        operatorInfo, true /* persistSelection */);
+                msg.sendToTarget();
+            });
         }
 
         return true;
@@ -205,6 +210,15 @@
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
+                case EVENT_SET_NETWORK_SELECTION_MANUALLY_DONE:
+                    setProgressBarVisible(false);
+                    getPreferenceScreen().setEnabled(true);
+
+                    boolean isSucceed = (boolean) msg.obj;
+                    mSelectedPreference.setSummary(isSucceed
+                            ? R.string.network_connected
+                            : R.string.network_could_not_connect);
+                    break;
                 case EVENT_NETWORK_SCAN_RESULTS:
                     List<CellInfo> results = aggregateCellInfoList((List<CellInfo>) msg.obj);
                     mCellInfoList = new ArrayList<>(results);
@@ -268,7 +282,7 @@
         for (int index = 0; index < mCellInfoList.size(); index++) {
             if (!mCellInfoList.get(index).isRegistered()) {
                 NetworkOperatorPreference pref = new NetworkOperatorPreference(
-                        mCellInfoList.get(index), getContext(), mForbiddenPlmns, mShow4GForLTE);
+                        mCellInfoList.get(index), getPrefContext(), mForbiddenPlmns, mShow4GForLTE);
                 pref.setKey(CellInfoUtil.getNetworkTitle(mCellInfoList.get(index)));
                 pref.setOrder(index);
                 mPreferenceCategory.addPreference(pref);
@@ -304,7 +318,7 @@
             CellInfo cellInfo = CellInfoUtil.wrapCellInfoWithCellIdentity(cellIdentity);
             if (cellInfo != null) {
                 NetworkOperatorPreference pref = new NetworkOperatorPreference(
-                        cellInfo, getContext(), mForbiddenPlmns, mShow4GForLTE);
+                        cellInfo, getPrefContext(), mForbiddenPlmns, mShow4GForLTE);
                 pref.setTitle(mTelephonyManager.getNetworkOperatorName());
                 pref.setSummary(R.string.network_connected);
                 // Update the signal strength icon, since the default signalStrength value would be
@@ -343,7 +357,7 @@
     private void addConnectedNetworkOperatorPreference(CellInfo cellInfo) {
         mConnectedPreferenceCategory.removeAll();
         final NetworkOperatorPreference pref = new NetworkOperatorPreference(
-                cellInfo, getContext(), mForbiddenPlmns, mShow4GForLTE);
+                cellInfo, getPrefContext(), mForbiddenPlmns, mShow4GForLTE);
         pref.setSummary(R.string.network_connected);
         mConnectedPreferenceCategory.addPreference(pref);
         mConnectedPreferenceCategory.setVisible(true);
diff --git a/tests/robotests/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java b/tests/robotests/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java
index 3ebeba1..956b601 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/NetworkSelectSettingsTest.java
@@ -81,6 +81,9 @@
 
         mNetworkSelectSettings = spy(new NetworkSelectSettings());
         doReturn(mContext).when(mNetworkSelectSettings).getContext();
+        doReturn(mPreferenceManager).when(mNetworkSelectSettings).getPreferenceManager();
+        doReturn(mContext).when(mPreferenceManager).getContext();
+
         mNetworkSelectSettings.mTelephonyManager = mTelephonyManager;
         mNetworkSelectSettings.mConnectedPreferenceCategory = mConnectedPreferenceCategory;
         mNetworkSelectSettings.mPreferenceCategory = mPreferenceCategory;