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.settingslib.wifi;
     18 
     19 import android.content.Context;
     20 import android.net.ConnectivityManager;
     21 import android.net.NetworkInfo;
     22 import android.net.wifi.WifiConfiguration;
     23 import android.net.wifi.WifiInfo;
     24 import android.os.Bundle;
     25 
     26 /**
     27 * Build and return a valid AccessPoint.
     28 *
     29 * Only intended for testing the AccessPoint class;
     30 * AccessPoints were designed to only be populated
     31 * by the mechanisms of scan results and wifi configurations.
     32 */
     33 public class TestAccessPointBuilder {
     34     // match the private values in WifiManager
     35     private static final int MIN_RSSI = -100;
     36     private static final int MAX_RSSI = -55;
     37 
     38     // set some sensible defaults
     39     private int mRssi = AccessPoint.UNREACHABLE_RSSI;
     40     private int mNetworkId = WifiConfiguration.INVALID_NETWORK_ID;
     41     private String ssid = "TestSsid";
     42     private NetworkInfo mNetworkInfo = null;
     43     private String mFqdn = null;
     44     private String mProviderFriendlyName = null;
     45     private WifiConfiguration mWifiConfig;
     46     private WifiInfo mWifiInfo;
     47 
     48     Context mContext;
     49 
     50     public TestAccessPointBuilder(Context context) {
     51         mContext = context;
     52     }
     53 
     54     public AccessPoint build() {
     55         Bundle bundle = new Bundle();
     56 
     57         WifiConfiguration wifiConfig = new WifiConfiguration();
     58         wifiConfig.networkId = mNetworkId;
     59 
     60         bundle.putString(AccessPoint.KEY_SSID, ssid);
     61         bundle.putParcelable(AccessPoint.KEY_CONFIG, wifiConfig);
     62         bundle.putParcelable(AccessPoint.KEY_NETWORKINFO, mNetworkInfo);
     63         bundle.putParcelable(AccessPoint.KEY_WIFIINFO, mWifiInfo);
     64         if (mFqdn != null) {
     65             bundle.putString(AccessPoint.KEY_FQDN, mFqdn);
     66         }
     67         if (mProviderFriendlyName != null) {
     68             bundle.putString(AccessPoint.KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName);
     69         }
     70         AccessPoint ap = new AccessPoint(mContext, bundle);
     71         ap.setRssi(mRssi);
     72         return ap;
     73     }
     74 
     75     public TestAccessPointBuilder setActive(boolean active) {
     76         if (active) {
     77             mNetworkInfo = new NetworkInfo(
     78                 ConnectivityManager.TYPE_DUMMY,
     79                 ConnectivityManager.TYPE_DUMMY,
     80                 "TestNetwork",
     81                 "TestNetwork");
     82         } else {
     83             mNetworkInfo = null;
     84         }
     85         return this;
     86     }
     87 
     88     /**
     89      * Set the rssi based upon the desired signal level.
     90      *
     91      * <p>Side effect: if this AccessPoint was previously unreachable,
     92      * setting the level will also make it reachable.
     93      */
     94     public TestAccessPointBuilder setLevel(int level) {
     95         // Reversal of WifiManager.calculateSignalLevels
     96         if (level == 0) {
     97             mRssi = MIN_RSSI;
     98         } else if (level >= AccessPoint.SIGNAL_LEVELS) {
     99             mRssi = MAX_RSSI;
    100         } else {
    101             float inputRange = MAX_RSSI - MIN_RSSI;
    102             float outputRange = AccessPoint.SIGNAL_LEVELS - 1;
    103             mRssi = (int) (level * inputRange / outputRange + MIN_RSSI);
    104         }
    105         return this;
    106     }
    107 
    108     public TestAccessPointBuilder setNetworkInfo(NetworkInfo info) {
    109         mNetworkInfo = info;
    110         return this;
    111     }
    112 
    113     public TestAccessPointBuilder setRssi(int rssi) {
    114         mRssi = rssi;
    115         return this;
    116     }
    117 
    118     /**
    119     * Set whether the AccessPoint is reachable.
    120     * Side effect: if the signal level was not previously set,
    121     * making an AccessPoint reachable will set the signal to the minimum level.
    122     */
    123     public TestAccessPointBuilder setReachable(boolean reachable) {
    124         if (reachable) {
    125             // only override the mRssi if it hasn't been set yet
    126             if (mRssi == AccessPoint.UNREACHABLE_RSSI) {
    127                 mRssi = MIN_RSSI;
    128             }
    129         } else {
    130             mRssi = AccessPoint.UNREACHABLE_RSSI;
    131         }
    132         return this;
    133     }
    134 
    135     public TestAccessPointBuilder setSaved(boolean saved){
    136         if (saved) {
    137              mNetworkId = 1;
    138         } else {
    139              mNetworkId = WifiConfiguration.INVALID_NETWORK_ID;
    140         }
    141         return this;
    142     }
    143 
    144     public TestAccessPointBuilder setSsid(String newSsid) {
    145         ssid = newSsid;
    146         return this;
    147     }
    148 
    149     public TestAccessPointBuilder setFqdn(String fqdn) {
    150         mFqdn = fqdn;
    151         return this;
    152     }
    153 
    154     public TestAccessPointBuilder setProviderFriendlyName(String friendlyName) {
    155         mProviderFriendlyName = friendlyName;
    156         return this;
    157     }
    158 
    159     public TestAccessPointBuilder setWifiInfo(WifiInfo info) {
    160         mWifiInfo = info;
    161         return this;
    162     }
    163 
    164     /**
    165      * Set the networkId in the WifiConfig.
    166      *
    167      * <p>Setting this to a value other than {@link WifiConfiguration#INVALID_NETWORK_ID} makes this
    168      * AccessPoint a saved network.
    169      */
    170     public TestAccessPointBuilder setNetworkId(int networkId) {
    171         mNetworkId = networkId;
    172         return this;
    173     }
    174 }
    175