From ae700b3eaec8d6be8780aed78d5e0d5cbbf59da1 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Mon, 26 Mar 2018 19:01:31 +0300 Subject: [PATCH] Add database helper for wikivoyage --- .../src/net/osmand/IndexConstants.java | 3 +- .../src/net/osmand/plus/AppInitializer.java | 2 + .../net/osmand/plus/OsmandApplication.java | 6 + .../wikivoyage/data/WikivoyageDbHelper.java | 123 ++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java diff --git a/OsmAnd-java/src/net/osmand/IndexConstants.java b/OsmAnd-java/src/net/osmand/IndexConstants.java index 5d41d0a9c7..508f98d4b3 100644 --- a/OsmAnd-java/src/net/osmand/IndexConstants.java +++ b/OsmAnd-java/src/net/osmand/IndexConstants.java @@ -54,12 +54,13 @@ public class IndexConstants { public static final String SRTM_INDEX_DIR = "srtm/"; //$NON-NLS-1$ public static final String ROADS_INDEX_DIR = "roads/"; //$NON-NLS-1$ public static final String WIKI_INDEX_DIR = "wiki/"; //$NON-NLS-1$ + public static final String WIKIVOYAGE_INDEX_DIR = "wikivoyage/"; public static final String AV_INDEX_DIR = "avnotes/"; //$NON-NLS-1$ public static final String FONT_INDEX_DIR = "fonts/"; //$NON-NLS-1$ public static final String VOICE_INDEX_DIR = "voice/"; //$NON-NLS-1$ public static final String RENDERERS_DIR = "rendering/"; //$NON-NLS-1$ public static final String ROUTING_XML_FILE= "routing.xml"; - + public static final String SQLITE_EXT = ".sqlitedb"; //$NON-NLS-1$ public static final String TEMP_SOURCE_TO_LOAD = "temp"; diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 738ab9c61e..56f92ede64 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -47,6 +47,7 @@ import net.osmand.plus.voice.CommandPlayer; import net.osmand.plus.voice.CommandPlayerException; import net.osmand.plus.voice.MediaCommandPlayerImpl; import net.osmand.plus.voice.TTSCommandPlayerImpl; +import net.osmand.plus.wikivoyage.data.WikivoyageDbHelper; import net.osmand.render.RenderingRulesStorage; import net.osmand.router.RoutingConfiguration; import net.osmand.util.Algorithms; @@ -445,6 +446,7 @@ public class AppInitializer implements IProgress { app.mapMarkersDbHelper = startupInit(new MapMarkersDbHelper(app), MapMarkersDbHelper.class); app.mapMarkersHelper = startupInit(new MapMarkersHelper(app), MapMarkersHelper.class); app.searchUICore = startupInit(new QuickSearchHelper(app), QuickSearchHelper.class); + app.wikivoyageDbHelper = startupInit(new WikivoyageDbHelper(app), WikivoyageDbHelper.class); initOpeningHoursParser(); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index cdd793c103..dd284ecb20 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -55,6 +55,7 @@ import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.search.QuickSearchHelper; import net.osmand.plus.voice.CommandPlayer; +import net.osmand.plus.wikivoyage.data.WikivoyageDbHelper; import net.osmand.router.RoutingConfiguration; import net.osmand.search.SearchUICore; import net.osmand.util.Algorithms; @@ -117,6 +118,7 @@ public class OsmandApplication extends MultiDexApplication { OsmandRegions regions; GeocodingLookupService geocodingLookupService; QuickSearchHelper searchUICore; + WikivoyageDbHelper wikivoyageDbHelper; RoutingConfiguration.Builder defaultRoutingConfig; private Locale preferredLocale = null; @@ -390,6 +392,10 @@ public class OsmandApplication extends MultiDexApplication { return searchUICore; } + public WikivoyageDbHelper getWikivoyageDbHelper() { + return wikivoyageDbHelper; + } + public CommandPlayer getPlayer() { return player; } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java new file mode 100644 index 0000000000..165a9d1555 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageDbHelper.java @@ -0,0 +1,123 @@ +package net.osmand.plus.wikivoyage.data; + +import android.support.annotation.Nullable; + +import net.osmand.IndexConstants; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; +import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; + +import java.util.ArrayList; +import java.util.List; + +public class WikivoyageDbHelper { + + private static final String DB_NAME = "wikivoyage.sqlite"; + + private static final String ARTICLES_TABLE_NAME = "wikivoyage_articles"; + private static final String ARTICLES_COL_ID = "article_id"; + private static final String ARTICLES_COL_TITLE = "title"; + private static final String ARTICLES_COL_CONTENT = "content_gzblob"; + private static final String ARTICLES_COL_IS_PART_OF = "is_part_of"; + private static final String ARTICLES_COL_LAT = "lat"; + private static final String ARTICLES_COL_LON = "lon"; + private static final String ARTICLES_COL_IMAGE_TITLE = "image_title"; + private static final String ARTICLES_COL_GPX_GZ = "gpx_gz"; + private static final String ARTICLES_COL_CITY_ID = "city_id"; + private static final String ARTICLES_COL_ORIGINAL_ID = "original_id"; + private static final String ARTICLES_COL_LANG = "lang"; + + private static final String ARTICLES_TABLE_SELECT = "SELECT " + + ARTICLES_COL_ID + ", " + + ARTICLES_COL_TITLE + ", " + + ARTICLES_COL_CONTENT + ", " + + ARTICLES_COL_IS_PART_OF + ", " + + ARTICLES_COL_LAT + ", " + + ARTICLES_COL_LON + ", " + + ARTICLES_COL_IMAGE_TITLE + ", " + + ARTICLES_COL_GPX_GZ + ", " + + ARTICLES_COL_CITY_ID + ", " + + ARTICLES_COL_ORIGINAL_ID + ", " + + ARTICLES_COL_LANG + + " FROM " + ARTICLES_TABLE_NAME; + + private static final String SEARCH_TABLE_NAME = "wikivoyage_search"; + private static final String SEARCH_COL_SEARCH_TERM = "search_term"; + private static final String SEARCH_COL_CITY_ID = "city_id"; + private static final String SEARCH_COL_ARTICLE_TITLE = "article_title"; + private static final String SEARCH_COL_LANG = "lang"; + + private static final String SEARCH_TABLE_SELECT = "SELECT " + + SEARCH_COL_SEARCH_TERM + ", " + + SEARCH_COL_CITY_ID + ", " + + SEARCH_COL_ARTICLE_TITLE + ", " + + SEARCH_COL_LANG + + " FROM " + SEARCH_TABLE_NAME; + + private final OsmandApplication application; + + public WikivoyageDbHelper(OsmandApplication application) { + this.application = application; + } + + public List search(String searchQuery) { + List res = new ArrayList<>(); + SQLiteConnection conn = openConnection(); + if (conn != null) { + try { + String dbQuery = SEARCH_TABLE_SELECT + " WHERE " + SEARCH_COL_SEARCH_TERM + " LIKE " + "'%" + searchQuery + "%'"; + SQLiteCursor cursor = conn.rawQuery(dbQuery, null); + if (cursor.moveToFirst()) { + do { + res.add(readSearchResult(cursor)); + } while (cursor.moveToNext()); + } + cursor.close(); + } finally { + conn.close(); + } + } + return res; + } + + @Nullable + private SQLiteConnection openConnection() { + String path = application.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR + DB_NAME).getAbsolutePath(); + return application.getSQLiteAPI().openByAbsolutePath(path, true); + } + + private SearchResult readSearchResult(SQLiteCursor query) { + SearchResult res = new SearchResult(); + + res.searchTerm = query.getString(0); + res.cityId = query.getLong(1); + res.articleTitle = query.getString(2); + res.lang = query.getString(3); + + return res; + } + + public static class SearchResult { + + private String searchTerm; + private long cityId; + private String articleTitle; + private String lang; + + public String getSearchTerm() { + return searchTerm; + } + + public long getCityId() { + return cityId; + } + + public String getArticleTitle() { + return articleTitle; + } + + public String getLang() { + return lang; + } + } +}