Skip to content

Commit

Permalink
Merge pull request #384 from enviroCar/revert-380-revert-379-feature/…
Browse files Browse the repository at this point in the history
…automatic-upload

Revert "Feature/automatic upload""
  • Loading branch information
dewall authored Nov 28, 2019
2 parents 3e69786 + 22e115c commit 028830a
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 59 deletions.
29 changes: 29 additions & 0 deletions org.envirocar.app/res/drawable/ic_cloud_upload_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
Copyright (C) 2013 - 2019 the enviroCar community
This file is part of the enviroCar app.
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.
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.
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/.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM14,13v4h-4v-4H7l5,-5 5,5h-3z"/>
</vector>
8 changes: 8 additions & 0 deletions org.envirocar.app/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@
<string name="notification_autorecording_gps_disabled_subtext">GPS ist deaktiviert. Bitte aktivieren Sie Ihr GPS.</string>
<string name="notification_autorecording_bluetooth_disabled_subtext">Bluetooth ist deaktiviert. Bitte aktivieren Sie Ihr Bluetooth.</string>

<string name="notification_autoupload_success_title">Hochladen einer Fahrt erfolgreich</string>
<string name="notification_autoupload_success_sub">%s wurde erfolgreich auf die enviroCar-Plattform übertragen.</string>
<string name="notification_autoupload_uploading_title">Hochladen einer Fahrt</string>
<string name="notification_autoupload_uploading_sub">%s wird als Open Data auf die enviroCar-Plattform geschoben.</string>
<string name="notification_autoupload_error_title">Hochladen einer Fahrt fehlerhaft</string>
<string name="notification_autoupload_error_sub">Fehler beim Hochladen auf die enviroCar-Plattform.</string>


<!-- ################# -->
<!-- Permissions -->
<!-- ################# -->
Expand Down
6 changes: 3 additions & 3 deletions org.envirocar.app/res/values-de/strings_activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
<string name="pref_settings_other_summary">Version, Lizenzen, etc.</string>

<!-- General Settings-->
<string name="pref_always_upload">Automatisches Hochladen</string>
<string name="pref_always_upload_sub">Automatisches Hochladen von aufgezeichneten Fahrten bei aktiver WiFI Verbinding.</string>
<string name="pref_automatic_upload">Automatisches Hochladen</string>
<string name="pref_automatic_upload_sub">Ermöglicht das automatische Hochladen von aufgenommenen Fahrten nach Beendigung der Aufnahme. Es wird lediglich direkt im Anschluss versucht den Track hochzuladen. Wenn der Upload fehlschlägt (z.B. keine Internetverbindung), muss der Track manuell hochgeladen werden.</string>
<string name="pref_display_always_activ">Bildschrim bleibt aktiv</string>
<string name="pref_display_always_activ_sub">Bildschirm bleibt bei Aufzeichnung einer Fahrt aktiv.</string>
<string name="pref_imperial_unit_summary">Imperiale Maßeinheiten (Meilen) für die Anzeige nutzen</string>
<string name="pref_obfuscate_track">Start und Ziel anonymisieren</string>
<string name="pref_obfuscate_track">Start und Ziel verschleiern</string>
<string name="pref_obfuscate_track_summary">Nur Messungen, die ab einer Entfernung von 250 Metern zum Start/Ziel sowie eine Minute nach Beginn und vor Ende der Route stattfanden, hochladen.</string>
<string name="pref_text_to_speech">Sprachausgabe</string>
<string name="pref_text_to_speech_summary">Ermöglicht die Sprachausgabe von spezifischen Ereignissen, wie z.B. OBD-Verbindung hergestellt oder Aufzeichnung wurde beendet.</string>
Expand Down
9 changes: 8 additions & 1 deletion org.envirocar.app/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<string name="prefkey_imperial_unit" tools:ignore="MissingTranslation">pref_imperial_unit</string>
<string name="prefkey_text_to_speech" tools:ignore="MissingTranslation">pref_text_to_speech</string>
<string name="prefkey_display_always_active" tools:ignore="MissingTranslation">pref_display_always_active</string>
<string name="prefkey_always_upload" tools:ignore="MissingTranslation">pref_always_upload</string>
<string name="prefkey_automatic_upload" tools:ignore="MissingTranslation">pref_automatic_upload</string>
<string name="prefkey_gps_mode_ar" tools:ignore="MissingTranslation">prefkey_gps_mode_ar</string>


Expand All @@ -166,6 +166,13 @@
<string name="notification_autorecording_gps_disabled_subtext">GPS is disabled. Please activate your GPS.</string>
<string name="notification_autorecording_bluetooth_disabled_subtext">Bluetooth is disabled. Please activate your Bluetooth.</string>

<string name="notification_autoupload_success_title">Track Upload Successful</string>
<string name="notification_autoupload_success_sub">%s has been successfully pushed as Open Data to the enviroCar platform.</string>
<string name="notification_autoupload_uploading_title">Uploading Track</string>
<string name="notification_autoupload_uploading_sub">%s gets pushed as Open Data to the enviroCar platform.</string>
<string name="notification_autoupload_error_title">Track Upload Error</string>
<string name="notification_autoupload_error_sub">Error while uploading to the enviroCar platform.</string>


<!-- ################# -->
<!-- Permissions -->
Expand Down
4 changes: 2 additions & 2 deletions org.envirocar.app/res/values/strings_activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<string name="pref_settings_other_summary">Version, Licenses, etc.</string>

<!-- General Settings-->
<string name="pref_always_upload">Automatic Upload (in WiFi only)</string>
<string name="pref_always_upload_sub">Enables the automatic upload of all recorded tracks when connecting to a wireless network.</string>
<string name="pref_automatic_upload">Automatic Upload</string>
<string name="pref_automatic_upload_sub">Enables the automatic upload of recorded tracks after the recording has been finished. It is only tried to upload the track directly afterwards. If the upload fails (e.g. no internet connection), the track has to be uploaded manually.</string>
<string name="pref_display_always_activ">Keep the Screen On</string>
<string name="pref_display_always_activ_sub">When checked, this setting keeps the screen on while the application is running.</string>
<string name="pref_imperial_unit_summary">Use imperial units (miles) for displaying values.</string>
Expand Down
4 changes: 2 additions & 2 deletions org.envirocar.app/res/xml/preferences_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<PreferenceCategory android:title="@string/pref_settings_general">
<CheckBoxPreference
android:key="pref_always_upload"
android:title="@string/pref_always_upload"
android:summary="@string/pref_always_upload_sub"/>
android:title="@string/pref_automatic_upload"
android:summary="@string/pref_automatic_upload_sub"/>
<CheckBoxPreference
android:key="pref_display_always_activ"
android:title="@string/pref_display_always_activ"
Expand Down
10 changes: 5 additions & 5 deletions org.envirocar.app/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<PreferenceCategory
android:title="@string/pref_settings_general"
app:iconSpaceReserved="false">
<!-- <CheckBoxPreference-->
<!-- android:key="@string/prefkey_always_upload"-->
<!-- android:summary="@string/pref_always_upload_sub"-->
<!-- android:title="@string/pref_always_upload"-->
<!-- app:iconSpaceReserved="false" />-->
<CheckBoxPreference
android:key="@string/prefkey_automatic_upload"
android:summary="@string/pref_automatic_upload_sub"
android:title="@string/pref_automatic_upload"
app:iconSpaceReserved="false" />
<CheckBoxPreference
android:key="@string/prefkey_display_always_active"
android:summary="@string/pref_display_always_activ_sub"
Expand Down
3 changes: 3 additions & 0 deletions org.envirocar.app/src/org/envirocar/app/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.envirocar.app.handler.ApplicationSettings;
import org.envirocar.app.handler.LocationHandler;
import org.envirocar.app.handler.userstatistics.UserStatisticsProcessor;
import org.envirocar.app.notifications.AutomaticUploadNotificationHandler;
import org.envirocar.app.notifications.NotificationHandler;
import org.envirocar.app.rxutils.RxBroadcastReceiver;
import org.envirocar.core.injection.InjectApplicationScope;
Expand Down Expand Up @@ -84,6 +85,8 @@ public class BaseApplication extends Application {
protected UserStatisticsProcessor statisticsProcessor;
@Inject
protected LocationHandler locationHandler;
@Inject
protected AutomaticUploadNotificationHandler automaticUploadHandler;


private CompositeDisposable disposables = new CompositeDisposable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.envirocar.app;

import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -166,4 +167,10 @@ public CacheDirectoryProvider provideCacheDirectoryProvider(@InjectApplicationSc
return () -> Util.resolveCacheFolder(context);
}

@Provides
@Singleton
public NotificationManager provideNotificationManager(@InjectApplicationScope Context context){
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
public class ApplicationSettings {

// Default values
public static final boolean DEFAULT_AUTOMATIC_UPLOAD = false;
public static final boolean DEFAULT_BLUETOOTH_AUTOCONNECT = false;
public static final boolean DEFAULT_DISPLAY_STAYS_ACTIVE = false;
public static final boolean DEFAULT_TEXT_TO_SPEECH = false;
public static final boolean DEFAULT_BLUETOOTH_SERVICE_AUTOSTART = true;
public static final boolean DEFAULT_PREF_ENABLE_GPS_BASED_TRACK_RECORDING = false;
public static final boolean DEFAULT_AUTOMATIC_UPLOAD_OF_TRACKS = false;
public static final boolean DEFAULT_OBFUSCATION = false;
public static final int DEFAULT_BLUETOOTH_DISCOVERY_INTERVAL = 60;
public static final int DEFAULT_TRACK_TRIM_DURATION = 110;
Expand All @@ -72,9 +72,9 @@ public class ApplicationSettings {
// public static final String PREF_GPS_BASED_TRACKING = "pref_gps_based_tracking";

// General Settings settings
public static Observable<Boolean> getAutomaticUploadOfTracksObservable(Context context) {
public static Observable<Boolean> getAutomaticUploadObservable(Context context){
return getRxSharedPreferences(context)
.getBoolean(s(context, R.string.prefkey_always_upload), DEFAULT_AUTOMATIC_UPLOAD_OF_TRACKS)
.getBoolean(s(context, R.string.prefkey_automatic_upload), DEFAULT_AUTOMATIC_UPLOAD)
.asObservable();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package org.envirocar.app.notifications;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.core.app.NotificationCompat;

import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import org.envirocar.app.R;
import org.envirocar.app.handler.ApplicationSettings;
import org.envirocar.app.interactor.UploadTrack;
import org.envirocar.core.InternetAccessProvider;
import org.envirocar.core.entity.Track;
import org.envirocar.core.events.TrackFinishedEvent;
import org.envirocar.core.injection.InjectApplicationScope;
import org.envirocar.core.logging.Logger;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.disposables.Disposable;

/**
* @author dewall
*/
@Singleton
public class AutomaticUploadNotificationHandler {
private static final Logger LOG = Logger.getLogger(AutomaticUploadNotificationHandler.class);
private static final String DELETE_ACTION = "com.envirocar.app.events.handler.automaticupload.delete";
private static final IntentFilter DELETE_FILTER = new IntentFilter(DELETE_ACTION);

private enum UploadState {
UPLOADED,
UPLOADING,
ERROR
}

private final Context context;
private final UploadTrack uploadTrack;
private final InternetAccessProvider accessProvider;
private final NotificationManager notificationManager;
private final AutomaticUploadNotification uploadNotification;
private final Bus bus;

private Disposable uploadDisposable;

/**
* Constructor.
*
* @param context
* @param uploadTrack
* @param accessProvider
* @param notificationManager
*/
@Inject
public AutomaticUploadNotificationHandler(@InjectApplicationScope Context context, UploadTrack uploadTrack, InternetAccessProvider accessProvider, NotificationManager notificationManager, Bus bus) {
this.context = context;
this.uploadTrack = uploadTrack;
this.accessProvider = accessProvider;
this.notificationManager = notificationManager;
this.bus = bus;
this.uploadNotification = new AutomaticUploadNotification();

bus.register(this);
}

@Subscribe
public void onTrackFinishedEvent(TrackFinishedEvent e) {
if (!ApplicationSettings.getAutomaticUploadObservable(context).blockingFirst())
return;

LOG.info("Received event %s. Automatic upload is enabled. Trying to upload track");
if (!accessProvider.isConnected()) {
LOG.info("No Internet connection available");
return;
}

if (e.mTrack == null || e.mTrack.getMeasurements().size() <= 2) {
LOG.info("Track has no or too less measurements to upload -> ignoring");
return;
}

// upload the track
this.uploadNotification.setState(UploadState.UPLOADING, e.mTrack);
this.uploadDisposable = uploadTrack.execute(new UploadTrack.Params(e.mTrack))
.subscribe(this::onTrackUploaded, this::onTrackUploadError);
}

/**
* Handler method for onNext()
*
* @param track the uploaded track
*/
private void onTrackUploaded(Track track) {
LOG.info("Track %s has been automatically uploaded.", track.getName());
uploadNotification.setState(UploadState.UPLOADED, track);
}

/**
* Handler method for onError()
*
* @param e the exception
*/
private void onTrackUploadError(Throwable e) {
LOG.error(e);
uploadNotification.setState(UploadState.ERROR);
}

private final class AutomaticUploadNotification implements EnviroCarNotification {
private final String CHANNEL_ID = "com.envirocar.app.events.handler.automaticupload.notification";
private final String CHANNEL_NAME = "Automatic Upload Notification";
private final String CHANNEL_DESCRIPTION = "Notification for the automatic upload of tracks.";
private final int NOTIFICATION_ID = 284;

private final String channel;
private Notification notification;

/**
* @author dewall
*/
AutomaticUploadNotification() {
this.channel = createChannel(notificationManager, CHANNEL_ID, CHANNEL_NAME, CHANNEL_DESCRIPTION, NotificationManager.IMPORTANCE_LOW);
}

public void setState(UploadState state){
setState(state, null);
}

public void setState(UploadState state, Track track) {
String title = null;
String text = null;

switch(state){
case UPLOADED:
title = context.getString(R.string.notification_autoupload_success_title);
text = String.format(context.getString(R.string.notification_autoupload_success_sub), track.getName());
break;
case UPLOADING:
title = context.getString(R.string.notification_autoupload_uploading_title);
text = String.format(context.getString(R.string.notification_autoupload_uploading_sub), track.getName());
break;
case ERROR:
title = context.getString(R.string.notification_autoupload_error_title);
text = String.format(context.getString(R.string.notification_autoupload_error_title));
break;
}

this.notification = new NotificationCompat.Builder(context, channel)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.build();

notificationManager.notify(NOTIFICATION_ID, this.notification);
}

/**
* Deletes the notification
*/
@Override
public void cancel() {
notificationManager.cancel(NOTIFICATION_ID);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.envirocar.app.notifications;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.Color;
import android.os.Build;

/**
* @author dewall
*/
public interface EnviroCarNotification {

default String createChannel(NotificationManager notificationManager, String channelId, String channelName, String channelDescription, int importance) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setDescription(channelDescription);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);

if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}

return channelId;
}
return "";
}

void cancel();
}
Loading

0 comments on commit 028830a

Please sign in to comment.