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; package net.osmand.plus.api;
import androidx.annotation.Nullable;
public interface SQLiteAPI { public interface SQLiteAPI {
@ -55,8 +56,6 @@ public interface SQLiteAPI {
void close(); void close();
} }
public interface SQLiteStatement { public interface SQLiteStatement {
@ -81,7 +80,9 @@ public interface SQLiteAPI {
} }
public SQLiteConnection getOrCreateDatabase(String name, boolean readOnly); @Nullable
SQLiteConnection getOrCreateDatabase(String name, boolean readOnly);
public SQLiteConnection openByAbsolutePath(String path, boolean readOnly); @Nullable
SQLiteConnection openByAbsolutePath(String path, boolean readOnly);
} }

View file

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

View file

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