Home | History | Annotate | Download | only in deviceinfo
      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.content.Context;
     20 import android.net.ConnectivityManager;
     21 import android.net.LinkProperties;
     22 import android.net.wifi.WifiManager;
     23 import android.support.annotation.VisibleForTesting;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v7.preference.PreferenceScreen;
     26 
     27 import com.android.settingslib.R;
     28 import com.android.settingslib.core.lifecycle.Lifecycle;
     29 
     30 import java.net.InetAddress;
     31 import java.util.Iterator;
     32 
     33 /**
     34  * Preference controller for IP address
     35  */
     36 public abstract class AbstractIpAddressPreferenceController
     37         extends AbstractConnectivityPreferenceController {
     38 
     39     @VisibleForTesting
     40     static final String KEY_IP_ADDRESS = "wifi_ip_address";
     41 
     42     private static final String[] CONNECTIVITY_INTENTS = {
     43             ConnectivityManager.CONNECTIVITY_ACTION,
     44             WifiManager.LINK_CONFIGURATION_CHANGED_ACTION,
     45             WifiManager.NETWORK_STATE_CHANGED_ACTION,
     46     };
     47 
     48     private Preference mIpAddress;
     49     private final ConnectivityManager mCM;
     50 
     51     public AbstractIpAddressPreferenceController(Context context, Lifecycle lifecycle) {
     52         super(context, lifecycle);
     53         mCM = context.getSystemService(ConnectivityManager.class);
     54     }
     55 
     56     @Override
     57     public boolean isAvailable() {
     58         return true;
     59     }
     60 
     61     @Override
     62     public String getPreferenceKey() {
     63         return KEY_IP_ADDRESS;
     64     }
     65 
     66     @Override
     67     public void displayPreference(PreferenceScreen screen) {
     68         super.displayPreference(screen);
     69         mIpAddress = screen.findPreference(KEY_IP_ADDRESS);
     70         updateConnectivity();
     71     }
     72 
     73     @Override
     74     protected String[] getConnectivityIntents() {
     75         return CONNECTIVITY_INTENTS;
     76     }
     77 
     78     @Override
     79     protected void updateConnectivity() {
     80         String ipAddress = getDefaultIpAddresses(mCM);
     81         if (ipAddress != null) {
     82             mIpAddress.setSummary(ipAddress);
     83         } else {
     84             mIpAddress.setSummary(R.string.status_unavailable);
     85         }
     86     }
     87 
     88     /**
     89      * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
     90      * addresses.
     91      * @param cm ConnectivityManager
     92      * @return the formatted and newline-separated IP addresses, or null if none.
     93      */
     94     private static String getDefaultIpAddresses(ConnectivityManager cm) {
     95         LinkProperties prop = cm.getActiveLinkProperties();
     96         return formatIpAddresses(prop);
     97     }
     98 
     99     private static String formatIpAddresses(LinkProperties prop) {
    100         if (prop == null) return null;
    101         Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
    102         // If there are no entries, return null
    103         if (!iter.hasNext()) return null;
    104         // Concatenate all available addresses, newline separated
    105         StringBuilder addresses = new StringBuilder();
    106         while (iter.hasNext()) {
    107             addresses.append(iter.next().getHostAddress());
    108             if (iter.hasNext()) addresses.append("\n");
    109         }
    110         return addresses.toString();
    111     }
    112 }
    113