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