1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.deviceinfo; 18 19 import android.annotation.SuppressLint; 20 import android.content.Context; 21 import android.net.ConnectivityManager; 22 import android.net.wifi.WifiInfo; 23 import android.net.wifi.WifiManager; 24 import android.provider.Settings; 25 import android.support.annotation.VisibleForTesting; 26 import android.support.v7.preference.Preference; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.text.TextUtils; 29 30 import com.android.settingslib.R; 31 import com.android.settingslib.core.lifecycle.Lifecycle; 32 33 /** 34 * Preference controller for WIFI MAC address 35 */ 36 public abstract class AbstractWifiMacAddressPreferenceController 37 extends AbstractConnectivityPreferenceController { 38 39 @VisibleForTesting 40 static final String KEY_WIFI_MAC_ADDRESS = "wifi_mac_address"; 41 42 private static final String[] CONNECTIVITY_INTENTS = { 43 ConnectivityManager.CONNECTIVITY_ACTION, 44 WifiManager.LINK_CONFIGURATION_CHANGED_ACTION, 45 WifiManager.NETWORK_STATE_CHANGED_ACTION, 46 }; 47 48 private Preference mWifiMacAddress; 49 private final WifiManager mWifiManager; 50 AbstractWifiMacAddressPreferenceController(Context context, Lifecycle lifecycle)51 public AbstractWifiMacAddressPreferenceController(Context context, Lifecycle lifecycle) { 52 super(context, lifecycle); 53 mWifiManager = context.getSystemService(WifiManager.class); 54 } 55 56 @Override isAvailable()57 public boolean isAvailable() { 58 return true; 59 } 60 61 @Override getPreferenceKey()62 public String getPreferenceKey() { 63 return KEY_WIFI_MAC_ADDRESS; 64 } 65 66 @Override displayPreference(PreferenceScreen screen)67 public void displayPreference(PreferenceScreen screen) { 68 super.displayPreference(screen); 69 mWifiMacAddress = screen.findPreference(KEY_WIFI_MAC_ADDRESS); 70 updateConnectivity(); 71 } 72 73 @Override getConnectivityIntents()74 protected String[] getConnectivityIntents() { 75 return CONNECTIVITY_INTENTS; 76 } 77 78 @SuppressLint("HardwareIds") 79 @Override updateConnectivity()80 protected void updateConnectivity() { 81 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 82 final int macRandomizationMode = Settings.Global.getInt(mContext.getContentResolver(), 83 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 0); 84 final String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress(); 85 86 if (TextUtils.isEmpty(macAddress)) { 87 mWifiMacAddress.setSummary(R.string.status_unavailable); 88 } else if (macRandomizationMode == 1 && WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) { 89 mWifiMacAddress.setSummary(R.string.wifi_status_mac_randomized); 90 } else { 91 mWifiMacAddress.setSummary(macAddress); 92 } 93 } 94 } 95