Skip to content

Commit

Permalink
Added a feature to disable notifications on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 20, 2018
1 parent 67ced89 commit cb7f7af
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 74 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/marianhello/bgloc/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;

import com.marianhello.bgloc.data.AbstractLocationTemplate;
import com.marianhello.bgloc.data.LocationTemplate;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class Config implements Parcelable
private Boolean stopOnTerminate;
private Boolean startOnBoot;
private Boolean startForeground;
private Boolean notificationsEnabled;
private Boolean stopOnStillActivity;
private String url;
private String syncUrl;
Expand Down Expand Up @@ -83,6 +85,7 @@ public Config(Config config) {
this.stopOnTerminate = config.stopOnTerminate;
this.startOnBoot = config.startOnBoot;
this.startForeground = config.startForeground;
this.notificationsEnabled = config.notificationsEnabled;
this.stopOnStillActivity = config.stopOnStillActivity;
this.url = config.url;
this.syncUrl = config.syncUrl;
Expand All @@ -107,6 +110,7 @@ private Config(Parcel in) {
setStopOnTerminate((Boolean) in.readValue(null));
setStartOnBoot((Boolean) in.readValue(null));
setStartForeground((Boolean) in.readValue(null));
setNotificationsEnabled((Boolean) in.readValue(null));
setLocationProvider(in.readInt());
setInterval(in.readInt());
setFastestInterval(in.readInt());
Expand Down Expand Up @@ -139,6 +143,7 @@ public static Config getDefault() {
config.stopOnTerminate = true;
config.startOnBoot = false;
config.startForeground = true;
config.notificationsEnabled = true;
config.stopOnStillActivity = true;
config.url = "";
config.syncUrl = "";
Expand Down Expand Up @@ -168,6 +173,7 @@ public void writeToParcel(Parcel out, int flags) {
out.writeValue(getStopOnTerminate());
out.writeValue(getStartOnBoot());
out.writeValue(getStartForeground());
out.writeValue(getNotificationsEnabled());
out.writeInt(getLocationProvider());
out.writeInt(getInterval());
out.writeInt(getFastestInterval());
Expand Down Expand Up @@ -342,6 +348,19 @@ public void setStartForeground(Boolean startForeground) {
this.startForeground = startForeground;
}

public boolean hasNotificationsEnabled() {
return notificationsEnabled != null;
}

@Nullable
public Boolean getNotificationsEnabled() {
return notificationsEnabled;
}

public void setNotificationsEnabled(@Nullable Boolean notificationsEnabled) {
this.notificationsEnabled = notificationsEnabled;
}

public boolean hasLocationProvider() {
return locationProvider != null;
}
Expand Down Expand Up @@ -515,6 +534,7 @@ public String toString () {
.append(" stopOnStillActivity=").append(getStopOnStillActivity())
.append(" startOnBoot=").append(getStartOnBoot())
.append(" startForeground=").append(getStartForeground())
.append(" notificationsEnabled=").append(getNotificationsEnabled())
.append(" locationProvider=").append(getLocationProvider())
.append(" nTitle=").append(getNotificationTitle())
.append(" nText=").append(getNotificationText())
Expand Down Expand Up @@ -595,6 +615,9 @@ public static Config merge(Config config1, Config config2) {
if (config2.hasStartForeground()) {
merger.setStartForeground(config2.getStartForeground());
}
if (config2.hasNotificationsEnabled()) {
merger.setNotificationsEnabled(config2.getNotificationsEnabled());
}
if (config2.hasStopOnStillActivity()) {
merger.setStopOnStillActivity(config2.getStopOnStillActivity());
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/marianhello/bgloc/LocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,18 @@ public void run() {
// was not running in foreground, so start in foreground
startForeground();
} else {
// was running in foreground, so just update existing notification
Notification notification = new NotificationHelper.NotificationFactory(LocationService.this).getNotification(
mConfig.getNotificationTitle(),
mConfig.getNotificationText(),
mConfig.getLargeNotificationIcon(),
mConfig.getSmallNotificationIcon(),
mConfig.getNotificationIconColor());

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
if (currentConfig.getNotificationsEnabled()) {
// was running in foreground, so just update existing notification
Notification notification = new NotificationHelper.NotificationFactory(LocationService.this).getNotification(
mConfig.getNotificationTitle(),
mConfig.getNotificationText(),
mConfig.getLargeNotificationIcon(),
mConfig.getSmallNotificationIcon(),
mConfig.getNotificationIconColor());

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ protected AbstractLocationProvider(Context context) {

@Override
public void onCreate() {
toneGenerator = new android.media.ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
//toneGenerator = new android.media.ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
}

@Override
public void onDestroy() {
toneGenerator.release();
//toneGenerator.release();
toneGenerator = null;
}

Expand Down
80 changes: 49 additions & 31 deletions src/oreo/java/com/marianhello/bgloc/sync/SyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter implements Uploadin
private ConfigurationDAO configDAO;
private NotificationManager notificationManager;
private BatchManager batchManager;
private boolean notificationsEnabled = true;

private org.slf4j.Logger logger;

Expand Down Expand Up @@ -99,6 +100,9 @@ public void onPerformSync(
return;
}

//noinspection ConstantConditions
notificationsEnabled = !config.hasNotificationsEnabled() || config.getNotificationsEnabled();

Long batchStartMillis = System.currentTimeMillis();
boolean isForced = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL);
int syncThreshold = isForced ? 0 : config.getSyncThreshold();
Expand Down Expand Up @@ -137,54 +141,68 @@ public void onPerformSync(
}

private boolean uploadLocations(File file, String url, HashMap httpHeaders) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), NotificationHelper.SYNC_CHANNEL_ID);
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
notificationManager.notify(NOTIFICATION_ID, builder.build());
NotificationCompat.Builder builder = null;

if (notificationsEnabled) {
builder = new NotificationCompat.Builder(getContext(), NotificationHelper.SYNC_CHANNEL_ID);
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}

try {
int responseCode = HttpPostService.postFile(url, file, httpHeaders, this);
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
builder.setContentText("Sync completed");
} else {
builder.setContentText("Sync failed due server error");

if (builder != null) {
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
builder.setContentText("Sync completed");
} else {
builder.setContentText("Sync failed due server error");
}
}

return responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED;
} catch (IOException e) {
logger.warn("Error uploading locations: {}", e.getMessage());
builder.setContentText("Sync failed: " + e.getMessage());

if (builder != null)
builder.setContentText("Sync failed: " + e.getMessage());
} finally {
logger.info("Syncing endAt: {}", System.currentTimeMillis());

builder.setOngoing(false);
builder.setProgress(0, 0, false);
builder.setAutoCancel(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());

Handler h = new Handler(Looper.getMainLooper());
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
public void run() {
logger.info("Notification cancelledAt: {}", System.currentTimeMillis());
notificationManager.cancel(NOTIFICATION_ID);
}
}, delayInMilliseconds);
if (builder != null) {
builder.setOngoing(false);
builder.setProgress(0, 0, false);
builder.setAutoCancel(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());

Handler h = new Handler(Looper.getMainLooper());
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
public void run() {
logger.info("Notification cancelledAt: {}", System.currentTimeMillis());
notificationManager.cancel(NOTIFICATION_ID);
}
}, delayInMilliseconds);
}
}

return false;
}

public void uploadListener(int progress) {
logger.debug("Syncing progress: {} updatedAt: {}", progress, System.currentTimeMillis());
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), NotificationHelper.SYNC_CHANNEL_ID);
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
builder.setProgress(100, progress, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());

if (notificationsEnabled) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), NotificationHelper.SYNC_CHANNEL_ID);
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
builder.setProgress(100, progress, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
}
80 changes: 49 additions & 31 deletions src/preoreo/java/com/marianhello/bgloc/sync/SyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter implements Uploadin
private ConfigurationDAO configDAO;
private NotificationManager notificationManager;
private BatchManager batchManager;
private boolean notificationsEnabled = true;

private org.slf4j.Logger logger;

Expand Down Expand Up @@ -99,6 +100,9 @@ public void onPerformSync(
return;
}

//noinspection ConstantConditions
notificationsEnabled = !config.hasNotificationsEnabled() || config.getNotificationsEnabled();

Long batchStartMillis = System.currentTimeMillis();
boolean isForced = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL);
int syncThreshold = isForced ? 0 : config.getSyncThreshold();
Expand Down Expand Up @@ -137,54 +141,68 @@ public void onPerformSync(
}

private boolean uploadLocations(File file, String url, HashMap httpHeaders) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
notificationManager.notify(NOTIFICATION_ID, builder.build());
NotificationCompat.Builder builder = null;

if (notificationsEnabled) {
builder = new NotificationCompat.Builder(getContext());
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}

try {
int responseCode = HttpPostService.postFile(url, file, httpHeaders, this);
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
builder.setContentText("Sync completed");
} else {
builder.setContentText("Sync failed due server error");

if (builder != null) {
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
builder.setContentText("Sync completed");
} else {
builder.setContentText("Sync failed due server error");
}
}

return responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED;
} catch (IOException e) {
logger.warn("Error uploading locations: {}", e.getMessage());
builder.setContentText("Sync failed: " + e.getMessage());

if (builder != null)
builder.setContentText("Sync failed: " + e.getMessage());
} finally {
logger.info("Syncing endAt: {}", System.currentTimeMillis());

builder.setOngoing(false);
builder.setProgress(0, 0, false);
builder.setAutoCancel(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());

Handler h = new Handler(Looper.getMainLooper());
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
public void run() {
logger.info("Notification cancelledAt: {}", System.currentTimeMillis());
notificationManager.cancel(NOTIFICATION_ID);
}
}, delayInMilliseconds);
if (builder != null) {
builder.setOngoing(false);
builder.setProgress(0, 0, false);
builder.setAutoCancel(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());

Handler h = new Handler(Looper.getMainLooper());
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
public void run() {
logger.info("Notification cancelledAt: {}", System.currentTimeMillis());
notificationManager.cancel(NOTIFICATION_ID);
}
}, delayInMilliseconds);
}
}

return false;
}

public void uploadListener(int progress) {
logger.debug("Syncing progress: {} updatedAt: {}", progress, System.currentTimeMillis());
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
builder.setProgress(100, progress, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());

if (notificationsEnabled) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());
builder.setOngoing(true);
builder.setContentTitle("Syncing locations");
builder.setContentText("Sync in progress");
builder.setSmallIcon(android.R.drawable.ic_dialog_info);
builder.setProgress(100, progress, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
}

0 comments on commit cb7f7af

Please sign in to comment.