refactored retrieval of default preferences

This commit is contained in:
Daniel Gultsch 2017-07-01 13:41:24 +02:00
parent 7d4d5c437a
commit 2b9cdde558
5 changed files with 48 additions and 50 deletions

View file

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.UUID; 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) { private void activateGracePeriod(Account account) {
long duration = mXmppConnectionService.getLongPreference("grace_period_length",R.integer.grace_period) * 1000;
long duration; Log.d(Config.LOGTAG,account.getJid().toBareJid()+": activating grace period till "+TIME_FORMAT.format(new Date(System.currentTimeMillis() + 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)));
account.activateGracePeriod(duration); account.activateGracePeriod(duration);
} }
} }

View file

@ -51,14 +51,7 @@ public class AbstractConnectionManager {
} }
public long getAutoAcceptFileSize() { public long getAutoAcceptFileSize() {
long defaultValue = this.getXmppConnectionService().getResources().getInteger(R.integer.auto_accept_filesize); return this.mXmppConnectionService.getLongPreference("auto_accept_file_size",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;
}
} }
public boolean hasStoragePermission() { public boolean hasStoragePermission() {

View file

@ -9,6 +9,7 @@ import android.graphics.Typeface;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle; import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.app.NotificationCompat.Builder;
@ -85,20 +86,20 @@ public class NotificationService {
} }
public boolean notificationsEnabled() { 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() { private boolean notificationsFromStrangers() {
return mXmppConnectionService.getPreferences().getBoolean("notifications_from_strangers", return mXmppConnectionService.getBooleanPreference("notifications_from_strangers",R.bool.notifications_from_strangers);
mXmppConnectionService.getResources().getBoolean(R.bool.notifications_from_strangers));
} }
public boolean isQuietHours() { 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; return false;
} }
final long startTime = mXmppConnectionService.getPreferences().getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
final long endTime = mXmppConnectionService.getPreferences().getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE) % Config.MILLISECONDS_IN_DAY; 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; final long nowTime = Calendar.getInstance().getTimeInMillis() % Config.MILLISECONDS_IN_DAY;
if (endTime < startTime) { if (endTime < startTime) {
@ -251,7 +252,7 @@ public class NotificationService {
public void updateNotification(final boolean notify) { public void updateNotification(final boolean notify) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService); final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
final SharedPreferences preferences = mXmppConnectionService.getPreferences(); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
if (notifications.size() == 0) { if (notifications.size() == 0) {
notificationManager.cancel(NOTIFICATION_ID); notificationManager.cancel(NOTIFICATION_ID);
@ -725,7 +726,7 @@ public class NotificationService {
errors.add(account); 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()); notificationManager.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
} }
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);

View file

@ -27,6 +27,8 @@ import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.security.KeyChain; import android.security.KeyChain;
import android.support.annotation.BoolRes;
import android.support.annotation.IntegerRes;
import android.support.v4.app.RemoteInput; import android.support.v4.app.RemoteInput;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
@ -861,19 +863,19 @@ public class XmppConnectionService extends Service {
} }
private boolean dndOnSilentMode() { 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() { 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() { 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() { 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() { private String getCompressPicturesPreference() {
@ -1090,8 +1092,8 @@ public class XmppConnectionService extends Service {
} }
} }
private boolean keepForegroundService() { public boolean keepForegroundService() {
return getPreferences().getBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE,getResources().getBoolean(R.bool.enable_foreground_service)); return getBooleanPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE,R.bool.enable_foreground_service);
} }
@Override @Override
@ -2746,7 +2748,7 @@ public class XmppConnectionService extends Service {
} }
public void createContact(Contact contact) { 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) { if (autoGrant) {
contact.setOption(Contact.Options.PREEMPTIVE_GRANT); contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
contact.setOption(Contact.Options.ASKING); contact.setOption(Contact.Options.ASKING);
@ -3249,50 +3251,58 @@ public class XmppConnectionService extends Service {
updateConversationUi(); updateConversationUi();
} }
public SharedPreferences getPreferences() { private SharedPreferences getPreferences() {
return PreferenceManager return PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
.getDefaultSharedPreferences(getApplicationContext());
} }
public long getAutomaticMessageDeletionDate() { public long getAutomaticMessageDeletionDate() {
try { final long timeout = getLongPreference(SettingsActivity.AUTOMATIC_MESSAGE_DELETION,R.integer.automatic_message_deletion);
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 * 1000));
return timeout == 0 ? timeout : System.currentTimeMillis() - timeout;
} catch (NumberFormatException e) {
return 0;
} }
public long getLongPreference(String name, @IntegerRes int res) {
long defaultValue = getResources().getInteger(res);
try {
return Long.parseLong(getPreferences().getString(name,String.valueOf(defaultValue)));
} catch (NumberFormatException e) {
return defaultValue;
}
}
public boolean getBooleanPreference(String name, @BoolRes int res) {
return getPreferences().getBoolean(name,getResources().getBoolean(res));
} }
public boolean confirmMessages() { 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() { 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() { 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() { private boolean respectAutojoin() {
return getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)); return getBooleanPreference("autojoin", R.bool.autojoin);
} }
public boolean indicateReceived() { 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() { 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() { 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() { 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() { public int unreadCount() {
@ -3446,7 +3456,7 @@ public class XmppConnectionService extends Service {
public void updateMemorizingTrustmanager() { public void updateMemorizingTrustmanager() {
final MemorizingTrustManager tm; 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) { if (dontTrustSystemCAs) {
tm = new MemorizingTrustManager(getApplicationContext(), null); tm = new MemorizingTrustManager(getApplicationContext(), null);
} else { } else {
@ -3959,7 +3969,7 @@ public class XmppConnectionService extends Service {
} }
public boolean blindTrustBeforeVerification() { 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() { public ShortcutService getShortcutService() {

View file

@ -1267,7 +1267,7 @@ public class ConversationActivity extends XmppActivity
} }
forbidProcessingPendings = false; forbidProcessingPendings = false;
if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService)) { if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService) && !mRedirected.get()) {
openBatteryOptimizationDialogIfNeeded(); openBatteryOptimizationDialogIfNeeded();
} }
if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) { if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {