android: Move preferences class out of settings

This commit is contained in:
Colin Edwards 2023-12-22 19:10:45 -06:00
parent 2638396df5
commit 625ab6decd
3 changed files with 38 additions and 33 deletions

View File

@ -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;
}
}

View File

@ -13,12 +13,12 @@ public class Receiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d("Receiver", "onReceive: " + intent); Log.d("Receiver", "onReceive: " + intent);
if (intent.getAction() == "android.intent.action.BOOT_COMPLETED") { if (intent.getAction() == "android.intent.action.BOOT_COMPLETED") {
if (Settings.Preferences.getBoolean(context, if (Preferences.getBoolean(context,
Settings.Preferences.KEY_RUN_ON_BOOT, Preferences.KEY_RUN_ON_BOOT,
false)) { false)) {
final boolean wakelock = final boolean wakelock =
Settings.Preferences.getBoolean(context, Preferences.getBoolean(context,
Settings.Preferences.KEY_WAKELOCK, false); Preferences.KEY_WAKELOCK, false);
Main.start(context, wakelock); Main.start(context, wakelock);
} }
} }

View File

@ -7,9 +7,6 @@ import java.util.LinkedList;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; 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_STARTED = 2;
private static final int MSG_LOG = 3; 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() { private Handler mHandler = new Handler(new Handler.Callback() {
@Override @Override
public boolean handleMessage(Message msg) { public boolean handleMessage(Message msg) {