From 61758009f62c67f9556165c2372c13cafca41f12 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 4 Feb 2021 15:05:20 +0200 Subject: [PATCH] Fix possible npe with favourites db --- OsmAnd/src/net/osmand/plus/api/SQLiteAPI.java | 13 +++++---- .../net/osmand/plus/api/SQLiteAPIImpl.java | 28 ++++++++++--------- .../net/osmand/plus/poi/PoiFiltersHelper.java | 16 ++++++----- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/api/SQLiteAPI.java b/OsmAnd/src/net/osmand/plus/api/SQLiteAPI.java index 98aa5cfd1b..a16067628d 100644 --- a/OsmAnd/src/net/osmand/plus/api/SQLiteAPI.java +++ b/OsmAnd/src/net/osmand/plus/api/SQLiteAPI.java @@ -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); } diff --git a/OsmAnd/src/net/osmand/plus/api/SQLiteAPIImpl.java b/OsmAnd/src/net/osmand/plus/api/SQLiteAPIImpl.java index 7199388596..34c13259fe 100644 --- a/OsmAnd/src/net/osmand/plus/api/SQLiteAPIImpl.java +++ b/OsmAnd/src/net/osmand/plus/api/SQLiteAPIImpl.java @@ -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); } } diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index 2e9e11c5c3..e41391f369 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -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;