/** * Called by {@link MainActivity} when a new location is found by the GPS location provider. * Stores the location and updates GPS display and map view. */ public void onLocationChanged(Location location) { if (location.hasAccuracy()) { Float getAcc = (float) 0.0; if (mainActivity.prefUnitType) { getAcc = (float) (location.getAccuracy()); } else { getAcc = (float) (location.getAccuracy() * (float) 3.28084); } gpsAccuracy.setText(String.format("%.0f", getAcc)); gpsAccuracyUnit.setText( getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet))); } else { gpsAccuracy.setText(getString(R.string.value_none)); gpsAccuracyUnit.setText(""); } if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_DECIMAL) { gpsCoordLayout.setVisibility(View.GONE); gpsLatLayout.setVisibility(View.VISIBLE); gpsLonLayout.setVisibility(View.VISIBLE); gpsLat.setText( String.format("%.5f%s", location.getLatitude(), getString(R.string.unit_degree))); gpsLon.setText( String.format("%.5f%s", location.getLongitude(), getString(R.string.unit_degree))); } else if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_MIN) { gpsCoordLayout.setVisibility(View.GONE); gpsLatLayout.setVisibility(View.VISIBLE); gpsLonLayout.setVisibility(View.VISIBLE); double dec = location.getLatitude(); double deg = (int) dec; double min = 60.0 * (dec - deg); gpsLat.setText( String.format( "%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/ 0.0005)); dec = location.getLongitude(); deg = (int) dec; min = 60.0 * (dec - deg); gpsLon.setText( String.format( "%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/ 0.0005)); } else if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_SEC) { gpsCoordLayout.setVisibility(View.GONE); gpsLatLayout.setVisibility(View.VISIBLE); gpsLonLayout.setVisibility(View.VISIBLE); double dec = location.getLatitude(); double deg = (int) dec; double tmp = 60.0 * (dec - deg); double min = (int) tmp; double sec = 60.0 * (tmp - min); gpsLat.setText( String.format( "%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min, sec + /*rounding*/ 0.05)); dec = location.getLongitude(); deg = (int) dec; tmp = 60.0 * (dec - deg); min = (int) tmp; sec = 60.0 * (tmp - min); gpsLon.setText( String.format( "%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min, sec + /*rounding*/ 0.05)); } else if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_MGRS) { gpsLatLayout.setVisibility(View.GONE); gpsLonLayout.setVisibility(View.GONE); gpsCoordLayout.setVisibility(View.VISIBLE); gpsCoord.setText( new LatLng(location.getLatitude(), location.getLongitude()) .toMGRSRef() .toString(MGRSRef.PRECISION_1M)); } if (mainActivity.prefUtc) df.setTimeZone(TimeZone.getTimeZone("UTC")); else df.setTimeZone(TimeZone.getDefault()); gpsTime.setText(df.format(new Date(location.getTime()))); if (location.hasAltitude()) { Float getAltitude = (float) 0.0; if (mainActivity.prefUnitType) { getAltitude = (float) (location.getAltitude()); } else { getAltitude = (float) (location.getAltitude() * (float) 3.28084); } gpsAlt.setText(String.format("%.0f", getAltitude)); gpsAltUnit.setText( getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet))); orDeclination.setText( String.format( "%.0f%s", new GeomagneticField( (float) location.getLatitude(), (float) location.getLongitude(), (float) (getAltitude), location.getTime()) .getDeclination(), getString(R.string.unit_degree))); } else { gpsAlt.setText(getString(R.string.value_none)); gpsAltUnit.setText(""); orDeclination.setText(getString(R.string.value_none)); } if (location.hasBearing()) { gpsBearing.setText( String.format("%.0f%s", location.getBearing(), getString(R.string.unit_degree))); gpsOrientation.setText( MainActivity.formatOrientation(this.getContext(), location.getBearing())); } else { gpsBearing.setText(getString(R.string.value_none)); gpsOrientation.setText(getString(R.string.value_none)); } if (location.hasSpeed()) { Float getSpeed = (float) 0.0; if (mainActivity.prefUnitType) { getSpeed = (float) (location.getSpeed() * 3.6f); } else { getSpeed = (float) (location.getSpeed() * 3.6f * 2.23694f); } gpsSpeed.setText(String.format("%.0f", getSpeed)); gpsSpeedUnit.setText( getString(((mainActivity.prefUnitType) ? R.string.unit_km_h : R.string.unit_mph))); } else { gpsSpeed.setText(getString(R.string.value_none)); gpsSpeedUnit.setText(""); } // note: getting number of sats in fix by looking for "satellites" // in location's extras doesn't seem to work, always returns 0 sats }
/** Called by {@link MainActivity} when a sensor's reading changes. Updates sensor display. */ public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: accX.setText(String.format("%." + mAccSensorRes + "f", event.values[0])); accY.setText(String.format("%." + mAccSensorRes + "f", event.values[1])); accZ.setText(String.format("%." + mAccSensorRes + "f", event.values[2])); accTotal.setText( String.format( "%." + mAccSensorRes + "f", Math.sqrt( Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); accStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_ORIENTATION: orAzimuth.setText( String.format("%.0f%s", event.values[0], getString(R.string.unit_degree))); orAziText.setText(MainActivity.formatOrientation(this.getContext(), event.values[0])); orPitch.setText(String.format("%.0f%s", event.values[1], getString(R.string.unit_degree))); orRoll.setText(String.format("%.0f%s", event.values[2], getString(R.string.unit_degree))); orStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_GYROSCOPE: rotX.setText(String.format("%." + mGyroSensorRes + "f", event.values[0])); rotY.setText(String.format("%." + mGyroSensorRes + "f", event.values[1])); rotZ.setText(String.format("%." + mGyroSensorRes + "f", event.values[2])); rotTotal.setText( String.format( "%." + mGyroSensorRes + "f", Math.sqrt( Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); rotStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_MAGNETIC_FIELD: magX.setText(String.format("%." + mMagSensorRes + "f", event.values[0])); magY.setText(String.format("%." + mMagSensorRes + "f", event.values[1])); magZ.setText(String.format("%." + mMagSensorRes + "f", event.values[2])); magTotal.setText( String.format( "%." + mMagSensorRes + "f", Math.sqrt( Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)))); magStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_LIGHT: light.setText(String.format("%." + mLightSensorRes + "f", event.values[0])); lightStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_PROXIMITY: proximity.setText(String.format("%." + mProximitySensorRes + "f", event.values[0])); proximityStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_PRESSURE: metPressure.setText(String.format("%." + mPressureSensorRes + "f", event.values[0])); pressureStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_RELATIVE_HUMIDITY: metHumid.setText(String.format("%." + mHumiditySensorRes + "f", event.values[0])); humidStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; case Sensor.TYPE_AMBIENT_TEMPERATURE: metTemp.setText(String.format("%." + mTempSensorRes + "f", event.values[0])); tempStatus.setTextColor(getResources().getColor(accuracyToColor(event.accuracy))); break; } }