WifiNetworkScoreCache.java revision f6c6d3ccda924ad5f11883bc346b4eb4ada1bc06
1/* 2 * Copyright (C) 2014 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.server.wifi; 18 19import android.Manifest.permission; 20import android.content.Context; 21import android.net.INetworkScoreCache; 22import android.net.NetworkKey; 23import android.net.ScoredNetwork; 24import android.net.wifi.ScanResult; 25import android.net.wifi.WifiManager; 26import android.util.Log; 27 28import java.io.FileDescriptor; 29import java.io.PrintWriter; 30import java.util.HashMap; 31import java.util.List; 32import java.util.Map; 33 34public class WifiNetworkScoreCache extends INetworkScoreCache.Stub 35 { 36 37 private static String TAG = "WifiNetworkScoreCache"; 38 private boolean DBG = true; 39 private final Context mContext; 40 41 // The key is of the form "<ssid>"<bssid> 42 // TODO: What about SSIDs that can't be encoded as UTF-8? 43 private final Map<String, ScoredNetwork> mNetworkCache; 44 45 public WifiNetworkScoreCache(Context context) { 46 mContext = context; 47 mNetworkCache = new HashMap<String, ScoredNetwork>(); 48 } 49 50 @Override public final void updateScores(List<android.net.ScoredNetwork> networks) { 51 if (networks == null) { 52 return; 53 } 54 Log.e(TAG, "updateScores list size=" + networks.size()); 55 56 synchronized(mNetworkCache) { 57 for (ScoredNetwork network : networks) { 58 String networkKey = buildNetworkKey(network); 59 if (networkKey == null) continue; 60 mNetworkCache.put(networkKey, network); 61 } 62 } 63 } 64 65 @Override public final void clearScores() { 66 synchronized (mNetworkCache) { 67 mNetworkCache.clear(); 68 } 69 } 70 71 public boolean isScoredNetwork(ScanResult result) { 72 String key = buildNetworkKey(result); 73 if (key == null) return false; 74 75 //find it 76 synchronized(mNetworkCache) { 77 ScoredNetwork network = mNetworkCache.get(key); 78 if (network != null) { 79 return true; 80 } 81 } 82 return false; 83 } 84 85 public int getNetworkScore(ScanResult result) { 86 87 int score = -1; 88 89 String key = buildNetworkKey(result); 90 if (key == null) return score; 91 92 //find it 93 synchronized(mNetworkCache) { 94 ScoredNetwork network = mNetworkCache.get(key); 95 if (network != null && network.rssiCurve != null) { 96 score = network.rssiCurve.lookupScore(result.level); 97 if (DBG) { 98 Log.e(TAG, "getNetworkScore found Herrevad network" + key 99 + " score " + Integer.toString(score) 100 + " RSSI " + result.level); 101 } 102 } 103 } 104 return score; 105 } 106 107 private String buildNetworkKey(ScoredNetwork network) { 108 if (network.networkKey == null) return null; 109 if (network.networkKey.wifiKey == null) return null; 110 if (network.networkKey.type == NetworkKey.TYPE_WIFI) { 111 String key = network.networkKey.wifiKey.ssid; 112 if (key == null) return null; 113 if (network.networkKey.wifiKey.bssid != null) { 114 key = key + network.networkKey.wifiKey.bssid; 115 } 116 return key; 117 } 118 return null; 119 } 120 121 private String buildNetworkKey(ScanResult result) { 122 if (result.SSID == null) { 123 return null; 124 } 125 StringBuilder key = new StringBuilder("\""); 126 key.append(result.SSID); 127 key.append("\""); 128 if (result.BSSID != null) { 129 key.append(result.BSSID); 130 } 131 return key.toString(); 132 } 133 134 @Override protected final void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 135 mContext.enforceCallingOrSelfPermission(permission.DUMP, TAG); 136 writer.println("WifiNetworkScoreCache"); 137 writer.println(" All score curves:"); 138 for (Map.Entry<String, ScoredNetwork> entry : mNetworkCache.entrySet()) { 139 writer.println(" " + entry.getKey() + ": " + entry.getValue().rssiCurve); 140 } 141 writer.println(" Current network scores:"); 142 WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); 143 for (ScanResult scanResult : wifiManager.getScanResults()) { 144 writer.println(" " + buildNetworkKey(scanResult) + ": " + getNetworkScore(scanResult)); 145 } 146 } 147 148} 149