diff --git a/app/src/main/java/com/best/deskclock/DeskClock.java b/app/src/main/java/com/best/deskclock/DeskClock.java index 797e2dee1..c62d47f45 100644 --- a/app/src/main/java/com/best/deskclock/DeskClock.java +++ b/app/src/main/java/com/best/deskclock/DeskClock.java @@ -61,6 +61,7 @@ import androidx.viewpager.widget.ViewPager.OnPageChangeListener; import com.best.deskclock.actionbarmenu.MenuItemControllerFactory; import com.best.deskclock.actionbarmenu.OptionsMenuManager; import com.best.deskclock.actionbarmenu.SettingsMenuItemController; +import com.best.deskclock.bedtime.BedtimeService; import com.best.deskclock.data.DataModel; import com.best.deskclock.data.DataModel.SilentSetting; 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. 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. mOptionsMenuManager.addMenuItemController(new SettingsMenuItemController(this)); mOptionsMenuManager.addMenuItemController( @@ -337,24 +341,8 @@ public class DeskClock extends AppCompatActivity @Override protected void onResume() { super.onResume(); - - 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); - } - } - } - } + // Displays the right tab if the application has been minimized and then reopened from the notification. + showTabFromNotifications(); // ViewPager does not save state; this honors the selected tab in the user interface. 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. */ diff --git a/app/src/main/java/com/best/deskclock/bedtime/BedtimeFragment.java b/app/src/main/java/com/best/deskclock/bedtime/BedtimeFragment.java index 940fa6019..c7b72379c 100644 --- a/app/src/main/java/com/best/deskclock/bedtime/BedtimeFragment.java +++ b/app/src/main/java/com/best/deskclock/bedtime/BedtimeFragment.java @@ -104,7 +104,6 @@ public final class BedtimeFragment extends DeskClockFragment implements mEmptyView.setCompoundDrawablePadding(Utils.toPixel(30, getContext())); mEmptyViewController = new EmptyViewController(mBedtimeView, mMainLayout, mEmptyView); - mEmptyViewController.setEmpty(mAlarm == null); mTxtBedtime = view.findViewById(R.id.bedtime_time); mTxtWakeup = view.findViewById(R.id.wakeup_time); diff --git a/app/src/main/java/com/best/deskclock/bedtime/BedtimeService.java b/app/src/main/java/com/best/deskclock/bedtime/BedtimeService.java index 8e69f1412..a9c065742 100644 --- a/app/src/main/java/com/best/deskclock/bedtime/BedtimeService.java +++ b/app/src/main/java/com/best/deskclock/bedtime/BedtimeService.java @@ -17,7 +17,6 @@ package com.best.deskclock.bedtime; import static com.best.deskclock.NotificationUtils.BEDTIME_NOTIFICATION_CHANNEL_ID; -import static com.best.deskclock.uidata.UiDataModel.Tab.BEDTIME; import android.Manifest; import android.app.AlarmManager; @@ -40,6 +39,7 @@ import android.os.Environment; import android.os.IBinder; import android.provider.Settings; +import androidx.annotation.StringRes; import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationCompat; 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.provider.Alarm; import com.best.deskclock.provider.AlarmInstance; -import com.best.deskclock.uidata.UiDataModel; import java.io.File; import java.io.FileOutputStream; @@ -67,9 +66,8 @@ import java.util.Objects; 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"; // Notification @@ -99,14 +97,6 @@ public final class BedtimeService extends Service { case ACTION_BED_REMIND_NOTIF -> showRemindNotification(context); case ACTION_LAUNCH_BEDTIME -> startBed(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 -> { stopBed(context, saver); AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE); @@ -211,12 +201,20 @@ public final class BedtimeService extends Service { } 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( context, BEDTIME_NOTIFICATION_CHANNEL_ID) .setShowWhen(false) .setContentTitle(context.getString(R.string.bedtime_reminder_notification_title, bedtime)) .setContentText(context.getString(R.string.bedtime_reminder_notification_text, wake, diff)) + .setContentIntent(pendingShowApp) .setColor(context.getColor(R.color.md_theme_primary)) .setSmallIcon(R.drawable.ic_moon) .setPriority(NotificationCompat.PRIORITY_HIGH) @@ -224,12 +222,6 @@ public final class BedtimeService extends Service { .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .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); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 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) { 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( context, BEDTIME_NOTIFICATION_CHANNEL_ID) .setShowWhen(false) .setContentTitle(context.getString(R.string.bedtime_notification_title)) .setContentText(text) + .setContentIntent(pendingShowApp) .setColor(context.getColor(R.color.md_theme_primary)) .setSmallIcon(R.drawable.ic_tab_bedtime) .setPriority(NotificationCompat.PRIORITY_HIGH) @@ -261,12 +262,6 @@ public final class BedtimeService extends Service { .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .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); pause.setAction(ACTION_BEDTIME_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) { 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( context, BEDTIME_NOTIFICATION_CHANNEL_ID) .setShowWhen(false) .setContentTitle(context.getString(R.string.bedtime_paused_notification_title)) .setContentText(text) + .setContentIntent(pendingShowApp) .setColor(context.getColor(R.color.md_theme_primary)) .setSmallIcon(R.drawable.ic_moon) .setPriority(NotificationCompat.PRIORITY_HIGH) @@ -309,12 +313,6 @@ public final class BedtimeService extends Service { .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .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); it.setAction(ACTION_LAUNCH_BEDTIME); builder.addAction(R.drawable.ic_fab_play, context.getString(R.string.bedtime_notification_resume_action), PendingIntent.getService(context, notifId,