Skip to content

Commit da4bcb9

Browse files
committed
Migrate SharedPreferences to DataStore for app state keys.
1 parent 5b05f56 commit da4bcb9

7 files changed

Lines changed: 89 additions & 71 deletions

File tree

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
3535
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
3636
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
3737
coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
38+
datastore-core = { module = "androidx.datastore:datastore-core", version.ref = "datastore" }
3839
datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
3940
junit5-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit5" }
4041
junit5-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit5" }

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ dependencies {
7171
api libs.coroutines.core
7272
api libs.coroutines.android
7373

74+
api libs.datastore.core
7475
api libs.datastore.preferences
7576

7677
api 'com.github.ncmud:mth:2.0.4'

lib/src/main/java/org/ncmud/mudwammer/launcher/Launcher.java

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import android.content.Intent;
1212
import android.content.ServiceConnection;
1313
import android.content.SharedPreferences;
14-
import android.content.SharedPreferences.Editor;
14+
import org.ncmud.mudwammer.data.AppStateKeys;
15+
import org.ncmud.mudwammer.data.AppStateStore;
1516
import android.content.pm.ApplicationInfo;
1617
import android.content.pm.PackageManager;
1718
import android.content.pm.PackageManager.NameNotFoundException;
@@ -426,8 +427,7 @@ public void onClick(View v) {
426427
// if(mode == LAUNCH_MODE.TEST) {
427428
if (ConfigurationLoader.isTestMode(this)) {
428429
int readver =
429-
this.getSharedPreferences("TEST_VERSION_DOWHATSNEW", Context.MODE_PRIVATE)
430-
.getInt("TEST_VERSION", 0);
430+
AppStateStore.getInt(this, AppStateKeys.INSTANCE.getTEST_VERSION(), 0);
431431
int testVersion = 0;
432432
try {
433433
testVersion =
@@ -442,11 +442,7 @@ public void onClick(View v) {
442442

443443
if (testVersion != readver) {
444444
dowhatsnew = true;
445-
SharedPreferences.Editor edit =
446-
this.getSharedPreferences("TEST_VERSION_DOWHATSNEW", Context.MODE_PRIVATE)
447-
.edit();
448-
edit.putInt("TEST_VERSION", testVersion);
449-
edit.apply();
445+
AppStateStore.putInt(this, AppStateKeys.INSTANCE.getTEST_VERSION(), testVersion);
450446
}
451447
}
452448

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

631-
SharedPreferences pref = Launcher.this.getSharedPreferences("STATUS_BAR_HEIGHT", 0);
632-
Editor e = pref.edit();
633-
e.putInt("STATUS_BAR_HEIGHT", statusBarHeight);
634-
e.putInt("TITLE_BAR_HEIGHT", titleBarHeight);
635-
e.apply();
627+
AppStateStore.putInt(Launcher.this, AppStateKeys.INSTANCE.getSTATUS_BAR_HEIGHT(), statusBarHeight);
628+
AppStateStore.putInt(Launcher.this, AppStateKeys.INSTANCE.getTITLE_BAR_HEIGHT(), titleBarHeight);
636629

637630
MudConnection muc = apdapter.getItem(arg2);
638631

@@ -1247,29 +1240,6 @@ public void onClick(DialogInterface dialog, int which) {
12471240
}
12481241

12491242
private void DoNewStartup() {
1250-
Pattern invalidchars = Pattern.compile("\\W");
1251-
Matcher replacebadchars = invalidchars.matcher(launch.getDisplayName());
1252-
String prefsname = replacebadchars.replaceAll("") + ".PREFS";
1253-
// prefsname = prefsname.replaceAll("/", "");
1254-
1255-
SharedPreferences sprefs = Launcher.this.getSharedPreferences(prefsname, 0);
1256-
// servicestarted = prefs.getBoolean("CONNECTED", false);
1257-
// finishStart = prefs.getBoolean("FINISHSTART", true);
1258-
SharedPreferences.Editor editor = sprefs.edit();
1259-
editor.putBoolean("CONNECTED", false);
1260-
editor.putBoolean("FINISHSTART", true);
1261-
editor.apply();
1262-
// Log.e("LAUNCHER","SERVICE NOT STARTED, AM RESETTING THE INITIALIZER BOOLS IN " +
1263-
// prefsname);
1264-
1265-
// Launcher.this.startActivity(the_intent);
1266-
// SharedPreferences sprefs = Launcher.this.getSharedPreferences(prefsname,0);
1267-
// SharedPreferences.Editor editor = sprefs.edit();
1268-
// editor.putBoolean("CONNECTED", false);
1269-
// editor.putBoolean("FINISHSTART", true);
1270-
editor.apply();
1271-
1272-
// launch = muc;
12731243
DoFinalStartup();
12741244
}
12751245

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

1307-
SharedPreferences prefs = Launcher.this.getSharedPreferences("SERVICE_INFO", 0);
1308-
Editor edit = prefs.edit();
1309-
1310-
edit.putString("SETTINGS_PATH", launch.getDisplayName());
1311-
edit.apply();
1312-
13131277
// this.unbindService(connectionChecker);
13141278

13151279
Launcher.this.startActivity(the_intent);
@@ -1991,10 +1955,6 @@ public void handleMessage(Message msg) {
19911955
if (outer == null) return;
19921956
switch (msg.what) {
19931957
case MESSAGE_USERNAME:
1994-
SharedPreferences.Editor edit =
1995-
outer.getSharedPreferences("TEST_USER", Context.MODE_PRIVATE).edit();
1996-
edit.putString("USER_NAME", (String) msg.obj);
1997-
edit.apply();
19981958
break;
19991959
case MESSAGE_WHATSNEW:
20001960
break;

lib/src/main/java/org/ncmud/mudwammer/service/Connection.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
package org.ncmud.mudwammer.service;
55

66
import android.content.Context;
7-
import android.content.SharedPreferences;
7+
import org.ncmud.mudwammer.data.AppStateKeys;
8+
import org.ncmud.mudwammer.data.AppStateStore;
89
import android.content.pm.ApplicationInfo;
910
import android.content.pm.PackageManager;
1011
import android.content.pm.PackageManager.NameNotFoundException;
@@ -447,17 +448,15 @@ public Connection(
447448
},
448449
() -> self.mPump != null && self.mPump.isConnected());
449450

450-
SharedPreferences sprefs = this.getContext().getSharedPreferences("STATUS_BAR_HEIGHT", 0);
451451
mStatusBarHeight =
452-
sprefs.getInt(
453-
"STATUS_BAR_HEIGHT",
452+
AppStateStore.getInt(this.getContext(), AppStateKeys.INSTANCE.getSTATUS_BAR_HEIGHT(),
454453
(int)
455454
(STATUS_BAR_DEFAULT_SIZE
456455
* this.getContext()
457456
.getResources()
458457
.getDisplayMetrics()
459458
.density));
460-
mTitleBarHeight = sprefs.getInt("TITLE_BAR_HEIGHT", 0);
459+
mTitleBarHeight = AppStateStore.getInt(this.getContext(), AppStateKeys.INSTANCE.getTITLE_BAR_HEIGHT(), 0);
461460

462461
mLoaded = true;
463462

lib/src/main/java/org/ncmud/mudwammer/service/StellarService.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import android.app.Service;
1212
import android.content.Context;
1313
import android.content.Intent;
14-
import android.content.SharedPreferences;
14+
import org.ncmud.mudwammer.data.AppStateKeys;
15+
import org.ncmud.mudwammer.data.AppStateStore;
1516
import android.content.pm.ApplicationInfo;
1617
import android.content.pm.PackageManager;
1718
import android.content.pm.PackageManager.NameNotFoundException;
@@ -171,9 +172,7 @@ public final void onCreate() {
171172
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
172173
mNotificationManager.cancelAll();
173174

174-
SharedPreferences prefs = this.getSharedPreferences("SERVICE_INFO", 0);
175-
176-
int libsver = prefs.getInt("CURRENT_LUA_LIBS_VERSION", 0);
175+
int libsver = AppStateStore.getInt(this, AppStateKeys.INSTANCE.getLUA_LIBS_VERSION(), 0);
177176
Bundle meta = null;
178177
try {
179178
meta =
@@ -188,10 +187,7 @@ public final void onCreate() {
188187
// copy new libs.
189188
try {
190189
updateLibs();
191-
// updatelibsver needs to be incremented and saved back into the shared preferences
192-
SharedPreferences.Editor editor = prefs.edit();
193-
editor.putInt("CURRENT_LUA_LIBS_VERSION", packagever);
194-
editor.apply();
190+
AppStateStore.putInt(this, AppStateKeys.INSTANCE.getLUA_LIBS_VERSION(), packagever);
195191
} catch (NameNotFoundException e) {
196192
e.printStackTrace();
197193
} catch (IOException e) {

lib/src/main/java/org/ncmud/mudwammer/window/MainWindow.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import android.content.Intent;
1212
import android.content.ServiceConnection;
1313
import android.content.SharedPreferences;
14+
import org.ncmud.mudwammer.data.AppStateKeys;
15+
import org.ncmud.mudwammer.data.AppStateStore;
1416
import android.content.pm.ActivityInfo;
1517
import android.content.pm.ApplicationInfo;
1618
import android.content.pm.PackageManager;
@@ -330,12 +332,10 @@ public void onCreate(Bundle icicle) {
330332
// org.ncmud.mudwammer.crashreport.CrashReporter(this.getApplicationContext()));
331333
}
332334

333-
SharedPreferences sprefs = this.getSharedPreferences("STATUS_BAR_HEIGHT", 0);
334335
statusBarHeight =
335-
sprefs.getInt(
336-
"STATUS_BAR_HEIGHT",
336+
AppStateStore.getInt(this, AppStateKeys.INSTANCE.getSTATUS_BAR_HEIGHT(),
337337
(int) (25 * this.getResources().getDisplayMetrics().density));
338-
titleBarHeight = sprefs.getInt("TITLE_BAR_HEIGHT", 0);
338+
titleBarHeight = AppStateStore.getInt(this, AppStateKeys.INSTANCE.getTITLE_BAR_HEIGHT(), 0);
339339
setContentView(R.layout.window_layout);
340340

341341
androidx.appcompat.widget.Toolbar myToolbar =
@@ -951,10 +951,6 @@ private void handleMainMessage(Message msg) {
951951
String serviceBindAction =
952952
ConfigurationLoader.getConfigurationValue(
953953
"serviceBindAction", MainWindow.this);
954-
SharedPreferences.Editor edit =
955-
getSharedPreferences("CONNECT_TO", Context.MODE_PRIVATE).edit();
956-
edit.putString("CONNECT_TO", getIntent().getStringExtra("DISPLAY"));
957-
edit.apply();
958954
bindService(
959955
new Intent(
960956
serviceBindAction,
@@ -2359,10 +2355,6 @@ public void onResume() {
23592355
//this.startService(new Intent(org.ncmud.mudwammer.service.IStellarService.class.getName() + ".MODE_TEST"));
23602356
this.bindService(new Intent(org.ncmud.mudwammer.service.IStellarService.class.getName()+".MODE_TEST"), mConnection, 0);
23612357
}*/
2362-
SharedPreferences.Editor edit =
2363-
MainWindow.this.getSharedPreferences("CONNECT_TO", Context.MODE_PRIVATE).edit();
2364-
edit.putString("CONNECT_TO", MainWindow.this.getIntent().getStringExtra("DISPLAY"));
2365-
edit.apply();
23662358
String serviceBindAction =
23672359
ConfigurationLoader.getConfigurationValue("serviceBindAction", this);
23682360
this.bindService(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.ncmud.mudwammer.data
2+
3+
import android.content.Context
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.core.MultiProcessDataStoreFactory
6+
import androidx.datastore.core.Serializer
7+
import androidx.datastore.preferences.core.MutablePreferences
8+
import androidx.datastore.preferences.core.Preferences
9+
import androidx.datastore.preferences.core.PreferencesSerializer
10+
import androidx.datastore.preferences.core.edit
11+
import androidx.datastore.preferences.core.intPreferencesKey
12+
import kotlinx.coroutines.flow.first
13+
import kotlinx.coroutines.runBlocking
14+
import okio.buffer
15+
import okio.sink
16+
import okio.source
17+
import java.io.InputStream
18+
import java.io.OutputStream
19+
20+
object AppStateKeys {
21+
val STATUS_BAR_HEIGHT = intPreferencesKey("status_bar_height")
22+
val TITLE_BAR_HEIGHT = intPreferencesKey("title_bar_height")
23+
val TEST_VERSION = intPreferencesKey("test_version")
24+
val LUA_LIBS_VERSION = intPreferencesKey("lua_libs_version")
25+
}
26+
27+
private object PreferencesJavaIoSerializer : Serializer<Preferences> {
28+
override val defaultValue: Preferences = PreferencesSerializer.defaultValue
29+
30+
override suspend fun readFrom(input: InputStream): Preferences =
31+
PreferencesSerializer.readFrom(input.source().buffer())
32+
33+
override suspend fun writeTo(t: Preferences, output: OutputStream) {
34+
val sink = output.sink().buffer()
35+
PreferencesSerializer.writeTo(t, sink)
36+
sink.flush()
37+
}
38+
}
39+
40+
object AppStateStore {
41+
@Volatile
42+
private var instance: DataStore<Preferences>? = null
43+
44+
fun getInstance(context: Context): DataStore<Preferences> {
45+
return instance ?: synchronized(this) {
46+
instance ?: MultiProcessDataStoreFactory.create<Preferences>(
47+
serializer = PreferencesJavaIoSerializer,
48+
produceFile = {
49+
context.applicationContext.filesDir.resolve("datastore/app_state.preferences_pb")
50+
}
51+
).also { instance = it }
52+
}
53+
}
54+
55+
@JvmStatic
56+
fun getInt(context: Context, key: Preferences.Key<Int>, defaultValue: Int): Int = runBlocking {
57+
getInstance(context).data.first()[key] ?: defaultValue
58+
}
59+
60+
@JvmStatic
61+
fun putInt(context: Context, key: Preferences.Key<Int>, value: Int): Unit = runBlocking {
62+
getInstance(context).edit { it[key] = value }
63+
}
64+
65+
@JvmStatic
66+
fun getAll(context: Context): Preferences = runBlocking {
67+
getInstance(context).data.first()
68+
}
69+
}

0 commit comments

Comments
 (0)