TrackerActivity.java revision 8ab8b411b81e4581060a31a97a0cb204a803b2e5
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.locationtracker; 18 19import com.android.locationtracker.data.DateUtils; 20import com.android.locationtracker.data.TrackerDataHelper; 21import com.android.locationtracker.data.TrackerListHelper; 22 23import android.app.AlertDialog; 24import android.app.ListActivity; 25import android.content.DialogInterface; 26import android.content.DialogInterface.OnClickListener; 27import android.content.Intent; 28import android.database.Cursor; 29import android.location.LocationManager; 30import android.os.Bundle; 31import android.util.Log; 32import android.view.Menu; 33import android.view.MenuInflater; 34import android.view.MenuItem; 35import android.widget.Toast; 36 37import java.io.BufferedWriter; 38import java.io.File; 39import java.io.FileWriter; 40import java.io.IOException; 41import java.io.Writer; 42 43/** 44 * Activity for location tracker service 45 * 46 * Contains facilities for starting and 47 * stopping location tracker service, as well as displaying the current location 48 * data 49 * 50 * Future enhancements: 51 * - export data as dB 52 * - enable/disable "start service" and "stop service" menu items as 53 * appropriate 54 */ 55public class TrackerActivity extends ListActivity { 56 57 static final String LOG_TAG = "LocationTracker"; 58 59 private TrackerListHelper mDataHelper; 60 61 /** 62 * Retrieves and displays the currently logged location data from file 63 * 64 * @param icicle 65 */ 66 @Override 67 protected void onCreate(Bundle icicle) { 68 super.onCreate(icicle); 69 70 mDataHelper = new TrackerListHelper(this); 71 mDataHelper.bindListUI(R.layout.entrylist_item); 72 } 73 74 /** 75 * Builds the menu 76 * 77 * @param menu - menu to add items to 78 */ 79 @Override 80 public boolean onCreateOptionsMenu(Menu menu) { 81 MenuInflater menuInflater = getMenuInflater(); 82 menuInflater.inflate(R.menu.menu, menu); 83 return true; 84 } 85 86 /** 87 * Handles menu item selection 88 * 89 * @param item - the selected menu item 90 */ 91 @Override 92 public boolean onOptionsItemSelected(MenuItem item) { 93 switch (item.getItemId()) { 94 case R.id.start_service_menu: { 95 startService(new Intent(TrackerActivity.this, 96 TrackerService.class)); 97 break; 98 } 99 case R.id.stop_service_menu: { 100 stopService(new Intent(TrackerActivity.this, 101 TrackerService.class)); 102 break; 103 } 104 case R.id.settings_menu: { 105 launchSettings(); 106 break; 107 } 108 case R.id.export_kml: { 109 exportKML(); 110 break; 111 } 112 case R.id.export_csv: { 113 exportCSV(); 114 break; 115 } 116 case R.id.clear_data_menu: { 117 clearData(); 118 break; 119 } 120 } 121 return super.onOptionsItemSelected(item); 122 } 123 124 private void clearData() { 125 Runnable clearAction = new Runnable() { 126 public void run() { 127 TrackerDataHelper helper = 128 new TrackerDataHelper(TrackerActivity.this); 129 helper.deleteAll(); 130 } 131 }; 132 showConfirm(R.string.delete_confirm, clearAction); 133 } 134 135 private void showConfirm(int textId, final Runnable onConfirmAction) { 136 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 137 dialogBuilder.setTitle(R.string.confirm_title); 138 dialogBuilder.setMessage(textId); 139 dialogBuilder.setPositiveButton(android.R.string.ok, 140 new OnClickListener() { 141 public void onClick(DialogInterface dialog, int which) { 142 onConfirmAction.run(); 143 } 144 }); 145 dialogBuilder.setNegativeButton(android.R.string.cancel, 146 new OnClickListener() { 147 public void onClick(DialogInterface dialog, int which) { 148 // do nothing 149 } 150 }); 151 dialogBuilder.show(); 152 } 153 154 private void exportCSV() { 155 String exportFileName = getUniqueFileName("csv"); 156 exportFile(null, exportFileName, new TrackerDataHelper(this, 157 TrackerDataHelper.CSV_FORMATTER)); 158 } 159 160 private void exportKML() { 161 String exportFileName = getUniqueFileName( 162 LocationManager.NETWORK_PROVIDER + ".kml"); 163 exportFile(LocationManager.NETWORK_PROVIDER, exportFileName, 164 new TrackerDataHelper(this, TrackerDataHelper.KML_FORMATTER)); 165 exportFileName = getUniqueFileName( 166 LocationManager.GPS_PROVIDER + ".kml"); 167 exportFile(LocationManager.GPS_PROVIDER, exportFileName, 168 new TrackerDataHelper(this, TrackerDataHelper.KML_FORMATTER)); 169 } 170 171 private void exportFile(String tagFilter, 172 String exportFileName, 173 TrackerDataHelper trackerData) { 174 BufferedWriter exportWriter = null; 175 Cursor cursor = trackerData.query(tagFilter); 176 try { 177 exportWriter = new BufferedWriter(new FileWriter(exportFileName)); 178 exportWriter.write(trackerData.getOutputHeader()); 179 180 String line = null; 181 182 while ((line = trackerData.getNextOutput(cursor)) != null) { 183 exportWriter.write(line); 184 } 185 exportWriter.write(trackerData.getOutputFooter()); 186 Toast.makeText(this, "Successfully exported data to " + 187 exportFileName, Toast.LENGTH_SHORT).show(); 188 189 } catch (IOException e) { 190 Toast.makeText(this, "Error exporting file: " + 191 e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); 192 193 Log.e(LOG_TAG, "Error exporting file", e); 194 } finally { 195 closeWriter(exportWriter); 196 if (cursor != null) { 197 cursor.close(); 198 } 199 } 200 } 201 202 private void closeWriter(Writer exportWriter) { 203 if (exportWriter != null) { 204 try { 205 exportWriter.close(); 206 } catch (IOException e) { 207 Log.e(LOG_TAG, "error closing file", e); 208 } 209 } 210 } 211 212 private String getUniqueFileName(String ext) { 213 File dir = new File("/sdcard/locationtracker"); 214 if (!dir.exists()) { 215 dir.mkdir(); 216 } 217 return "/sdcard/locationtracker/tracking-" + 218 DateUtils.getCurrentTimestamp() + "." + ext; 219 } 220 221 private void launchSettings() { 222 Intent settingsIntent = new Intent(); 223 settingsIntent.setClass(this, SettingsActivity.class); 224 startActivity(settingsIntent); 225 } 226} 227