• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.text.TextUtils;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
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     @VisibleForTesting
42     static final int OFF = 0;
43     @VisibleForTesting
44     static final int ON = 1;
45 
46     private static final String[] CONNECTIVITY_INTENTS = {
47             ConnectivityManager.CONNECTIVITY_ACTION,
48             WifiManager.ACTION_LINK_CONFIGURATION_CHANGED,
49             WifiManager.NETWORK_STATE_CHANGED_ACTION,
50     };
51 
52     private Preference mWifiMacAddress;
53     private final WifiManager mWifiManager;
54 
AbstractWifiMacAddressPreferenceController(Context context, Lifecycle lifecycle)55     public AbstractWifiMacAddressPreferenceController(Context context, Lifecycle lifecycle) {
56         super(context, lifecycle);
57         mWifiManager = context.getSystemService(WifiManager.class);
58     }
59 
60     @Override
isAvailable()61     public boolean isAvailable() {
62         return mWifiManager != null;
63     }
64 
65     @Override
getPreferenceKey()66     public String getPreferenceKey() {
67         return KEY_WIFI_MAC_ADDRESS;
68     }
69 
70     @Override
displayPreference(PreferenceScreen screen)71     public void displayPreference(PreferenceScreen screen) {
72         super.displayPreference(screen);
73         mWifiMacAddress = screen.findPreference(KEY_WIFI_MAC_ADDRESS);
74         updateConnectivity();
75     }
76 
77     @Override
getConnectivityIntents()78     protected String[] getConnectivityIntents() {
79         return CONNECTIVITY_INTENTS;
80     }
81 
82     @SuppressLint("HardwareIds")
83     @Override
updateConnectivity()84     protected void updateConnectivity() {
85         if (mWifiManager == null || mWifiMacAddress == null) {
86             return;
87         }
88 
89         final String[] macAddresses = mWifiManager.getFactoryMacAddresses();
90         String macAddress = null;
91         if (macAddresses != null && macAddresses.length > 0) {
92             macAddress = macAddresses[0];
93         }
94 
95         if (TextUtils.isEmpty(macAddress) || macAddress.equals(WifiInfo.DEFAULT_MAC_ADDRESS)) {
96             mWifiMacAddress.setSummary(R.string.status_unavailable);
97         } else {
98             mWifiMacAddress.setSummary(macAddress);
99         }
100     }
101 }
102