Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification signal settings. #305 #403

Merged
merged 7 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void onReceive(Context context, Intent intent)
{
Uri taskUri = intent.getData();

boolean silent = intent.getBooleanExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, false);
boolean noSignal = intent.getBooleanExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, false);
// check for pinned task
if (TaskNotificationHandler.isTaskPinned(context, taskUri))
{
TaskNotificationHandler.sendPinnedTaskStartNotification(context, taskUri, silent);
TaskNotificationHandler.sendPinnedTaskStartNotification(context, taskUri, noSignal);
return;
}

Expand All @@ -64,7 +64,7 @@ public void onReceive(Context context, Intent intent)
String timezone = intent.getStringExtra(TaskContract.EXTRA_TASK_TIMEZONE);
int notificationId = (int) ContentUris.parseId(taskUri);

NotificationActionUtils.sendStartNotification(context, title, taskUri, notificationId, startDate, startAllDay, timezone, silent);
NotificationActionUtils.sendStartNotification(context, title, taskUri, notificationId, startDate, startAllDay, timezone, noSignal);

}
}
Expand All @@ -74,12 +74,12 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE))
{
Uri taskUri = intent.getData();

boolean silent = intent.getBooleanExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, false);
boolean noSignal = intent.getBooleanExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, false);

// check for pinned task
if (TaskNotificationHandler.isTaskPinned(context, taskUri))
{
TaskNotificationHandler.sendPinnedTaskDueNotification(context, taskUri, silent);
TaskNotificationHandler.sendPinnedTaskDueNotification(context, taskUri, noSignal);
return;
}

Expand All @@ -91,7 +91,7 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE))
String timezone = intent.getStringExtra(TaskContract.EXTRA_TASK_TIMEZONE);
int notificationId = (int) ContentUris.parseId(taskUri);

NotificationActionUtils.sendDueAlarmNotification(context, title, taskUri, notificationId, dueDate, dueAllDay, timezone, silent);
NotificationActionUtils.sendDueAlarmNotification(context, title, taskUri, notificationId, dueDate, dueAllDay, timezone, noSignal);
}
}

Expand All @@ -101,7 +101,7 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE))
public boolean isNotificationEnabled(Context context)
{
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
return settings.getBoolean(context.getString(R.string.schedjoules_pref_notification_enabled), true);
return settings.getBoolean(context.getString(R.string.opentasks_pref_notification_enabled), true);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import android.widget.RemoteViews;

import org.dmfs.tasks.R;
import org.dmfs.tasks.notification.signals.NoSignal;
import org.dmfs.tasks.notification.signals.SwitchableSignal;
import org.dmfs.tasks.utils.DateFormatter;
import org.dmfs.tasks.utils.DateFormatter.DateFormatContext;

Expand All @@ -60,9 +62,9 @@ public class NotificationActionUtils
public final static String EXTRA_NOTIFICATION_ACTION = "org.dmfs.tasks.extra.notification.EXTRA_NOTIFICATION_ACTION";

/**
* Boolean extra to indicate that a notification is supposed to be silent, i.e. should not play a sound when it's fired.
* Boolean extra to indicate that a notification is not supposed to be signalling, i.e. should not play sound, vibrate or blink the lights when it's fired.
*/
public final static String EXTRA_SILENT_NOTIFICATION = "org.dmfs.provider.tasks.extra.SILENT";
public final static String EXTRA_NOTIFICATION_NO_SIGNAL = "org.dmfs.provider.tasks.extra.NO_SIGNAL";

private static long TIMEOUT_MILLIS = 10000;
private static long sUndoTimeoutMillis = -1;
Expand Down Expand Up @@ -110,14 +112,7 @@ public static void sendDueAlarmNotification(Context context, String title, Uri t
mBuilder.setTicker(title);

// enable light, sound and vibration
if (silent)
{
mBuilder.setDefaults(0);
}
else
{
mBuilder.setDefaults(Notification.DEFAULT_ALL);
}
mBuilder.setDefaults(new SwitchableSignal(context, silent).value());

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(Intent.ACTION_VIEW);
Expand Down Expand Up @@ -199,14 +194,7 @@ public static void sendStartNotification(Context context, String title, Uri task
mBuilder.setTicker(title);

// enable light, sound and vibration
if (silent)
{
mBuilder.setDefaults(0);
}
else
{
mBuilder.setDefaults(Notification.DEFAULT_ALL);
}
mBuilder.setDefaults(new SwitchableSignal(context, silent).value());

// set notification time
// set displayed time
Expand Down Expand Up @@ -277,8 +265,7 @@ public static void createUndoNotification(final Context context, NotificationAct
builder.setSmallIcon(R.drawable.ic_notification);
builder.setWhen(action.getWhen());

// disable sound & vibration
builder.setDefaults(0);
builder.setDefaults(new NoSignal().value());

final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification);
undoView.setTextViewText(R.id.description_text, context.getString(action.mActionTextResId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.dmfs.tasks.notification;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.Notification;
Expand Down Expand Up @@ -56,6 +55,7 @@
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.notification.NotificationActionUtils.NotificationAction;
import org.dmfs.tasks.notification.signals.SwitchableSignal;

import java.util.ArrayList;
import java.util.TimeZone;
Expand Down Expand Up @@ -131,7 +131,7 @@ public int onStartCommand(Intent intent, int flags, int startId)
{

String intentAction = intent.getAction();
boolean silent = intent.getBooleanExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, false);
boolean noSignal = intent.getBooleanExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, false);
if (intentAction != null)
{
switch (intentAction)
Expand All @@ -142,8 +142,8 @@ public int onStartCommand(Intent intent, int flags, int startId)

case ACTION_PINNED_TASK_START:
case ACTION_PINNED_TASK_DUE:
updateNotifications(true, !silent, !silent);
if (!silent)
updateNotifications(true, noSignal, !noSignal);
if (!noSignal)
{
delayedCancelHeadsUpNotification();
}
Expand Down Expand Up @@ -176,23 +176,23 @@ public int onStartCommand(Intent intent, int flags, int startId)
case Intent.ACTION_BOOT_COMPLETED:
case Intent.ACTION_REBOOT:
case TaskNotificationHandler.ACTION_FASTBOOT:
updateNotifications(true, false, false);
updateNotifications(true, true, false);
break;

case Intent.ACTION_DATE_CHANGED:
case Intent.ACTION_TIME_CHANGED:
case Intent.ACTION_TIMEZONE_CHANGED:
case ACTION_NEXT_DAY:
updateNextDayAlarm();
updateNotifications(false, false, false);
updateNotifications(false, true, false);
break;

case ACTION_CANCEL_HEADUP_NOTIFICATION:
updateNotifications(false, false, false);
updateNotifications(false, true, false);
break;

default:
updateNotifications(false, false, false);
updateNotifications(false, true, false);
break;
}
}
Expand All @@ -207,27 +207,15 @@ public int onStartCommand(Intent intent, int flags, int startId)
}


@SuppressWarnings("unused")
private void pinNewTask(Intent intent)
{
// check for new task to pin
if (intent.hasExtra(EXTRA_NEW_PINNED_TASK))
{
ContentSet newTaskToPin = intent.getParcelableExtra(EXTRA_NEW_PINNED_TASK);
makePinNotification(this, mBuilder, newTaskToPin, true, true, false);
}
}


private void updateNotifications(boolean isReboot, boolean withSound, boolean withHeadsUpNotification)
private void updateNotifications(boolean isReboot, boolean noSignal, boolean withHeadsUpNotification)
{
// update pinned tasks
mTasksToPin = queryTasksToPin();
updatePinnedNotifications(mTasksToPin, isReboot, withSound, withHeadsUpNotification);
updatePinnedNotifications(mTasksToPin, isReboot, noSignal, withHeadsUpNotification);
}


private void updatePinnedNotifications(ArrayList<ContentSet> tasksToPin, boolean isReboot, boolean withSound, boolean withHeadsUpNotification)
private void updatePinnedNotifications(ArrayList<ContentSet> tasksToPin, boolean isReboot, boolean noSignal, boolean withHeadsUpNotification)
{
ArrayList<Uri> pinnedTaskUris = TaskNotificationHandler.getPinnedTaskUris(this);

Expand All @@ -238,7 +226,7 @@ private void updatePinnedNotifications(ArrayList<ContentSet> tasksToPin, boolean
boolean isAlreadyShown = pinnedTaskUris.contains(taskContentSet.getUri());
Integer taskId = TaskFieldAdapters.TASK_ID.get(taskContentSet);
notificationManager.notify(taskId,
makePinNotification(this, mBuilder, taskContentSet, !isAlreadyShown || withSound, !isAlreadyShown || withSound, withHeadsUpNotification));
makePinNotification(this, mBuilder, taskContentSet, isAlreadyShown && noSignal, isAlreadyShown && noSignal, withHeadsUpNotification));
}

// remove old notifications
Expand Down Expand Up @@ -279,32 +267,6 @@ private boolean containsTask(ArrayList<ContentSet> tasks, Uri taskUri)
}


@SuppressLint("InlinedApi")
public void resendPinNotification(Uri taskUri)
{
if (taskUri == null)
{
return;
}
long taskId = ContentUris.parseId(taskUri);
if (taskId < 0)
{
return;
}
Integer notificationId = Long.valueOf(taskId).intValue();
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setOngoing(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
mBuilder.setPriority(Notification.PRIORITY_HIGH);
}

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, mBuilder.build());
}


private ArrayList<ContentSet> queryTasksToPin()
{
ArrayList<ContentSet> tasksToPin = new ArrayList<ContentSet>(20);
Expand Down Expand Up @@ -347,7 +309,7 @@ private ArrayList<ContentSet> queryTasksToPin()


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static Notification makePinNotification(Context context, Builder builder, ContentSet task, boolean withSound, boolean withTickerText,
private static Notification makePinNotification(Context context, Builder builder, ContentSet task, boolean noSignal, boolean withTickerText,
boolean withHeadsUpNotification)
{
Resources resources = context.getResources();
Expand Down Expand Up @@ -430,15 +392,7 @@ private static Notification makePinNotification(Context context, Builder builder
// unpin action
builder.addAction(NotificationUpdaterService.getUnpinAction(context, TaskFieldAdapters.TASK_ID.get(task), task.getUri()));

// sound
if (withSound)
{
builder.setDefaults(Notification.DEFAULT_ALL);
}
else
{
builder.setDefaults(Notification.DEFAULT_LIGHTS);
}
builder.setDefaults(new SwitchableSignal(context, noSignal).value());

return builder.build();
}
Expand Down Expand Up @@ -525,7 +479,7 @@ private void resendNotification(NotificationAction notificationAction)
startIntent.setPackage(getApplicationContext().getPackageName());
startIntent.putExtra(TaskContract.EXTRA_TASK_TITLE, notificationAction.title());
startIntent.putExtra(TaskContract.EXTRA_TASK_TIMESTAMP, notificationAction.getWhen());
startIntent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, true);
startIntent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, true);
sendBroadcast(startIntent);

// Due broadcast
Expand All @@ -534,7 +488,7 @@ private void resendNotification(NotificationAction notificationAction)
dueIntent.setPackage(getApplicationContext().getPackageName());
dueIntent.putExtra(TaskContract.EXTRA_TASK_TITLE, notificationAction.title());
dueIntent.putExtra(TaskContract.EXTRA_TASK_TIMESTAMP, notificationAction.getWhen());
dueIntent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, true);
dueIntent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, true);
sendBroadcast(dueIntent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ static ArrayList<Uri> getPinnedTaskUris(Context context)
}


public static void sendPinnedTaskStartNotification(Context context, Uri taskUri, boolean silent)
public static void sendPinnedTaskStartNotification(Context context, Uri taskUri, boolean noSignal)
{
Intent intent = new Intent(context, NotificationUpdaterService.class);
intent.setAction(NotificationUpdaterService.ACTION_PINNED_TASK_START);
intent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, silent);
intent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, noSignal);
intent.setData(taskUri);
context.startService(intent);
}


public static void sendPinnedTaskDueNotification(Context context, Uri taskUri, boolean silent)
public static void sendPinnedTaskDueNotification(Context context, Uri taskUri, boolean noSignal)
{
Intent intent = new Intent(context, NotificationUpdaterService.class);
intent.setAction(NotificationUpdaterService.ACTION_PINNED_TASK_DUE);
intent.putExtra(NotificationActionUtils.EXTRA_SILENT_NOTIFICATION, silent);
intent.putExtra(NotificationActionUtils.EXTRA_NOTIFICATION_NO_SIGNAL, noSignal);
intent.setData(taskUri);
context.startService(intent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 dmfs GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dmfs.tasks.notification.signals;

import android.app.Notification;


/**
* Represents {@link NotificationSignal} for {@link Notification#DEFAULT_ALL}.
*
* @author Gabor Keszthelyi
*/
public final class AllSignal implements NotificationSignal
{
@Override
public int value()
{
return Notification.DEFAULT_ALL;
}
}
Loading