• 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 true;
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         if (isAvailable()) {
74             mWifiMacAddress = screen.findPreference(KEY_WIFI_MAC_ADDRESS);
75             updateConnectivity();
76         }
77     }
78 
79     @Override
getConnectivityIntents()80     protected String[] getConnectivityIntents() {
81         return CONNECTIVITY_INTENTS;
82     }
83 
84     @SuppressLint("HardwareIds")
85     @Override
updateConnectivity()86     protected void updateConnectivity() {
87         final String[] macAddresses = mWifiManager.getFactoryMacAddresses();
88         String macAddress = null;
89         if (macAddresses != null && macAddresses.length > 0) {
90             macAddress = macAddresses[0];
91         }
92 
93         if (TextUtils.isEmpty(macAddress) || macAddress.equals(WifiInfo.DEFAULT_MAC_ADDRESS)) {
94             mWifiMacAddress.setSummary(R.string.status_unavailable);
95         } else {
96             mWifiMacAddress.setSummary(macAddress);
97         }
98     }
99 }
100