• 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.settings.wifi;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.net.wifi.WifiInfo;
24 import android.net.wifi.WifiManager;
25 import android.provider.Settings;
26 import android.text.TextUtils;
27 
28 import androidx.core.text.BidiFormatter;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settings.Utils;
34 import com.android.settings.core.PreferenceControllerMixin;
35 import com.android.settingslib.core.AbstractPreferenceController;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 import com.android.settingslib.core.lifecycle.LifecycleObserver;
38 import com.android.settingslib.core.lifecycle.events.OnPause;
39 import com.android.settingslib.core.lifecycle.events.OnResume;
40 
41 /**
42  * {@link PreferenceControllerMixin} that updates MAC/IP address.
43  */
44 public class WifiInfoPreferenceController extends AbstractPreferenceController
45         implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
46 
47     private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
48     private static final String KEY_MAC_ADDRESS = "mac_address";
49 
50     private final IntentFilter mFilter;
51     private final WifiManager mWifiManager;
52 
53     private Preference mWifiMacAddressPref;
54     private Preference mWifiIpAddressPref;
55 
WifiInfoPreferenceController(Context context, Lifecycle lifecycle, WifiManager wifiManager)56     public WifiInfoPreferenceController(Context context, Lifecycle lifecycle,
57             WifiManager wifiManager) {
58         super(context);
59         mWifiManager = wifiManager;
60         mFilter = new IntentFilter();
61         mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
62         mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
63 
64         lifecycle.addObserver(this);
65     }
66 
67     @Override
isAvailable()68     public boolean isAvailable() {
69         return true;
70     }
71 
72     @Override
getPreferenceKey()73     public String getPreferenceKey() {
74         // Returns null because this controller contains more than 1 preference.
75         return null;
76     }
77 
78     @Override
displayPreference(PreferenceScreen screen)79     public void displayPreference(PreferenceScreen screen) {
80         super.displayPreference(screen);
81         mWifiMacAddressPref = screen.findPreference(KEY_MAC_ADDRESS);
82         mWifiMacAddressPref.setSelectable(false);
83         mWifiIpAddressPref = screen.findPreference(KEY_CURRENT_IP_ADDRESS);
84         mWifiIpAddressPref.setSelectable(false);
85     }
86 
87     @Override
onResume()88     public void onResume() {
89         mContext.registerReceiver(mReceiver, mFilter);
90         updateWifiInfo();
91     }
92 
93     @Override
onPause()94     public void onPause() {
95         mContext.unregisterReceiver(mReceiver);
96     }
97 
updateWifiInfo()98     public void updateWifiInfo() {
99         if (mWifiMacAddressPref != null) {
100             final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
101             final boolean macRandomizationSupported = mContext.getResources().getBoolean(
102                 com.android.internal.R.bool.config_wifi_connected_mac_randomization_supported);
103             final String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
104 
105             if (macRandomizationSupported && WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
106                 mWifiMacAddressPref.setSummary(R.string.wifi_status_mac_randomized);
107             } else if (TextUtils.isEmpty(macAddress)
108                     || WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
109                 mWifiMacAddressPref.setSummary(R.string.status_unavailable);
110             } else {
111                 mWifiMacAddressPref.setSummary(macAddress);
112             }
113         }
114         if (mWifiIpAddressPref != null) {
115             final String ipAddress = Utils.getWifiIpAddresses(mContext);
116             mWifiIpAddressPref.setSummary(ipAddress == null
117                     ? mContext.getString(R.string.status_unavailable)
118                     : BidiFormatter.getInstance().unicodeWrap(ipAddress));
119         }
120     }
121 
122     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
123         @Override
124         public void onReceive(Context context, Intent intent) {
125             String action = intent.getAction();
126             if (action.equals(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION) ||
127                     action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
128                 updateWifiInfo();
129             }
130         }
131     };
132 }
133