/walkytalky/src/com/googlecode/eyesfree/walkytalky/WifiPointsOfInterestLocator.java
Java | 109 lines | 73 code | 14 blank | 22 comment | 19 complexity | 8a361f23b9a3e6501f5686beafd4abec MD5 | raw file
1/* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.eyesfree.walkytalky; 18 19import com.whereabout.common.MapInfo; 20import com.whereabout.common.Utils; 21import com.whereabout.location.LocationManager; 22 23import android.graphics.Point; 24import android.location.Location; 25import android.os.Bundle; 26 27import java.io.IOException; 28import java.util.ArrayList; 29 30/** 31 * Finds the points of interest that are nearby given a map and a Location 32 * object that has wifi data extras. 33 * 34 * @author chaitaynag@google.com (Chaitanya Gharpure) 35 * @author clchen@google.com (Charles L. Chen) 36 */ 37public class WifiPointsOfInterestLocator { 38 private MapInfo mapInfo; 39 40 private String currentMap; 41 42 public WifiPointsOfInterestLocator() { 43 } 44 45 private void setMap(String mapName) { 46 if (mapName.equals(currentMap)) { 47 return; 48 } 49 try { 50 mapInfo = Utils.getMapInfo(com.whereabout.common.Constants.MAP_DIR + "/" + mapName 51 + ".map"); 52 currentMap = mapName; 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 58 private ArrayList<String> getWifiLocationsOfInterestIds(Location loc) { 59 ArrayList<String> interests = new ArrayList<String>(); 60 61 Bundle extras = loc.getExtras(); 62 63 String[] matchMaps = extras.getStringArray(LocationManager.MATCH_MAPS); 64 String[] matchLocations = extras.getStringArray(LocationManager.MATCH_LOCATIONS); 65 double[] matchScores = extras.getDoubleArray(LocationManager.MATCH_SCORES); 66 int[] matchXCoords = extras.getIntArray(LocationManager.MATCH_XCOORDS); 67 int[] matchYCoords = extras.getIntArray(LocationManager.MATCH_YCOORDS); 68 int x = extras.getInt(LocationManager.FINAL_X); 69 int y = extras.getInt(LocationManager.FINAL_Y); 70 71 if ((x == 0 && y == 0) || matchLocations == null) { 72 return null; 73 } 74 if ((matchLocations[0] != null) && (matchScores[0] < 10)) { 75 setMap(matchLocations[0].substring(0, matchLocations[0].lastIndexOf('_'))); 76 } else { 77 return null; 78 } 79 int i = 0; 80 for (Point p : mapInfo.allPoints) { 81 String id = mapInfo.allIds.get(i); 82 String name = mapInfo.locNameIdMap.get(mapInfo.allIds.get(i)); 83 if (name != null) { 84 double dist = Math.sqrt((p.y - y) * (p.y - y) + (p.x - x) * (p.x - x)); 85 if (dist < 100) { 86 interests.add(id); 87 } 88 } 89 i++; 90 } 91 return interests; 92 } 93 94 public ArrayList<String> getWifiLocationsOfInterest(Location loc) { 95 ArrayList<String> locationsOfInterest = new ArrayList<String>(); 96 ArrayList<String> ids = getWifiLocationsOfInterestIds(loc); 97 if (ids == null) { 98 return null; 99 } 100 for (String id : ids) { 101 String name = mapInfo.locNameIdMap.get(id); 102 if (!locationsOfInterest.contains(name)) { 103 locationsOfInterest.add(name); 104 } 105 } 106 return locationsOfInterest; 107 } 108 109}