Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlacesDisplay.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.placesdisplay;
  23. import android.app.AlertDialog.Builder;
  24. import android.content.DialogInterface;
  25. import android.content.DialogInterface.OnClickListener;
  26. import android.content.Intent;
  27. import android.content.pm.PackageManager.NameNotFoundException;
  28. import android.content.res.Resources;
  29. import android.database.Cursor;
  30. import android.net.Uri;
  31. import android.os.Bundle;
  32. import android.text.format.DateFormat;
  33. import com.flurry.android.FlurryAgent;
  34. import com.google.android.maps.GeoPoint;
  35. import com.google.android.maps.MapActivity;
  36. import com.google.android.maps.MapView;
  37. import com.google.android.maps.Overlay;
  38. import com.google.android.maps.OverlayItem;
  39. import java.util.Date;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. import uk.co.md87.android.contextapi.ContextApi;
  44. import uk.co.md87.android.contextapi.ContextApi.Places.ColumnNames;
  45. /**
  46. * Activity which displays all known places on a map.
  47. *
  48. * @author chris
  49. */
  50. public class PlacesDisplay extends MapActivity {
  51. /** Called when the activity is first created. */
  52. @Override
  53. public void onCreate(Bundle icicle) {
  54. super.onCreate(icicle);
  55. FlurryAgent.onStartSession(this, "XXXXGE95S8R54R6M7S6X");
  56. final String pkg = "uk.co.md87.android.contextanalyser";
  57. try {
  58. getPackageManager().getApplicationInfo(pkg, 0);
  59. } catch (NameNotFoundException ex) {
  60. FlurryAgent.onEvent("analyser_not_installed");
  61. Builder builder = new Builder(this);
  62. builder.setTitle("Context Analyser needed");
  63. builder.setMessage("This application requires the 'Context Analyser'"
  64. + " application to be installed. Would you like "
  65. + "to install it now?");
  66. builder.setPositiveButton("Install", new OnClickListener() {
  67. public void onClick(DialogInterface arg0, int arg1) {
  68. FlurryAgent.onEvent("launch_market");
  69. startActivity(new Intent(Intent.ACTION_VIEW,
  70. Uri.parse("market://search?q=pname:" + pkg)));
  71. finish();
  72. }
  73. });
  74. builder.setNegativeButton("Quit", new OnClickListener() {
  75. public void onClick(DialogInterface arg0, int arg1) {
  76. FlurryAgent.onEvent("abort_install");
  77. finish();
  78. }
  79. });
  80. builder.show();
  81. return;
  82. }
  83. setContentView(R.layout.main);
  84. MapView mapView = (MapView) findViewById(R.id.mapview);
  85. mapView.setBuiltInZoomControls(true);
  86. PlacesItemisedOverlay overlay = new PlacesItemisedOverlay(this,
  87. Resources.getSystem().getDrawable(android.R.drawable.btn_star_big_on));
  88. List<Overlay> mapOverlays = mapView.getOverlays();
  89. final Cursor cursor = managedQuery(ContextApi.Places.CONTENT_URI,
  90. new String[] { ColumnNames.LATITUDE, ColumnNames.LONGITUDE,
  91. ColumnNames._ID, ColumnNames.NAME, ColumnNames.LAST_VISIT,
  92. ColumnNames.VISIT_COUNT }, null, null, null);
  93. final java.text.DateFormat dateFormat = DateFormat.getDateFormat(this);
  94. final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(this);
  95. if (cursor.moveToFirst()) {
  96. final int latitudeColumn = cursor.getColumnIndex(ColumnNames.LATITUDE);
  97. final int longitudeColumn = cursor.getColumnIndex(ColumnNames.LONGITUDE);
  98. final int nameColumn = cursor.getColumnIndex(ColumnNames.NAME);
  99. final int idColumn = cursor.getColumnIndex(ColumnNames._ID);
  100. final int lastVisitColumn = cursor.getColumnIndex(ColumnNames.LAST_VISIT);
  101. final int visitCountColumn = cursor.getColumnIndex(ColumnNames.VISIT_COUNT);
  102. final Map<Integer, OverlayItem> places
  103. = new HashMap<Integer, OverlayItem>(cursor.getCount());
  104. do {
  105. final double latitude = cursor.getDouble(latitudeColumn);
  106. final double longitude = cursor.getDouble(longitudeColumn);
  107. final String name = cursor.getString(nameColumn);
  108. final long lastVisit = cursor.getLong(lastVisitColumn);
  109. final int visitCount = cursor.getInt(visitCountColumn);
  110. final int id = cursor.getInt(idColumn);
  111. final GeoPoint point = new GeoPoint((int) (latitude * 1000000),
  112. (int) (longitude * 1000000));
  113. final Date date = new Date(lastVisit * 1000);
  114. final OverlayItem overlayitem = new OverlayItem(point, name,
  115. "Visited " + visitCount + " time" + (visitCount == 1 ? "" : "s")
  116. + "\nLast visited on " + dateFormat.format(date)
  117. + " at " + timeFormat.format(date));
  118. overlay.addOverlay(overlayitem);
  119. places.put(id, overlayitem);
  120. } while (cursor.moveToNext());
  121. mapOverlays.add(new JourneysOverlay(this, places));
  122. mapOverlays.add(overlay);
  123. }
  124. }
  125. @Override
  126. protected boolean isRouteDisplayed() {
  127. return false;
  128. }
  129. @Override
  130. protected void onDestroy() {
  131. super.onDestroy();
  132. FlurryAgent.onEndSession(this);
  133. }
  134. }