-
Notifications
You must be signed in to change notification settings - Fork 521
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
decouple storage from activity lifecycle #62
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
*/ | ||
package io.liteglue; | ||
|
||
|
||
import android.app.Activity; | ||
|
||
import com.facebook.react.ReactPackage; | ||
|
@@ -19,19 +18,23 @@ | |
import java.util.List; | ||
|
||
public class SQLitePluginPackage implements ReactPackage { | ||
private Activity activity = null; | ||
|
||
public SQLitePluginPackage(Activity activity){ | ||
this.activity = activity; | ||
/** | ||
* @deprecated Please use version without activity parameter | ||
* activity parameter is ignored | ||
*/ | ||
public SQLitePluginPackage(Activity activity) { | ||
this(); | ||
} | ||
|
||
public SQLitePluginPackage(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same backward incompatibility issue. Keep both constructors. |
||
} | ||
|
||
@Override | ||
public List<NativeModule> createNativeModules( | ||
ReactApplicationContext reactContext) { | ||
List<NativeModule> modules = new ArrayList<>(); | ||
|
||
modules.add(new SQLitePlugin(reactContext, activity)); | ||
|
||
modules.add(new SQLitePlugin(reactContext)); | ||
return modules; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,13 @@ | |
package org.pgsqlite; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.database.Cursor; | ||
import android.database.CursorWindow; | ||
import android.database.sqlite.SQLiteCursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteException; | ||
import android.database.sqlite.SQLiteStatement; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.util.Base64; | ||
import android.util.Log; | ||
|
@@ -48,7 +47,7 @@ | |
import java.io.IOException; | ||
|
||
|
||
public class SQLitePlugin extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks { | ||
public class SQLitePlugin extends ReactContextBaseJavaModule { | ||
|
||
private static final String PLUGIN_NAME = "SQLite"; | ||
|
||
|
@@ -76,62 +75,16 @@ public class SQLitePlugin extends ReactContextBaseJavaModule implements Applicat | |
/** | ||
* Linked activity | ||
*/ | ||
protected Activity activity = null; | ||
protected Context context = null; | ||
|
||
/** | ||
* Thread pool for database operations | ||
*/ | ||
protected ExecutorService threadPool; | ||
|
||
@Override | ||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityStarted(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityResumed(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityPaused(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityStopped(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | ||
|
||
} | ||
|
||
/** | ||
* If activity matche linked Activity of this plugin, all open databases are closed. | ||
* | ||
* @param activity | ||
*/ | ||
@Override | ||
public void onActivityDestroyed(Activity activity) { | ||
Activity myActivity = this.getActivity(); | ||
if (activity == myActivity){ | ||
myActivity.getApplication().unregisterActivityLifecycleCallbacks(this); | ||
Log.d(LOG_TAG, "linked activity destroyed - closing all open databases"); | ||
this.closeAllOpenDatabases(); | ||
} | ||
} | ||
|
||
public SQLitePlugin(ReactApplicationContext reactContext, Activity activity) { | ||
public SQLitePlugin(ReactApplicationContext reactContext) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backward compatibility issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the SQLitePluginPackage class from pgsqlite is not included in this PR? Should be change in similar fashion as the liteglue class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no compatibility issue as Package is used by external users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, looks good. |
||
super(reactContext); | ||
this.activity = activity; | ||
this.activity.getApplication().registerActivityLifecycleCallbacks(this); | ||
this.context = reactContext.getApplicationContext(); | ||
this.threadPool = Executors.newCachedThreadPool(); | ||
} | ||
|
||
|
@@ -228,8 +181,8 @@ protected ExecutorService getThreadPool(){ | |
* | ||
* @return linked activity | ||
*/ | ||
protected Activity getActivity(){ | ||
return this.activity; | ||
protected Context getContext(){ | ||
return this.context; | ||
} | ||
|
||
/** | ||
|
@@ -416,14 +369,14 @@ private SQLiteDatabase openDatabase(String dbname, String assetFilePath, int ope | |
if (assetFilePath != null && assetFilePath.length() > 0) { | ||
if (assetFilePath.compareTo("1") == 0) { | ||
assetFilePath = "www/" + dbname; | ||
in = this.getActivity().getAssets().open(assetFilePath); | ||
in = this.getContext().getAssets().open(assetFilePath); | ||
Log.v("info", "Located pre-populated DB asset in app bundle www subdirectory: " + assetFilePath); | ||
} else if (assetFilePath.charAt(0) == '~') { | ||
assetFilePath = assetFilePath.startsWith("~/") ? assetFilePath.substring(2) : assetFilePath.substring(1); | ||
in = this.getActivity().getAssets().open(assetFilePath); | ||
in = this.getContext().getAssets().open(assetFilePath); | ||
Log.v("info", "Located pre-populated DB asset in app bundle subdirectory: " + assetFilePath); | ||
} else { | ||
File filesDir = this.getActivity().getFilesDir(); | ||
File filesDir = this.getContext().getFilesDir(); | ||
assetFilePath = assetFilePath.startsWith("/") ? assetFilePath.substring(1) : assetFilePath; | ||
File assetFile = new File(filesDir, assetFilePath); | ||
in = new FileInputStream(assetFile); | ||
|
@@ -437,7 +390,7 @@ private SQLiteDatabase openDatabase(String dbname, String assetFilePath, int ope | |
|
||
if (dbfile == null) { | ||
openFlags = SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY; | ||
dbfile = this.getActivity().getDatabasePath(dbname); | ||
dbfile = this.getContext().getDatabasePath(dbname); | ||
|
||
if (!dbfile.exists() && in != null) { | ||
Log.v("info", "Copying pre-populated db asset to destination"); | ||
|
@@ -479,8 +432,7 @@ private SQLiteDatabase openDatabase(String dbname, String assetFilePath, int ope | |
* @param dbfile The File of the destination db | ||
* @param assetFileInputStream input file stream for pre-populated db asset | ||
*/ | ||
private void createFromAssets(String dbName, File dbfile, InputStream assetFileInputStream) | ||
{ | ||
private void createFromAssets(String dbName, File dbfile, InputStream assetFileInputStream) { | ||
OutputStream out = null; | ||
|
||
try { | ||
|
@@ -587,7 +539,7 @@ private void deleteDatabase(String dbname, CallbackContext cbc) { | |
*/ | ||
@SuppressLint("NewApi") | ||
private boolean deleteDatabaseNow(String dbname) { | ||
File dbfile = this.getActivity().getDatabasePath(dbname); | ||
File dbfile = this.getContext().getDatabasePath(dbname); | ||
if (android.os.Build.VERSION.SDK_INT >= 11) { | ||
// Use try & catch just in case android.os.Build.VERSION.SDK_INT >= 16 was lying: | ||
try { | ||
|
@@ -604,7 +556,7 @@ private boolean deleteDatabaseNow(String dbname) { | |
|
||
private boolean deleteDatabasePreHoneycomb(File dbfile) { | ||
try { | ||
return this.getActivity().deleteDatabase(dbfile.getAbsolutePath()); | ||
return this.getContext().deleteDatabase(dbfile.getAbsolutePath()); | ||
} catch (Exception e) { | ||
Log.e(SQLitePlugin.class.getSimpleName(), "couldn't delete database", e); | ||
return false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned, this create unnecessary backward incompatibility. Instead of removing original constructor, I think it will be more appropriate to add a new constructor taking only reactContext. This way both modes of creation are supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no incompatibility as everyone would use Package, there is no need to create Module directly, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, since we have these two constructors at the Package level it should be all right.