@Override protected void onDestroy() { Utilities.LogDebug("GpsMainActivity.onDestroy"); StopAndUnbindServiceIfRequired(); super.onDestroy(); }
/** Stops the service if it isn't logging. Also unbinds. */ private void StopAndUnbindServiceIfRequired() { Utilities.LogDebug("GpsMainActivity.StopAndUnbindServiceIfRequired"); if (Session.isBoundToService()) { unbindService(gpsServiceConnection); Session.setBoundToService(false); } if (!Session.isStarted()) { Utilities.LogDebug("StopServiceIfRequired - Stopping the service"); // serviceIntent = new Intent(this, GpsLoggingService.class); stopService(serviceIntent); } }
/** Clears the table, removes all values. */ public void ClearForm() { Utilities.LogDebug("GpsMainActivity.ClearForm"); TextView tvLatitude = (TextView) findViewById(R.id.txtLatitude); TextView tvLongitude = (TextView) findViewById(R.id.txtLongitude); TextView tvDateTime = (TextView) findViewById(R.id.txtDateTimeAndProvider); TextView tvAltitude = (TextView) findViewById(R.id.txtAltitude); TextView txtSpeed = (TextView) findViewById(R.id.txtSpeed); TextView txtSatellites = (TextView) findViewById(R.id.txtSatellites); TextView txtDirection = (TextView) findViewById(R.id.txtDirection); TextView txtAccuracy = (TextView) findViewById(R.id.txtAccuracy); tvLatitude.setText(""); tvLongitude.setText(""); tvDateTime.setText(""); tvAltitude.setText(""); txtSpeed.setText(""); txtSatellites.setText(""); txtDirection.setText(""); txtAccuracy.setText(""); }
/** Event raised when the form is created for the first time */ @Override public void onCreate(Bundle savedInstanceState) { Utilities.LogDebug("GpsMainActivity.onCreate"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String lang = prefs.getString("locale_override", ""); if (!lang.equalsIgnoreCase("")) { Locale locale = new Locale(lang); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getApplicationContext() .getResources() .updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics()); } super.onCreate(savedInstanceState); Utilities.LogInfo("GPSLogger started"); setContentView(R.layout.main); GetPreferences(); StartAndBindService(); }
/** Starts the service and binds the activity to it. */ private void StartAndBindService() { Utilities.LogDebug("StartAndBindService - binding now"); serviceIntent = new Intent(this, GpsLoggingService.class); // Start the service in case it isn't already running startService(serviceIntent); // Now bind to service bindService(serviceIntent, gpsServiceConnection, Context.BIND_AUTO_CREATE); Session.setBoundToService(true); }
private void EmailNow() { Utilities.LogDebug("GpsMainActivity.EmailNow"); if (Utilities.IsEmailSetup()) { loggingService.ForceEmailLogFile(); } else { Intent emailSetup = new Intent(getApplicationContext(), AutoEmailActivity.class); startActivity(emailSetup); } }
public void OnLocationUpdate(Location loc) { Utilities.LogDebug("GpsMainActivity.OnLocationUpdate"); DisplayLocationInfo(loc); ShowPreferencesSummary(); SetMainButtonChecked(true); if (Session.isSinglePointMode()) { loggingService.StopLogging(); SetMainButtonEnabled(true); Session.setSinglePointMode(false); } }
/** Called when the toggle button is clicked */ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Utilities.LogDebug("GpsMainActivity.onCheckedChanged"); if (isChecked) { GetPreferences(); SetSinglePointButtonEnabled(false); loggingService.StartLogging(); } else { SetSinglePointButtonEnabled(true); loggingService.StopLogging(); } }
/** Called when the single point button is clicked */ public void onClick(View view) { Utilities.LogDebug("GpsMainActivity.onClick"); if (!Session.isStarted()) { SetMainButtonEnabled(false); loggingService.StartLogging(); Session.setSinglePointMode(true); } else if (Session.isStarted() && Session.isSinglePointMode()) { loggingService.StopLogging(); SetMainButtonEnabled(true); Session.setSinglePointMode(false); } }
private void Annotate(String description) { Utilities.LogDebug("GpsMainActivity.Annotate(description)"); List<IFileLogger> loggers = FileLoggerFactory.GetFileLoggers(); for (IFileLogger logger : loggers) { try { logger.Annotate(description, Session.getCurrentLocationInfo()); SetStatus(getString(R.string.description_added)); Session.setAllowDescription(false); } catch (Exception e) { SetStatus(getString(R.string.could_not_write_to_file)); } } }
private void UploadToDropBox() { Utilities.LogDebug("GpsMainActivity.UploadToDropBox"); final DropBoxHelper dropBoxHelper = new DropBoxHelper(getApplicationContext(), this); if (!dropBoxHelper.IsLinked()) { startActivity(new Intent("com.mendhak.gpslogger.DROPBOX_SETUP")); return; } final File gpxFolder = new File(Environment.getExternalStorageDirectory(), "GPSLogger"); if (gpxFolder.exists()) { String[] enumeratedFiles = gpxFolder.list(); List<String> fileList = new ArrayList<String>(Arrays.asList(enumeratedFiles)); Collections.reverse(fileList); final String[] files = fileList.toArray(new String[fileList.size()]); final Dialog dialog = new Dialog(this); dialog.setTitle(R.string.dropbox_upload); dialog.setContentView(R.layout.filelist); ListView thelist = (ListView) dialog.findViewById(R.id.listViewFiles); thelist.setAdapter( new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_list_item_single_choice, files)); thelist.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> av, View v, int index, long arg) { dialog.dismiss(); String chosenFileName = files[index]; Utilities.ShowProgress( GpsMainActivity.this, getString(R.string.dropbox_uploading), getString(R.string.please_wait)); dropBoxHelper.UploadFile(chosenFileName); } }); dialog.show(); } else { Utilities.MsgBox(getString(R.string.sorry), getString(R.string.no_files_found), this); } }
/** Prompts user for input, then adds text to log file */ private void Annotate() { Utilities.LogDebug("GpsMainActivity.Annotate"); if (!AppSettings.shouldLogToGpx() && !AppSettings.shouldLogToKml()) { return; } if (!Session.shoulAllowDescription()) { Utilities.MsgBox( getString(R.string.not_yet), getString(R.string.cant_add_description_until_next_point), GetActivity()); return; } AlertDialog.Builder alert = new AlertDialog.Builder(GpsMainActivity.this); alert.setTitle(R.string.add_description); alert.setMessage(R.string.letters_numbers); // Set an EditText view to get user input final EditText input = new EditText(getApplicationContext()); alert.setView(input); alert.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { final String desc = Utilities.CleanDescription(input.getText().toString()); Annotate(desc); } }); alert.setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Cancelled. } }); alert.show(); }
/** Uploads a GPS Trace to OpenStreetMap.org. */ private void UploadToOpenStreetMap() { Utilities.LogDebug("GpsMainactivity.UploadToOpenStreetMap"); if (!OSMHelper.IsOsmAuthorized(getApplicationContext())) { startActivity(OSMHelper.GetOsmSettingsIntent(getApplicationContext())); return; } final String goToOsmSettings = getString(R.string.menu_settings); final File gpxFolder = new File(Environment.getExternalStorageDirectory(), "GPSLogger"); if (gpxFolder.exists()) { FilenameFilter select = new FilenameFilter() { public boolean accept(File dir, String filename) { return filename.toLowerCase().contains(".gpx"); } }; String[] enumeratedFiles = gpxFolder.list(select); List<String> fileList = new ArrayList<String>(Arrays.asList(enumeratedFiles)); Collections.reverse(fileList); fileList.add(0, goToOsmSettings); final String[] files = fileList.toArray(new String[fileList.size()]); final Dialog dialog = new Dialog(this); dialog.setTitle(R.string.osm_pick_file); dialog.setContentView(R.layout.filelist); ListView thelist = (ListView) dialog.findViewById(R.id.listViewFiles); thelist.setAdapter( new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_list_item_single_choice, files)); thelist.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> av, View v, int index, long arg) { dialog.dismiss(); String chosenFileName = files[index]; if (chosenFileName.equalsIgnoreCase(goToOsmSettings)) { startActivity(OSMHelper.GetOsmSettingsIntent(getApplicationContext())); } else { OSMHelper osm = new OSMHelper(GpsMainActivity.this, GpsMainActivity.this); Utilities.ShowProgress( GpsMainActivity.this, getString(R.string.osm_uploading), getString(R.string.please_wait)); osm.UploadGpsTrace(chosenFileName); } } }); dialog.show(); } else { Utilities.MsgBox(getString(R.string.sorry), getString(R.string.no_files_found), this); } }
/** * Given a location fix, processes it and displays it in the table on the form. * * @param loc Location information */ private void DisplayLocationInfo(Location loc) { Utilities.LogDebug("GpsMainActivity.DisplayLocationInfo"); try { if (loc == null) { return; } TextView tvLatitude = (TextView) findViewById(R.id.txtLatitude); TextView tvLongitude = (TextView) findViewById(R.id.txtLongitude); TextView tvDateTime = (TextView) findViewById(R.id.txtDateTimeAndProvider); TextView tvAltitude = (TextView) findViewById(R.id.txtAltitude); TextView txtSpeed = (TextView) findViewById(R.id.txtSpeed); TextView txtSatellites = (TextView) findViewById(R.id.txtSatellites); TextView txtDirection = (TextView) findViewById(R.id.txtDirection); TextView txtAccuracy = (TextView) findViewById(R.id.txtAccuracy); String providerName = loc.getProvider(); if (providerName.equalsIgnoreCase("gps")) { providerName = getString(R.string.providername_gps); } else { providerName = getString(R.string.providername_celltower); } tvDateTime.setText( new Date(Session.getLatestTimeStamp()).toLocaleString() + getString(R.string.providername_using, providerName)); tvLatitude.setText(String.valueOf(loc.getLatitude())); tvLongitude.setText(String.valueOf(loc.getLongitude())); if (loc.hasAltitude()) { double altitude = loc.getAltitude(); if (AppSettings.shouldUseImperial()) { tvAltitude.setText( String.valueOf(Utilities.MetersToFeet(altitude)) + getString(R.string.feet)); } else { tvAltitude.setText(String.valueOf(altitude) + getString(R.string.meters)); } } else { tvAltitude.setText(R.string.not_applicable); } if (loc.hasSpeed()) { float speed = loc.getSpeed(); String unit; if (AppSettings.shouldUseImperial()) { if (speed > 1.47) { speed = speed * 0.6818f; unit = getString(R.string.miles_per_hour); } else { speed = Utilities.MetersToFeet(speed); unit = getString(R.string.feet_per_second); } } else { if (speed > 0.277) { speed = speed * 3.6f; unit = getString(R.string.kilometers_per_hour); } else { unit = getString(R.string.meters_per_second); } } txtSpeed.setText(String.valueOf(speed) + unit); } else { txtSpeed.setText(R.string.not_applicable); } if (loc.hasBearing()) { float bearingDegrees = loc.getBearing(); String direction; direction = Utilities.GetBearingDescription(bearingDegrees, getApplicationContext()); txtDirection.setText( direction + "(" + String.valueOf(Math.round(bearingDegrees)) + getString(R.string.degree_symbol) + ")"); } else { txtDirection.setText(R.string.not_applicable); } if (!Session.isUsingGps()) { txtSatellites.setText(R.string.not_applicable); Session.setSatelliteCount(0); } if (loc.hasAccuracy()) { float accuracy = loc.getAccuracy(); if (AppSettings.shouldUseImperial()) { txtAccuracy.setText( getString( R.string.accuracy_within, String.valueOf(Utilities.MetersToFeet(accuracy)), getString(R.string.feet))); } else { txtAccuracy.setText( getString( R.string.accuracy_within, String.valueOf(accuracy), getString(R.string.meters))); } } else { txtAccuracy.setText(R.string.not_applicable); } } catch (Exception ex) { SetStatus(getString(R.string.error_displaying, ex.getMessage())); } }
/** * Sets the message in the top status label. * * @param message The status message */ private void SetStatus(String message) { Utilities.LogDebug("GpsMainActivity.SetStatus: " + message); TextView tvStatus = (TextView) findViewById(R.id.textStatus); tvStatus.setText(message); Utilities.LogInfo(message); }
public void OnStopLogging() { Utilities.LogDebug("GpsMainActivity.OnStopLogging"); SetMainButtonChecked(false); }
/** Displays a human readable summary of the preferences chosen by the user on the main form */ private void ShowPreferencesSummary() { Utilities.LogDebug("GpsMainActivity.ShowPreferencesSummary"); try { TextView txtLoggingTo = (TextView) findViewById(R.id.txtLoggingTo); TextView txtFrequency = (TextView) findViewById(R.id.txtFrequency); TextView txtDistance = (TextView) findViewById(R.id.txtDistance); TextView txtAutoEmail = (TextView) findViewById(R.id.txtAutoEmail); if (!AppSettings.shouldLogToKml() && !AppSettings.shouldLogToGpx()) { txtLoggingTo.setText(R.string.summary_loggingto_screen); } else if (AppSettings.shouldLogToGpx() && AppSettings.shouldLogToKml()) { txtLoggingTo.setText(R.string.summary_loggingto_both); } else { txtLoggingTo.setText((AppSettings.shouldLogToGpx() ? "GPX" : "KML")); } if (AppSettings.getMinimumSeconds() > 0) { String descriptiveTime = Utilities.GetDescriptiveTimeString( AppSettings.getMinimumSeconds(), getApplicationContext()); txtFrequency.setText(descriptiveTime); } else { txtFrequency.setText(R.string.summary_freq_max); } if (AppSettings.getMinimumDistanceInMeters() > 0) { if (AppSettings.shouldUseImperial()) { int minimumDistanceInFeet = Utilities.MetersToFeet(AppSettings.getMinimumDistanceInMeters()); txtDistance.setText( ((minimumDistanceInFeet == 1) ? getString(R.string.foot) : String.valueOf(minimumDistanceInFeet) + getString(R.string.feet))); } else { txtDistance.setText( ((AppSettings.getMinimumDistanceInMeters() == 1) ? getString(R.string.meter) : String.valueOf(AppSettings.getMinimumDistanceInMeters()) + getString(R.string.meters))); } } else { txtDistance.setText(R.string.summary_dist_regardless); } if (AppSettings.isAutoEmailEnabled()) { String autoEmailResx; if (AppSettings.getAutoEmailDelay() == 0) { autoEmailResx = "autoemail_frequency_whenistop"; } else { autoEmailResx = "autoemail_frequency_" + String.valueOf(AppSettings.getAutoEmailDelay()).replace(".", ""); } String autoEmailDesc = getString(getResources().getIdentifier(autoEmailResx, "string", getPackageName())); txtAutoEmail.setText(autoEmailDesc); } else { TableRow trAutoEmail = (TableRow) findViewById(R.id.trAutoEmail); trAutoEmail.setVisibility(View.INVISIBLE); } onFileName(Session.getCurrentFileName()); } catch (Exception ex) { Utilities.LogError("ShowPreferencesSummary", ex); } }
/** * Allows user to send a GPX/KML file along with location, or location only using a provider. * 'Provider' means any application that can accept such an intent (Facebook, SMS, Twitter, Email, * K-9, Bluetooth) */ private void Share() { Utilities.LogDebug("GpsMainActivity.Share"); try { final String locationOnly = getString(R.string.sharing_location_only); final File gpxFolder = new File(Environment.getExternalStorageDirectory(), "GPSLogger"); if (gpxFolder.exists()) { String[] enumeratedFiles = gpxFolder.list(); List<String> fileList = new ArrayList<String>(Arrays.asList(enumeratedFiles)); Collections.reverse(fileList); fileList.add(0, locationOnly); final String[] files = fileList.toArray(new String[fileList.size()]); final Dialog dialog = new Dialog(this); dialog.setTitle(R.string.sharing_pick_file); dialog.setContentView(R.layout.filelist); ListView thelist = (ListView) dialog.findViewById(R.id.listViewFiles); thelist.setAdapter( new ArrayAdapter<String>( getApplicationContext(), android.R.layout.simple_list_item_single_choice, files)); thelist.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> av, View v, int index, long arg) { dialog.dismiss(); String chosenFileName = files[index]; final Intent intent = new Intent(Intent.ACTION_SEND); // intent.setType("text/plain"); intent.setType("*/*"); if (chosenFileName.equalsIgnoreCase(locationOnly)) { intent.setType("text/plain"); } intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.sharing_mylocation)); if (Session.hasValidLocation()) { String bodyText = getString( R.string.sharing_latlong_text, String.valueOf(Session.getCurrentLatitude()), String.valueOf(Session.getCurrentLongitude())); intent.putExtra(Intent.EXTRA_TEXT, bodyText); intent.putExtra("sms_body", bodyText); } if (chosenFileName.length() > 0 && !chosenFileName.equalsIgnoreCase(locationOnly)) { intent.putExtra( Intent.EXTRA_STREAM, Uri.fromFile(new File(gpxFolder, chosenFileName))); } startActivity(Intent.createChooser(intent, getString(R.string.sharing_via))); } }); dialog.show(); } else { Utilities.MsgBox(getString(R.string.sorry), getString(R.string.no_files_found), this); } } catch (Exception ex) { Utilities.LogError("Share", ex); } }
@Override public void run() { try { RandomAccessFile raf; String dateTimeString = Utilities.GetIsoDateTime(new Date(loc.getTime())); String placemarkHead = "<Placemark>\n<gx:Track>\n"; String placemarkTail = "</gx:Track>\n</Placemark></Document></kml>\n"; synchronized (Kml22FileLogger.lock) { if (!kmlFile.exists()) { kmlFile.createNewFile(); FileOutputStream initialWriter = new FileOutputStream(kmlFile, true); BufferedOutputStream initialOutput = new BufferedOutputStream(initialWriter); StringBuilder initialXml = new StringBuilder(); initialXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); initialXml.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\" "); initialXml.append("xmlns:gx=\"http://www.google.com/kml/ext/2.2\" "); initialXml.append("xmlns:kml=\"http://www.opengis.net/kml/2.2\" "); initialXml.append("xmlns:atom=\"http://www.w3.org/2005/Atom\">"); initialXml.append("<Document>"); initialXml.append("<name>").append(dateTimeString).append("</name>\n"); initialXml.append("</Document></kml>\n"); initialOutput.write(initialXml.toString().getBytes()); initialOutput.flush(); initialOutput.close(); // New file, so new track segment addNewTrackSegment = true; } if (addNewTrackSegment) { raf = new RandomAccessFile(kmlFile, "rw"); raf.seek(kmlFile.length() - 18); raf.write((placemarkHead + placemarkTail).getBytes()); raf.close(); } StringBuilder coords = new StringBuilder(); coords.append("\n<when>"); coords.append(dateTimeString); coords.append("</when>\n<gx:coord>"); coords.append(String.valueOf(loc.getLongitude())); coords.append(" "); coords.append(String.valueOf(loc.getLatitude())); coords.append(" "); coords.append(String.valueOf(loc.getAltitude())); coords.append("</gx:coord>\n"); coords.append(placemarkTail); raf = new RandomAccessFile(kmlFile, "rw"); raf.seek(kmlFile.length() - 42); raf.write(coords.toString().getBytes()); raf.close(); Utilities.LogDebug("Finished writing to KML22 File"); } } catch (Exception e) { Utilities.LogError("Kml22FileLogger.Write", e); } }
@Override protected void onResume() { Utilities.LogDebug("GpsMainactivity.onResume"); super.onResume(); StartAndBindService(); }