Update notification for Android Oreo

- Add link to notification channel settings
- Remove snooze button

Closes #400
pull/419/merge
Alinson S. Xavier 8 years ago
parent 2bfd4a942d
commit 5865eb41f7

@ -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);
}

@ -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);
}
}
}

@ -204,4 +204,6 @@
<string name="by_score">By score</string>
<string name="download">Download</string>
<string name="export">Export</string>
<string name="customize_notification_summary">Change sound, vibration, light and other notification settings</string>
<string name="customize_notification">Customize notifications</string>
</resources>

@ -68,6 +68,11 @@
android:title="@string/sticky_notifications"
android:summary="@string/sticky_notifications_description"/>
<Preference
android:key="reminderCustomize"
android:summary="@string/customize_notification_summary"
android:title="@string/customize_notification"/>
</PreferenceCategory>
<PreferenceCategory

Loading…
Cancel
Save