1 /* 2 * Copyright (C) 2020 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.server.wifi.util; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.net.wifi.WifiConfiguration; 22 23 import com.android.server.wifi.ScanResultMatchInfo; 24 import com.android.wifi.resources.R; 25 26 /** 27 * Create a lru list to store the network connection order. sorted by most recently connected 28 * first. 29 */ 30 public class LruConnectionTracker { 31 private final LruList<ScanResultMatchInfo> mList; 32 private final Context mContext; LruConnectionTracker(int size, Context context)33 public LruConnectionTracker(int size, Context context) { 34 mList = new LruList<>(size); 35 mContext = context; 36 } 37 /** 38 * Check if a WifiConfiguration is in the most recently connected list. 39 */ isMostRecentlyConnected(@onNull WifiConfiguration config)40 public boolean isMostRecentlyConnected(@NonNull WifiConfiguration config) { 41 return getAgeIndexOfNetwork(config) < mContext.getResources() 42 .getInteger(R.integer.config_wifiMaxPnoSsidCount); 43 } 44 45 /** 46 * Add a WifiConfiguration into the most recently connected list. 47 */ addNetwork(@onNull WifiConfiguration config)48 public void addNetwork(@NonNull WifiConfiguration config) { 49 mList.add(ScanResultMatchInfo.fromWifiConfiguration(config)); 50 } 51 52 /** 53 * Remove a network from the list. 54 */ removeNetwork(@onNull WifiConfiguration config)55 public void removeNetwork(@NonNull WifiConfiguration config) { 56 mList.remove(ScanResultMatchInfo.fromWifiConfiguration(config)); 57 } 58 59 /** 60 * Get the index of the input config inside the MostRecentlyConnectNetworkList. 61 * If input config is in the list will return index number, 62 * otherwise return {@code Integer.MAX_VALUE} 63 */ getAgeIndexOfNetwork(@onNull WifiConfiguration config)64 public int getAgeIndexOfNetwork(@NonNull WifiConfiguration config) { 65 int index = mList.indexOf(ScanResultMatchInfo.fromWifiConfiguration(config)); 66 // Not in the most recently connected list will return the MAX_INT 67 if (index < 0) { 68 return Integer.MAX_VALUE; 69 } 70 return index; 71 } 72 } 73