mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Update notification for Android Oreo
- Add link to notification channel settings - Remove snooze button Closes #400
This commit is contained in:
@@ -22,12 +22,16 @@ package org.isoron.uhabits.activities.settings;
|
|||||||
import android.app.backup.*;
|
import android.app.backup.*;
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import android.os.*;
|
import android.os.*;
|
||||||
|
import android.provider.*;
|
||||||
import android.support.v7.preference.*;
|
import android.support.v7.preference.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.activities.habits.list.*;
|
import org.isoron.uhabits.activities.habits.list.*;
|
||||||
|
import org.isoron.uhabits.notifications.*;
|
||||||
import org.isoron.uhabits.utils.*;
|
import org.isoron.uhabits.utils.*;
|
||||||
|
|
||||||
|
import static android.os.Build.VERSION.*;
|
||||||
|
|
||||||
public class SettingsFragment extends PreferenceFragmentCompat
|
public class SettingsFragment extends PreferenceFragmentCompat
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener
|
implements SharedPreferences.OnSharedPreferenceChangeListener
|
||||||
{
|
{
|
||||||
@@ -61,6 +65,14 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
setResultOnPreferenceClick("bugReport", ListHabitsScreen.RESULT_BUG_REPORT);
|
setResultOnPreferenceClick("bugReport", ListHabitsScreen.RESULT_BUG_REPORT);
|
||||||
|
|
||||||
updateRingtoneDescription();
|
updateRingtoneDescription();
|
||||||
|
|
||||||
|
if (SDK_INT < Build.VERSION_CODES.O)
|
||||||
|
findPreference("reminderCustomize").setVisible(false);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
findPreference("reminderSound").setVisible(false);
|
||||||
|
findPreference("pref_snooze_interval").setVisible(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,6 +100,17 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
RINGTONE_REQUEST_CODE);
|
RINGTONE_REQUEST_CODE);
|
||||||
return true;
|
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);
|
return super.onPreferenceTreeClick(preference);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ package org.isoron.uhabits.notifications;
|
|||||||
import android.app.*;
|
import android.app.*;
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import android.graphics.*;
|
import android.graphics.*;
|
||||||
|
import android.os.*;
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
import android.support.v4.app.*;
|
import android.support.v4.app.*;
|
||||||
import android.support.v4.app.NotificationCompat.*;
|
import android.support.v4.app.NotificationCompat.*;
|
||||||
@@ -39,12 +40,15 @@ import java.util.*;
|
|||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
import static android.graphics.BitmapFactory.*;
|
import static android.graphics.BitmapFactory.*;
|
||||||
|
import static android.os.Build.VERSION.*;
|
||||||
import static org.isoron.uhabits.utils.RingtoneUtils.*;
|
import static org.isoron.uhabits.utils.RingtoneUtils.*;
|
||||||
|
|
||||||
@AppScope
|
@AppScope
|
||||||
public class NotificationTray
|
public class NotificationTray
|
||||||
implements CommandRunner.Listener, Preferences.Listener
|
implements CommandRunner.Listener, Preferences.Listener
|
||||||
{
|
{
|
||||||
|
public static final String REMINDERS_CHANNEL_ID = "REMINDERS";
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
|
||||||
@@ -196,10 +200,6 @@ public class NotificationTray
|
|||||||
context.getString(R.string.check),
|
context.getString(R.string.check),
|
||||||
pendingIntents.addCheckmark(habit, timestamp));
|
pendingIntents.addCheckmark(habit, timestamp));
|
||||||
|
|
||||||
Action snoozeAction = new Action(R.drawable.ic_action_snooze,
|
|
||||||
context.getString(R.string.snooze),
|
|
||||||
pendingIntents.snoozeNotification(habit));
|
|
||||||
|
|
||||||
Bitmap wearableBg =
|
Bitmap wearableBg =
|
||||||
decodeResource(context.getResources(), R.drawable.stripe);
|
decodeResource(context.getResources(), R.drawable.stripe);
|
||||||
|
|
||||||
@@ -208,30 +208,38 @@ public class NotificationTray
|
|||||||
// WearableExtender.
|
// WearableExtender.
|
||||||
WearableExtender wearableExtender = new WearableExtender()
|
WearableExtender wearableExtender = new WearableExtender()
|
||||||
.setBackground(wearableBg)
|
.setBackground(wearableBg)
|
||||||
.addAction(checkAction)
|
.addAction(checkAction);
|
||||||
.addAction(snoozeAction);
|
|
||||||
|
|
||||||
Notification notification = new NotificationCompat.Builder(context)
|
Builder builder = new Builder(context, REMINDERS_CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
.setContentTitle(habit.getName())
|
.setContentTitle(habit.getName())
|
||||||
.setContentText(habit.getDescription())
|
.setContentText(habit.getDescription())
|
||||||
.setContentIntent(pendingIntents.showHabit(habit))
|
.setContentIntent(pendingIntents.showHabit(habit))
|
||||||
.setDeleteIntent(pendingIntents.dismissNotification(habit))
|
.setDeleteIntent(pendingIntents.dismissNotification(habit))
|
||||||
.addAction(checkAction)
|
.addAction(checkAction)
|
||||||
.addAction(snoozeAction)
|
|
||||||
.setSound(getRingtoneUri(context))
|
.setSound(getRingtoneUri(context))
|
||||||
.extend(wearableExtender)
|
|
||||||
.setWhen(reminderTime)
|
.setWhen(reminderTime)
|
||||||
.setShowWhen(true)
|
.setShowWhen(true)
|
||||||
.setOngoing(preferences.shouldMakeNotificationsSticky())
|
.setOngoing(preferences.shouldMakeNotificationsSticky());
|
||||||
.build();
|
|
||||||
|
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 notificationManager =
|
||||||
(NotificationManager) context.getSystemService(
|
(NotificationManager) context.getSystemService(
|
||||||
Activity.NOTIFICATION_SERVICE);
|
Activity.NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
|
createAndroidNotificationChannel(context);
|
||||||
int notificationId = getNotificationId(habit);
|
int notificationId = getNotificationId(habit);
|
||||||
notificationManager.notify(notificationId, notification);
|
notificationManager.notify(notificationId, builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean shouldShowReminderToday()
|
private boolean shouldShowReminderToday()
|
||||||
@@ -245,4 +253,19 @@ public class NotificationTray
|
|||||||
return reminderDays[weekday];
|
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="by_score">By score</string>
|
||||||
<string name="download">Download</string>
|
<string name="download">Download</string>
|
||||||
<string name="export">Export</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>
|
</resources>
|
||||||
@@ -68,6 +68,11 @@
|
|||||||
android:title="@string/sticky_notifications"
|
android:title="@string/sticky_notifications"
|
||||||
android:summary="@string/sticky_notifications_description"/>
|
android:summary="@string/sticky_notifications_description"/>
|
||||||
|
|
||||||
|
<Preference
|
||||||
|
android:key="reminderCustomize"
|
||||||
|
android:summary="@string/customize_notification_summary"
|
||||||
|
android:title="@string/customize_notification"/>
|
||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
<PreferenceCategory
|
<PreferenceCategory
|
||||||
|
|||||||
Reference in New Issue
Block a user