-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from enviroCar/revert-380-revert-379-feature/…
…automatic-upload Revert "Feature/automatic upload""
- Loading branch information
Showing
18 changed files
with
325 additions
and
59 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
org.envirocar.app/res/drawable/ic_cloud_upload_black_24dp.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
...envirocar.app/src/org/envirocar/app/notifications/AutomaticUploadNotificationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
org.envirocar.app/src/org/envirocar/app/notifications/EnviroCarNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.