Merge "Update "Play <APP> to" to "Play <APP> on"" into rvc-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d230d92..dc343a5 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -176,6 +176,8 @@
     <string name="bluetooth_empty_list_user_restricted">You don\u2019t have permission to change Bluetooth settings.</string>
     <!-- Title for bluetooth pairing item [CHAR LIMIT=60] -->
     <string name="bluetooth_pairing_pref_title">Pair new device</string>
+    <!-- Keywords for bluetooth pairing item [CHAR LIMIT=30] -->
+    <string name="keywords_add_bt_device">bluetooth</string>
 
     <!-- Bluetooth Visibility message.  This message informs the user that their device is now visible to other bluetooth devices.  [CHAR LIMIT=NONE] -->
     <string name="bluetooth_is_visible_message"><xliff:g id="device_name">%1$s</xliff:g> is visible to nearby devices while Bluetooth settings is open.</string>
diff --git a/res/xml/connected_devices.xml b/res/xml/connected_devices.xml
index a1736b2..b2794c2 100644
--- a/res/xml/connected_devices.xml
+++ b/res/xml/connected_devices.xml
@@ -43,6 +43,7 @@
         android:summary="@string/connected_device_add_device_summary"
         android:fragment="com.android.settings.bluetooth.BluetoothPairingDetail"
         settings:allowDividerAbove="true"
+        settings:keywords="@string/keywords_add_bt_device"
         settings:userRestriction="no_config_bluetooth"
         settings:useAdminDisabledSummary="true"
         settings:controller="com.android.settings.connecteddevice.AddDevicePreferenceController"/>
diff --git a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
index a063327..a86b441 100644
--- a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
+++ b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
@@ -63,6 +63,7 @@
         LifecycleObserver, OnStart, OnStop, OnDestroy, CachedBluetoothDevice.Callback {
     private static final String TAG = "AdvancedBtHeaderCtrl";
     private static final int LOW_BATTERY_LEVEL = 15;
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
 
     @VisibleForTesting
     LayoutPreference mLayoutPreference;
@@ -215,6 +216,11 @@
 
         final int batteryLevel = BluetoothUtils.getIntMetaData(bluetoothDevice, batteryMetaKey);
         final boolean charging = BluetoothUtils.getBooleanMetaData(bluetoothDevice, chargeMetaKey);
+        if (DBG) {
+            Log.d(TAG, "updateSubLayout() icon : " + iconMetaKey + ", battery : " + batteryMetaKey
+                    + ", charge : " + chargeMetaKey + ", batteryLevel : " + batteryLevel
+                    + ", charging : " + charging + ", iconUri : " + iconUri);
+        }
         if (batteryLevel != BluetoothUtils.META_INT_ERROR) {
             linearLayout.setVisibility(View.VISIBLE);
             final TextView textView = linearLayout.findViewById(R.id.bt_battery_summary);
@@ -268,6 +274,9 @@
         final BluetoothDevice bluetoothDevice = mCachedDevice.getDevice();
         final String iconUri = BluetoothUtils.getStringMetaData(bluetoothDevice,
                 BluetoothDevice.METADATA_MAIN_ICON);
+        if (DBG) {
+            Log.d(TAG, "updateDisconnectLayout() iconUri : " + iconUri);
+        }
         if (iconUri != null) {
             final ImageView imageView = linearLayout.findViewById(R.id.header_icon);
             updateIcon(imageView, iconUri);
diff --git a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
index aa4f869..27d63bf 100644
--- a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
@@ -33,7 +33,7 @@
         implements Preference.OnPreferenceClickListener {
 
     private static final String TAG = "AvailableMediaBluetoothDeviceUpdater";
-    private static final boolean DBG = false;
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
 
     private static final String PREF_KEY = "available_media_bt";
 
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
index 21312a5..8cb698f 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
@@ -52,7 +52,7 @@
 public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
         LocalBluetoothProfileManager.ServiceListener {
     private static final String TAG = "BluetoothDeviceUpdater";
-    private static final boolean DBG = false;
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
 
     protected final MetricsFeatureProvider mMetricsFeatureProvider;
     protected final DevicePreferenceCallback mDevicePreferenceCallback;
diff --git a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
index c7cae7f..fc1b9b7 100644
--- a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
@@ -33,7 +33,7 @@
 public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
 
     private static final String TAG = "ConnBluetoothDeviceUpdater";
-    private static final boolean DBG = false;
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
 
     private static final String PREF_KEY = "connected_bt";
 
diff --git a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java
index bbcd13c..466d60e 100644
--- a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java
@@ -38,7 +38,7 @@
         implements Preference.OnPreferenceClickListener {
 
     private static final String TAG = "SavedBluetoothDeviceUpdater";
-    private static final boolean DBG = false;
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
 
     private static final String PREF_KEY = "saved_bt";
 
diff --git a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
index b417c3d..4178f6c 100644
--- a/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
+++ b/src/com/android/settings/datausage/DataUsageSummaryPreferenceController.java
@@ -47,8 +47,10 @@
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnStart;
 import com.android.settingslib.net.DataUsageController;
+import com.android.settingslib.utils.ThreadUtils;
 
 import java.util.List;
+import java.util.concurrent.Future;
 
 /**
  * This is the controller for a data usage header that retrieves carrier data from the new
@@ -99,6 +101,8 @@
 
     private Intent mManageSubscriptionIntent;
 
+    private Future<Long> mHistoricalUsageLevel;
+
     public DataUsageSummaryPreferenceController(Activity activity,
             Lifecycle lifecycle, PreferenceFragmentCompat fragment, int subscriptionId) {
         super(activity, KEY);
@@ -206,13 +210,13 @@
             updateConfiguration(mContext, mSubId, subInfo);
         }
 
+        mHistoricalUsageLevel = ThreadUtils.postOnBackgroundThread(() ->
+                mDataUsageController.getHistoricalUsageLevel(mDefaultTemplate));
+
         final DataUsageController.DataUsageInfo info =
                 mDataUsageController.getDataUsageInfo(mDefaultTemplate);
 
         long usageLevel = info.usageLevel;
-        if (usageLevel <= 0L) {
-            usageLevel = mDataUsageController.getHistoricalUsageLevel(mDefaultTemplate);
-        }
 
         if (subInfo != null) {
             mDataInfoController.updateDataLimit(info, mPolicyEditor.getPolicy(mDefaultTemplate));
@@ -222,7 +226,7 @@
             summaryPreference.setWifiMode(/* isWifiMode */ true, /* usagePeriod */
                     info.period, /* isSingleWifi */ false);
             summaryPreference.setLimitInfo(null);
-            summaryPreference.setUsageNumbers(usageLevel,
+            summaryPreference.setUsageNumbers(displayUsageLevel(usageLevel),
                     /* dataPlanSize */ -1L,
                     /* hasMobileData */ true);
             summaryPreference.setChartEnabled(false);
@@ -235,11 +239,6 @@
         }
 
         refreshDataplanInfo(info, subInfo);
-        if ((mDataplanUse <= 0L) && (mSnapshotTime < 0)) {
-            Log.d(TAG, "Display data usage from history");
-            mDataplanUse = usageLevel;
-            mSnapshotTime = -1L;
-        }
 
         if (info.warningLevel > 0 && info.limitLevel > 0) {
             summaryPreference.setLimitInfo(TextUtils.expandTemplate(
@@ -258,6 +257,12 @@
             summaryPreference.setLimitInfo(null);
         }
 
+        if ((mDataplanUse <= 0L) && (mSnapshotTime < 0)) {
+            Log.d(TAG, "Display data usage from history");
+            mDataplanUse = displayUsageLevel(usageLevel);
+            mSnapshotTime = -1L;
+        }
+
         summaryPreference.setUsageNumbers(mDataplanUse, mDataplanSize, mHasMobileData);
 
         if (mDataBarSize <= 0) {
@@ -272,6 +277,17 @@
                 mDataplanCount, mManageSubscriptionIntent);
     }
 
+    private long displayUsageLevel(long usageLevel) {
+        if (usageLevel > 0) {
+            return usageLevel;
+        }
+        try {
+            usageLevel = mHistoricalUsageLevel.get();
+        } catch (Exception ex) {
+        }
+        return usageLevel;
+    }
+
     // TODO(b/70950124) add test for this method once the robolectric shadow run script is
     // completed (b/3526807)
     private void refreshDataplanInfo(DataUsageController.DataUsageInfo info,
diff --git a/src/com/android/settings/network/telephony/ApnPreferenceController.java b/src/com/android/settings/network/telephony/ApnPreferenceController.java
index dd68129..8442de2 100644
--- a/src/com/android/settings/network/telephony/ApnPreferenceController.java
+++ b/src/com/android/settings/network/telephony/ApnPreferenceController.java
@@ -93,6 +93,9 @@
     @Override
     public void updateState(Preference preference) {
         super.updateState(preference);
+        if (mPreference == null) {
+            return;
+        }
         ((RestrictedPreference) mPreference).setDisabledByAdmin(
                 MobileNetworkUtils.isDpcApnEnforced(mContext)
                         ? RestrictedLockUtilsInternal.getDeviceOwner(mContext)
diff --git a/src/com/android/settings/network/telephony/DataUsagePreferenceController.java b/src/com/android/settings/network/telephony/DataUsagePreferenceController.java
index 035a8c1..08524d6 100644
--- a/src/com/android/settings/network/telephony/DataUsagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/DataUsagePreferenceController.java
@@ -45,6 +45,7 @@
 
     private Future<NetworkTemplate> mTemplateFuture;
     private AtomicReference<NetworkTemplate> mTemplate;
+    private Future<Long> mHistoricalUsageLevel;
 
     public DataUsagePreferenceController(Context context, String key) {
         super(context, key);
@@ -127,11 +128,17 @@
         final DataUsageController controller = new DataUsageController(context);
         controller.setSubscriptionId(subId);
 
+        mHistoricalUsageLevel = ThreadUtils.postOnBackgroundThread(() ->
+                controller.getHistoricalUsageLevel(getNetworkTemplate()));
+
         final DataUsageController.DataUsageInfo usageInfo = getDataUsageInfo(controller);
 
         long usageLevel = usageInfo.usageLevel;
         if (usageLevel <= 0L) {
-            usageLevel = controller.getHistoricalUsageLevel(getNetworkTemplate());
+            try {
+                usageLevel = mHistoricalUsageLevel.get();
+            } catch (Exception exception) {
+            }
         }
         if (usageLevel <= 0L) {
             return null;
diff --git a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
index 650890e..779802a 100644
--- a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
+++ b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java
@@ -35,7 +35,6 @@
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
-import com.android.settings.core.BasePreferenceController;
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.network.SubscriptionsChangeListener;
 
@@ -47,8 +46,8 @@
  * what mobile network subscription is used by default for some service controlled by the
  * SubscriptionManager. This can be used for services such as Calls or SMS.
  */
-public abstract class DefaultSubscriptionController extends BasePreferenceController implements
-        LifecycleObserver, Preference.OnPreferenceChangeListener,
+public abstract class DefaultSubscriptionController extends TelephonyBasePreferenceController
+        implements LifecycleObserver, Preference.OnPreferenceChangeListener,
         SubscriptionsChangeListener.SubscriptionsChangeListenerClient {
     private static final String TAG = "DefaultSubController";
 
@@ -85,7 +84,7 @@
     protected abstract void setDefaultSubscription(int subscriptionId);
 
     @Override
-    public int getAvailabilityStatus() {
+    public int getAvailabilityStatus(int subId) {
         final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(mManager);
         if (subs.size() > 1) {
             return AVAILABLE;
diff --git a/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java b/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java
index f5bcce7..d14c8d0 100644
--- a/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java
+++ b/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java
@@ -20,30 +20,37 @@
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 
-import com.android.settings.core.BasePreferenceController;
 import com.android.settings.network.SubscriptionUtil;
 
-public class DisableSimFooterPreferenceController extends BasePreferenceController {
-    private int mSubId;
+/**
+ * Shows information about disable a physical SIM.
+ */
+public class DisableSimFooterPreferenceController extends TelephonyBasePreferenceController {
 
+    /**
+     * Constructor
+     */
     public DisableSimFooterPreferenceController(Context context, String preferenceKey) {
         super(context, preferenceKey);
-        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     }
 
+    /**
+     * re-init for SIM based on given subscription ID.
+     * @param subId is the given subscription ID
+     */
     public void init(int subId) {
         mSubId = subId;
     }
 
     @Override
-    public int getAvailabilityStatus() {
-        if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+    public int getAvailabilityStatus(int subId) {
+        if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             return CONDITIONALLY_UNAVAILABLE;
         }
 
         SubscriptionManager subManager = mContext.getSystemService(SubscriptionManager.class);
         for (SubscriptionInfo info : SubscriptionUtil.getAvailableSubscriptions(mContext)) {
-            if (info.getSubscriptionId() == mSubId) {
+            if (info.getSubscriptionId() == subId) {
                 if (info.isEmbedded() || SubscriptionUtil.showToggleForPhysicalSim(subManager)) {
                     return CONDITIONALLY_UNAVAILABLE;
                 }
diff --git a/src/com/android/settings/network/telephony/NetworkSelectSettings.java b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
index 584848f..7877063 100644
--- a/src/com/android/settings/network/telephony/NetworkSelectSettings.java
+++ b/src/com/android/settings/network/telephony/NetworkSelectSettings.java
@@ -217,16 +217,13 @@
             switch (msg.what) {
                 case EVENT_SET_NETWORK_SELECTION_MANUALLY_DONE:
                     final boolean isSucceed = (boolean) msg.obj;
-                    if (isSucceed) {
-                        // Don't enable screen here. Wait until result of network re-scan.
-                        startNetworkQuery();
-                    } else {
-                        stopNetworkQuery();
-                        setProgressBarVisible(false);
-                        getPreferenceScreen().setEnabled(true);
-                        // For failure case, only update the summary of selected item.
-                        mSelectedPreference.setSummary(R.string.network_could_not_connect);
-                    }
+                    stopNetworkQuery();
+                    setProgressBarVisible(false);
+                    getPreferenceScreen().setEnabled(true);
+
+                    mSelectedPreference.setSummary(isSucceed
+                            ? R.string.network_connected
+                            : R.string.network_could_not_connect);
                     break;
                 case EVENT_NETWORK_SCAN_RESULTS:
                     final List<CellInfo> results = (List<CellInfo>) msg.obj;
@@ -418,6 +415,7 @@
                     // (it would be quite confusing why the connected network has no signal)
                     pref.setIcon(SignalStrength.NUM_SIGNAL_STRENGTH_BINS - 1);
                     mPreferenceCategory.addPreference(pref);
+                    break;
                 }
             }
         }