Context-detection API for Android developed as a university project
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DataHelper.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.contextanalyser;
  23. import android.content.Context;
  24. import android.database.Cursor;
  25. import android.database.sqlite.SQLiteDatabase;
  26. import android.database.sqlite.SQLiteOpenHelper;
  27. import android.database.sqlite.SQLiteStatement;
  28. import android.location.Location;
  29. /**
  30. *
  31. * @author chris
  32. */
  33. public class DataHelper {
  34. private static final String LOCATIONS_TABLE = "locations";
  35. private static final String DATABASE_NAME = "contextapi.db";
  36. private static final int DATABASE_VERSION = 1;
  37. private static final String INSERT_LOCATION = "insert into "
  38. + LOCATIONS_TABLE + "(name, lat, lon) values (?, ?, ?)";
  39. private static final String LOCATION_QUERY = "lat > %1$s - 0.005 and "
  40. + "lat < %1$s + 0.005 and lon > %2$s - 0.01 and lon < %2$s + 0.01";
  41. private final SQLiteStatement insertLocationStatement;
  42. private final Context context;
  43. private SQLiteDatabase db;
  44. public DataHelper(final Context context) {
  45. this.context = context;
  46. final OpenHelper helper = new OpenHelper(context);
  47. this.db = helper.getWritableDatabase();
  48. this.insertLocationStatement = db.compileStatement(INSERT_LOCATION);
  49. }
  50. public long addLocation(final String name, final double lat, final double lon) {
  51. insertLocationStatement.bindString(1, name);
  52. insertLocationStatement.bindDouble(2, lat);
  53. insertLocationStatement.bindDouble(3, lon);
  54. return insertLocationStatement.executeInsert();
  55. }
  56. public LocationResult findLocation(final double lat, final double lon) {
  57. final Cursor cursor = db.query(LOCATIONS_TABLE,
  58. new String[] { "id", "name", "lat", "lon" },
  59. String.format(LOCATION_QUERY, lat, lon), null, null, null, null);
  60. if (cursor.moveToFirst()) {
  61. do {
  62. final float[] res = new float[1];
  63. Location.distanceBetween(lat, lon, cursor.getDouble(2), cursor.getDouble(3), res);
  64. if (res[0] <= 500) {
  65. return new LocationResult(cursor.getLong(0), cursor.getString(1),
  66. cursor.getDouble(2), cursor.getDouble(3));
  67. }
  68. } while (cursor.moveToNext());
  69. }
  70. if (cursor != null && !cursor.isClosed()) {
  71. cursor.close();
  72. }
  73. return null;
  74. }
  75. public static class LocationResult {
  76. private final long id;
  77. private final String name;
  78. private final double lat, lon;
  79. public LocationResult(long id, String name, double lat, double lon) {
  80. this.id = id;
  81. this.name = name;
  82. this.lat = lat;
  83. this.lon = lon;
  84. }
  85. public long getId() {
  86. return id;
  87. }
  88. public double getLat() {
  89. return lat;
  90. }
  91. public double getLon() {
  92. return lon;
  93. }
  94. public String getName() {
  95. return name;
  96. }
  97. }
  98. private static class OpenHelper extends SQLiteOpenHelper {
  99. public OpenHelper(final Context context) {
  100. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public void onCreate(final SQLiteDatabase db) {
  105. db.execSQL("CREATE TABLE " + LOCATIONS_TABLE
  106. + " (id INTEGER PRIMARY KEY, name TEXT, lon REAL, lat REAL)");
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
  111. final int newVersion) {
  112. // Do nothing. Yet.
  113. }
  114. }
  115. }