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