diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 2333cb1d..3e6f86cb 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.activities.main; import android.content.Intent; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.TabLayout; @@ -36,6 +37,7 @@ import static gr.thmmy.mthmmy.activities.downloads.DownloadsActivity.BUNDLE_DOWN import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_THUMBNAIL_URL; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME; +import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.DEFAULT_HOME_TAB; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; @@ -74,7 +76,6 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF if (sessionManager.isLoggedIn()) sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); - //Set up the ViewPager with the sections adapter. viewPager = findViewById(R.id.container); viewPager.setAdapter(sectionsPagerAdapter); @@ -82,6 +83,9 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF TabLayout tabLayout = findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + tabLayout.getTabAt(Integer.parseInt(sharedPrefs.getString(DEFAULT_HOME_TAB, "0"))).select(); + setMainActivity(this); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java index 63e440aa..40ade6b7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java @@ -8,6 +8,8 @@ import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; public class SettingsActivity extends BaseActivity { + public static final String DEFAULT_HOME_TAB = "pref_app_main_default_tab_key"; + public static final String NOTIFICATION_LED_KEY = "pref_notification_led_enable_key"; public static final String NOTIFICATION_VIBRATION_KEY = "pref_notification_vibration_enable_key"; public static final String APP_SIGNATURE_ENABLE_KEY = "pref_posting_app_signature_enable_key"; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java index 7e35d6d4..cd34473e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java @@ -9,10 +9,15 @@ import android.net.Uri; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; +import android.support.v7.preference.ListPreference; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; +import android.util.Log; import android.view.View; +import java.util.ArrayList; +import java.util.Arrays; + import gr.thmmy.mthmmy.R; public class SettingsFragment extends PreferenceFragmentCompat { @@ -20,6 +25,8 @@ public class SettingsFragment extends PreferenceFragmentCompat { //Preferences xml keys private static final String POSTING_CATEGORY = "pref_category_posting_key"; + private static final String DEFAULT_HOME_TAB = "pref_app_main_default_tab_key"; + private static final String POSTING_APP_SIGNATURE_ENABLE = "pref_posting_app_signature_enable_key"; private static final String SELECTED_NOTIFICATIONS_SOUND = "pref_notifications_select_sound_key"; //SharedPreferences keys @@ -31,6 +38,8 @@ public class SettingsFragment extends PreferenceFragmentCompat { private SharedPreferences settingsFile; private boolean isLoggedIn = false; + private ArrayList defaultHomeTabEntries = new ArrayList<>(); + private ArrayList defaultHomeTabValues = new ArrayList<>(); public static SettingsFragment newInstance(boolean isLoggedIn) { SettingsFragment fragment = new SettingsFragment(); @@ -40,12 +49,23 @@ public class SettingsFragment extends PreferenceFragmentCompat { return fragment; } + public SettingsFragment() { + defaultHomeTabEntries.add("Recent"); + defaultHomeTabEntries.add("Forum"); + defaultHomeTabEntries.add("Unread"); + + defaultHomeTabValues.add("0"); + defaultHomeTabValues.add("1"); + defaultHomeTabValues.add("2"); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); - if (args != null){ - isLoggedIn = args.getBoolean(ARG_IS_LOGGED_IN,false); + + if (args != null) { + isLoggedIn = args.getBoolean(ARG_IS_LOGGED_IN, false); } } @@ -59,6 +79,21 @@ public class SettingsFragment extends PreferenceFragmentCompat { public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); findPreference(POSTING_CATEGORY).setVisible(isLoggedIn); + findPreference(POSTING_APP_SIGNATURE_ENABLE).setVisible(isLoggedIn); + + if (!isLoggedIn && defaultHomeTabEntries.contains("Unread")) { + defaultHomeTabEntries.remove("Unread"); + defaultHomeTabValues.remove("2"); + } else if (isLoggedIn && !defaultHomeTabEntries.contains("Unread")) { + defaultHomeTabEntries.add("Unread"); + defaultHomeTabValues.add("2"); + } + + CharSequence[] tmpCs = defaultHomeTabEntries.toArray(new CharSequence[defaultHomeTabEntries.size()]); + ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntries(tmpCs); + + tmpCs = defaultHomeTabValues.toArray(new CharSequence[defaultHomeTabValues.size()]); + ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntryValues(tmpCs); } @Override @@ -114,5 +149,19 @@ public class SettingsFragment extends PreferenceFragmentCompat { public void updateUserLoginState(boolean isLoggedIn) { this.isLoggedIn = isLoggedIn; + + if (!isLoggedIn && defaultHomeTabEntries.contains("Unread")) { + defaultHomeTabEntries.remove("Unread"); + defaultHomeTabValues.remove("2"); + } else if (isLoggedIn && !defaultHomeTabEntries.contains("Unread")) { + defaultHomeTabEntries.add("Unread"); + defaultHomeTabValues.add("2"); + } + + CharSequence[] tmpCs = defaultHomeTabEntries.toArray(new CharSequence[defaultHomeTabEntries.size()]); + ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntries(tmpCs); + + tmpCs = defaultHomeTabValues.toArray(new CharSequence[defaultHomeTabValues.size()]); + ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntryValues(tmpCs); } } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java index ed837d78..8f27c963 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java @@ -7,6 +7,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -28,6 +29,7 @@ import gr.thmmy.mthmmy.model.PostNotification; import timber.log.Timber; import static android.support.v4.app.NotificationCompat.PRIORITY_MAX; +import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.NOTIFICATION_LED_KEY; import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.NOTIFICATION_VIBRATION_KEY; import static gr.thmmy.mthmmy.activities.settings.SettingsFragment.SELECTED_RINGTONE; import static gr.thmmy.mthmmy.activities.settings.SettingsFragment.SETTINGS_SHARED_PREFS; @@ -36,6 +38,7 @@ import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; public class NotificationService extends FirebaseMessagingService { private static final int buildVersion = Build.VERSION.SDK_INT; + private static final int disabledNotifiationsLedColor = Color.argb(0, 0, 0, 0); @Override public void onMessageReceived(RemoteMessage remoteMessage) { @@ -75,20 +78,36 @@ public class NotificationService extends FirebaseMessagingService { private void sendNotification(PostNotification postNotification) { Timber.i("Creating a notification..."); + //Reads notifications preferences SharedPreferences settingsFile = getSharedPreferences(SETTINGS_SHARED_PREFS, Context.MODE_PRIVATE); String notificationsSound = settingsFile.getString(SELECTED_RINGTONE, null); Uri notificationSoundUri = notificationsSound != null ? Uri.parse(notificationsSound) : null; SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + + boolean notificationsLedEnabled = sharedPrefs.getBoolean(NOTIFICATION_LED_KEY, true); boolean notificationsVibrateEnabled = sharedPrefs.getBoolean(NOTIFICATION_VIBRATION_KEY, true); + int notificationDefaultValues = -1; - int notificationDefaultValues = Notification.DEFAULT_LIGHTS; + if (notificationsLedEnabled) { + notificationDefaultValues = Notification.DEFAULT_LIGHTS; + } if (notificationsVibrateEnabled) { - notificationDefaultValues |= Notification.DEFAULT_VIBRATE; + if (notificationDefaultValues != -1) { + notificationDefaultValues |= Notification.DEFAULT_VIBRATE; + } else { + notificationDefaultValues = Notification.DEFAULT_VIBRATE; + } + } if (notificationSoundUri == null) { - notificationDefaultValues |= Notification.DEFAULT_SOUND; + if (notificationDefaultValues != -1) { + notificationDefaultValues = Notification.DEFAULT_SOUND; + } else { + notificationDefaultValues |= Notification.DEFAULT_SOUND; + } } + //Builds notification String topicUrl = "https://www.thmmy.gr/smf/index.php?topic=" + postNotification.getTopicId() + "." + postNotification.getPostId(); Intent intent = new Intent(this, TopicActivity.class); Bundle extras = new Bundle(); @@ -121,16 +140,22 @@ public class NotificationService extends FirebaseMessagingService { .setContentText(contentText) .setAutoCancel(true) .setContentIntent(pendingIntent) - .setDefaults(notificationDefaultValues) .setGroup(GROUP_KEY) .addExtras(notificationExtras); - //Checks for values other than defaults and applies them + + //Applies user's notifications preferences + if (notificationDefaultValues != -1) { + notificationBuilder.setDefaults(notificationDefaultValues); + } if (notificationSoundUri != null) { notificationBuilder.setSound(notificationSoundUri); } if (!notificationsVibrateEnabled) { notificationBuilder.setVibrate(new long[]{0L}); } + if (!notificationsLedEnabled) { + notificationBuilder.setLights(disabledNotifiationsLedColor, 0, 1000); + } if (buildVersion < Build.VERSION_CODES.O) notificationBuilder.setPriority(PRIORITY_MAX); diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 00000000..b00e9866 --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,13 @@ + + + + Recent + Forum + Unread + + + 0 + 1 + 2 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 05025b59..d5028149 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -106,10 +106,17 @@ Settings Settings + App + Home default tab + Select your preferred, home screen, default tab + Default home tab + Notifications Vibration + Notifications led + Enables/disables notifications led, if your device has one Notifications sound Select your preferred notification sound diff --git a/app/src/main/res/xml/app_preferences.xml b/app/src/main/res/xml/app_preferences.xml index 047c219d..c7d1b177 100644 --- a/app/src/main/res/xml/app_preferences.xml +++ b/app/src/main/res/xml/app_preferences.xml @@ -1,6 +1,19 @@ + + + + + +