/walkytalky/src/com/googlecode/eyesfree/walkytalky/WifiPointsOfInterestLocator.java

http://eyes-free.googlecode.com/ · 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. package com.googlecode.eyesfree.walkytalky;
  17. import com.whereabout.common.MapInfo;
  18. import com.whereabout.common.Utils;
  19. import com.whereabout.location.LocationManager;
  20. import android.graphics.Point;
  21. import android.location.Location;
  22. import android.os.Bundle;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. /**
  26. * Finds the points of interest that are nearby given a map and a Location
  27. * object that has wifi data extras.
  28. *
  29. * @author chaitaynag@google.com (Chaitanya Gharpure)
  30. * @author clchen@google.com (Charles L. Chen)
  31. */
  32. public class WifiPointsOfInterestLocator {
  33. private MapInfo mapInfo;
  34. private String currentMap;
  35. public WifiPointsOfInterestLocator() {
  36. }
  37. private void setMap(String mapName) {
  38. if (mapName.equals(currentMap)) {
  39. return;
  40. }
  41. try {
  42. mapInfo = Utils.getMapInfo(com.whereabout.common.Constants.MAP_DIR + "/" + mapName
  43. + ".map");
  44. currentMap = mapName;
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. private ArrayList<String> getWifiLocationsOfInterestIds(Location loc) {
  50. ArrayList<String> interests = new ArrayList<String>();
  51. Bundle extras = loc.getExtras();
  52. String[] matchMaps = extras.getStringArray(LocationManager.MATCH_MAPS);
  53. String[] matchLocations = extras.getStringArray(LocationManager.MATCH_LOCATIONS);
  54. double[] matchScores = extras.getDoubleArray(LocationManager.MATCH_SCORES);
  55. int[] matchXCoords = extras.getIntArray(LocationManager.MATCH_XCOORDS);
  56. int[] matchYCoords = extras.getIntArray(LocationManager.MATCH_YCOORDS);
  57. int x = extras.getInt(LocationManager.FINAL_X);
  58. int y = extras.getInt(LocationManager.FINAL_Y);
  59. if ((x == 0 && y == 0) || matchLocations == null) {
  60. return null;
  61. }
  62. if ((matchLocations[0] != null) && (matchScores[0] < 10)) {
  63. setMap(matchLocations[0].substring(0, matchLocations[0].lastIndexOf('_')));
  64. } else {
  65. return null;
  66. }
  67. int i = 0;
  68. for (Point p : mapInfo.allPoints) {
  69. String id = mapInfo.allIds.get(i);
  70. String name = mapInfo.locNameIdMap.get(mapInfo.allIds.get(i));
  71. if (name != null) {
  72. double dist = Math.sqrt((p.y - y) * (p.y - y) + (p.x - x) * (p.x - x));
  73. if (dist < 100) {
  74. interests.add(id);
  75. }
  76. }
  77. i++;
  78. }
  79. return interests;
  80. }
  81. public ArrayList<String> getWifiLocationsOfInterest(Location loc) {
  82. ArrayList<String> locationsOfInterest = new ArrayList<String>();
  83. ArrayList<String> ids = getWifiLocationsOfInterestIds(loc);
  84. if (ids == null) {
  85. return null;
  86. }
  87. for (String id : ids) {
  88. String name = mapInfo.locNameIdMap.get(id);
  89. if (!locationsOfInterest.contains(name)) {
  90. locationsOfInterest.add(name);
  91. }
  92. }
  93. return locationsOfInterest;
  94. }
  95. }