Skip to content

Commit

Permalink
Refactor ImageView and CompassListener into CompassView
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorch committed Apr 11, 2020
1 parent b6e9881 commit e0255a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -14,7 +13,6 @@
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
Expand Down Expand Up @@ -56,7 +54,7 @@
import de.storchp.opentracks.osmplugin.dashboardapi.APIConstants;
import de.storchp.opentracks.osmplugin.dashboardapi.TrackpointsColumn;
import de.storchp.opentracks.osmplugin.dashboardapi.TracksColumn;
import de.storchp.opentracks.osmplugin.maps.CompassListener;
import de.storchp.opentracks.osmplugin.maps.CompassView;
import de.storchp.opentracks.osmplugin.maps.MapsforgeMapView;
import de.storchp.opentracks.osmplugin.maps.StyleColorCreator;
import de.storchp.opentracks.osmplugin.maps.utils.PreferencesUtils;
Expand All @@ -83,7 +81,6 @@ public class MapsActivity extends BaseActivity {
private StyleColorCreator colorCreator = null;
private LatLong startPos;
private LatLong endPos;
private CompassListener compassListener;

static Paint createPaint(int color, int strokeWidth, Style style) {
Paint paint = AndroidGraphicFactory.INSTANCE.createPaint();
Expand All @@ -107,8 +104,6 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.maps_toolbar);
setSupportActionBar(toolbar);

compassListener = new CompassListener((SensorManager) getSystemService(SENSOR_SERVICE), (ImageView) findViewById(R.id.compass));

createMapViews();
createTileCaches();
createLayers();
Expand Down Expand Up @@ -438,7 +433,6 @@ public void onResume() {
if (this.layer instanceof TileDownloadLayer) {
((TileDownloadLayer) this.layer).onResume();
}
compassListener.onResume();
}

@Override
Expand All @@ -460,7 +454,6 @@ protected void onPause() {
if (this.layer instanceof TileDownloadLayer) {
((TileDownloadLayer) this.layer).onPause();
}
compassListener.onPause();
super.onPause();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package de.storchp.opentracks.osmplugin.maps;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class CompassListener implements SensorEventListener {
import androidx.annotation.Nullable;

import static android.content.Context.SENSOR_SERVICE;

public class CompassView extends androidx.appcompat.widget.AppCompatImageView implements SensorEventListener {

private SensorManager sensorManager;
private final float[] accelerometerReading = new float[3];
Expand All @@ -17,17 +22,33 @@ public class CompassListener implements SensorEventListener {
private boolean lastMagnetometerSet = false;
private final float[] rotationMatrix = new float[9];
private final float[] orientationAngles = new float[3];
private ImageView compassView;
private int lastDegreePos = -1;
private float[] lastDegrees = new float[5];
private float currentDegree = 0;

public CompassListener(SensorManager sensorManager, ImageView compassView) {
this.sensorManager = sensorManager;
this.compassView = compassView;
public CompassView(Context context) {
super(context);
createSensorManager(context);
}

public CompassView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
createSensorManager(context);
}

public CompassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
createSensorManager(context);
}

public void onResume() {
private void createSensorManager(Context context) {
this.sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer,
Expand All @@ -40,8 +61,10 @@ public void onResume() {
}
}

public void onPause() {
@Override
protected void onDetachedFromWindow() {
sensorManager.unregisterListener(this);
super.onDetachedFromWindow();
}

@Override
Expand All @@ -56,7 +79,7 @@ public void onSensorChanged(SensorEvent event) {
lastMagnetometerSet = true;
}

if (lastAccelerometerSet && lastMagnetometerSet && compassView != null) {
if (lastAccelerometerSet && lastMagnetometerSet) {
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading);
SensorManager.getOrientation(rotationMatrix, orientationAngles);
float azimuthInRadians = orientationAngles[0];
Expand Down Expand Up @@ -84,7 +107,7 @@ public void onSensorChanged(SensorEvent event) {
ra.setFillAfter(true);

// Start the animation
compassView.startAnimation(ra);
startAnimation(ra);
currentDegree = newDegree;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/mapviewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
android:textColor="#000"
android:textSize="@dimen/attribution_size" />

<ImageView
<de.storchp.opentracks.osmplugin.maps.CompassView
android:id="@+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down

0 comments on commit e0255a3

Please sign in to comment.