diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index d4adb0117..a50e5831f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -36,6 +36,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; //import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver; @@ -56,6 +57,7 @@ public class GBApplication extends Application { private static final int CURRENT_PREFS_VERSION = 2; private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); private static Appender fileLogger; + private static Prefs prefs; public static final String ACTION_QUIT = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; @@ -89,6 +91,7 @@ public class GBApplication extends Application { super.onCreate(); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + prefs = new Prefs(sharedPrefs); // don't do anything here before we set up logging, otherwise // slf4j may be implicitly initialized before we properly configured it. @@ -125,7 +128,7 @@ public class GBApplication extends Application { } public static boolean isFileLoggingEnabled() { - return sharedPrefs.getBoolean("log_to_file", false); + return prefs.getBoolean("log_to_file", false); } public static void setupLogging(boolean enable) { @@ -345,7 +348,7 @@ public class GBApplication extends Application { } public static boolean isDarkThemeEnabled() { - return sharedPrefs.getString("pref_key_theme", context.getString(R.string.pref_theme_value_light)).equals(context.getString(R.string.pref_theme_value_dark)); + return prefs.getString("pref_key_theme", context.getString(R.string.pref_theme_value_light)).equals(context.getString(R.string.pref_theme_value_dark)); } public static int getTextColor(Context context) { @@ -361,4 +364,7 @@ public class GBApplication extends Application { return typedValue.data; } + public static Prefs getPrefs() { + return prefs; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java index bef494383..c7db9fc71 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java @@ -4,11 +4,9 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.NavUtils; import android.support.v4.content.LocalBroadcastManager; import android.view.LayoutInflater; @@ -47,7 +45,6 @@ public class AppBlacklistActivity extends GBActivity { } }; - private SharedPreferences sharedPrefs; private IdentityHashMap nameMap; @Override @@ -56,7 +53,6 @@ public class AppBlacklistActivity extends GBActivity { setContentView(R.layout.activity_appblacklist); final PackageManager pm = getPackageManager(); - sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); final List packageList = pm.getInstalledApplications(PackageManager.GET_META_DATA); ListView appListView = (ListView) findViewById(R.id.appListView); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java index 5be4f6f09..2e6bbba04 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppManagerActivity.java @@ -4,10 +4,8 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.NavUtils; import android.support.v4.content.LocalBroadcastManager; import android.view.ContextMenu; @@ -34,6 +32,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp; import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class AppManagerActivity extends GBActivity { @@ -59,7 +58,7 @@ public class AppManagerActivity extends GBActivity { appList.add(new GBDeviceApp(uuid, appName, appCreator, "", appType)); } - if (sharedPrefs.getBoolean("pebble_force_untested", false)) { + if (prefs.getBoolean("pebble_force_untested", false)) { appList.addAll(getSystemApps()); } @@ -68,7 +67,7 @@ public class AppManagerActivity extends GBActivity { } }; - private SharedPreferences sharedPrefs; + private Prefs prefs; private final List appList = new ArrayList<>(); private GBDeviceAppAdapter mGBDeviceAppAdapter; @@ -130,7 +129,7 @@ public class AppManagerActivity extends GBActivity { throw new IllegalArgumentException("Must provide a device when invoking this activity"); } - sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + prefs = GBApplication.getPrefs(); setContentView(R.layout.activity_appmanager); @@ -150,7 +149,7 @@ public class AppManagerActivity extends GBActivity { appList.addAll(getCachedApps()); - if (sharedPrefs.getBoolean("pebble_force_untested", false)) { + if (prefs.getBoolean("pebble_force_untested", false)) { appList.addAll(getSystemApps()); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java index 49f8e7a75..c9a8c33d5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java @@ -16,6 +16,7 @@ import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.adapter.GBAlarmListAdapter; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ALARMS; @@ -34,12 +35,12 @@ public class ConfigureAlarms extends GBActivity { setContentView(R.layout.activity_configure_alarms); - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - preferencesAlarmListSet = sharedPrefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet()); + Prefs prefs = GBApplication.getPrefs(); + preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet()); if (preferencesAlarmListSet.isEmpty()) { //initialize the preferences preferencesAlarmListSet = new HashSet<>(Arrays.asList(GBAlarm.DEFAULT_ALARMS)); - sharedPrefs.edit().putStringSet(PREF_MIBAND_ALARMS, preferencesAlarmListSet).apply(); + prefs.getPreferences().edit().putStringSet(PREF_MIBAND_ALARMS, preferencesAlarmListSet).apply(); } mGBAlarmListAdapter = new GBAlarmListAdapter(this, preferencesAlarmListSet); @@ -66,9 +67,9 @@ public class ConfigureAlarms extends GBActivity { } private void updateAlarmsFromPrefs() { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - preferencesAlarmListSet = sharedPrefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet()); - int reservedSlots = Integer.parseInt(sharedPrefs.getString(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, "0")); + Prefs prefs = GBApplication.getPrefs(); + preferencesAlarmListSet = prefs.getStringSet(PREF_MIBAND_ALARMS, new HashSet()); + int reservedSlots = Integer.parseInt(prefs.getString(MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR, "0")); mGBAlarmListAdapter.setAlarmList(preferencesAlarmListSet, reservedSlots); mGBAlarmListAdapter.notifyDataSetChanged(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java index b9ad4d98b..9b6ab4b41 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenter.java @@ -46,6 +46,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class ControlCenter extends GBActivity { @@ -184,9 +185,9 @@ public class ControlCenter extends GBActivity { /* * Ask for permission to intercept notifications on first run. */ - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (sharedPrefs.getBoolean("firstrun", true)) { - sharedPrefs.edit().putBoolean("firstrun", false).apply(); + Prefs prefs = GBApplication.getPrefs(); + if (prefs.getBoolean("firstrun", true)) { + prefs.getPreferences().edit().putBoolean("firstrun", false).apply(); Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); startActivity(enableIntent); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/contentprovider/PebbleContentProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/contentprovider/PebbleContentProvider.java index c20a552dd..27976ff3f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/contentprovider/PebbleContentProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/contentprovider/PebbleContentProvider.java @@ -14,8 +14,10 @@ import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.content.LocalBroadcastManager; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class PebbleContentProvider extends ContentProvider { @@ -59,8 +61,8 @@ public class PebbleContentProvider extends ContentProvider { MatrixCursor mc = new MatrixCursor(columnNames); int connected = 0; int appMessage = 0; - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getContext()); - if (sharedPrefs.getBoolean("pebble_enable_pebblekit", false)) { + Prefs prefs = GBApplication.getPrefs(); + if (prefs.getBoolean("pebble_enable_pebblekit", false)) { appMessage = 1; } if (mGBDevice != null && mGBDevice.getType() == DeviceType.PEBBLE && mGBDevice.isInitialized()) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java index 9a2b9fac3..1a950c866 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java @@ -26,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class MiBandPairingActivity extends Activity { private static final Logger LOG = LoggerFactory.getLogger(MiBandPairingActivity.class); @@ -170,8 +171,8 @@ public class MiBandPairingActivity extends Activity { unregisterReceiver(mBondingReceiver); if (pairedSuccessfully) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - sharedPrefs.edit().putString(MiBandConst.PREF_MIBAND_ADDRESS, macAddress).apply(); + Prefs prefs = GBApplication.getPrefs(); + prefs.getPreferences().edit().putString(MiBandConst.PREF_MIBAND_ADDRESS, macAddress).apply(); } Intent intent = new Intent(this, ControlCenter.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java index f7017d7b1..e2b3a6567 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java @@ -14,6 +14,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class PebbleCoordinator extends AbstractDeviceCoordinator { public PebbleCoordinator() { @@ -45,8 +46,8 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator { @Override public SampleProvider getSampleProvider() { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext()); - int activityTracker = Integer.parseInt(sharedPrefs.getString("pebble_activitytracker", Integer.toString(SampleProvider.PROVIDER_PEBBLE_HEALTH))); + Prefs prefs = GBApplication.getPrefs(); + int activityTracker = Integer.parseInt(prefs.getString("pebble_activitytracker", Integer.toString(SampleProvider.PROVIDER_PEBBLE_HEALTH))); switch (activityTracker) { case SampleProvider.PROVIDER_PEBBLE_HEALTH: return new HealthSampleProvider(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java index 74a3d2dcf..2b21690ca 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java @@ -10,6 +10,7 @@ import android.support.v4.content.LocalBroadcastManager; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class BluetoothStateChangeReceiver extends BroadcastReceiver { @Override @@ -22,8 +23,8 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver { Intent refreshIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST); LocalBroadcastManager.getInstance(context).sendBroadcast(refreshIntent); - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!sharedPrefs.getBoolean("general_autoconnectonbluetooth", false)) { + Prefs prefs = GBApplication.getPrefs(); + if (!prefs.getBoolean("general_autoconnectonbluetooth", false)) { return; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java index 1b615d24b..8487ead6a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/K9Receiver.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class K9Receiver extends BroadcastReceiver { @@ -24,11 +25,11 @@ public class K9Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if ("never".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) { + Prefs prefs = GBApplication.getPrefs(); + if ("never".equals(prefs.getString("notification_mode_k9mail", "when_screen_off"))) { return; } - if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) { + if ("when_screen_off".equals(prefs.getString("notification_mode_k9mail", "when_screen_off"))) { PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powermanager.isScreenOn()) { return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index 124c0b99d..19d14bd7b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -30,6 +30,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class NotificationListener extends NotificationListenerService { @@ -161,8 +162,8 @@ public class NotificationListener extends NotificationListenerService { return; } - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); - if (!sharedPrefs.getBoolean("notifications_generic_whenscreenon", false)) { + Prefs prefs = GBApplication.getPrefs(); + if (!prefs.getBoolean("notifications_generic_whenscreenon", false)) { PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE); if (powermanager.isScreenOn()) { return; @@ -189,13 +190,13 @@ public class NotificationListener extends NotificationListenerService { } if (source.equals("eu.siacs.conversations")) { - if (!"never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { + if (!"never".equals(prefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { return; } } if (source.equals("com.fsck.k9")) { - if (!"never".equals(sharedPrefs.getString("notification_mode_k9mail", "when_screen_off"))) { + if (!"never".equals(prefs.getString("notification_mode_k9mail", "when_screen_off"))) { return; } } @@ -205,7 +206,7 @@ public class NotificationListener extends NotificationListenerService { source.equals("com.sonyericsson.conversations") || source.equals("com.android.messaging") || source.equals("org.smssecure.smssecure")) { - if (!"never".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) { + if (!"never".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) { return; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java index 01dab88f6..42e94e4b3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PebbleReceiver.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class PebbleReceiver extends BroadcastReceiver { @@ -23,11 +24,11 @@ public class PebbleReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if ("never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { + Prefs prefs = GBApplication.getPrefs(); + if ("never".equals(prefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { return; } - if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { + if ("when_screen_off".equals(prefs.getString("notification_mode_pebblemsg", "when_screen_off"))) { PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powermanager.isScreenOn()) { return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java index 010b5fb82..054723fd9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java @@ -9,6 +9,7 @@ import android.telephony.TelephonyManager; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class PhoneCallReceiver extends BroadcastReceiver { @@ -64,8 +65,8 @@ public class PhoneCallReceiver extends BroadcastReceiver { break; } if (callCommand != CallSpec.CALL_UNDEFINED) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if ("never".equals(sharedPrefs.getString("notification_mode_calls", "always"))) { + Prefs prefs = GBApplication.getPrefs(); + if ("never".equals(prefs.getString("notification_mode_calls", "always"))) { return; } CallSpec callSpec = new CallSpec(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java index a2dd09a32..d6db20274 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java @@ -12,17 +12,17 @@ import android.telephony.SmsMessage; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - if ("never".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) { + Prefs prefs = GBApplication.getPrefs(); + if ("never".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) { return; } - if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_sms", "when_screen_off"))) { + if ("when_screen_off".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) { PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (powermanager.isScreenOn()) { return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/TimeChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/TimeChangeReceiver.java index 82f0836b2..e2ea614c1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/TimeChangeReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/TimeChangeReceiver.java @@ -14,6 +14,7 @@ import java.util.GregorianCalendar; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class TimeChangeReceiver extends BroadcastReceiver { @@ -22,10 +23,10 @@ public class TimeChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + Prefs prefs = GBApplication.getPrefs(); final String action = intent.getAction(); - if (sharedPrefs.getBoolean("datetime_synconconnect", true) && (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED))) { + if (prefs.getBoolean("datetime_synconconnect", true) && (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED))) { Date newTime = GregorianCalendar.getInstance().getTime(); LOG.info("Time or Timezone changed, syncing with device: " + DateTimeUtils.formatDate(newTime) + " (" + newTime.toGMTString() + "), " + intent.getAction()); GBApplication.deviceService().onSetTime(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index c26cb1b86..b5b4b7fb6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -6,11 +6,9 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.IBinder; -import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.support.annotation.Nullable; import android.support.v4.content.LocalBroadcastManager; @@ -39,6 +37,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_APP_CONFIGURE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CALLSTATE; @@ -170,7 +169,7 @@ public class DeviceCommunicationService extends Service { // when we get past this, we should have valid mDeviceSupport and mGBDevice instances - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + Prefs prefs = GBApplication.getPrefs(); switch (action) { case ACTION_START: start(); @@ -181,8 +180,8 @@ public class DeviceCommunicationService extends Service { String btDeviceAddress = null; if (gbDevice == null) { btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS); - if (btDeviceAddress == null && sharedPrefs != null) { // may be null in test cases - btDeviceAddress = sharedPrefs.getString("last_device_address", null); + if (btDeviceAddress == null && prefs != null) { // may be null in test cases + btDeviceAddress = prefs.getString("last_device_address", null); } if (btDeviceAddress != null) { gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this); @@ -191,8 +190,8 @@ public class DeviceCommunicationService extends Service { btDeviceAddress = gbDevice.getAddress(); } - if (sharedPrefs != null) { - sharedPrefs.edit().putString("last_device_address", btDeviceAddress).apply(); + if (prefs != null) { + prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply(); } if (gbDevice != null && !isConnecting() && !isConnected()) { @@ -241,12 +240,12 @@ public class DeviceCommunicationService extends Service { if (((notificationSpec.flags & NotificationSpec.FLAG_WEARABLE_REPLY) > 0) || (notificationSpec.type == NotificationType.SMS && notificationSpec.phoneNumber != null)) { // NOTE: maybe not where it belongs - if (sharedPrefs.getBoolean("pebble_force_untested", false)) { + if (prefs.getBoolean("pebble_force_untested", false)) { // I would rather like to save that as an array in ShadredPreferences // this would work but I dont know how to do the same in the Settings Activity's xml ArrayList replies = new ArrayList<>(); for (int i = 1; i <= 16; i++) { - String reply = sharedPrefs.getString("canned_reply_" + i, null); + String reply = prefs.getString("canned_reply_" + i, null); if (reply != null && !reply.equals("")) { replies.add(reply); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java index b2a42630a..e3d22eaac 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleIoThread.java @@ -8,10 +8,8 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.net.Uri; import android.os.ParcelUuid; -import android.preference.PreferenceManager; import org.json.JSONArray; import org.json.JSONException; @@ -29,6 +27,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.UUID; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo; @@ -44,6 +43,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class PebbleIoThread extends GBDeviceIoThread { private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); @@ -62,7 +62,7 @@ public class PebbleIoThread extends GBDeviceIoThread { public static final String PEBBLEKIT_ACTION_APP_START = "com.getpebble.action.app.START"; public static final String PEBBLEKIT_ACTION_APP_STOP = "com.getpebble.action.app.STOP"; - final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + final Prefs prefs = GBApplication.getPrefs(); private final PebbleProtocol mPebbleProtocol; private final PebbleSupport mPebbleSupport; @@ -160,7 +160,7 @@ public class PebbleIoThread extends GBDeviceIoThread { mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol; mBtAdapter = btAdapter; mPebbleSupport = pebbleSupport; - mEnablePebblekit = sharedPrefs.getBoolean("pebble_enable_pebblekit", false); + mEnablePebblekit = prefs.getBoolean("pebble_enable_pebblekit", false); } @Override @@ -199,7 +199,7 @@ public class PebbleIoThread extends GBDeviceIoThread { return false; } - mPebbleProtocol.setForceProtocol(sharedPrefs.getBoolean("pebble_force_protocol", false)); + mPebbleProtocol.setForceProtocol(prefs.getBoolean("pebble_force_protocol", false)); mIsConnected = true; if (originalState == GBDevice.State.WAITING_FOR_RECONNECT) { @@ -364,7 +364,7 @@ public class PebbleIoThread extends GBDeviceIoThread { if (e.getMessage().contains("socket closed")) { //FIXME: this does not feel right LOG.info(e.getMessage()); mIsConnected = false; - int reconnectAttempts = Integer.valueOf(sharedPrefs.getString("pebble_reconnect_attempts", "10")); + int reconnectAttempts = Integer.valueOf(prefs.getString("pebble_reconnect_attempts", "10")); if (reconnectAttempts > 0) { gbDevice.setState(GBDevice.State.CONNECTING); gbDevice.sendDeviceUpdateIntent(getContext()); @@ -480,7 +480,7 @@ public class PebbleIoThread extends GBDeviceIoThread { private boolean evaluateGBDeviceEventPebble(GBDeviceEvent deviceEvent) { if (deviceEvent instanceof GBDeviceEventVersionInfo) { - if (sharedPrefs.getBoolean("datetime_synconconnect", true)) { + if (prefs.getBoolean("datetime_synconconnect", true)) { LOG.info("syncing time"); write(mPebbleProtocol.encodeSetTime()); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java index fce017656..053f034ef 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/GBMusicControlReceiver.java @@ -12,7 +12,9 @@ import android.view.KeyEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class GBMusicControlReceiver extends BroadcastReceiver { private static final Logger LOG = LoggerFactory.getLogger(GBMusicControlReceiver.class); @@ -53,8 +55,8 @@ public class GBMusicControlReceiver extends BroadcastReceiver { } if (keyCode != -1) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - String audioPlayer = sharedPrefs.getString("audio_player", "default"); + Prefs prefs = GBApplication.getPrefs(); + String audioPlayer = prefs.getString("audio_player", "default"); long eventtime = SystemClock.uptimeMillis(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java index b3aad4b79..ce41e842c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java @@ -12,6 +12,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.UnknownDeviceCoordinator; @@ -75,8 +76,8 @@ public class DeviceHelper { } } - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); - String miAddr = sharedPrefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, ""); + Prefs prefs = GBApplication.getPrefs(); + String miAddr = prefs.getString(MiBandConst.PREF_MIBAND_ADDRESS, ""); if (miAddr.length() > 0) { GBDevice miDevice = new GBDevice(miAddr, "MI", DeviceType.MIBAND); if (!availableDevices.contains(miDevice)) { @@ -84,8 +85,8 @@ public class DeviceHelper { } } - String pebbleEmuAddr = sharedPrefs.getString("pebble_emu_addr", ""); - String pebbleEmuPort = sharedPrefs.getString("pebble_emu_port", ""); + String pebbleEmuAddr = prefs.getString("pebble_emu_addr", ""); + String pebbleEmuPort = prefs.getString("pebble_emu_port", ""); if (pebbleEmuAddr.length() >= 7 && pebbleEmuPort.length() > 0) { GBDevice pebbleEmuDevice = new GBDevice(pebbleEmuAddr + ":" + pebbleEmuPort, "Pebble qemu", DeviceType.PEBBLE); availableDevices.add(pebbleEmuDevice); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/Prefs.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/Prefs.java new file mode 100644 index 000000000..fcb49112a --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/Prefs.java @@ -0,0 +1,113 @@ +package nodomain.freeyourgadget.gadgetbridge.util; + +import android.content.SharedPreferences; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Set; + +/** + * Wraps SharedPreferences to avoid ClassCastExceptions and others. + */ +public class Prefs { + private static final Logger LOG = LoggerFactory.getLogger(Prefs.class); + + private final SharedPreferences preferences; + + public Prefs(SharedPreferences preferences) { + this.preferences = preferences; + } + + public SharedPreferences getPreferences() { + return preferences; + } + + public String getString(String key, String defaultValue) { + String value = preferences.getString(key, defaultValue); + if (value == null || "".equals(value)) { + return defaultValue; + } + return value; + } + + public Set getStringSet(String key, Set defaultValue) { + Set value = preferences.getStringSet(key, defaultValue); + if (value == null || value.isEmpty()) { + return defaultValue; + } + return value; + } + + public int getInt(String key, int defaultValue) { + try { + return preferences.getInt(key, defaultValue); + } catch (Exception ex) { + try { + String value = preferences.getString(key, String.valueOf(defaultValue)); + if ("".equals(value)) { + return defaultValue; + } + return Integer.parseInt(value); + } catch (Exception ex2) { + logReadError(key, ex); + return defaultValue; + } + } + } + + public long getLong(String key, long defaultValue) { + try { + return preferences.getLong(key, defaultValue); + } catch (Exception ex) { + try { + String value = preferences.getString(key, String.valueOf(defaultValue)); + if ("".equals(value)) { + return defaultValue; + } + return Long.parseLong(value); + } catch (Exception ex2) { + logReadError(key, ex); + return defaultValue; + } + } + } + + public float getFloat(String key, float defaultValue) { + try { + return preferences.getFloat(key, defaultValue); + } catch (Exception ex) { + try { + String value = preferences.getString(key, String.valueOf(defaultValue)); + if ("".equals(value)) { + return defaultValue; + } + return Float.parseFloat(value); + } catch (Exception ex2) { + logReadError(key, ex); + return defaultValue; + } + } + } + + public boolean getBoolean(String key, boolean defaultValue) { + try { + return preferences.getBoolean(key, defaultValue); + } catch (Exception ex) { + try { + String value = preferences.getString(key, String.valueOf(defaultValue)); + if ("".equals(value)) { + return defaultValue; + } + return Boolean.parseBoolean(value); + } catch (Exception ex2) { + logReadError(key, ex); + return defaultValue; + } + } + } + + private void logReadError(String key, Exception ex) { + LOG.error("Error reading preference value: " + key + "; returning default value", ex); // log the first exception + } +}