From 5c1a4d71850cf78e22bce7cca1e2c00029ba2a50 Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Wed, 19 Jul 2017 11:37:19 +0200 Subject: [PATCH] Settings screen with Notifications checkbox. #279 --- .idea/dictionaries/dictionary.xml | 7 ++ opentasks/src/main/AndroidManifest.xml | 9 ++- .../org/dmfs/tasks/AppSettingsActivity.java | 70 +++++++++++++++++++ .../org/dmfs/tasks/AppSettingsFragment.java | 42 +++++++++++ .../java/org/dmfs/tasks/TaskListActivity.java | 15 +--- .../notification/AlarmBroadcastReceiver.java | 26 ++----- .../main/res/menu/task_list_activity_menu.xml | 11 +-- opentasks/src/main/res/values/prefs.xml | 7 ++ .../src/main/res/xml/app_preferences.xml | 8 +++ 9 files changed, 156 insertions(+), 39 deletions(-) create mode 100644 .idea/dictionaries/dictionary.xml create mode 100644 opentasks/src/main/java/org/dmfs/tasks/AppSettingsActivity.java create mode 100644 opentasks/src/main/java/org/dmfs/tasks/AppSettingsFragment.java create mode 100644 opentasks/src/main/res/values/prefs.xml create mode 100644 opentasks/src/main/res/xml/app_preferences.xml diff --git a/.idea/dictionaries/dictionary.xml b/.idea/dictionaries/dictionary.xml new file mode 100644 index 000000000..f2d484282 --- /dev/null +++ b/.idea/dictionaries/dictionary.xml @@ -0,0 +1,7 @@ + + + + opentasks + + + \ No newline at end of file diff --git a/opentasks/src/main/AndroidManifest.xml b/opentasks/src/main/AndroidManifest.xml index 6173a353b..c1cc6b5f9 100644 --- a/opentasks/src/main/AndroidManifest.xml +++ b/opentasks/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ - + @@ -347,6 +347,11 @@ android:scheme="content"/> + + = Build.VERSION_CODES.HONEYCOMB) + { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + } + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/AppSettingsFragment.java b/opentasks/src/main/java/org/dmfs/tasks/AppSettingsFragment.java new file mode 100644 index 000000000..62d9bf43b --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/AppSettingsFragment.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks; + +import android.os.Build; +import android.os.Bundle; +import android.preference.PreferenceFragment; +import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; + + +/** + * Fragment for the general app settings. + * + * @author Gabor Keszthelyi + */ +@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) +public final class AppSettingsFragment extends PreferenceFragment +{ + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + addPreferencesFromResource(R.xml.app_preferences); + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java b/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java index 8ff9f3dd8..1a434b465 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java +++ b/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java @@ -61,7 +61,6 @@ import org.dmfs.tasks.groupings.BySearch; import org.dmfs.tasks.groupings.ByStartDate; import org.dmfs.tasks.model.ContentSet; -import org.dmfs.tasks.notification.AlarmBroadcastReceiver; import org.dmfs.tasks.utils.AppCompatActivity; import org.dmfs.tasks.utils.ExpandableGroupDescriptor; import org.dmfs.tasks.utils.SearchHistoryHelper; @@ -597,13 +596,6 @@ public boolean onCreateOptionsMenu(Menu menu) addItem.setVisible(false); } - // restore menu state - MenuItem item = menu.findItem(R.id.menu_alarms); - if (item != null) - { - item.setChecked(AlarmBroadcastReceiver.getAlarmPreference(this)); - } - // search setupSearch(menu); @@ -626,12 +618,9 @@ else if (item.getItemId() == R.id.menu_visible_list) startActivity(settingsIntent); return true; } - else if (item.getItemId() == R.id.menu_alarms) + else if (item.getItemId() == R.id.opentasks_menu_app_settings) { - // set and save state - boolean activatedAlarms = !item.isChecked(); - item.setChecked(activatedAlarms); - AlarmBroadcastReceiver.setAlarmPreference(this, activatedAlarms); + startActivity(new Intent(this, AppSettingsActivity.class)); return true; } else diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java b/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java index 271c08be0..341217904 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/AlarmBroadcastReceiver.java @@ -22,7 +22,9 @@ import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; +import android.preference.PreferenceManager; +import org.dmfs.tasks.R; import org.dmfs.tasks.contract.TaskContract; @@ -34,10 +36,6 @@ public class AlarmBroadcastReceiver extends BroadcastReceiver { - private static String PREFS_NAME = "alarm_preferences"; - private static String PREF_ALARM_ACTIVATED = "preference_alarm_activated"; - - /** * Is called on an incoming alarm broadcast. Creates a notifications for this alarm. */ @@ -47,7 +45,7 @@ public void onReceive(Context context, Intent intent) // continue if alarms where enabled if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_STARTING)) { - if (getAlarmPreference(context)) + if (isNotificationEnabled(context)) { Uri taskUri = intent.getData(); @@ -72,7 +70,7 @@ public void onReceive(Context context, Intent intent) } else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE)) { - if (getAlarmPreference(context)) + if (isNotificationEnabled(context)) { Uri taskUri = intent.getData(); @@ -100,20 +98,10 @@ else if (intent.getAction().equals(TaskContract.ACTION_BROADCAST_TASK_DUE)) } - public static void setAlarmPreference(Context context, boolean value) - { - SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); - SharedPreferences.Editor editor = settings.edit(); - editor.putBoolean(PREF_ALARM_ACTIVATED, value); - editor.commit(); - - } - - - public static boolean getAlarmPreference(Context context) + public boolean isNotificationEnabled(Context context) { - SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); - return settings.getBoolean(PREF_ALARM_ACTIVATED, true); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); + return settings.getBoolean(context.getString(R.string.schedjoules_pref_notification_enabled), true); } } diff --git a/opentasks/src/main/res/menu/task_list_activity_menu.xml b/opentasks/src/main/res/menu/task_list_activity_menu.xml index 50d44f046..9635fd1d3 100644 --- a/opentasks/src/main/res/menu/task_list_activity_menu.xml +++ b/opentasks/src/main/res/menu/task_list_activity_menu.xml @@ -17,11 +17,12 @@ + app:showAsAction="never"/> + + android:id="@+id/opentasks_menu_app_settings" + android:title="@string/title_activity_settings" + android:visible="@bool/postHoneycomb" + app:showAsAction="never"/> \ No newline at end of file diff --git a/opentasks/src/main/res/values/prefs.xml b/opentasks/src/main/res/values/prefs.xml new file mode 100644 index 000000000..d2e1f7e09 --- /dev/null +++ b/opentasks/src/main/res/values/prefs.xml @@ -0,0 +1,7 @@ + + + + schedjoules_pref_notification_enabled + + \ No newline at end of file diff --git a/opentasks/src/main/res/xml/app_preferences.xml b/opentasks/src/main/res/xml/app_preferences.xml new file mode 100644 index 000000000..8c8b8c119 --- /dev/null +++ b/opentasks/src/main/res/xml/app_preferences.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file