Skip to content
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 gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
datastore-core = { module = "androidx.datastore:datastore-core", version.ref = "datastore" }
datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
junit5-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit5" }
junit5-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit5" }
Expand Down
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies {
api libs.coroutines.core
api libs.coroutines.android

api libs.datastore.core
api libs.datastore.preferences

api 'com.github.ncmud:mth:2.0.4'
Expand Down
52 changes: 6 additions & 46 deletions lib/src/main/java/org/ncmud/mudwammer/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import org.ncmud.mudwammer.data.AppStateKeys;
import org.ncmud.mudwammer.data.AppStateStore;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
Expand Down Expand Up @@ -426,8 +427,7 @@ public void onClick(View v) {
// if(mode == LAUNCH_MODE.TEST) {
if (ConfigurationLoader.isTestMode(this)) {
int readver =
this.getSharedPreferences("TEST_VERSION_DOWHATSNEW", Context.MODE_PRIVATE)
.getInt("TEST_VERSION", 0);
AppStateStore.getInt(this, AppStateKeys.INSTANCE.getTEST_VERSION(), 0);
int testVersion = 0;
try {
testVersion =
Expand All @@ -442,11 +442,7 @@ public void onClick(View v) {

if (testVersion != readver) {
dowhatsnew = true;
SharedPreferences.Editor edit =
this.getSharedPreferences("TEST_VERSION_DOWHATSNEW", Context.MODE_PRIVATE)
.edit();
edit.putInt("TEST_VERSION", testVersion);
edit.apply();
AppStateStore.putInt(this, AppStateKeys.INSTANCE.getTEST_VERSION(), testVersion);
}
}

Expand Down Expand Up @@ -628,11 +624,8 @@ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// titleBarHeight += statusBarHeight;
// }

SharedPreferences pref = Launcher.this.getSharedPreferences("STATUS_BAR_HEIGHT", 0);
Editor e = pref.edit();
e.putInt("STATUS_BAR_HEIGHT", statusBarHeight);
e.putInt("TITLE_BAR_HEIGHT", titleBarHeight);
e.apply();
AppStateStore.putInt(Launcher.this, AppStateKeys.INSTANCE.getSTATUS_BAR_HEIGHT(), statusBarHeight);
AppStateStore.putInt(Launcher.this, AppStateKeys.INSTANCE.getTITLE_BAR_HEIGHT(), titleBarHeight);

MudConnection muc = apdapter.getItem(arg2);

Expand Down Expand Up @@ -1247,29 +1240,6 @@ public void onClick(DialogInterface dialog, int which) {
}

private void DoNewStartup() {
Pattern invalidchars = Pattern.compile("\\W");
Matcher replacebadchars = invalidchars.matcher(launch.getDisplayName());
String prefsname = replacebadchars.replaceAll("") + ".PREFS";
// prefsname = prefsname.replaceAll("/", "");

SharedPreferences sprefs = Launcher.this.getSharedPreferences(prefsname, 0);
// servicestarted = prefs.getBoolean("CONNECTED", false);
// finishStart = prefs.getBoolean("FINISHSTART", true);
SharedPreferences.Editor editor = sprefs.edit();
editor.putBoolean("CONNECTED", false);
editor.putBoolean("FINISHSTART", true);
editor.apply();
// Log.e("LAUNCHER","SERVICE NOT STARTED, AM RESETTING THE INITIALIZER BOOLS IN " +
// prefsname);

// Launcher.this.startActivity(the_intent);
// SharedPreferences sprefs = Launcher.this.getSharedPreferences(prefsname,0);
// SharedPreferences.Editor editor = sprefs.edit();
// editor.putBoolean("CONNECTED", false);
// editor.putBoolean("FINISHSTART", true);
editor.apply();

// launch = muc;
DoFinalStartup();
}

Expand Down Expand Up @@ -1304,12 +1274,6 @@ private void DoFinalStartup() {
// Log.e("LAUNCHER","SERVICE NOT STARTED, AM RESETTING THE INITIALIZER BOOLS IN " +
// prefsname);

SharedPreferences prefs = Launcher.this.getSharedPreferences("SERVICE_INFO", 0);
Editor edit = prefs.edit();

edit.putString("SETTINGS_PATH", launch.getDisplayName());
edit.apply();

// this.unbindService(connectionChecker);

Launcher.this.startActivity(the_intent);
Expand Down Expand Up @@ -1991,10 +1955,6 @@ public void handleMessage(Message msg) {
if (outer == null) return;
switch (msg.what) {
case MESSAGE_USERNAME:
SharedPreferences.Editor edit =
outer.getSharedPreferences("TEST_USER", Context.MODE_PRIVATE).edit();
edit.putString("USER_NAME", (String) msg.obj);
edit.apply();
break;
case MESSAGE_WHATSNEW:
break;
Expand Down
9 changes: 4 additions & 5 deletions lib/src/main/java/org/ncmud/mudwammer/service/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package org.ncmud.mudwammer.service;

import android.content.Context;
import android.content.SharedPreferences;
import org.ncmud.mudwammer.data.AppStateKeys;
import org.ncmud.mudwammer.data.AppStateStore;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
Expand Down Expand Up @@ -447,17 +448,15 @@ public Connection(
},
() -> self.mPump != null && self.mPump.isConnected());

SharedPreferences sprefs = this.getContext().getSharedPreferences("STATUS_BAR_HEIGHT", 0);
mStatusBarHeight =
sprefs.getInt(
"STATUS_BAR_HEIGHT",
AppStateStore.getInt(this.getContext(), AppStateKeys.INSTANCE.getSTATUS_BAR_HEIGHT(),
(int)
(STATUS_BAR_DEFAULT_SIZE
* this.getContext()
.getResources()
.getDisplayMetrics()
.density));
mTitleBarHeight = sprefs.getInt("TITLE_BAR_HEIGHT", 0);
mTitleBarHeight = AppStateStore.getInt(this.getContext(), AppStateKeys.INSTANCE.getTITLE_BAR_HEIGHT(), 0);

mLoaded = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import org.ncmud.mudwammer.data.AppStateKeys;
import org.ncmud.mudwammer.data.AppStateStore;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
Expand Down Expand Up @@ -171,9 +172,7 @@ public final void onCreate() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();

SharedPreferences prefs = this.getSharedPreferences("SERVICE_INFO", 0);

int libsver = prefs.getInt("CURRENT_LUA_LIBS_VERSION", 0);
int libsver = AppStateStore.getInt(this, AppStateKeys.INSTANCE.getLUA_LIBS_VERSION(), 0);
Bundle meta = null;
try {
meta =
Expand All @@ -188,10 +187,7 @@ public final void onCreate() {
// copy new libs.
try {
updateLibs();
// updatelibsver needs to be incremented and saved back into the shared preferences
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("CURRENT_LUA_LIBS_VERSION", packagever);
editor.apply();
AppStateStore.putInt(this, AppStateKeys.INSTANCE.getLUA_LIBS_VERSION(), packagever);
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Expand Down
16 changes: 4 additions & 12 deletions lib/src/main/java/org/ncmud/mudwammer/window/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import org.ncmud.mudwammer.data.AppStateKeys;
import org.ncmud.mudwammer.data.AppStateStore;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -330,12 +332,10 @@ public void onCreate(Bundle icicle) {
// org.ncmud.mudwammer.crashreport.CrashReporter(this.getApplicationContext()));
}

SharedPreferences sprefs = this.getSharedPreferences("STATUS_BAR_HEIGHT", 0);
statusBarHeight =
sprefs.getInt(
"STATUS_BAR_HEIGHT",
AppStateStore.getInt(this, AppStateKeys.INSTANCE.getSTATUS_BAR_HEIGHT(),
(int) (25 * this.getResources().getDisplayMetrics().density));
titleBarHeight = sprefs.getInt("TITLE_BAR_HEIGHT", 0);
titleBarHeight = AppStateStore.getInt(this, AppStateKeys.INSTANCE.getTITLE_BAR_HEIGHT(), 0);
setContentView(R.layout.window_layout);

androidx.appcompat.widget.Toolbar myToolbar =
Expand Down Expand Up @@ -951,10 +951,6 @@ private void handleMainMessage(Message msg) {
String serviceBindAction =
ConfigurationLoader.getConfigurationValue(
"serviceBindAction", MainWindow.this);
SharedPreferences.Editor edit =
getSharedPreferences("CONNECT_TO", Context.MODE_PRIVATE).edit();
edit.putString("CONNECT_TO", getIntent().getStringExtra("DISPLAY"));
edit.apply();
bindService(
new Intent(
serviceBindAction,
Expand Down Expand Up @@ -2359,10 +2355,6 @@ public void onResume() {
//this.startService(new Intent(org.ncmud.mudwammer.service.IStellarService.class.getName() + ".MODE_TEST"));
this.bindService(new Intent(org.ncmud.mudwammer.service.IStellarService.class.getName()+".MODE_TEST"), mConnection, 0);
}*/
SharedPreferences.Editor edit =
MainWindow.this.getSharedPreferences("CONNECT_TO", Context.MODE_PRIVATE).edit();
edit.putString("CONNECT_TO", MainWindow.this.getIntent().getStringExtra("DISPLAY"));
edit.apply();
String serviceBindAction =
ConfigurationLoader.getConfigurationValue("serviceBindAction", this);
this.bindService(
Expand Down
69 changes: 69 additions & 0 deletions lib/src/main/kotlin/org/ncmud/mudwammer/data/AppStateStore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.ncmud.mudwammer.data

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.MultiProcessDataStoreFactory
import androidx.datastore.core.Serializer
import androidx.datastore.preferences.core.MutablePreferences
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.PreferencesSerializer
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okio.buffer
import okio.sink
import okio.source
import java.io.InputStream
import java.io.OutputStream

object AppStateKeys {
val STATUS_BAR_HEIGHT = intPreferencesKey("status_bar_height")
val TITLE_BAR_HEIGHT = intPreferencesKey("title_bar_height")
val TEST_VERSION = intPreferencesKey("test_version")
val LUA_LIBS_VERSION = intPreferencesKey("lua_libs_version")
}

private object PreferencesJavaIoSerializer : Serializer<Preferences> {
override val defaultValue: Preferences = PreferencesSerializer.defaultValue

override suspend fun readFrom(input: InputStream): Preferences =
PreferencesSerializer.readFrom(input.source().buffer())

override suspend fun writeTo(t: Preferences, output: OutputStream) {
val sink = output.sink().buffer()
PreferencesSerializer.writeTo(t, sink)
sink.flush()
}
}

object AppStateStore {
@Volatile
private var instance: DataStore<Preferences>? = null

fun getInstance(context: Context): DataStore<Preferences> {
return instance ?: synchronized(this) {
instance ?: MultiProcessDataStoreFactory.create<Preferences>(
serializer = PreferencesJavaIoSerializer,
produceFile = {
context.applicationContext.filesDir.resolve("datastore/app_state.preferences_pb")
}
).also { instance = it }
}
}

@JvmStatic
fun getInt(context: Context, key: Preferences.Key<Int>, defaultValue: Int): Int = runBlocking {
getInstance(context).data.first()[key] ?: defaultValue
}

@JvmStatic
fun putInt(context: Context, key: Preferences.Key<Int>, value: Int): Unit = runBlocking {
getInstance(context).edit { it[key] = value }
}

@JvmStatic
fun getAll(context: Context): Preferences = runBlocking {
getInstance(context).data.first()
}
}
Loading