Fix possible npe with favourites db

This commit is contained in:
Vitaliy 2021-02-04 15:05:20 +02:00
parent 4d518acf23
commit 61758009f6
3 changed files with 31 additions and 26 deletions

View file

@ -1,6 +1,7 @@
package net.osmand.plus.api;
import androidx.annotation.Nullable;
public interface SQLiteAPI {
@ -55,8 +56,6 @@ public interface SQLiteAPI {
void close();
}
public interface SQLiteStatement {
@ -80,8 +79,10 @@ public interface SQLiteAPI {
void bindBlob(int i, byte[] val);
}
public SQLiteConnection getOrCreateDatabase(String name, boolean readOnly);
public SQLiteConnection openByAbsolutePath(String path, boolean readOnly);
@Nullable
SQLiteConnection getOrCreateDatabase(String name, boolean readOnly);
@Nullable
SQLiteConnection openByAbsolutePath(String path, boolean readOnly);
}

View file

@ -1,15 +1,17 @@
package net.osmand.plus.api;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import org.apache.commons.logging.Log;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.Nullable;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import org.apache.commons.logging.Log;
public class SQLiteAPIImpl implements SQLiteAPI {
private OsmandApplication app;
@ -20,6 +22,7 @@ public class SQLiteAPIImpl implements SQLiteAPI {
}
@SuppressLint("InlinedApi")
@Nullable
@Override
public SQLiteConnection getOrCreateDatabase(String name, boolean readOnly) {
android.database.sqlite.SQLiteDatabase db = null;
@ -29,13 +32,12 @@ public class SQLiteAPIImpl implements SQLiteAPI {
} catch (RuntimeException e) {
LOG.error(e.getMessage(), e);
}
if(db == null) {
if (db == null) {
return null;
}
return new SQLiteDatabaseWrapper(db) ;
return new SQLiteDatabaseWrapper(db);
}
public class SQLiteDatabaseWrapper implements SQLiteConnection {
android.database.sqlite.SQLiteDatabase ds;
@ -206,15 +208,15 @@ public class SQLiteAPIImpl implements SQLiteAPI {
}
@Nullable
@Override
public SQLiteConnection openByAbsolutePath(String path, boolean readOnly) {
// fix http://stackoverflow.com/questions/26937152/workaround-for-nexus-9-sqlite-file-write-operations-on-external-dirs
android.database.sqlite.SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
readOnly? SQLiteDatabase.OPEN_READONLY : (SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING));
if(db == null) {
readOnly ? SQLiteDatabase.OPEN_READONLY : (SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING));
if (db == null) {
return null;
}
return new SQLiteDatabaseWrapper(db) ;
return new SQLiteDatabaseWrapper(db);
}
}

View file

@ -663,17 +663,19 @@ public class PoiFiltersHelper {
private SQLiteConnection openConnection(boolean readonly) {
conn = context.getSQLiteAPI().getOrCreateDatabase(DATABASE_NAME, readonly);
if (conn.getVersion() < DATABASE_VERSION) {
if (conn != null && conn.getVersion() < DATABASE_VERSION) {
if (readonly) {
conn.close();
conn = context.getSQLiteAPI().getOrCreateDatabase(DATABASE_NAME, false);
}
int version = conn.getVersion();
conn.setVersion(DATABASE_VERSION);
if (version == 0) {
onCreate(conn);
} else {
onUpgrade(conn, version, DATABASE_VERSION);
if (conn != null) {
int version = conn.getVersion();
conn.setVersion(DATABASE_VERSION);
if (version == 0) {
onCreate(conn);
} else {
onUpgrade(conn, version, DATABASE_VERSION);
}
}
}
return conn;