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.ScanResult;
23 import android.net.wifi.WifiConfiguration;
24 import android.net.wifi.WifiInfo;
25 import android.os.Bundle;
26 import android.os.Parcelable;
27 import android.support.annotation.Keep;
28 
29 import com.android.settingslib.wifi.AccessPoint.Speed;
30 
31 import java.util.ArrayList;
32 
33 /**
34 * Build and return a valid AccessPoint.
35 *
36 * Only intended for testing the AccessPoint class or creating Access points to be used in testing
37 * applications. AccessPoints were designed to only be populated by the mechanisms of scan results
38 * and wifi configurations.
39 */
40 @Keep
41 public class TestAccessPointBuilder {
42     // match the private values in WifiManager
43     private static final int MIN_RSSI = -100;
44     private static final int MAX_RSSI = -55;
45 
46     // set some sensible defaults
47     private String mBssid = null;
48     private int mSpeed = Speed.NONE;
49     private int mRssi = AccessPoint.UNREACHABLE_RSSI;
50     private int mNetworkId = WifiConfiguration.INVALID_NETWORK_ID;
51     private String ssid = "TestSsid";
52     private NetworkInfo mNetworkInfo = null;
53     private String mFqdn = null;
54     private String mProviderFriendlyName = null;
55     private int mSecurity = AccessPoint.SECURITY_NONE;
56     private WifiConfiguration mWifiConfig;
57     private WifiInfo mWifiInfo;
58     private boolean mIsCarrierAp = false;
59     private String mCarrierName = null;
60 
61     Context mContext;
62     private ArrayList<ScanResult> mScanResults;
63     private ArrayList<TimestampedScoredNetwork> mScoredNetworkCache;
64 
65     @Keep
TestAccessPointBuilder(Context context)66     public TestAccessPointBuilder(Context context) {
67         mContext = context;
68     }
69 
70     @Keep
build()71     public AccessPoint build() {
72         Bundle bundle = new Bundle();
73 
74         WifiConfiguration wifiConfig = new WifiConfiguration();
75         wifiConfig.networkId = mNetworkId;
76         wifiConfig.BSSID = mBssid;
77 
78         bundle.putString(AccessPoint.KEY_SSID, ssid);
79         bundle.putParcelable(AccessPoint.KEY_CONFIG, wifiConfig);
80         bundle.putParcelable(AccessPoint.KEY_NETWORKINFO, mNetworkInfo);
81         bundle.putParcelable(AccessPoint.KEY_WIFIINFO, mWifiInfo);
82         if (mFqdn != null) {
83             bundle.putString(AccessPoint.KEY_FQDN, mFqdn);
84         }
85         if (mProviderFriendlyName != null) {
86             bundle.putString(AccessPoint.KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName);
87         }
88         if (mScanResults != null) {
89             bundle.putParcelableArray(AccessPoint.KEY_SCANRESULTS,
90                     mScanResults.toArray(new Parcelable[mScanResults.size()]));
91         }
92         if (mScoredNetworkCache != null) {
93             bundle.putParcelableArrayList(AccessPoint.KEY_SCOREDNETWORKCACHE, mScoredNetworkCache);
94         }
95         bundle.putInt(AccessPoint.KEY_SECURITY, mSecurity);
96         bundle.putInt(AccessPoint.KEY_SPEED, mSpeed);
97         bundle.putBoolean(AccessPoint.KEY_IS_CARRIER_AP, mIsCarrierAp);
98         if (mCarrierName != null) {
99             bundle.putString(AccessPoint.KEY_CARRIER_NAME, mCarrierName);
100         }
101 
102         AccessPoint ap = new AccessPoint(mContext, bundle);
103         ap.setRssi(mRssi);
104         return ap;
105     }
106 
107     @Keep
setActive(boolean active)108     public TestAccessPointBuilder setActive(boolean active) {
109         if (active) {
110             mNetworkInfo = new NetworkInfo(
111                 ConnectivityManager.TYPE_DUMMY,
112                 ConnectivityManager.TYPE_DUMMY,
113                 "TestNetwork",
114                 "TestNetwork");
115         } else {
116             mNetworkInfo = null;
117         }
118         return this;
119     }
120 
121     /**
122      * Set the rssi based upon the desired signal level.
123      *
124      * <p>Side effect: if this AccessPoint was previously unreachable,
125      * setting the level will also make it reachable.
126      */
127     @Keep
setLevel(int level)128     public TestAccessPointBuilder setLevel(int level) {
129         // Reversal of WifiManager.calculateSignalLevels
130         if (level == 0) {
131             mRssi = MIN_RSSI;
132         } else if (level >= AccessPoint.SIGNAL_LEVELS) {
133             mRssi = MAX_RSSI;
134         } else {
135             float inputRange = MAX_RSSI - MIN_RSSI;
136             float outputRange = AccessPoint.SIGNAL_LEVELS - 1;
137             mRssi = (int) (level * inputRange / outputRange + MIN_RSSI);
138         }
139         return this;
140     }
141 
142     @Keep
setNetworkInfo(NetworkInfo info)143     public TestAccessPointBuilder setNetworkInfo(NetworkInfo info) {
144         mNetworkInfo = info;
145         return this;
146     }
147 
148     @Keep
setRssi(int rssi)149     public TestAccessPointBuilder setRssi(int rssi) {
150         mRssi = rssi;
151         return this;
152     }
153 
setSpeed(int speed)154     public TestAccessPointBuilder setSpeed(int speed) {
155         mSpeed = speed;
156         return this;
157     }
158 
159     /**
160     * Set whether the AccessPoint is reachable.
161     * Side effect: if the signal level was not previously set,
162     * making an AccessPoint reachable will set the signal to the minimum level.
163     */
164     @Keep
setReachable(boolean reachable)165     public TestAccessPointBuilder setReachable(boolean reachable) {
166         if (reachable) {
167             // only override the mRssi if it hasn't been set yet
168             if (mRssi == AccessPoint.UNREACHABLE_RSSI) {
169                 mRssi = MIN_RSSI;
170             }
171         } else {
172             mRssi = AccessPoint.UNREACHABLE_RSSI;
173         }
174         return this;
175     }
176 
177     @Keep
setSaved(boolean saved)178     public TestAccessPointBuilder setSaved(boolean saved){
179         if (saved) {
180              mNetworkId = 1;
181         } else {
182              mNetworkId = WifiConfiguration.INVALID_NETWORK_ID;
183         }
184         return this;
185     }
186 
187     @Keep
setSecurity(int security)188     public TestAccessPointBuilder setSecurity(int security) {
189         mSecurity = security;
190         return this;
191     }
192 
193     @Keep
setSsid(String newSsid)194     public TestAccessPointBuilder setSsid(String newSsid) {
195         ssid = newSsid;
196         return this;
197     }
198 
199     @Keep
setFqdn(String fqdn)200     public TestAccessPointBuilder setFqdn(String fqdn) {
201         mFqdn = fqdn;
202         return this;
203     }
204 
205     @Keep
setProviderFriendlyName(String friendlyName)206     public TestAccessPointBuilder setProviderFriendlyName(String friendlyName) {
207         mProviderFriendlyName = friendlyName;
208         return this;
209     }
210 
211     @Keep
setWifiInfo(WifiInfo info)212     public TestAccessPointBuilder setWifiInfo(WifiInfo info) {
213         mWifiInfo = info;
214         return this;
215     }
216 
217     /**
218      * Set the networkId in the WifiConfig.
219      *
220      * <p>Setting this to a value other than {@link WifiConfiguration#INVALID_NETWORK_ID} makes this
221      * AccessPoint a saved network.
222      */
223     @Keep
setNetworkId(int networkId)224     public TestAccessPointBuilder setNetworkId(int networkId) {
225         mNetworkId = networkId;
226         return this;
227     }
228 
setBssid(String bssid)229     public TestAccessPointBuilder setBssid(String bssid) {
230         mBssid = bssid;
231         return this;
232     }
233 
setScanResults(ArrayList<ScanResult> scanResults)234     public TestAccessPointBuilder setScanResults(ArrayList<ScanResult> scanResults) {
235         mScanResults = scanResults;
236         return this;
237     }
238 
setIsCarrierAp(boolean isCarrierAp)239     public TestAccessPointBuilder setIsCarrierAp(boolean isCarrierAp) {
240         mIsCarrierAp = isCarrierAp;
241         return this;
242     }
243 
setCarrierName(String carrierName)244     public TestAccessPointBuilder setCarrierName(String carrierName) {
245         mCarrierName = carrierName;
246         return this;
247     }
248 
setScoredNetworkCache( ArrayList<TimestampedScoredNetwork> scoredNetworkCache)249     public TestAccessPointBuilder setScoredNetworkCache(
250             ArrayList<TimestampedScoredNetwork> scoredNetworkCache) {
251         mScoredNetworkCache = scoredNetworkCache;
252         return this;
253     }
254 }
255