From 2b9cdde558ed96c23ca02357b3d9e4f47a80e764 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 1 Jul 2017 13:41:24 +0200 Subject: [PATCH] refactored retrieval of default preferences --- .../conversations/parser/MessageParser.java | 14 ++--- .../services/AbstractConnectionManager.java | 9 +-- .../services/NotificationService.java | 17 +++--- .../services/XmppConnectionService.java | 56 +++++++++++-------- .../ui/ConversationActivity.java | 2 +- 5 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 44c0db159..ef93faf59 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/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; @@ -748,18 +749,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/eu/siacs/conversations/services/AbstractConnectionManager.java b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java index 01a2a8ef1..04fb753c2 100644 --- a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java @@ -51,14 +51,7 @@ public class AbstractConnectionManager { } public long getAutoAcceptFileSize() { - long defaultValue = this.getXmppConnectionService().getResources().getInteger(R.integer.auto_accept_filesize); - String config = this.mXmppConnectionService.getPreferences().getString( - "auto_accept_file_size", String.valueOf(defaultValue)); - try { - return Long.parseLong(config); - } catch (NumberFormatException e) { - return defaultValue; - } + return this.mXmppConnectionService.getLongPreference("auto_accept_file_size",R.integer.auto_accept_filesize); } public boolean hasStoragePermission() { diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 539a4dedb..a50233821 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/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; @@ -85,20 +86,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) { @@ -251,7 +252,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); @@ -725,7 +726,7 @@ public class NotificationService { errors.add(account); } } - if (mXmppConnectionService.getPreferences().getBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE, mXmppConnectionService.getResources().getBoolean(R.bool.enable_foreground_service))) { + if (mXmppConnectionService.keepForegroundService()) { notificationManager.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); } final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 170fed794..d25bc43c3 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -27,6 +27,8 @@ import android.os.SystemClock; 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; @@ -861,19 +863,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() { @@ -1090,8 +1092,8 @@ public class XmppConnectionService extends Service { } } - private boolean keepForegroundService() { - return getPreferences().getBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE,getResources().getBoolean(R.bool.enable_foreground_service)); + public boolean keepForegroundService() { + return getBooleanPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE,R.bool.enable_foreground_service); } @Override @@ -2746,7 +2748,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); @@ -3249,50 +3251,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() { @@ -3446,7 +3456,7 @@ public class XmppConnectionService extends Service { public void updateMemorizingTrustmanager() { final MemorizingTrustManager tm; - final boolean dontTrustSystemCAs = getPreferences().getBoolean("dont_trust_system_cas", getResources().getBoolean(R.bool.dont_trust_system_cas)); + final boolean dontTrustSystemCAs = getBooleanPreference("dont_trust_system_cas", R.bool.dont_trust_system_cas); if (dontTrustSystemCAs) { tm = new MemorizingTrustManager(getApplicationContext(), null); } else { @@ -3959,7 +3969,7 @@ public class XmppConnectionService extends Service { } public boolean blindTrustBeforeVerification() { - return getPreferences().getBoolean(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION, getResources().getBoolean(R.bool.btbv)); + return getBooleanPreference(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION,R.bool.btbv); } public ShortcutService getShortcutService() { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 9debe440d..e4b681431 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -1267,7 +1267,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()) {