diff --git a/app/src/main/java/org/isoron/uhabits/activities/settings/SettingsFragment.java b/app/src/main/java/org/isoron/uhabits/activities/settings/SettingsFragment.java index 330e05b34..ee540597c 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/settings/SettingsFragment.java +++ b/app/src/main/java/org/isoron/uhabits/activities/settings/SettingsFragment.java @@ -22,12 +22,16 @@ package org.isoron.uhabits.activities.settings; import android.app.backup.*; import android.content.*; import android.os.*; +import android.provider.*; import android.support.v7.preference.*; import org.isoron.uhabits.R; import org.isoron.uhabits.activities.habits.list.*; +import org.isoron.uhabits.notifications.*; import org.isoron.uhabits.utils.*; +import static android.os.Build.VERSION.*; + public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -61,6 +65,14 @@ public class SettingsFragment extends PreferenceFragmentCompat setResultOnPreferenceClick("bugReport", ListHabitsScreen.RESULT_BUG_REPORT); updateRingtoneDescription(); + + if (SDK_INT < Build.VERSION_CODES.O) + findPreference("reminderCustomize").setVisible(false); + else + { + findPreference("reminderSound").setVisible(false); + findPreference("pref_snooze_interval").setVisible(false); + } } @Override @@ -88,6 +100,17 @@ public class SettingsFragment extends PreferenceFragmentCompat RINGTONE_REQUEST_CODE); return true; } + else if (key.equals("reminderCustomize")) + { + if (SDK_INT < Build.VERSION_CODES.O) return true; + + NotificationTray.createAndroidNotificationChannel(getContext()); + Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); + intent.putExtra(Settings.EXTRA_APP_PACKAGE, getContext().getPackageName()); + intent.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationTray.REMINDERS_CHANNEL_ID); + startActivity(intent); + return true; + } return super.onPreferenceTreeClick(preference); } diff --git a/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java b/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java index d6a0253a2..15fccba6a 100644 --- a/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java +++ b/app/src/main/java/org/isoron/uhabits/notifications/NotificationTray.java @@ -22,6 +22,7 @@ package org.isoron.uhabits.notifications; import android.app.*; import android.content.*; import android.graphics.*; +import android.os.*; import android.support.annotation.*; import android.support.v4.app.*; import android.support.v4.app.NotificationCompat.*; @@ -39,12 +40,15 @@ import java.util.*; import javax.inject.*; import static android.graphics.BitmapFactory.*; +import static android.os.Build.VERSION.*; import static org.isoron.uhabits.utils.RingtoneUtils.*; @AppScope public class NotificationTray implements CommandRunner.Listener, Preferences.Listener { + public static final String REMINDERS_CHANNEL_ID = "REMINDERS"; + @NonNull private final Context context; @@ -196,10 +200,6 @@ public class NotificationTray context.getString(R.string.check), pendingIntents.addCheckmark(habit, timestamp)); - Action snoozeAction = new Action(R.drawable.ic_action_snooze, - context.getString(R.string.snooze), - pendingIntents.snoozeNotification(habit)); - Bitmap wearableBg = decodeResource(context.getResources(), R.drawable.stripe); @@ -208,30 +208,38 @@ public class NotificationTray // WearableExtender. WearableExtender wearableExtender = new WearableExtender() .setBackground(wearableBg) - .addAction(checkAction) - .addAction(snoozeAction); + .addAction(checkAction); - Notification notification = new NotificationCompat.Builder(context) + Builder builder = new Builder(context, REMINDERS_CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(habit.getName()) .setContentText(habit.getDescription()) .setContentIntent(pendingIntents.showHabit(habit)) .setDeleteIntent(pendingIntents.dismissNotification(habit)) .addAction(checkAction) - .addAction(snoozeAction) .setSound(getRingtoneUri(context)) - .extend(wearableExtender) .setWhen(reminderTime) .setShowWhen(true) - .setOngoing(preferences.shouldMakeNotificationsSticky()) - .build(); + .setOngoing(preferences.shouldMakeNotificationsSticky()); + + if(SDK_INT < Build.VERSION_CODES.O) { + Action snoozeAction = new Action(R.drawable.ic_action_snooze, + context.getString(R.string.snooze), + pendingIntents.snoozeNotification(habit)); + + wearableExtender.addAction(snoozeAction); + builder.addAction(snoozeAction); + } + + builder.extend(wearableExtender); NotificationManager notificationManager = (NotificationManager) context.getSystemService( Activity.NOTIFICATION_SERVICE); + createAndroidNotificationChannel(context); int notificationId = getNotificationId(habit); - notificationManager.notify(notificationId, notification); + notificationManager.notify(notificationId, builder.build()); } private boolean shouldShowReminderToday() @@ -245,4 +253,19 @@ public class NotificationTray return reminderDays[weekday]; } } + + public static void createAndroidNotificationChannel(Context context) { + NotificationManager notificationManager = + (NotificationManager) context.getSystemService( + Activity.NOTIFICATION_SERVICE); + + if (SDK_INT >= Build.VERSION_CODES.O) + { + NotificationChannel channel = + new NotificationChannel(REMINDERS_CHANNEL_ID, + context.getResources().getString(R.string.reminder), + NotificationManager.IMPORTANCE_DEFAULT); + notificationManager.createNotificationChannel(channel); + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1e94bea9a..90483be90 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -204,4 +204,6 @@ By score Download Export + Change sound, vibration, light and other notification settings + Customize notifications \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 092a373b6..028c02ecb 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -68,6 +68,11 @@ android:title="@string/sticky_notifications" android:summary="@string/sticky_notifications_description"/> + +