diff --git a/app/src/main/java/com/google/android/stardroid/control/AstronomerModelImpl.java b/app/src/main/java/com/google/android/stardroid/control/AstronomerModelImpl.java index 877bf7117..4825a6ba3 100644 --- a/app/src/main/java/com/google/android/stardroid/control/AstronomerModelImpl.java +++ b/app/src/main/java/com/google/android/stardroid/control/AstronomerModelImpl.java @@ -247,7 +247,7 @@ private void calculatePointing() { } calculateLocalNorthAndUpInCelestialCoords(false); - calculateLocalNorthAndUpInPhoneCoordsFromAccelAndMagFieldSensors(); + calculateLocalNorthAndUpInPhoneCoordsFromSensors(); Matrix33 transform = matrixMultiply(axesMagneticCelestialMatrix, axesPhoneInverseMatrix); @@ -301,7 +301,7 @@ private void calculateLocalNorthAndUpInCelestialCoords(boolean forceUpdate) { * Calculates local North and Up vectors in terms of the phone's coordinate * frame from the magnetic field and accelerometer sensors. */ - private void calculateLocalNorthAndUpInPhoneCoordsFromAccelAndMagFieldSensors() { + private void calculateLocalNorthAndUpInPhoneCoordsFromSensors() { Vector3 magneticNorthPhone; Vector3 upPhone; Vector3 magneticEastPhone; @@ -334,31 +334,6 @@ private void calculateLocalNorthAndUpInPhoneCoordsFromAccelAndMagFieldSensors() axesPhoneInverseMatrix = new Matrix33(magneticNorthPhone, upPhone, magneticEastPhone, false); } - /** - * Calculates local North and Up vectors in terms of the phone's coordinate - * frame from the rotation matrix. A temporary hack till we rebuild the rendering codel. - */ - private void calculateLocalNorthAndUpInPhoneCoordsFromRotationMatrixSensor() { - // TODO(johntaylor): we can reduce the number of vector copies done in here. - Vector3 down = acceleration.copy(); - down.normalize(); - // Magnetic field goes *from* North to South, so reverse it. - Vector3 magneticFieldToNorth = magneticField.copy(); - magneticFieldToNorth.scale(-1); - magneticFieldToNorth.normalize(); - // This is the vector to magnetic North *along the ground*. - Vector3 magneticNorthPhone = addVectors(magneticFieldToNorth, - scaleVector(down, -scalarProduct(magneticFieldToNorth, down))); - magneticNorthPhone.normalize(); - Vector3 upPhone = scaleVector(down, -1); - Vector3 magneticEastPhone = vectorProduct(magneticNorthPhone, upPhone); - - // The matrix is orthogonal, so transpose it to find its inverse. - // Easiest way to do that is to construct it from row vectors instead - // of column vectors. - axesPhoneInverseMatrix = new Matrix33(magneticNorthPhone, upPhone, magneticEastPhone, false); - } - /** * Updates the angle between True North and Magnetic North. */ diff --git a/app/src/main/java/com/google/android/stardroid/control/SensorOrientationController.java b/app/src/main/java/com/google/android/stardroid/control/SensorOrientationController.java index 177c2a4a0..35f501786 100644 --- a/app/src/main/java/com/google/android/stardroid/control/SensorOrientationController.java +++ b/app/src/main/java/com/google/android/stardroid/control/SensorOrientationController.java @@ -14,20 +14,16 @@ package com.google.android.stardroid.control; -import android.content.Context; import android.content.SharedPreferences; -import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorListener; import android.hardware.SensorManager; -import android.preference.PreferenceManager; import android.util.Log; import com.google.android.stardroid.ApplicationConstants; import com.google.android.stardroid.util.MiscUtil; -import com.google.android.stardroid.util.SensorAccuracyReporter; import com.google.android.stardroid.util.smoothers.ExponentiallyWeightedSmoother; import com.google.android.stardroid.util.smoothers.PlainSmootherModelAdaptor; @@ -40,7 +36,7 @@ * @author John Taylor */ public class SensorOrientationController extends AbstractController - implements OnSharedPreferenceChangeListener, SensorEventListener { + implements SensorEventListener { // TODO(johntaylor): this class needs to be refactored to use the new // sensor API and to behave properly when sensors are not available. @@ -74,72 +70,62 @@ public SensorDampingSettings(float damping, int exponent) { private SensorListener accelerometerSmoother; private SensorListener compassSmoother; private Provider modelAdaptorProvider; - private SensorAccuracyReporter accuracyReporter; private Sensor rotationSensor; - private SharedPreferences sharedPreferences; @Inject - SensorOrientationController(Context context, SensorAccuracyReporter accuracyReporter, - Provider modelAdaptorProvider, - SensorManager manager) { + SensorOrientationController(Provider modelAdaptorProvider, + SensorManager manager, SharedPreferences sharedPreferences) { this.manager = manager; - sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); - sharedPreferences.registerOnSharedPreferenceChangeListener(this); - this.accuracyReporter = accuracyReporter; this.modelAdaptorProvider = modelAdaptorProvider; this.rotationSensor = manager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); + this.sharedPreferences = sharedPreferences; } @Override public void start() { PlainSmootherModelAdaptor modelAdaptor = modelAdaptorProvider.get(); - Log.d(TAG, "Exponentially weighted smoothers used"); - String dampingPreference = sharedPreferences.getString( - ApplicationConstants.SENSOR_DAMPING_PREF_KEY, - ApplicationConstants.SENSOR_DAMPING_STANDARD); - String speedPreference = sharedPreferences.getString(ApplicationConstants.SENSOR_SPEED_PREF_KEY, - ApplicationConstants.SENSOR_SPEED_STANDARD); - Log.d(TAG, "Sensor damping preference " + dampingPreference); - Log.d(TAG, "Sensor speed preference " + speedPreference); - int dampingIndex = 0; - if (ApplicationConstants.SENSOR_DAMPING_HIGH.equals(dampingPreference)) { - dampingIndex = 1; - } else if (ApplicationConstants.SENSOR_DAMPING_EXTRA_HIGH.equals(dampingPreference)) { - dampingIndex = 2; - } else if (ApplicationConstants.SENSOR_DAMPING_REALLY_HIGH.equals(dampingPreference)) { - dampingIndex = 3; - } - int sensorSpeed = SensorManager.SENSOR_DELAY_GAME; - if (ApplicationConstants.SENSOR_SPEED_SLOW.equals(speedPreference)) { - sensorSpeed = SensorManager.SENSOR_DELAY_NORMAL; - } else if (ApplicationConstants.SENSOR_SPEED_HIGH.equals(speedPreference)) { - sensorSpeed = SensorManager.SENSOR_DELAY_FASTEST; - } - accelerometerSmoother = new ExponentiallyWeightedSmoother( - modelAdaptor, - ACC_DAMPING_SETTINGS[dampingIndex].damping, - ACC_DAMPING_SETTINGS[dampingIndex].exponent); - compassSmoother = new ExponentiallyWeightedSmoother( - modelAdaptor, - MAG_DAMPING_SETTINGS[dampingIndex].damping, - MAG_DAMPING_SETTINGS[dampingIndex].exponent); - if (manager != null) { if (sharedPreferences.getBoolean(ApplicationConstants.SHARED_PREFERENCE_EXPERIMENTAL_USE_GYRO, false)) { + Log.d(TAG, "Using rotation sensor"); manager.registerListener(this, rotationSensor, SensorManager.SENSOR_DELAY_GAME); - manager.registerListener( - accuracyReporter, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), - SensorManager.SENSOR_DELAY_NORMAL); - manager.registerListener( - accuracyReporter, manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), - SensorManager.SENSOR_DELAY_NORMAL); } else { + Log.d(TAG, "Using classic sensors"); + Log.d(TAG, "Exponentially weighted smoothers used"); + String dampingPreference = sharedPreferences.getString( + ApplicationConstants.SENSOR_DAMPING_PREF_KEY, + ApplicationConstants.SENSOR_DAMPING_STANDARD); + String speedPreference = sharedPreferences.getString(ApplicationConstants.SENSOR_SPEED_PREF_KEY, + ApplicationConstants.SENSOR_SPEED_STANDARD); + Log.d(TAG, "Sensor damping preference " + dampingPreference); + Log.d(TAG, "Sensor speed preference " + speedPreference); + int dampingIndex = 0; + if (ApplicationConstants.SENSOR_DAMPING_HIGH.equals(dampingPreference)) { + dampingIndex = 1; + } else if (ApplicationConstants.SENSOR_DAMPING_EXTRA_HIGH.equals(dampingPreference)) { + dampingIndex = 2; + } else if (ApplicationConstants.SENSOR_DAMPING_REALLY_HIGH.equals(dampingPreference)) { + dampingIndex = 3; + } + int sensorSpeed = SensorManager.SENSOR_DELAY_GAME; + if (ApplicationConstants.SENSOR_SPEED_SLOW.equals(speedPreference)) { + sensorSpeed = SensorManager.SENSOR_DELAY_NORMAL; + } else if (ApplicationConstants.SENSOR_SPEED_HIGH.equals(speedPreference)) { + sensorSpeed = SensorManager.SENSOR_DELAY_FASTEST; + } + accelerometerSmoother = new ExponentiallyWeightedSmoother( + modelAdaptor, + ACC_DAMPING_SETTINGS[dampingIndex].damping, + ACC_DAMPING_SETTINGS[dampingIndex].exponent); + compassSmoother = new ExponentiallyWeightedSmoother( + modelAdaptor, + MAG_DAMPING_SETTINGS[dampingIndex].damping, + MAG_DAMPING_SETTINGS[dampingIndex].exponent); manager.registerListener(accelerometerSmoother, - SensorManager.SENSOR_ACCELEROMETER, - sensorSpeed); + SensorManager.SENSOR_ACCELEROMETER, + sensorSpeed); manager.registerListener(compassSmoother, SensorManager.SENSOR_MAGNETIC_FIELD, sensorSpeed); @@ -150,22 +136,14 @@ public void start() { @Override public void stop() { - Log.d(TAG, "Unregistering sensor listeners"); + Log.d( + TAG, "Unregistering sensor listeners: " + accelerometerSmoother + ", " + + compassSmoother + ", " + this); manager.unregisterListener(accelerometerSmoother); manager.unregisterListener(compassSmoother); manager.unregisterListener(this); } - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (ApplicationConstants.SENSOR_DAMPING_PREF_KEY.equals(key) || ApplicationConstants.SENSOR_SPEED_PREF_KEY.equals(key) - || ApplicationConstants.SHARED_PREFERENCE_EXPERIMENTAL_USE_GYRO.equals(key)) { - Log.d(TAG, "User sensor preferences changed - restarting sensor controllers"); - stop(); - start(); - } - } - @Override public void onSensorChanged(SensorEvent event) { if (event.sensor != rotationSensor) { diff --git a/app/src/main/java/com/google/android/stardroid/util/SensorAccuracyReporter.java b/app/src/main/java/com/google/android/stardroid/util/SensorAccuracyReporter.java deleted file mode 100644 index 65d5cf532..000000000 --- a/app/src/main/java/com/google/android/stardroid/util/SensorAccuracyReporter.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.google.android.stardroid.util; - -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; -import android.hardware.SensorManager; -import android.util.Log; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import javax.inject.Inject; - -/** - * Logs the reported accuracy of the compass. - */ -public class SensorAccuracyReporter implements SensorEventListener { - private String TAG = MiscUtil.getTag(this); - private Analytics analytics; - private long startTimeMs; - private Set highAccuracyAchievedForSensor = new HashSet<>(); - private Map timeSinceLastUpdateForSensor = new HashMap<>(); - - @Inject - SensorAccuracyReporter(Analytics analytics) { - this.analytics = analytics; - this.startTimeMs = System.currentTimeMillis(); - } - - @Override - public void onSensorChanged(SensorEvent event) { - // Ignore. - } - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - long currentTimeMs = System.currentTimeMillis(); - if (accuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH) { - // Record time to high accuracy. - int boundedElapsedTimeMs = boundedInt(currentTimeMs - startTimeMs); - if (!highAccuracyAchievedForSensor.contains(sensor)) { - Log.d(TAG, "Sensor " + sensor.getName() + " achieved high accuracy at time " - + boundedElapsedTimeMs + " milliseconds"); - highAccuracyAchievedForSensor.add(sensor); - analytics.trackEvent( - Analytics.SENSOR_CATEGORY, Analytics.HIGH_SENSOR_ACCURACY_ACHIEVED, - Analytics.getSafeNameForSensor(sensor), boundedElapsedTimeMs); - } - } - - int elapsedTimeSinceLastUpdateForThisSensor = boundedInt(currentTimeMs - - getLastUpdateTimeForSensorAndUpdate(sensor, currentTimeMs)); - - Log.d(TAG, sensor.getName() + " accuracy now " + accuracy + " after " - + elapsedTimeSinceLastUpdateForThisSensor + " elapsed milliseconds"); - - analytics.trackEvent( - Analytics.SENSOR_CATEGORY, - Analytics.SENSOR_ACCURACY_CHANGED + ":" + accuracyToString(accuracy), - Analytics.getSafeNameForSensor(sensor), elapsedTimeSinceLastUpdateForThisSensor); - } - - private int boundedInt(long value) { - return value > Integer.MAX_VALUE ? Integer.MAX_VALUE : - (int) value; - } - - private String accuracyToString(int acc) { - switch (acc) { - case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: - return "high"; - case SensorManager.SENSOR_STATUS_ACCURACY_LOW: - return "low"; - case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: - return "medium"; - case SensorManager.SENSOR_STATUS_NO_CONTACT: - return "nocontact"; - case SensorManager.SENSOR_STATUS_UNRELIABLE: - return "unreliable"; - default: - return "unknown"; - } - } - - private long getLastUpdateTimeForSensorAndUpdate(Sensor sensor, long currentTimeMs) { - long time; - if (timeSinceLastUpdateForSensor.containsKey(sensor)) { - time = timeSinceLastUpdateForSensor.get(sensor); - } else { - time = startTimeMs; - } - timeSinceLastUpdateForSensor.put(sensor, currentTimeMs); - return time; - } -} diff --git a/app/src/main/java/com/google/android/stardroid/views/PreferencesButton.java b/app/src/main/java/com/google/android/stardroid/views/PreferencesButton.java index a94265ed0..0c6e92d73 100644 --- a/app/src/main/java/com/google/android/stardroid/views/PreferencesButton.java +++ b/app/src/main/java/com/google/android/stardroid/views/PreferencesButton.java @@ -82,7 +82,7 @@ public void setAttrs(Context context, AttributeSet attrs) { imageOn = a.getDrawable(R.styleable.PreferencesButton_image_on); imageOff = a.getDrawable(R.styleable.PreferencesButton_image_off); prefKey = a.getString(R.styleable.PreferencesButton_pref_key); - defaultValue = a.getBoolean(R.styleable.PreferencesButton_default_value, false); + defaultValue = a.getBoolean(R.styleable.PreferencesButton_default_value, true); Log.d(TAG, "Preference key is " + prefKey); } @@ -101,9 +101,8 @@ private void setVisuallyOnOrOff() { private void setPreference() { Log.d(TAG, "Setting preference " + prefKey + " to... " + isOn); - // TODO(put this on a background thread) if (prefKey != null) { - preferences.edit().putBoolean(prefKey, isOn).commit(); + preferences.edit().putBoolean(prefKey, isOn).apply(); } } diff --git a/app/src/main/res/values/whatsnew.xml b/app/src/main/res/values/whatsnew.xml index 4235f1ddf..3b4bc7580 100644 --- a/app/src/main/res/values/whatsnew.xml +++ b/app/src/main/res/values/whatsnew.xml @@ -3,7 +3,7 @@ New in version %s - \t • Uses gyroscope for a smoother experience
+ \t • Gyroscope option (if your phone has one) for a smoother experience. Enable it from settings.
\t • Now in Greek! (thanks: ftsamis)
\t • Fixed Russian translations (thanks: AlexLitvino)
]]>