Skip to content

Commit

Permalink
chore(settings): dynamically generate notification settings
Browse files Browse the repository at this point in the history
  • Loading branch information
EdricChan03 committed Mar 5, 2018
1 parent 96c1547 commit 470bdbe
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 92 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/edricchan/studybuddy/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ public void setupNotificationChannels() {
channels.add(playbackChannel);

// Uncategorized notifications
NotificationChannel uncategorizedChannel = new NotificationChannel("uncategorized", getString(R.string.notification_channel_uncategorized_title), NotificationManager.IMPORTANCE_DEFAULT);
uncategorizedChannel.setDescription(getString(R.string.notification_channel_uncategorized_desc));
NotificationChannel uncategorizedChannel = new NotificationChannel("uncategorized", getString(R.string.notification_channel_uncategorised_title), NotificationManager.IMPORTANCE_DEFAULT);
uncategorizedChannel.setDescription(getString(R.string.notification_channel_uncategorised_desc));
uncategorizedChannel.setShowBadge(true);
channels.add(uncategorizedChannel);
// Pass list to method
Expand Down
186 changes: 133 additions & 53 deletions app/src/main/java/com/edricchan/studybuddy/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.RingtonePreference;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.Snackbar;
Expand All @@ -40,11 +41,13 @@
class MyNotificationChannel {
private int notificationTitle;
private int notificationDesc;
private int notificationId;
private int index;

public MyNotificationChannel(int notificationTitle, int notificationDesc, int index) {
public MyNotificationChannel(int notificationTitle, int notificationDesc, int notificationId, int index) {
this.notificationTitle = notificationTitle;
this.notificationDesc = notificationDesc;
this.notificationId = notificationId;
this.index = index;
}

Expand All @@ -59,6 +62,10 @@ public int getNotificationDesc() {
public int getIndex() {
return this.index;
}

public int getNotificationId() {
return this.notificationId;
}
}

/**
Expand Down Expand Up @@ -269,67 +276,120 @@ public static class NotificationPreferenceFragment extends PreferenceFragment {
List<MyNotificationChannel> notificationChannels = new ArrayList<MyNotificationChannel>();
PreferenceScreen preferenceScreen;
String[] notificationChannelIds = new String[]{"todo_updates", "weekly_summary", "sync", "app_updates", "playback", "uncategorized"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
setHasOptionsMenu(true);
preferenceScreen = getPreferenceScreen();
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_todo_updates_title, R.string.notification_channel_todo_updates_desc, 0));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_weekly_summary_title, R.string.notification_channel_weekly_summary_desc, 1));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_sync_title, R.string.notification_channel_sync_desc, 2));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_app_updates_title, R.string.notification_channel_app_updates_desc, 3));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_playback_title, R.string.notification_channel_playback_desc, 4));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_uncategorized_title, R.string.notification_channel_uncategorized_desc, 5));
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
/*
Check if user is running on Android Oreo since there's notification channels support
- Removes the multi select preference
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
preferenceScreen.removePreference(findPreference("notifications_categories"));
PreferenceCategory notificationPrefCategory = new PreferenceCategory(preferenceScreen.getContext());
notificationPrefCategory.setTitle("Notification Channels");
preferenceScreen.addPreference(notificationPrefCategory);
Preference allNotificationsPreference = new Preference(preferenceScreen.getContext());
allNotificationsPreference.setTitle(R.string.notification_channel_all_channels_title);
allNotificationsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
SharedPreferences sharedPreferences;

void showOreoSettings() {
PreferenceCategory notificationPrefCategory = new PreferenceCategory(preferenceScreen.getContext());
notificationPrefCategory.setTitle("Notification Channels");
preferenceScreen.addPreference(notificationPrefCategory);
Preference allNotificationsPreference = new Preference(preferenceScreen.getContext());
allNotificationsPreference.setTitle(R.string.notification_channel_all_channels_title);
allNotificationsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());
startActivity(intent);
}
return false;
}
});
notificationPrefCategory.addPreference(allNotificationsPreference);
for (MyNotificationChannel notificationChannel : notificationChannels) {
Preference notificationPreference = new Preference(preferenceScreen.getContext());
notificationPreference.setTitle(notificationChannel.getNotificationTitle());
notificationPreference.setSummary(notificationChannel.getNotificationDesc());
notificationPreference.setKey(notificationChannelIds[notificationChannel.getIndex()]);
notificationPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, preference.getKey());
startActivity(intent);
}
return false;
}
});
notificationPrefCategory.addPreference(allNotificationsPreference);
for (MyNotificationChannel notificationChannel : notificationChannels) {
Preference notificationPreference = new Preference(preferenceScreen.getContext());
notificationPreference.setTitle(notificationChannel.getNotificationTitle());
notificationPreference.setSummary(notificationChannel.getNotificationDesc());
notificationPreference.setKey(notificationChannelIds[notificationChannel.getIndex()]);
notificationPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, preference.getKey());
startActivity(intent);
}
return false;
}
});
notificationPrefCategory.addPreference(notificationPreference);
}
notificationPrefCategory.addPreference(notificationPreference);
}
}

void showPreOreoSettings() {
SwitchPreference enableAllNotificationChannelsPreference = new SwitchPreference(preferenceScreen.getContext());
enableAllNotificationChannelsPreference.setTitle(R.string.enable_all_notification_channels_pref_title);
enableAllNotificationChannelsPreference.setKey("enable_all_notification_channels");
preferenceScreen.addPreference(enableAllNotificationChannelsPreference);
for (MyNotificationChannel notificationChannel : notificationChannels) {
PreferenceCategory preOreoNotificationChannelCategory = new PreferenceCategory(preferenceScreen.getContext());
preferenceScreen.addPreference(preOreoNotificationChannelCategory);
preOreoNotificationChannelCategory.setTitle(notificationChannel.getNotificationTitle());
preOreoNotificationChannelCategory.setDependency("enable_all_notification_channels");
Preference preOreoNotificationDescPreference = new Preference(preferenceScreen.getContext());
preOreoNotificationDescPreference.setEnabled(false);
preOreoNotificationDescPreference.setTitle("About this channel");
preOreoNotificationDescPreference.setSummary(notificationChannel.getNotificationDesc());
preOreoNotificationChannelCategory.addPreference(preOreoNotificationDescPreference);
SwitchPreference enableNotificationPreference = new SwitchPreference(preferenceScreen.getContext());
enableNotificationPreference.setChecked(true);
enableNotificationPreference.setTitle("Enable notification channel");
enableNotificationPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println("Preference change: " + newValue);
return true;
}
});
enableNotificationPreference.setKey("notification_channel_enabled_" + getString(notificationChannel.getNotificationId()));
SwitchPreference enableVibratePreference = new SwitchPreference(preferenceScreen.getContext());
preOreoNotificationChannelCategory.addPreference(enableNotificationPreference);
enableVibratePreference.setTitle("Vibrate");
enableVibratePreference.setSummary("Whether to vibrate when you receive a notification from this channel");
enableVibratePreference.setDefaultValue(true);
enableVibratePreference.setIcon(R.drawable.ic_vibrate_white_24dp);
enableVibratePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println("Preference change: " + newValue);
return true;
}
});
preOreoNotificationChannelCategory.addPreference(enableVibratePreference);
RingtonePreference notificationRingtonePreference = new RingtonePreference(preferenceScreen.getContext());
notificationRingtonePreference.setIcon(R.drawable.ic_music_white_24dp);
notificationRingtonePreference.setTitle("Set ringtone");
bindPreferenceSummaryToValue(notificationRingtonePreference);
preOreoNotificationChannelCategory.addPreference(notificationRingtonePreference);
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
setHasOptionsMenu(true);
preferenceScreen = this.getPreferenceScreen();
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_todo_updates_title, R.string.notification_channel_todo_updates_desc, R.string.notification_channel_todo_updates_id, 0));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_weekly_summary_title, R.string.notification_channel_weekly_summary_desc, R.string.notification_channel_weekly_summary_id, 1));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_sync_title, R.string.notification_channel_sync_desc, R.string.notification_channel_sync_id, 2));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_app_updates_title, R.string.notification_channel_app_updates_desc, R.string.notification_channel_app_updates_id, 3));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_playback_title, R.string.notification_channel_playback_desc, R.string.notification_channel_playback_id, 4));
notificationChannels.add(new MyNotificationChannel(R.string.notification_channel_uncategorised_title, R.string.notification_channel_uncategorised_desc, R.string.notification_channel_uncategorised_id, 5));
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
/*
Check if user is running on Android Oreo since there's notification channels support
- Removes the multi select preference
*/
if (sharedPreferences.getBoolean("enable_pre_oreo_explicit", false) || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// User is using pre-Oreo or has enabled the explicit pre-Oreo checkbox
System.out.println("Showing pre-Oreo settings");
showPreOreoSettings();
} else {
System.out.println("Showing Oreo settings");
// User is using Android Oreo
showOreoSettings();
}
}

Expand All @@ -350,13 +410,33 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_help:
// TODO: Add some stuff
return true;
case R.id.action_checkbox_ui_for_pre_oreo:
if (item.isChecked()) {
item.setChecked(false);
System.out.println("Checked -> Unchecked");
sharedPreferences.edit()
.putBoolean("enable_pre_oreo_explicit", false)
.apply();
} else {
item.setChecked(true);
System.out.println("Unchecked -> checked");
sharedPreferences.edit()
.putBoolean("enable_pre_oreo_explicit", true)
.apply();
}
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_settings, menu);
inflater.inflate(R.menu.menu_settings_notifications, menu);
if (BuildConfig.DEBUG) {
menu.findItem(R.id.action_checkbox_ui_for_pre_oreo)
.setVisible(true)
.setChecked(sharedPreferences.getBoolean("enable_pre_oreo_explicit", false));
}
}

public void registerOnSharedPreferenceChangeListener() {
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<string name="action_calendar_title">Calendar</string>
<string name="action_notifications_title">Notifications</string>
<string name="action_help">Help</string>
<string name="action_pre_oreo_ui">Show UI for pre-Oreo devices</string>

<string-array name="pref_updates_channels_values">
<item>1</item>
Expand Down Expand Up @@ -140,14 +141,23 @@
<!-- Notification channels -->
<string name="notification_channel_todo_updates_title">Todo updates</string>
<string name="notification_channel_todo_updates_desc">Shows updates on the status of todos</string>
<string name="notification_channel_todo_updates_id">todo_updates</string>
<string name="notification_channel_weekly_summary_title">Weekly summary</string>
<string name="notification_channel_weekly_summary_desc">Shows a weekly summary on your studying status</string>
<string name="notification_channel_weekly_summary_id">weekly_summary</string>
<string name="notification_channel_sync_title">Sync</string>
<string name="notification_channel_sync_desc">Shows the sync progress of data to the cloud</string>
<string name="notification_channel_sync_id">sync</string>
<string name="notification_channel_app_updates_title">App updates</string>
<string name="notification_channel_app_updates_desc">Updates to the app</string>
<string name="notification_channel_app_updates_id">app_updates</string>
<string name="notification_channel_playback_title">Playback</string>
<string name="notification_channel_playback_desc">Shows music that is currently being played by the app</string>
<string name="notification_channel_uncategorized_title">Uncategorized</string>
<string name="notification_channel_uncategorized_desc">All other notifications that don\'t fit in any of the previous channels</string>
<string name="notification_channel_playback_id">playback</string>
<string name="notification_channel_uncategorised_title">Uncategorised</string>
<string name="notification_channel_uncategorised_desc">All other notifications that don\'t fit in any of the previous channels</string>
<string name="notification_channel_uncategorised_id">uncategorised</string>

<!-- For devices running pre-Android Oreo -->
<string name="enable_all_notification_channels_pref_title">Enable all notification channels</string>
</resources>
37 changes: 2 additions & 35 deletions app/src/main/res/xml/pref_notification.xml
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<SwitchPreference
android:defaultValue="true"
android:key="notifications_new_message"
android:title="@string/pref_title_new_message_notifications"
android:summary="@string/pref_desc_new_message_notifications"
/>

<!-- Allows the user to choose a ringtone in the 'notification' category. -->
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<!-- NOTE: RingtonePreference's summary should be set to its value by the activity code. -->
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:dependency="notifications_new_message"
android:key="notifications_new_message_ringtone"
android:ringtoneType="notification"
android:title="@string/pref_title_ringtone" />

<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<SwitchPreference
android:defaultValue="true"
android:dependency="notifications_new_message"
android:key="notifications_new_message_vibrate"
android:title="@string/pref_title_vibrate"/>
<MultiSelectListPreference
android:dependency="notifications_new_message"
android:key="notifications_categories"
android:title="@string/pref_title_notification_categories"
android:summary="@string/pref_desc_notification_categories"
android:entries="@array/pref_notification_categories"
android:entryValues="@array/pref_notification_categories_values"
android:defaultValue="@array/pref_notification_categories_default"
/>
<!-- This preference screen will be filled in via code -->
</PreferenceScreen>

0 comments on commit 470bdbe

Please sign in to comment.