Skip to content

Commit 648e22a

Browse files
author
Krzysztof Borowy
committed
fix: preventive measures
1 parent 9ac0f05 commit 648e22a

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

android/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def useDedicatedExecutor = rootProject.hasProperty('AsyncStorage_dedicatedExecut
3333
? rootProject.properties['AsyncStorage_dedicatedExecutor']
3434
: false
3535

36+
def disableLocalizedCollators = rootProject.hasProperty('AsyncStorage_noLocalizedCollators')
37+
? rootProject.properties['AsyncStorage_noLocalizedCollators']
38+
: false
39+
3640
apply plugin: 'com.android.library'
3741

3842
android {
@@ -43,6 +47,7 @@ android {
4347
targetSdkVersion safeExtGet('targetSdkVersion', 28)
4448
buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
4549
buildConfigField("boolean", "AsyncStorage_useDedicatedExecutor", "${useDedicatedExecutor}")
50+
buildConfigField("boolean", "AsyncStorage_noLocalizedCollators", "${disableLocalizedCollators}")
4651
}
4752
lintOptions {
4853
abortOnError false

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.facebook.react.bridge.Arguments;
1616
import com.facebook.react.bridge.Callback;
1717
import com.facebook.react.bridge.GuardedAsyncTask;
18+
import com.facebook.react.bridge.LifecycleEventListener;
1819
import com.facebook.react.bridge.ReactApplicationContext;
1920
import com.facebook.react.bridge.ReactContextBaseJavaModule;
2021
import com.facebook.react.bridge.ReactMethod;
@@ -33,7 +34,7 @@
3334

3435
@ReactModule(name = AsyncStorageModule.NAME)
3536
public final class AsyncStorageModule
36-
extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable {
37+
extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable, LifecycleEventListener {
3738

3839
// changed name to not conflict with AsyncStorage from RN repo
3940
public static final String NAME = "RNC_AsyncSQLiteDBStorage";
@@ -91,6 +92,7 @@ public AsyncStorageModule(ReactApplicationContext reactContext) {
9192
AsyncStorageModule(ReactApplicationContext reactContext, Executor executor) {
9293
super(reactContext);
9394
this.executor = new SerialExecutor(executor);
95+
reactContext.addLifecycleEventListener(this);
9496
mReactDatabaseSupplier = ReactDatabaseSupplier.getInstance(reactContext);
9597
}
9698

@@ -118,6 +120,18 @@ public void clearSensitiveData() {
118120
mReactDatabaseSupplier.clearAndCloseDatabase();
119121
}
120122

123+
@Override
124+
public void onHostResume() {}
125+
126+
@Override
127+
public void onHostPause() {}
128+
129+
@Override
130+
public void onHostDestroy() {
131+
// ensure we close database when activity is destroyed
132+
mReactDatabaseSupplier.closeDatabase();
133+
}
134+
121135
/**
122136
* Given an array of keys, this returns a map of (key, value) pairs for the keys found, and
123137
* (key, null) for the keys that haven't been found.

android/src/main/java/com/reactnativecommunity/asyncstorage/ReactDatabaseSupplier.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper {
4444
private Context mContext;
4545
private @Nullable SQLiteDatabase mDb;
4646
private long mMaximumDatabaseSize = BuildConfig.AsyncStorage_db_size * 1024L * 1024L;
47+
private boolean disableLocalizedCollators = BuildConfig.AsyncStorage_noLocalizedCollators;
4748

4849
private ReactDatabaseSupplier(Context context) {
4950
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@@ -85,7 +86,13 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
8586
if (tries > 0) {
8687
deleteDatabase();
8788
}
88-
mDb = getWritableDatabase();
89+
90+
if(disableLocalizedCollators) {
91+
String dbPath = mContext.getDatabasePath(DATABASE_NAME).getPath();
92+
mDb = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);
93+
} else {
94+
mDb = getWritableDatabase();
95+
}
8996
break;
9097
} catch (SQLiteException e) {
9198
lastSQLiteException = e;
@@ -151,7 +158,7 @@ private synchronized boolean deleteDatabase() {
151158
return mContext.deleteDatabase(DATABASE_NAME);
152159
}
153160

154-
private synchronized void closeDatabase() {
161+
public synchronized void closeDatabase() {
155162
if (mDb != null && mDb.isOpen()) {
156163
mDb.close();
157164
mDb = null;

example/android/gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
# Enable dedicated thread pool executor
2424
AsyncStorage_dedicatedExecutor=true
25+
AsyncStorage_noLocalizedCollators=true
2526

2627
android.useAndroidX=true
2728
android.enableJetifier=true

0 commit comments

Comments
 (0)