Fix clicked bedtime notification

- The application reopens on the bedtime tab when the notification is clicked;
- Don't display the empty view of bedtime if an alarm has already been created;
- Fix lint warnings about bedtime notifications;
- The application reopens on the correct tab if the application is closed and the stopwatch, timer or bedtime notification is clicked;
This commit is contained in:
BlackyHawky 2024-03-17 02:01:25 +01:00
parent eb2e18df20
commit c3b7b5434a
3 changed files with 58 additions and 49 deletions

View file

@ -61,6 +61,7 @@ import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import com.best.deskclock.actionbarmenu.MenuItemControllerFactory; import com.best.deskclock.actionbarmenu.MenuItemControllerFactory;
import com.best.deskclock.actionbarmenu.OptionsMenuManager; import com.best.deskclock.actionbarmenu.OptionsMenuManager;
import com.best.deskclock.actionbarmenu.SettingsMenuItemController; import com.best.deskclock.actionbarmenu.SettingsMenuItemController;
import com.best.deskclock.bedtime.BedtimeService;
import com.best.deskclock.data.DataModel; import com.best.deskclock.data.DataModel;
import com.best.deskclock.data.DataModel.SilentSetting; import com.best.deskclock.data.DataModel.SilentSetting;
import com.best.deskclock.data.OnSilentSettingsListener; import com.best.deskclock.data.OnSilentSettingsListener;
@ -227,6 +228,9 @@ public class DeskClock extends AppCompatActivity
// and the necessary permissions to be granted by the user. // and the necessary permissions to be granted by the user.
firstRunDialog(); firstRunDialog();
// Displays the right tab if the application has been closed and then reopened from the notification.
showTabFromNotifications();
// Configure the menu item controllers add behavior to the toolbar. // Configure the menu item controllers add behavior to the toolbar.
mOptionsMenuManager.addMenuItemController(new SettingsMenuItemController(this)); mOptionsMenuManager.addMenuItemController(new SettingsMenuItemController(this));
mOptionsMenuManager.addMenuItemController( mOptionsMenuManager.addMenuItemController(
@ -337,24 +341,8 @@ public class DeskClock extends AppCompatActivity
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// Displays the right tab if the application has been minimized and then reopened from the notification.
final Intent intent = getIntent(); showTabFromNotifications();
if (intent != null) {
final String action = intent.getAction();
if (action != null) {
int label = intent.getIntExtra(Events.EXTRA_EVENT_LABEL, R.string.label_intent);
switch (action) {
case TimerService.ACTION_SHOW_TIMER -> {
Events.sendTimerEvent(R.string.action_show, label);
UiDataModel.getUiDataModel().setSelectedTab(UiDataModel.Tab.TIMERS);
}
case StopwatchService.ACTION_SHOW_STOPWATCH -> {
Events.sendStopwatchEvent(R.string.action_show, label);
UiDataModel.getUiDataModel().setSelectedTab(UiDataModel.Tab.STOPWATCH);
}
}
}
}
// ViewPager does not save state; this honors the selected tab in the user interface. // ViewPager does not save state; this honors the selected tab in the user interface.
updateCurrentTab(); updateCurrentTab();
@ -562,6 +550,30 @@ public class DeskClock extends AppCompatActivity
} }
} }
private void showTabFromNotifications() {
final Intent intent = getIntent();
if (intent != null) {
final String action = intent.getAction();
if (action != null) {
int label = intent.getIntExtra(Events.EXTRA_EVENT_LABEL, R.string.label_intent);
switch (action) {
case TimerService.ACTION_SHOW_TIMER -> {
Events.sendTimerEvent(R.string.action_show, label);
UiDataModel.getUiDataModel().setSelectedTab(UiDataModel.Tab.TIMERS);
}
case StopwatchService.ACTION_SHOW_STOPWATCH -> {
Events.sendStopwatchEvent(R.string.action_show, label);
UiDataModel.getUiDataModel().setSelectedTab(UiDataModel.Tab.STOPWATCH);
}
case BedtimeService.ACTION_SHOW_BEDTIME -> {
Events.sendBedtimeEvent(R.string.action_show, label);
UiDataModel.getUiDataModel().setSelectedTab(UiDataModel.Tab.BEDTIME);
}
}
}
}
}
/** /**
* Configure the {@link #mBottomNavigation} to display UiDataModel's selected tab. * Configure the {@link #mBottomNavigation} to display UiDataModel's selected tab.
*/ */

View file

@ -104,7 +104,6 @@ public final class BedtimeFragment extends DeskClockFragment implements
mEmptyView.setCompoundDrawablePadding(Utils.toPixel(30, getContext())); mEmptyView.setCompoundDrawablePadding(Utils.toPixel(30, getContext()));
mEmptyViewController = new EmptyViewController(mBedtimeView, mMainLayout, mEmptyView); mEmptyViewController = new EmptyViewController(mBedtimeView, mMainLayout, mEmptyView);
mEmptyViewController.setEmpty(mAlarm == null);
mTxtBedtime = view.findViewById(R.id.bedtime_time); mTxtBedtime = view.findViewById(R.id.bedtime_time);
mTxtWakeup = view.findViewById(R.id.wakeup_time); mTxtWakeup = view.findViewById(R.id.wakeup_time);

View file

@ -17,7 +17,6 @@
package com.best.deskclock.bedtime; package com.best.deskclock.bedtime;
import static com.best.deskclock.NotificationUtils.BEDTIME_NOTIFICATION_CHANNEL_ID; import static com.best.deskclock.NotificationUtils.BEDTIME_NOTIFICATION_CHANNEL_ID;
import static com.best.deskclock.uidata.UiDataModel.Tab.BEDTIME;
import android.Manifest; import android.Manifest;
import android.app.AlarmManager; import android.app.AlarmManager;
@ -40,6 +39,7 @@ import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.provider.Settings; import android.provider.Settings;
import androidx.annotation.StringRes;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
@ -55,7 +55,6 @@ import com.best.deskclock.data.DataModel;
import com.best.deskclock.events.Events; import com.best.deskclock.events.Events;
import com.best.deskclock.provider.Alarm; import com.best.deskclock.provider.Alarm;
import com.best.deskclock.provider.AlarmInstance; import com.best.deskclock.provider.AlarmInstance;
import com.best.deskclock.uidata.UiDataModel;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -67,9 +66,8 @@ import java.util.Objects;
public final class BedtimeService extends Service { public final class BedtimeService extends Service {
private static final String ACTION_PREFIX = "com.android.deskclock.action."; private static final String ACTION_PREFIX = "com.best.deskclock.action.";
// shows the tab with the bedtime
public static final String ACTION_SHOW_BEDTIME = ACTION_PREFIX + "SHOW_BEDTIME"; public static final String ACTION_SHOW_BEDTIME = ACTION_PREFIX + "SHOW_BEDTIME";
// Notification // Notification
@ -99,14 +97,6 @@ public final class BedtimeService extends Service {
case ACTION_BED_REMIND_NOTIF -> showRemindNotification(context); case ACTION_BED_REMIND_NOTIF -> showRemindNotification(context);
case ACTION_LAUNCH_BEDTIME -> startBed(context, saver); case ACTION_LAUNCH_BEDTIME -> startBed(context, saver);
case ACTION_BEDTIME_CANCEL -> stopBed(context, saver); case ACTION_BEDTIME_CANCEL -> stopBed(context, saver);
case ACTION_SHOW_BEDTIME -> {
Events.sendBedtimeEvent(R.string.action_show, R.string.label_notification);
// Open DeskClock positioned on the bedtime tab.
UiDataModel.getUiDataModel().setSelectedTab(BEDTIME);
final Intent showBedtime = new Intent(this, DeskClock.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showBedtime);
}
case ACTION_BEDTIME_PAUSE -> { case ACTION_BEDTIME_PAUSE -> {
stopBed(context, saver); stopBed(context, saver);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE); AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
@ -211,12 +201,20 @@ public final class BedtimeService extends Service {
} }
wake = h + ":" + alarm.minutes + ending; wake = h + ":" + alarm.minutes + ending;
// Intent to load the app when the notification is tapped.
@StringRes final int eventLabel = R.string.label_notification;
final Intent showApp = new Intent(context, DeskClock.class)
.setAction(ACTION_SHOW_BEDTIME)
.putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);
final PendingIntent pendingShowApp = Utils.pendingActivityIntent(context, showApp);
NotificationCompat.Builder builder = new NotificationCompat.Builder( NotificationCompat.Builder builder = new NotificationCompat.Builder(
context, BEDTIME_NOTIFICATION_CHANNEL_ID) context, BEDTIME_NOTIFICATION_CHANNEL_ID)
.setShowWhen(false) .setShowWhen(false)
.setContentTitle(context.getString(R.string.bedtime_reminder_notification_title, bedtime)) .setContentTitle(context.getString(R.string.bedtime_reminder_notification_title, bedtime))
.setContentText(context.getString(R.string.bedtime_reminder_notification_text, wake, diff)) .setContentText(context.getString(R.string.bedtime_reminder_notification_text, wake, diff))
.setContentIntent(pendingShowApp)
.setColor(context.getColor(R.color.md_theme_primary)) .setColor(context.getColor(R.color.md_theme_primary))
.setSmallIcon(R.drawable.ic_moon) .setSmallIcon(R.drawable.ic_moon)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
@ -224,12 +222,6 @@ public final class BedtimeService extends Service {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLocalOnly(true); .setLocalOnly(true);
// Setup content intent
Intent i = new Intent(context, BedtimeService.class);
i.setAction(ACTION_SHOW_BEDTIME);
builder.setContentIntent(PendingIntent.getService(context, notifId,
i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
NotificationManagerCompat nm = NotificationManagerCompat.from(context); NotificationManagerCompat nm = NotificationManagerCompat.from(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationUtils.createChannel(context, BEDTIME_NOTIFICATION_CHANNEL_ID); NotificationUtils.createChannel(context, BEDTIME_NOTIFICATION_CHANNEL_ID);
@ -249,11 +241,20 @@ public final class BedtimeService extends Service {
private static void showLaunchNotification(Context context, String text) { private static void showLaunchNotification(Context context, String text) {
LogUtils.v("Displaying upcomming notif:" + "Bed"); LogUtils.v("Displaying upcomming notif:" + "Bed");
// Intent to load the app when the notification is tapped.
@StringRes final int eventLabel = R.string.label_notification;
final Intent showApp = new Intent(context, DeskClock.class)
.setAction(ACTION_SHOW_BEDTIME)
.putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);
final PendingIntent pendingShowApp = Utils.pendingActivityIntent(context, showApp);
NotificationCompat.Builder builder = new NotificationCompat.Builder( NotificationCompat.Builder builder = new NotificationCompat.Builder(
context, BEDTIME_NOTIFICATION_CHANNEL_ID) context, BEDTIME_NOTIFICATION_CHANNEL_ID)
.setShowWhen(false) .setShowWhen(false)
.setContentTitle(context.getString(R.string.bedtime_notification_title)) .setContentTitle(context.getString(R.string.bedtime_notification_title))
.setContentText(text) .setContentText(text)
.setContentIntent(pendingShowApp)
.setColor(context.getColor(R.color.md_theme_primary)) .setColor(context.getColor(R.color.md_theme_primary))
.setSmallIcon(R.drawable.ic_tab_bedtime) .setSmallIcon(R.drawable.ic_tab_bedtime)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
@ -261,12 +262,6 @@ public final class BedtimeService extends Service {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLocalOnly(true); .setLocalOnly(true);
// Setup content intent
Intent i = new Intent(context, BedtimeService.class);
i.setAction(ACTION_SHOW_BEDTIME);
builder.setContentIntent(PendingIntent.getService(context, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
Intent pause = new Intent(context, BedtimeService.class); Intent pause = new Intent(context, BedtimeService.class);
pause.setAction(ACTION_BEDTIME_PAUSE); pause.setAction(ACTION_BEDTIME_PAUSE);
builder.addAction(R.drawable.ic_fab_pause, context.getString(R.string.bedtime_notification_action_pause), builder.addAction(R.drawable.ic_fab_pause, context.getString(R.string.bedtime_notification_action_pause),
@ -297,11 +292,20 @@ public final class BedtimeService extends Service {
private static void showPausedNotification(Context context, String text) { private static void showPausedNotification(Context context, String text) {
LogUtils.v("Displaying upcomming notif:" + "Bed"); LogUtils.v("Displaying upcomming notif:" + "Bed");
// Intent to load the app when the notification is tapped.
@StringRes final int eventLabel = R.string.label_notification;
final Intent showApp = new Intent(context, DeskClock.class)
.setAction(ACTION_SHOW_BEDTIME)
.putExtra(Events.EXTRA_EVENT_LABEL, eventLabel);
final PendingIntent pendingShowApp = Utils.pendingActivityIntent(context, showApp);
NotificationCompat.Builder builder = new NotificationCompat.Builder( NotificationCompat.Builder builder = new NotificationCompat.Builder(
context, BEDTIME_NOTIFICATION_CHANNEL_ID) context, BEDTIME_NOTIFICATION_CHANNEL_ID)
.setShowWhen(false) .setShowWhen(false)
.setContentTitle(context.getString(R.string.bedtime_paused_notification_title)) .setContentTitle(context.getString(R.string.bedtime_paused_notification_title))
.setContentText(text) .setContentText(text)
.setContentIntent(pendingShowApp)
.setColor(context.getColor(R.color.md_theme_primary)) .setColor(context.getColor(R.color.md_theme_primary))
.setSmallIcon(R.drawable.ic_moon) .setSmallIcon(R.drawable.ic_moon)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
@ -309,12 +313,6 @@ public final class BedtimeService extends Service {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLocalOnly(true); .setLocalOnly(true);
// Setup content intent
Intent i = new Intent(context, BedtimeService.class);
i.setAction(ACTION_SHOW_BEDTIME);
builder.setContentIntent(PendingIntent.getService(context, notifId,
i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
Intent it = new Intent(context, BedtimeService.class); Intent it = new Intent(context, BedtimeService.class);
it.setAction(ACTION_LAUNCH_BEDTIME); it.setAction(ACTION_LAUNCH_BEDTIME);
builder.addAction(R.drawable.ic_fab_play, context.getString(R.string.bedtime_notification_resume_action), PendingIntent.getService(context, notifId, builder.addAction(R.drawable.ic_fab_play, context.getString(R.string.bedtime_notification_resume_action), PendingIntent.getService(context, notifId,