Add database helper for wikivoyage

This commit is contained in:
Alexander Sytnyk 2018-03-26 19:01:31 +03:00
parent 2ebca53e25
commit ae700b3eae
4 changed files with 133 additions and 1 deletions

View file

@ -54,6 +54,7 @@ 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$

View file

@ -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();
}

View file

@ -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;
}

View file

@ -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<SearchResult> search(String searchQuery) {
List<SearchResult> 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;
}
}
}