Skip to content

Commit

Permalink
Update notification for Android Oreo
Browse files Browse the repository at this point in the history
- Add link to notification channel settings
- Remove snooze button

Closes iSoron#400
  • Loading branch information
iSoron committed Apr 21, 2018
1 parent 2bfd4a9 commit 5865eb4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
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
{
Expand Down Expand Up @@ -61,6 +65,14 @@ public void onCreate(Bundle savedInstanceState)
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
Expand Down Expand Up @@ -88,6 +100,17 @@ public boolean onPreferenceTreeClick(Preference preference)
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.*;
Expand All @@ -39,12 +40,15 @@
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;

Expand Down Expand Up @@ -196,10 +200,6 @@ public void onPostExecute()
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);

Expand All @@ -208,30 +208,38 @@ public void onPostExecute()
// 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()
Expand All @@ -245,4 +253,19 @@ private boolean shouldShowReminderToday()
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);
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5865eb4

Please sign in to comment.