From e84a1e030313cbc2875c9d3ee1f016808d33dab3 Mon Sep 17 00:00:00 2001
From: Tad <tad@spotco.us>
Date: Mon, 18 Apr 2016 08:53:03 -0400
Subject: [PATCH] Implement MAC Randomization

Change-Id: Id074d011eac7c33af267de4c1649670c32770cb5
---
 res/values/cm_strings.xml                      |  4 ++++
 res/xml/wireless_settings.xml                  |  6 ++++++
 src/com/android/settings/WirelessSettings.java | 24 +++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
index 128660e..8aa4785 100644
--- a/res/values/cm_strings.xml
+++ b/res/values/cm_strings.xml
@@ -1187,4 +1187,8 @@
 
     <!-- Label for settings shortcut: carrier selection -->
     <string name="shortcut_carrier_title">Network operators</string>
+
+    <!-- randomize MAC addresses -->
+    <string name="randomize_mac_title">Randomize MAC addresses</string>
+    <string name="randomize_mac_summary">Assign random MAC addresses when network devices are enabled</string>
 </resources>
diff --git a/res/xml/wireless_settings.xml b/res/xml/wireless_settings.xml
index 8882003..4a2d5d0 100644
--- a/res/xml/wireless_settings.xml
+++ b/res/xml/wireless_settings.xml
@@ -19,6 +19,12 @@
         android:title="@string/radio_controls_title">
 
     <SwitchPreference
+        android:key="randomize_mac"
+        android:title="@string/randomize_mac_title"
+        android:summary="@string/randomize_mac_summary"
+        android:persistent="false" />
+
+    <SwitchPreference
         android:key="toggle_airplane"
         android:title="@string/airplane_mode"
         android:persistent="false"
diff --git a/src/com/android/settings/WirelessSettings.java b/src/com/android/settings/WirelessSettings.java
index 4a0c9e3..4432940 100644
--- a/src/com/android/settings/WirelessSettings.java
+++ b/src/com/android/settings/WirelessSettings.java
@@ -37,6 +37,7 @@ import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.preference.Preference;
+import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.PreferenceCategory;
 import android.preference.PreferenceScreen;
 import android.preference.SwitchPreference;
@@ -58,9 +59,11 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
-public class WirelessSettings extends SettingsPreferenceFragment implements Indexable {
+public class WirelessSettings extends SettingsPreferenceFragment
+        implements OnPreferenceChangeListener, Indexable {
     private static final String TAG = "WirelessSettings";
 
+    private static final String KEY_RANDOMIZE_MAC = "randomize_mac";
     private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
     private static final String KEY_NFC_CATEGORY_SETTINGS = "nfc_category_settings";
     private static final String KEY_TOGGLE_NFC = "toggle_nfc";
@@ -76,9 +79,12 @@ public class WirelessSettings extends SettingsPreferenceFragment implements Inde
     private static final String KEY_WFC_SETTINGS = "wifi_calling_settings";
     private static final String KEY_NFC_PAYMENT_SETTINGS = "nfc_payment_settings";
 
+    private static final String RANDOMIZE_PERSIST_PROP = "persist.privacy.randomize_mac";
+
     public static final String EXIT_ECM_RESULT = "exit_ecm_result";
     public static final int REQUEST_CODE_EXIT_ECM = 1;
 
+    private SwitchPreference mRandomizeMac;
     private AirplaneModeEnabler mAirplaneModeEnabler;
     private SwitchPreference mAirplaneModePreference;
     private NfcEnabler mNfcEnabler;
@@ -232,6 +238,8 @@ public class WirelessSettings extends SettingsPreferenceFragment implements Inde
         final boolean isSecondaryUser = myUserId != UserHandle.USER_OWNER;
 
         final Activity activity = getActivity();
+        mRandomizeMac = (SwitchPreference) findPreference(KEY_RANDOMIZE_MAC);
+        mRandomizeMac.setOnPreferenceChangeListener(this);
         mAirplaneModePreference = (SwitchPreference) findPreference(KEY_TOGGLE_AIRPLANE);
         SwitchPreference nfc = (SwitchPreference) findPreference(KEY_TOGGLE_NFC);
         PreferenceScreen androidBeam = (PreferenceScreen) findPreference(KEY_ANDROID_BEAM_SETTINGS);
@@ -370,6 +378,10 @@ public class WirelessSettings extends SettingsPreferenceFragment implements Inde
     public void onResume() {
         super.onResume();
 
+        if (mRandomizeMac != null) {
+            mRandomizeMac.setChecked(SystemProperties.getBoolean(RANDOMIZE_PERSIST_PROP, true));
+        }
+
         mAirplaneModeEnabler.resume();
         if (mNfcEnabler != null) {
             mNfcEnabler.resume();
@@ -390,6 +402,16 @@ public class WirelessSettings extends SettingsPreferenceFragment implements Inde
         }
     }
 
+     @Override
+     public boolean onPreferenceChange(Preference preference, Object newValue) {
+         if (preference == mRandomizeMac) {
+             SystemProperties.set(RANDOMIZE_PERSIST_PROP, (Boolean) newValue ? "1" : "0");
+             return true;
+         }
+
+         return false;
+     }
+
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
-- 
2.8.0

