Home | History | Annotate | Download | only in deviceinfo
      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.WifiManager;
     23 import android.text.TextUtils;
     24 
     25 import androidx.annotation.VisibleForTesting;
     26 import androidx.preference.Preference;
     27 import androidx.preference.PreferenceScreen;
     28 
     29 import com.android.settingslib.R;
     30 import com.android.settingslib.core.lifecycle.Lifecycle;
     31 
     32 /**
     33  * Preference controller for WIFI MAC address
     34  */
     35 public abstract class AbstractWifiMacAddressPreferenceController
     36         extends AbstractConnectivityPreferenceController {
     37 
     38     @VisibleForTesting
     39     static final String KEY_WIFI_MAC_ADDRESS = "wifi_mac_address";
     40     @VisibleForTesting
     41     static final int OFF = 0;
     42     @VisibleForTesting
     43     static final int ON = 1;
     44 
     45     private static final String[] CONNECTIVITY_INTENTS = {
     46             ConnectivityManager.CONNECTIVITY_ACTION,
     47             WifiManager.LINK_CONFIGURATION_CHANGED_ACTION,
     48             WifiManager.NETWORK_STATE_CHANGED_ACTION,
     49     };
     50 
     51     private Preference mWifiMacAddress;
     52     private final WifiManager mWifiManager;
     53 
     54     public AbstractWifiMacAddressPreferenceController(Context context, Lifecycle lifecycle) {
     55         super(context, lifecycle);
     56         mWifiManager = context.getSystemService(WifiManager.class);
     57     }
     58 
     59     @Override
     60     public boolean isAvailable() {
     61         return true;
     62     }
     63 
     64     @Override
     65     public String getPreferenceKey() {
     66         return KEY_WIFI_MAC_ADDRESS;
     67     }
     68 
     69     @Override
     70     public void displayPreference(PreferenceScreen screen) {
     71         super.displayPreference(screen);
     72         mWifiMacAddress = screen.findPreference(KEY_WIFI_MAC_ADDRESS);
     73         updateConnectivity();
     74     }
     75 
     76     @Override
     77     protected String[] getConnectivityIntents() {
     78         return CONNECTIVITY_INTENTS;
     79     }
     80 
     81     @SuppressLint("HardwareIds")
     82     @Override
     83     protected void updateConnectivity() {
     84         final String[] macAddresses = mWifiManager.getFactoryMacAddresses();
     85         String macAddress = null;
     86         if (macAddresses != null && macAddresses.length > 0) {
     87             macAddress = macAddresses[0];
     88         }
     89 
     90         if (TextUtils.isEmpty(macAddress)) {
     91             mWifiMacAddress.setSummary(R.string.status_unavailable);
     92         } else {
     93             mWifiMacAddress.setSummary(macAddress);
     94         }
     95     }
     96 }
     97