Skip to content

Commit

Permalink
Merge pull request #531 from enviroCar/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
dewall authored Jul 15, 2020
2 parents 5525770 + f7a98c7 commit 4cd543a
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 124 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ ext {
compileSdkVersion = 29
targetSdkVersion = 29
buildToolsVersion = '29.0.2'
versionCode = 48
versionName = "1.1.8"
versionCode = 49
versionName = "1.1.9"

javaCompileVersion = JavaVersion.VERSION_1_8

Expand Down
4 changes: 2 additions & 2 deletions org.envirocar.app/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="org.envirocar.app"
android:installLocation="internalOnly"
android:versionCode="48"
android:versionName="1.1.8">
android:versionCode="49"
android:versionName="1.1.9">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Expand Down
2 changes: 1 addition & 1 deletion org.envirocar.app/res/layout/send_log_layout_new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
android:layout_height="wrap_content"
android:gravity="start|top"
android:imeOptions="actionNext"
android:inputType="number"
android:inputType="date|time"
android:padding="12dp" />

</com.google.android.material.textfield.TextInputLayout>
Expand Down
4 changes: 2 additions & 2 deletions org.envirocar.app/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
<!-- ################# -->
<string name="report_issue_header">Report an Issue</string>
<string name="report_issue_summary">Summary of what went wrong</string>
<string name="report_issue_time_since_crash">Time since crash:</string>
<string name="report_issue_minutes">in minutes</string>
<string name="report_issue_time_since_crash">Estimated time</string>
<string name="report_issue_minutes">10:00</string>

<string name="report_issue_no_checkbox_selected_title">No Checkbox Selected</string>
<string name="report_issue_no_checkbox_selected_content">You have not selected any of the checkboxes. These help developers sort through issues quickly and resolve them. Please consider filling those that are relevant.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private Single<Track> stopTrack() {
LOGGER.info("Trying to stop track");

// Fire a new TrackFinishedEvent on the event bus.
mBus.post(new TrackFinishedEvent(currentTrack));
// mBus.post(new TrackFinishedEvent(currentTrack));
LOGGER.info("posted via eventbus");
track.setTrackStatus(Track.TrackStatus.FINISHED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
.doOnDispose(() -> LOG.info("Location Provider has been disposed!"))
.subscribe(() -> LOG.info("Completed"), LOG::error));
this.recordingStrategy.startRecording(this, new RecordingStrategy.RecordingListener() {
private boolean trackFinished = false;

@Override
public void onRecordingStateChanged(RecordingState recordingState) {
RECORDING_STATE = recordingState;
Expand All @@ -149,8 +151,11 @@ public void onRecordingStateChanged(RecordingState recordingState) {

@Override
public void onTrackFinished(Track track) {
LOG.info("Track has been finished. Throwing TrackFinishedEvent.");
bus.post(new TrackFinishedEvent(track));
if (!trackFinished) {
trackFinished = true;
LOG.info("Track has been finished. Throwing TrackFinishedEvent.");
bus.post(new TrackFinishedEvent(track));
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Copyright (C) 2013 - 2019 the enviroCar community
*
* <p>
* This file is part of the enviroCar app.
*
* <p>
* The enviroCar app is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* The enviroCar app is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License along
* with the enviroCar app. If not, see http://www.gnu.org/licenses/.
*/
Expand All @@ -24,11 +24,11 @@
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.OnNmeaMessageListener;
import android.os.Build;
import android.os.Bundle;

import com.squareup.otto.Bus;

import org.envirocar.app.handler.LocationHandler;
import org.envirocar.app.recording.RecordingScope;
import org.envirocar.core.events.gps.GpsDOPEvent;
import org.envirocar.core.events.gps.GpsLocationChangedEvent;
Expand All @@ -38,6 +38,8 @@
import org.envirocar.core.logging.Logger;
import org.envirocar.core.utils.PermissionUtils;

import java.lang.reflect.Method;

import javax.inject.Inject;

import io.reactivex.Completable;
Expand Down Expand Up @@ -76,100 +78,87 @@ public void onProviderDisabled(String provider) {
}
};

// /**
// * Used for receiving NMEA sentences from the GPS.
// */
// private final OnNmeaMessageListener nmeaListener = new OnNmeaMessageListener() {
// @Override
// public void onNmeaMessage(String nmea, long timestamp) {
//
// }
// };

// private final GpsStatus.NmeaListener mNmeaListener = new GpsStatus.NmeaListener() {
// @Override
// public void onNmeaReceived(long timestamp, String nmea) {
// // eg2.: $GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35
// if (nmea.startsWith(GPGSA)) {
// boolean fix = true;
// if (nmea.charAt(7) == ',' || nmea.charAt(9) == '1') {
// fix = false; // no GPS fix, skip.
// }
//
// int checksumIndex = nmea.lastIndexOf("*");
// String[] values;
// if (checksumIndex > 0) {
// values = nmea.substring(0, checksumIndex).split(NMEA_SEP);
// } else {
// return; // no checksum, skip.
// }
//
// int numberOfSats = resolveSatelliteCount(values);
//
// // fire an event on the GPS status (fix and number of sats)
// mBus.post(new GpsSatelliteFixEvent(numberOfSats, fix));
//
// Double pdop = null, hdop = null, vdop = null;
// if (values.length > 15) {
// pdop = parseDopString(values[15]);
// }
// if (values.length > 16) {
// hdop = parseDopString(values[16]);
// }
// if (values.length > 17) {
// vdop = parseDopString(values[17]);
// }
//
// // Only if positional, horizontal, and vertical DOP is available, then
// // set the DOP accordingly.
// if (pdop != null || hdop != null || vdop != null) {
// // Dultion of Precision (DOP) to specify multiplicative effect of
// // navigation satellite geometry on positional measurement precision.
// // fire an event on the GPS DOP
// mBus.post(new GpsDOPEvent(pdop, hdop, vdop));
// }
// }
// }
//
// /**
// * Resolves the number of satellites.
// *
// * @param values
// * @return number of satellites.
// */
// private int resolveSatelliteCount(String[] values) {
// if (values == null || values.length < 3) {
// return 0;
// }
//
// int result = 0;
// for (int i = 3; i < 15; i++) {
// if (i > values.length - 1) {
// break;
// }
//
// if (!values[i].trim().isEmpty()) {
// result++;
// }
// }
// return result;
// }
//
// /**
// *
// * @param string
// * @return
// */
// private Double parseDopString(String string) {
// if (string == null || string.isEmpty()) return null;
// try {
// return Double.parseDouble(string.trim());
// } catch (RuntimeException e) {
// // TODO no exception catching?
// }
// return null;
// }
// };

private void onNewNmeaUpdate(String nmea, long timestamp) {
// eg2.: $GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35
if (nmea.startsWith(GPGSA)) {
boolean fix = true;
if (nmea.charAt(7) == ',' || nmea.charAt(9) == '1') {
fix = false; // no GPS fix, skip.
}

int checksumIndex = nmea.lastIndexOf("*");
String[] values;
if (checksumIndex > 0) {
values = nmea.substring(0, checksumIndex).split(NMEA_SEP);
} else {
return; // no checksum, skip.
}

int numberOfSats = resolveSatelliteCount(values);

// fire an event on the GPS status (fix and number of sats)
mBus.post(new GpsSatelliteFixEvent(numberOfSats, fix));

Double pdop = null, hdop = null, vdop = null;
if (values.length > 15) {
pdop = parseDopString(values[15]);
}
if (values.length > 16) {
hdop = parseDopString(values[16]);
}
if (values.length > 17) {
vdop = parseDopString(values[17]);
}

// Only if positional, horizontal, and vertical DOP is available, then
// set the DOP accordingly.
if (pdop != null || hdop != null || vdop != null) {
// Dultion of Precision (DOP) to specify multiplicative effect of
// navigation satellite geometry on positional measurement precision.
// fire an event on the GPS DOP
mBus.post(new GpsDOPEvent(pdop, hdop, vdop));
}
}
}

/**
* Resolves the number of satellites.
*
* @param values
* @return number of satellites.
*/
private int resolveSatelliteCount(String[] values) {
if (values == null || values.length < 3) {
return 0;
}

int result = 0;
for (int i = 3; i < 15; i++) {
if (i > values.length - 1) {
break;
}

if (!values[i].trim().isEmpty()) {
result++;
}
}
return result;
}

/**
* @param string
* @return
*/
private Double parseDopString(String string) {
if (string == null || string.isEmpty()) return null;
try {
return Double.parseDouble(string.trim());
} catch (RuntimeException e) {
// TODO no exception catching?
}
return null;
}

// Injected variables.
private final Context mContext;
Expand All @@ -180,6 +169,8 @@ public void onProviderDisabled(String provider) {
// Location fields
private Location mLastBestLocation;

private Method oldAddNmeaMethod;

/**
* Constructor.
*
Expand Down Expand Up @@ -207,13 +198,44 @@ public Completable startLocating() {
emitter.onError(new PermissionException("User has not activated Location permission"));

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
// mLocationManager.addNmeaListener(mNmeaListener);

emitter.setCancellable(() -> {
LOGGER.info("stopLocating()");
mLocationManager.removeUpdates(mLocationListener);
// mLocationManager.removeNmeaListener(mNmeaListener);
});
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final OnNmeaMessageListener nmeaListener = (nmea, timestamp) -> onNewNmeaUpdate(nmea, timestamp);
mLocationManager.addNmeaListener(nmeaListener);

emitter.setCancellable(() -> {
LOGGER.info("stopLocating()");
mLocationManager.removeUpdates(mLocationListener);
mLocationManager.removeNmeaListener(nmeaListener);
});
} else {
final GpsStatus.NmeaListener nmeaListener = (timestamp, nmea) -> onNewNmeaUpdate(nmea, timestamp);

// get the old add nmea method via reflection.
if (oldAddNmeaMethod == null) {
oldAddNmeaMethod = LocationManager.class.getMethod("addNmeaListener", GpsStatus.NmeaListener.class);
}
oldAddNmeaMethod.invoke(mLocationManager, nmeaListener);
emitter.setCancellable(() -> {
LOGGER.info("stopLocating()");
mLocationManager.removeUpdates(mLocationListener);

try {
Method oldRemoveNmeaMethod = LocationManager.class.getMethod("removeNmeaListener", GpsStatus.NmeaListener.class);
oldRemoveNmeaMethod.invoke(mLocationManager, nmeaListener);
} catch (Exception e){
LOGGER.error("unable to remove nmea listener", e);
}
});
}
} catch (Exception e) {
LOGGER.error("Error while initiating NMEA Listener", e);
emitter.setCancellable(() -> {
LOGGER.info("stopLocating()");
mLocationManager.removeUpdates(mLocationListener);
});
}
});
}
}
Loading

0 comments on commit 4cd543a

Please sign in to comment.