From e879c6e1c2e2d56c1d1818460fc3b870fa39869e Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 1 Aug 2017 10:44:14 +0200 Subject: refactored retrieval of default preferences --- .../de/pixart/messenger/parser/MessageParser.java | 13 ++---- .../messenger/services/NotificationService.java | 15 +++---- .../messenger/services/XmppConnectionService.java | 48 +++++++++++++--------- .../pixart/messenger/ui/ConversationActivity.java | 2 +- 4 files changed, 42 insertions(+), 36 deletions(-) (limited to 'src/main/java/de/pixart') diff --git a/src/main/java/de/pixart/messenger/parser/MessageParser.java b/src/main/java/de/pixart/messenger/parser/MessageParser.java index c5e818f1a..9a8e005b8 100644 --- a/src/main/java/de/pixart/messenger/parser/MessageParser.java +++ b/src/main/java/de/pixart/messenger/parser/MessageParser.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.UUID; @@ -734,17 +735,11 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece } } - private static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss"); + private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH); private void activateGracePeriod(Account account) { - long duration; - long defaultValue = mXmppConnectionService.getResources().getInteger(R.integer.grace_period); - try { - duration = Long.parseLong(mXmppConnectionService.getPreferences().getString("grace_period_length", String.valueOf(defaultValue))) * 1000; - } catch (NumberFormatException e) { - duration = defaultValue * 1000; - } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": activating grace period (" + duration + ") till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration))); + long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000; + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration))); account.activateGracePeriod(duration); } } diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index ee5db3156..7c8cef886 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -9,6 +9,7 @@ import android.graphics.Typeface; import android.net.Uri; import android.os.Build; import android.os.SystemClock; +import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.BigPictureStyle; import android.support.v4.app.NotificationCompat.Builder; @@ -90,20 +91,20 @@ public class NotificationService { } public boolean notificationsEnabled() { - return mXmppConnectionService.getPreferences().getBoolean("show_notification", mXmppConnectionService.getResources().getBoolean(R.bool.show_notification)); + return mXmppConnectionService.getBooleanPreference("show_notification", R.bool.show_notification); } private boolean notificationsFromStrangers() { - return mXmppConnectionService.getPreferences().getBoolean("notifications_from_strangers", - mXmppConnectionService.getResources().getBoolean(R.bool.notifications_from_strangers)); + return mXmppConnectionService.getBooleanPreference("notifications_from_strangers", R.bool.notifications_from_strangers); } public boolean isQuietHours() { - if (!mXmppConnectionService.getPreferences().getBoolean("enable_quiet_hours", mXmppConnectionService.getResources().getBoolean(R.bool.enable_quiet_hours))) { + if (!mXmppConnectionService.getBooleanPreference("enable_quiet_hours", R.bool.enable_quiet_hours)) { return false; } - final long startTime = mXmppConnectionService.getPreferences().getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; - final long endTime = mXmppConnectionService.getPreferences().getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); + final long startTime = preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; + final long endTime = preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; final long nowTime = Calendar.getInstance().getTimeInMillis() % Config.MILLISECONDS_IN_DAY; if (endTime < startTime) { @@ -261,7 +262,7 @@ public class NotificationService { public void updateNotification(final boolean notify) { final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService); - final SharedPreferences preferences = mXmppConnectionService.getPreferences(); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService); if (notifications.size() == 0) { notificationManager.cancel(NOTIFICATION_ID); diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index ef83aa227..d8f69b6e9 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -29,6 +29,8 @@ import android.os.Vibrator; import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.security.KeyChain; +import android.support.annotation.BoolRes; +import android.support.annotation.IntegerRes; import android.support.v4.app.RemoteInput; import android.util.DisplayMetrics; import android.util.Log; @@ -871,19 +873,19 @@ public class XmppConnectionService extends Service { } private boolean dndOnSilentMode() { - return getPreferences().getBoolean(SettingsActivity.DND_ON_SILENT_MODE, getResources().getBoolean(R.bool.dnd_on_silent_mode)); + return getBooleanPreference(SettingsActivity.DND_ON_SILENT_MODE, R.bool.dnd_on_silent_mode); } private boolean manuallyChangePresence() { - return getPreferences().getBoolean(SettingsActivity.MANUALLY_CHANGE_PRESENCE, getResources().getBoolean(R.bool.manually_change_presence)); + return getBooleanPreference(SettingsActivity.MANUALLY_CHANGE_PRESENCE, R.bool.manually_change_presence); } private boolean treatVibrateAsSilent() { - return getPreferences().getBoolean(SettingsActivity.TREAT_VIBRATE_AS_SILENT, getResources().getBoolean(R.bool.treat_vibrate_as_silent)); + return getBooleanPreference(SettingsActivity.TREAT_VIBRATE_AS_SILENT, R.bool.treat_vibrate_as_silent); } private boolean awayWhenScreenOff() { - return getPreferences().getBoolean(SettingsActivity.AWAY_WHEN_SCREEN_IS_OFF, getResources().getBoolean(R.bool.away_when_screen_off)); + return getBooleanPreference(SettingsActivity.AWAY_WHEN_SCREEN_IS_OFF, R.bool.away_when_screen_off); } private String getCompressPicturesPreference() { @@ -2914,7 +2916,7 @@ public class XmppConnectionService extends Service { } public void createContact(Contact contact) { - boolean autoGrant = getPreferences().getBoolean("grant_new_contacts", getResources().getBoolean(R.bool.grant_new_contacts)); + boolean autoGrant = getBooleanPreference("grant_new_contacts", R.bool.grant_new_contacts); if (autoGrant) { contact.setOption(Contact.Options.PREEMPTIVE_GRANT); contact.setOption(Contact.Options.ASKING); @@ -3417,50 +3419,58 @@ public class XmppConnectionService extends Service { updateConversationUi(); } - public SharedPreferences getPreferences() { - return PreferenceManager - .getDefaultSharedPreferences(getApplicationContext()); + private SharedPreferences getPreferences() { + return PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); } public long getAutomaticMessageDeletionDate() { + final long timeout = getLongPreference(SettingsActivity.AUTOMATIC_MESSAGE_DELETION, R.integer.automatic_message_deletion); + return timeout == 0 ? timeout : (System.currentTimeMillis() - (timeout * 1000)); + } + + public long getLongPreference(String name, @IntegerRes int res) { + long defaultValue = getResources().getInteger(res); try { - final long timeout = Long.parseLong(getPreferences().getString(SettingsActivity.AUTOMATIC_MESSAGE_DELETION, String.valueOf(getResources().getInteger(R.integer.automatic_message_deletion)))) * 1000; - return timeout == 0 ? timeout : System.currentTimeMillis() - timeout; + return Long.parseLong(getPreferences().getString(name,String.valueOf(defaultValue))); } catch (NumberFormatException e) { - return 0; + return defaultValue; } } + public boolean getBooleanPreference(String name, @BoolRes int res) { + return getPreferences().getBoolean(name, getResources().getBoolean(res)); + } + public boolean confirmMessages() { - return getPreferences().getBoolean("confirm_messages", getResources().getBoolean(R.bool.confirm_messages)); + return getBooleanPreference("confirm_messages", R.bool.confirm_messages); } public boolean allowMessageCorrection() { - return getPreferences().getBoolean("allow_message_correction", getResources().getBoolean(R.bool.allow_message_correction)); + return getBooleanPreference("allow_message_correction", R.bool.allow_message_correction); } public boolean sendChatStates() { - return getPreferences().getBoolean("chat_states", getResources().getBoolean(R.bool.chat_states)); + return getBooleanPreference("chat_states", R.bool.chat_states); } private boolean respectAutojoin() { - return getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)); + return getBooleanPreference("autojoin", R.bool.autojoin); } public boolean indicateReceived() { - return getPreferences().getBoolean("indicate_received", getResources().getBoolean(R.bool.indicate_received)); + return getBooleanPreference("indicate_received", R.bool.indicate_received); } public boolean useTorToConnect() { - return Config.FORCE_ORBOT || getPreferences().getBoolean("use_tor", getResources().getBoolean(R.bool.use_tor)); + return Config.FORCE_ORBOT || getBooleanPreference("use_tor", R.bool.use_tor); } public boolean showExtendedConnectionOptions() { - return getPreferences().getBoolean("show_connection_options", getResources().getBoolean(R.bool.show_connection_options)); + return getBooleanPreference("show_connection_options", R.bool.show_connection_options); } public boolean broadcastLastActivity() { - return getPreferences().getBoolean(SettingsActivity.BROADCAST_LAST_ACTIVITY, getResources().getBoolean(R.bool.last_activity)); + return getBooleanPreference(SettingsActivity.BROADCAST_LAST_ACTIVITY, R.bool.last_activity); } public int unreadCount() { diff --git a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java index 7dccdd8b9..bc2e8622e 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java @@ -1463,7 +1463,7 @@ public class ConversationActivity extends XmppActivity } forbidProcessingPendings = false; - if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService)) { + if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService) && !mRedirected.get()) { openBatteryOptimizationDialogIfNeeded(); } if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) { -- cgit v1.2.3