From 625ab6decd5cf1a04dd9b2a5a262114116aa5f00 Mon Sep 17 00:00:00 2001 From: Colin Edwards Date: Fri, 22 Dec 2023 19:10:45 -0600 Subject: [PATCH] android: Move preferences class out of settings --- .../main/java/org/musicpd/Preferences.java | 34 +++++++++++++++++++ .../src/main/java/org/musicpd/Receiver.java | 8 ++--- .../src/main/java/org/musicpd/Settings.java | 29 ---------------- 3 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 android/app/src/main/java/org/musicpd/Preferences.java diff --git a/android/app/src/main/java/org/musicpd/Preferences.java b/android/app/src/main/java/org/musicpd/Preferences.java new file mode 100644 index 000000000..61eca8318 --- /dev/null +++ b/android/app/src/main/java/org/musicpd/Preferences.java @@ -0,0 +1,34 @@ +package org.musicpd; + +import static android.content.Context.MODE_PRIVATE; + +import android.content.Context; +import android.content.SharedPreferences; + +public class Preferences { + private static final String TAG = "Settings"; + + public static final String KEY_RUN_ON_BOOT ="run_on_boot"; + public static final String KEY_WAKELOCK ="wakelock"; + public static final String KEY_PAUSE_ON_HEADPHONES_DISCONNECT ="pause_on_headphones_disconnect"; + + public static SharedPreferences get(Context context) { + return context.getSharedPreferences(TAG, MODE_PRIVATE); + } + + public static void putBoolean(Context context, String key, boolean value) { + final SharedPreferences prefs = get(context); + + if (prefs == null) + return; + final SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean(key, value); + editor.apply(); + } + + public static boolean getBoolean(Context context, String key, boolean defValue) { + final SharedPreferences prefs = get(context); + + return prefs != null ? prefs.getBoolean(key, defValue) : defValue; + } +} \ No newline at end of file diff --git a/android/app/src/main/java/org/musicpd/Receiver.java b/android/app/src/main/java/org/musicpd/Receiver.java index 2dff4965c..631cbc6e3 100644 --- a/android/app/src/main/java/org/musicpd/Receiver.java +++ b/android/app/src/main/java/org/musicpd/Receiver.java @@ -13,12 +13,12 @@ public class Receiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Log.d("Receiver", "onReceive: " + intent); if (intent.getAction() == "android.intent.action.BOOT_COMPLETED") { - if (Settings.Preferences.getBoolean(context, - Settings.Preferences.KEY_RUN_ON_BOOT, + if (Preferences.getBoolean(context, + Preferences.KEY_RUN_ON_BOOT, false)) { final boolean wakelock = - Settings.Preferences.getBoolean(context, - Settings.Preferences.KEY_WAKELOCK, false); + Preferences.getBoolean(context, + Preferences.KEY_WAKELOCK, false); Main.start(context, wakelock); } } diff --git a/android/app/src/main/java/org/musicpd/Settings.java b/android/app/src/main/java/org/musicpd/Settings.java index c0c00b9ac..4d035f9e5 100644 --- a/android/app/src/main/java/org/musicpd/Settings.java +++ b/android/app/src/main/java/org/musicpd/Settings.java @@ -7,9 +7,6 @@ import java.util.LinkedList; import android.Manifest; import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; @@ -40,32 +37,6 @@ public class Settings extends Activity { private static final int MSG_STARTED = 2; private static final int MSG_LOG = 3; - public static class Preferences { - public static final String KEY_RUN_ON_BOOT ="run_on_boot"; - public static final String KEY_WAKELOCK ="wakelock"; - public static final String KEY_PAUSE_ON_HEADPHONES_DISCONNECT ="pause_on_headphones_disconnect"; - - public static SharedPreferences get(Context context) { - return context.getSharedPreferences(TAG, MODE_PRIVATE); - } - - public static void putBoolean(Context context, String key, boolean value) { - final SharedPreferences prefs = get(context); - - if (prefs == null) - return; - final Editor editor = prefs.edit(); - editor.putBoolean(key, value); - editor.apply(); - } - - public static boolean getBoolean(Context context, String key, boolean defValue) { - final SharedPreferences prefs = get(context); - - return prefs != null ? prefs.getBoolean(key, defValue) : defValue; - } - } - private Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) {