Merge "Revert "Development Options: toggle binderized hals""
diff --git a/res/xml/development_prefs.xml b/res/xml/development_prefs.xml
index b655865..0c6932d 100644
--- a/res/xml/development_prefs.xml
+++ b/res/xml/development_prefs.xml
@@ -198,6 +198,13 @@
             android:summary="@string/bluetooth_disable_absolute_volume_summary"/>
 
         <ListPreference
+            android:key="bluetooth_select_avrcp_version"
+            android:title="@string/bluetooth_select_avrcp_version_string"
+            android:dialogTitle="@string/bluetooth_select_avrcp_version_dialog_title"
+            android:entries="@array/bluetooth_avrcp_versions"
+            android:entryValues="@array/bluetooth_avrcp_version_values" />
+
+        <ListPreference
             android:key="bluetooth_select_a2dp_codec"
             android:title="@string/bluetooth_select_a2dp_codec_type"
             android:dialogTitle="@string/bluetooth_select_a2dp_codec_type_dialog_title"
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java
index f074abc..06c3050 100644
--- a/src/com/android/settings/DevelopmentSettings.java
+++ b/src/com/android/settings/DevelopmentSettings.java
@@ -199,7 +199,10 @@
                                     "bluetooth_disable_absolute_volume";
     private static final String BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY =
                                     "persist.bluetooth.disableabsvol";
+    private static final String BLUETOOTH_AVRCP_VERSION_PROPERTY =
+                                    "persist.bluetooth.avrcpversion";
 
+    private static final String BLUETOOTH_SELECT_AVRCP_VERSION_KEY = "bluetooth_select_avrcp_version";
     private static final String BLUETOOTH_SELECT_A2DP_CODEC_KEY = "bluetooth_select_a2dp_codec";
     private static final String BLUETOOTH_SELECT_A2DP_SAMPLE_RATE_KEY = "bluetooth_select_a2dp_sample_rate";
     private static final String BLUETOOTH_SELECT_A2DP_BITS_PER_SAMPLE_KEY = "bluetooth_select_a2dp_bits_per_sample";
@@ -278,6 +281,7 @@
 
     private BluetoothA2dp mBluetoothA2dp;
     private final Object mBluetoothA2dpLock = new Object();
+    private ListPreference mBluetoothSelectAvrcpVersion;
     private ListPreference mBluetoothSelectA2dpCodec;
     private ListPreference mBluetoothSelectA2dpSampleRate;
     private ListPreference mBluetoothSelectA2dpBitsPerSample;
@@ -471,6 +475,7 @@
         mWebViewMultiprocess = findAndInitSwitchPref(WEBVIEW_MULTIPROCESS_KEY);
         mBluetoothDisableAbsVolume = findAndInitSwitchPref(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_KEY);
 
+        mBluetoothSelectAvrcpVersion = addListPreference(BLUETOOTH_SELECT_AVRCP_VERSION_KEY);
         mBluetoothSelectA2dpCodec = addListPreference(BLUETOOTH_SELECT_A2DP_CODEC_KEY);
         mBluetoothSelectA2dpSampleRate = addListPreference(BLUETOOTH_SELECT_A2DP_SAMPLE_RATE_KEY);
         mBluetoothSelectA2dpBitsPerSample = addListPreference(BLUETOOTH_SELECT_A2DP_BITS_PER_SAMPLE_KEY);
@@ -1764,6 +1769,14 @@
         String[] summaries;
         int index;
 
+        // Init the AVRCP Version - Default
+        values = getResources().getStringArray(R.array.bluetooth_avrcp_version_values);
+        summaries = getResources().getStringArray(R.array.bluetooth_avrcp_versions);
+        String value = SystemProperties.get(BLUETOOTH_AVRCP_VERSION_PROPERTY, values[0]);
+        index = mBluetoothSelectAvrcpVersion.findIndexOfValue(value);
+        mBluetoothSelectAvrcpVersion.setValue(values[index]);
+        mBluetoothSelectAvrcpVersion.setSummary(summaries[index]);
+
         // Init the Codec Type - Default
         values = getResources().getStringArray(R.array.bluetooth_a2dp_codec_values);
         summaries = getResources().getStringArray(R.array.bluetooth_a2dp_codec_summaries);
@@ -1800,6 +1813,15 @@
         mBluetoothSelectA2dpLdacPlaybackQuality.setSummary(summaries[index]);
     }
 
+    private void writeBluetoothAvrcpVersion(Object newValue) {
+        SystemProperties.set(BLUETOOTH_AVRCP_VERSION_PROPERTY, newValue.toString());
+        int index = mBluetoothSelectAvrcpVersion.findIndexOfValue(newValue.toString());
+        if (index >= 0) {
+            String[] titles = getResources().getStringArray(R.array.bluetooth_avrcp_versions);
+            mBluetoothSelectAvrcpVersion.setSummary(titles[index]);
+        }
+    }
+
     private void updateBluetoothA2dpConfigurationValues() {
         int index;
         String[] summaries;
@@ -2525,6 +2547,9 @@
                 toast.show();
             }
             return false;
+        } else if (preference == mBluetoothSelectAvrcpVersion) {
+           writeBluetoothAvrcpVersion(newValue);
+           return true;
         } else if ((preference == mBluetoothSelectA2dpCodec) ||
                    (preference == mBluetoothSelectA2dpSampleRate) ||
                    (preference == mBluetoothSelectA2dpBitsPerSample) ||
diff --git a/src/com/android/settings/vpn2/VpnSettings.java b/src/com/android/settings/vpn2/VpnSettings.java
index 0d968ab..29a67ab 100644
--- a/src/com/android/settings/vpn2/VpnSettings.java
+++ b/src/com/android/settings/vpn2/VpnSettings.java
@@ -19,6 +19,7 @@
 import android.annotation.UiThread;
 import android.annotation.WorkerThread;
 import android.app.AppOpsManager;
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageInfo;
@@ -208,9 +209,16 @@
     public boolean handleMessage(Message message) {
         mUpdater.removeMessages(RESCAN_MESSAGE);
 
+        //Return if activity has been recycled
+        final Activity activity = getActivity();
+        if (activity == null) {
+            return true;
+        }
+        final Context context = activity.getApplicationContext();
+
         // Run heavy RPCs before switching to UI thread
         final List<VpnProfile> vpnProfiles = loadVpnProfiles(mKeyStore);
-        final List<AppVpnInfo> vpnApps = getVpnApps(getActivity(), /* includeProfiles */ true);
+        final List<AppVpnInfo> vpnApps = getVpnApps(context, /* includeProfiles */ true);
 
         final Map<String, LegacyVpnInfo> connectedLegacyVpns = getConnectedLegacyVpns();
         final Set<AppVpnInfo> connectedAppVpns = getConnectedAppVpns();
@@ -219,7 +227,7 @@
         final String lockdownVpnKey = VpnUtils.getLockdownVpn();
 
         // Refresh list of VPNs
-        getActivity().runOnUiThread(new Runnable() {
+        activity.runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 // Can't do anything useful if the context has gone away
@@ -271,7 +279,6 @@
                 }
             }
         });
-
         mUpdater.sendEmptyMessageDelayed(RESCAN_MESSAGE, RESCAN_INTERVAL_MS);
         return true;
     }
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index dcf722b..aef5416 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -299,6 +299,8 @@
 
         // On/off switch is hidden for Setup Wizard (returns null)
         mWifiEnabler = createWifiEnabler();
+
+        mWifiTracker.startTracking();
     }
 
     /**
@@ -318,7 +320,6 @@
             mWifiEnabler.resume(activity);
         }
 
-        mWifiTracker.startTracking();
         activity.invalidateOptionsMenu();
     }
 
@@ -328,7 +329,11 @@
         if (mWifiEnabler != null) {
             mWifiEnabler.pause();
         }
+    }
 
+    @Override
+    public void onStop() {
+        super.onStop();
         mWifiTracker.stopTracking();
     }