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 531c491
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 75 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 @@ -23,6 +23,7 @@ public static abstract class ConfigurationEntry implements BaseColumns {
public static final String COLUMN_NAME_STOP_TERMINATE = "stop_terminate";
public static final String COLUMN_NAME_START_BOOT = "start_boot";
public static final String COLUMN_NAME_START_FOREGROUND = "start_foreground";
public static final String COLUMN_NAME_NOTIFICATIONS_ENABLED = "notifications_enabled";
public static final String COLUMN_NAME_STOP_ON_STILL = "stop_still";
public static final String COLUMN_NAME_LOCATION_PROVIDER = "service_provider";
public static final String COLUMN_NAME_INTERVAL = "interval";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Config retrieveConfiguration() throws JSONException {
ConfigurationEntry.COLUMN_NAME_STOP_ON_STILL,
ConfigurationEntry.COLUMN_NAME_START_BOOT,
ConfigurationEntry.COLUMN_NAME_START_FOREGROUND,
ConfigurationEntry.COLUMN_NAME_NOTIFICATIONS_ENABLED,
ConfigurationEntry.COLUMN_NAME_LOCATION_PROVIDER,
ConfigurationEntry.COLUMN_NAME_INTERVAL,
ConfigurationEntry.COLUMN_NAME_FASTEST_INTERVAL,
Expand Down Expand Up @@ -111,6 +112,7 @@ private Config hydrate(Cursor c) throws JSONException {
config.setStopOnStillActivity( (c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_STOP_ON_STILL)) == 1) ? true : false );
config.setStartOnBoot( (c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_START_BOOT)) == 1) ? true : false );
config.setStartForeground( (c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_START_FOREGROUND)) == 1) ? true : false );
config.setNotificationsEnabled( (c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_NOTIFICATIONS_ENABLED)) == 1) ? true : false );
config.setLocationProvider(c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_LOCATION_PROVIDER)));
config.setInterval(c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_INTERVAL)));
config.setFastestInterval(c.getInt(c.getColumnIndex(ConfigurationEntry.COLUMN_NAME_FASTEST_INTERVAL)));
Expand Down Expand Up @@ -141,6 +143,7 @@ private ContentValues getContentValues(Config config) throws NullPointerExceptio
values.put(ConfigurationEntry.COLUMN_NAME_STOP_ON_STILL, (config.getStopOnStillActivity() == true) ? 1 : 0);
values.put(ConfigurationEntry.COLUMN_NAME_START_BOOT, (config.getStartOnBoot() == true) ? 1 : 0);
values.put(ConfigurationEntry.COLUMN_NAME_START_FOREGROUND, (config.getStartForeground() == true) ? 1 : 0);
values.put(ConfigurationEntry.COLUMN_NAME_NOTIFICATIONS_ENABLED, (config.getNotificationsEnabled() == true) ? 1 : 0);
values.put(ConfigurationEntry.COLUMN_NAME_LOCATION_PROVIDER, config.getLocationProvider());
values.put(ConfigurationEntry.COLUMN_NAME_INTERVAL, config.getInterval());
values.put(ConfigurationEntry.COLUMN_NAME_FASTEST_INTERVAL, config.getFastestInterval());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class SQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {
private static final String TAG = SQLiteOpenHelper.class.getName();
public static final String SQLITE_DATABASE_NAME = "cordova_bg_geolocation.db";
public static final int DATABASE_VERSION = 14;
public static final int DATABASE_VERSION = 15;

private static final String TEXT_TYPE = " TEXT";
private static final String INTEGER_TYPE = " INTEGER";
Expand Down Expand Up @@ -60,6 +60,7 @@ public class SQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {
ConfigurationEntry.COLUMN_NAME_STOP_ON_STILL + INTEGER_TYPE + COMMA_SEP +
ConfigurationEntry.COLUMN_NAME_START_BOOT + INTEGER_TYPE + COMMA_SEP +
ConfigurationEntry.COLUMN_NAME_START_FOREGROUND + INTEGER_TYPE + COMMA_SEP +
ConfigurationEntry.COLUMN_NAME_NOTIFICATIONS_ENABLED + INTEGER_TYPE + COMMA_SEP +
ConfigurationEntry.COLUMN_NAME_LOCATION_PROVIDER + TEXT_TYPE + COMMA_SEP +
ConfigurationEntry.COLUMN_NAME_INTERVAL + INTEGER_TYPE + COMMA_SEP +
ConfigurationEntry.COLUMN_NAME_FASTEST_INTERVAL + INTEGER_TYPE + COMMA_SEP +
Expand Down Expand Up @@ -165,6 +166,9 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 13:
alterSql.add("ALTER TABLE " + LocationEntry.TABLE_NAME +
" ADD COLUMN " + LocationEntry.COLUMN_NAME_MOCK_FLAGS + INTEGER_TYPE);
case 14:
alterSql.add("ALTER TABLE " + ConfigurationEntry.TABLE_NAME +
" ADD COLUMN " + ConfigurationEntry.COLUMN_NAME_NOTIFICATIONS_ENABLED + INTEGER_TYPE);

break; // DO NOT FORGET TO MOVE DOWN BREAK ON DB UPGRADE!!!
default:
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());
}
}
}
Loading

0 comments on commit 531c491

Please sign in to comment.