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 5.9KB

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