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