Browse Source

Add default home tab and notifications led enable settings, Minor fixes

pull/27/head
Apostolos Fanakis 7 years ago
parent
commit
9e79f0af22
  1. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java
  3. 53
      app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java
  4. 31
      app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java
  5. 13
      app/src/main/res/values/arrays.xml
  6. 7
      app/src/main/res/values/strings.xml
  7. 19
      app/src/main/res/xml/app_preferences.xml

6
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);
}

2
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";

53
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<String> defaultHomeTabEntries = new ArrayList<>();
private ArrayList<String> 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);
}
}

31
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) {
if (notificationDefaultValues != -1) {
notificationDefaultValues |= Notification.DEFAULT_VIBRATE;
} else {
notificationDefaultValues = Notification.DEFAULT_VIBRATE;
}
}
if (notificationSoundUri == null) {
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);

13
app/src/main/res/values/arrays.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pref_app_main_default_tab_entries">
<item>Recent</item>
<item>Forum</item>
<item>Unread</item>
</string-array>
<string-array name="pref_app_main_default_tab_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>

7
app/src/main/res/values/strings.xml

@ -106,10 +106,17 @@
<string name="action_settings">Settings</string>
<string name="title_activity_settings">Settings</string>
<string name="pref_category_app">App</string>
<string name="pref_title_app_main_default_tab">Home default tab</string>
<string name="pref_summary_app_main_default_tab">Select your preferred, home screen, default tab</string>
<string name="pref_app_main_default_tab_dialog_title">Default home tab</string>
<string name="pref_category_notifications">Notifications</string>
<!--<string name="pref_title_notifications_enable">New posts notification</string>
<string name="pref_summary_notifications_enable">Toggle notifications state</string>-->
<string name="pref_title_notification_vibration_enable">Vibration</string>
<string name="pref_title_notification_led_enable">Notifications led</string>
<string name="pref_summary_notification_led_enable">Enables/disables notifications led, if your device has one</string>
<string name="pref_title_notifications_sound">Notifications sound</string>
<string name="pref_summary_notifications_sound">Select your preferred notification sound</string>

19
app/src/main/res/xml/app_preferences.xml

@ -1,6 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceCategory android:title="@string/pref_category_app">
<android.support.v7.preference.ListPreference
android:defaultValue="0"
android:dialogTitle="@string/pref_app_main_default_tab_dialog_title"
android:entries="@array/pref_app_main_default_tab_entries"
android:entryValues="@array/pref_app_main_default_tab_values"
android:key="pref_app_main_default_tab_key"
android:summary="@string/pref_summary_app_main_default_tab"
android:title="@string/pref_title_app_main_default_tab" />
</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.PreferenceCategory android:title="@string/pref_category_notifications">
<!--<android.support.v7.preference.SwitchPreferenceCompat
@ -14,6 +27,12 @@
android:key="pref_notification_vibration_enable_key"
android:title="@string/pref_title_notification_vibration_enable" />
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="pref_notification_led_enable_key"
android:summary="@string/pref_summary_notification_led_enable"
android:title="@string/pref_title_notification_led_enable" />
<Preference
android:key="pref_notifications_select_sound_key"
android:summary="@string/pref_summary_notifications_sound"

Loading…
Cancel
Save