Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pandroid: GlobalConfig and create node data storage #348

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pandroid/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:glEsVersion="0x0030001"/>

<application
android:name=".app.PandroidApplication"
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.panda3ds.pandroid.app;

import android.app.Application;
import android.content.Context;

import com.panda3ds.pandroid.data.config.GlobalConfig;

public class PandroidApplication extends Application {
private static Context appContext;

@Override
public void onCreate() {
super.onCreate();
appContext = this;
GlobalConfig.initialize();
}

public static Context getAppContext() {
return appContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.panda3ds.pandroid.data.config;

import android.content.Context;
import android.content.SharedPreferences;

import com.panda3ds.pandroid.app.PandroidApplication;
import com.panda3ds.pandroid.utils.Constants;

import java.io.Serializable;

public class GlobalConfig {
private static SharedPreferences data;

public static void initialize() {
data = PandroidApplication.getAppContext()
.getSharedPreferences(Constants.PREF_GLOBAL_CONFIG, Context.MODE_PRIVATE);
}

public static <T extends Serializable> T get(Key<T> key) {
Serializable value;
if (key.defaultValue instanceof String) {
value = data.getString(key.name, (String) key.defaultValue);
} else if (key.defaultValue instanceof Integer) {
value = data.getInt(key.name, (int) key.defaultValue);
} else if (key.defaultValue instanceof Boolean) {
value = data.getBoolean(key.name, (boolean) key.defaultValue);
} else if (key.defaultValue instanceof Long) {
value = data.getLong(key.name, (long) key.defaultValue);
} else {
value = data.getFloat(key.name, (float) key.defaultValue);
}
return (T) value;
}

public static synchronized <T extends Serializable> void set(Key<T> key, T value) {
if (value instanceof String) {
data.edit().putString(key.name, (String) value).apply();
} else if (value instanceof Integer) {
data.edit().putInt(key.name, (int) value).apply();
} else if (value instanceof Boolean) {
data.edit().putBoolean(key.name, (boolean) value).apply();
} else if (value instanceof Long) {
data.edit().putLong(key.name, (long) value).apply();
} else if (value instanceof Float) {
data.edit().putFloat(key.name, (float) value).apply();
} else {
throw new IllegalArgumentException("Invalid global config value instance");
}
}

private static class Key<T extends Serializable> {
private final String name;
private final T defaultValue;

private Key(String name, T defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class Constants {

public static final String ACTIVITY_PARAMETER_PATH = "path";
public static final String LOG_TAG = "pandroid";

public static final String PREF_GLOBAL_CONFIG = "app.GlobalConfig";
}
Loading