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 {
@ -80,8 +79,10 @@ public interface SQLiteAPI {
void bindBlob(int i, byte[] val); void bindBlob(int i, byte[] val);
} }
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,17 +663,19 @@ 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);
} }
int version = conn.getVersion(); if (conn != null) {
conn.setVersion(DATABASE_VERSION); int version = conn.getVersion();
if (version == 0) { conn.setVersion(DATABASE_VERSION);
onCreate(conn); if (version == 0) {
} else { onCreate(conn);
onUpgrade(conn, version, DATABASE_VERSION); } else {
onUpgrade(conn, version, DATABASE_VERSION);
}
} }
} }
return conn; return conn;