Merge pull request #1047 from Bars107/master

Added database. Updated search poi activity
This commit is contained in:
Denis 2015-01-24 19:16:41 +02:00
commit fa4c723f9a
7 changed files with 250 additions and 189 deletions

View file

@ -32,6 +32,13 @@
android:id="@android:id/list"
style="@style/OsmandListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
android:layout_height="0dp"
android:layout_weight="1"/>
<net.osmand.plus.activities.search.toolbar.SplitToolbar
android:orientation="horizontal"
android:id="@+id/bottomControls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

View file

@ -54,7 +54,7 @@ public class ContributionVersionActivity extends OsmandListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pathToDownload = getMyApplication().getAppPath("osmandToInstall.apk");
setContentView(android.R.layout.list_content);
setContentView(R.layout.default_list_view);
getSupportActionBar().setSubtitle(R.string.select_build_to_install);
String installDate = getMyApplication().getSettings().CONTRIBUTION_INSTALL_APP_DATE.get();

View file

@ -4,6 +4,7 @@
package net.osmand.plus.activities.search;
import android.support.v7.widget.Toolbar;
import gnu.trove.set.hash.TLongHashSet;
import java.util.ArrayList;
@ -131,10 +132,11 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu = getClearToolbar(true).getMenu();
boolean light = getMyApplication().getSettings().isLightActionBar();
searchPOILevel = menu.add(0, SEARCH_MORE, 0, R.string.search_POI_level_btn);
MenuItemCompat.setShowAsAction(searchPOILevel,
MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
searchPOILevel.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
@ -147,7 +149,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
showFilterItem = menu.add(0, FILTER, 0, R.string.search_poi_filter);
MenuItemCompat.setShowAsAction(showFilterItem,
MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
showFilterItem = showFilterItem.setIcon(light ? R.drawable.ic_action_filter_light: R.drawable.ic_action_filter_dark);
showFilterItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
@ -179,7 +181,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
showOnMapItem = menu.add(0, SHOW_ON_MAP, 0, R.string.search_shown_on_map);
MenuItemCompat.setShowAsAction(showOnMapItem,
MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
showOnMapItem = showOnMapItem.setIcon(light ? R.drawable.ic_action_map_marker_light : R.drawable.ic_action_map_marker_dark);
showOnMapItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
@ -199,6 +201,14 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
});
return true;
}
public Toolbar getClearToolbar(boolean visible) {
final Toolbar tb = (Toolbar) findViewById(R.id.bottomControls);
tb.setTitle(null);
tb.getMenu().clear();
tb.setVisibility(visible? View.VISIBLE : View.GONE);
return tb;
}
private boolean searchMore() {
String query = searchFilter.getText().toString().trim();

View file

@ -6,7 +6,6 @@ import java.util.Comparator;
import java.util.List;
import android.graphics.Typeface;
import android.media.Image;
import android.widget.*;
import net.osmand.plus.R;
import net.osmand.plus.base.BasicProgressAsyncTask;
@ -20,7 +19,7 @@ import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import net.osmand.plus.helpers.DownloadFrequencyHelper;
import net.osmand.plus.helpers.DatabaseHelper;
import net.osmand.plus.helpers.FontCache;
/**
@ -79,8 +78,9 @@ public class DashUpdatesFragment extends DashBaseFragment {
Collections.sort(itemList, new Comparator<IndexItem>() {
@Override
public int compare(IndexItem indexItem, IndexItem t1) {
DownloadFrequencyHelper helper = BaseDownloadActivity.downloadListIndexThread.getDbHelper();
return (int)(helper.getCount(t1.getBasename()) - helper.getCount(indexItem.getBasename()));
DatabaseHelper helper = BaseDownloadActivity.downloadListIndexThread.getDbHelper();
return (int)(helper.getCount(t1.getBasename(), DatabaseHelper.DOWNLOAD_ENTRY) -
helper.getCount(indexItem.getBasename(), DatabaseHelper.DOWNLOAD_ENTRY));
}
});
View mainView = getView();

View file

@ -24,7 +24,7 @@ import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning;
import net.osmand.plus.helpers.DownloadFrequencyHelper;
import net.osmand.plus.helpers.DatabaseHelper;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.util.Algorithms;
@ -58,17 +58,17 @@ public class DownloadIndexesThread {
private java.text.DateFormat dateFormat;
private List<IndexItem> itemsToUpdate = new ArrayList<IndexItem>();
DownloadFrequencyHelper dbHelper;
DatabaseHelper dbHelper;
public DownloadIndexesThread(Context ctx) {
this.ctx = ctx;
app = (OsmandApplication) ctx.getApplicationContext();
downloadFileHelper = new DownloadFileHelper(app);
dateFormat = app.getResourceManager().getDateFormat();
dbHelper = new DownloadFrequencyHelper(app);
dbHelper = new DatabaseHelper(app);
}
public DownloadFrequencyHelper getDbHelper(){
public DatabaseHelper getDbHelper(){
return dbHelper;
}
@ -155,12 +155,12 @@ public class DownloadIndexesThread {
uiActivity.updateDownloadButton(false);
DownloadEntry item = (DownloadEntry)o;
String name = item.item.getBasename();
long count = dbHelper.getCount(name) + 1;
DownloadFrequencyHelper.HistoryEntry entry = new DownloadFrequencyHelper.HistoryEntry(name,count);
long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1;
DatabaseHelper.HistoryEntry entry = new DatabaseHelper.HistoryEntry(name,count);
if (count == 1) {
dbHelper.add(entry);
dbHelper.add(entry, DatabaseHelper.DOWNLOAD_ENTRY);
} else {
dbHelper.update(entry);
dbHelper.update(entry, DatabaseHelper.DOWNLOAD_ENTRY);
}
}
} else if (o instanceof IndexItem) {
@ -170,8 +170,8 @@ public class DownloadIndexesThread {
uiActivity.updateDownloadButton(false);
IndexItem item = (IndexItem)o;
long count = dbHelper.getCount(item.getBasename()) + 1;
dbHelper.add(new DownloadFrequencyHelper.HistoryEntry(item.getBasename(), count));
long count = dbHelper.getCount(item.getBasename(), DatabaseHelper.DOWNLOAD_ENTRY) + 1;
dbHelper.add(new DatabaseHelper.HistoryEntry(item.getBasename(), count), DatabaseHelper.DOWNLOAD_ENTRY);
}
} else if (o instanceof String) {
String message = (String) o;

View file

@ -0,0 +1,214 @@
package net.osmand.plus.helpers;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.api.SQLiteAPI;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Denis
* on 23.01.15.
*/
public class DatabaseHelper {
public static final int DOWNLOAD_ENTRY = 0;
public static final int FAVORITES_ENTRY = 1;
private static final String DB_NAME = "download_history"; //$NON-NLS-1$
private static final int DB_VERSION = 1;
private static final String DOWNLOADS_TABLE_NAME = "downloads"; //$NON-NLS-1$
private static final String FAVORITES_TABLE_NAME = "favorites"; //$NON-NLS-1$
private static final String HISTORY_COL_NAME = "name"; //$NON-NLS-1$
private static final String HISTORY_COL_COUNT = "count"; //$NON-NLS-1$
private static final String DOWNLOAD_TABLE_CREATE = "CREATE TABLE " + DOWNLOADS_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
HISTORY_COL_NAME + " TEXT, " + HISTORY_COL_COUNT + " long);"; //$NON-NLS-1$ //$NON-NLS-2$
private static final String FAVORITES_TABLE_CREATE = "CREATE TABLE " + FAVORITES_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
HISTORY_COL_NAME + " TEXT, " + HISTORY_COL_COUNT + " long);"; //$NON-NLS-1$ //$NON-NLS-2$
private OsmandApplication app;
public static class HistoryEntry {
long count;
String name;
public HistoryEntry(String name, long count){
this.count = count;
this.name = name;
}
public String getName() {
return name;
}
public long getCount() {
return count;
}
}
public DatabaseHelper(OsmandApplication context) {
app = context;
}
private SQLiteAPI.SQLiteConnection openConnection(boolean readonly) {
SQLiteAPI.SQLiteConnection conn = app.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly);
if (conn.getVersion() == 0 || DB_VERSION != conn.getVersion()) {
if (readonly) {
conn.close();
conn = app.getSQLiteAPI().getOrCreateDatabase(DB_NAME, true);
}
if (conn.getVersion() == 0) {
conn.setVersion(DB_VERSION);
onCreate(conn);
} else {
onUpgrade(conn, conn.getVersion(), DB_VERSION);
}
}
return conn;
}
public void onCreate(SQLiteAPI.SQLiteConnection db) {
db.execSQL(DOWNLOAD_TABLE_CREATE);
db.execSQL(FAVORITES_TABLE_CREATE);
}
public void onUpgrade(SQLiteAPI.SQLiteConnection db, int oldVersion, int newVersion) {
}
public boolean remove(HistoryEntry e, int type){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
switch (type){
case DOWNLOAD_ENTRY:
db.execSQL("DELETE FROM " + DOWNLOADS_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + " = ?", new Object[] { e.getName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
case FAVORITES_ENTRY:
db.execSQL("DELETE FROM " + FAVORITES_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + " = ?", new Object[] { e.getName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
} finally {
db.close();
}
return true;
}
return false;
}
public boolean removeAll(){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
db.execSQL("DELETE FROM " + DOWNLOADS_TABLE_NAME); //$NON-NLS-1$
} finally {
db.close();
}
return true;
}
return false;
}
public boolean update(HistoryEntry e, int type){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
switch (type) {
case DOWNLOAD_ENTRY:
db.execSQL("UPDATE " + DOWNLOADS_TABLE_NAME + " SET " + HISTORY_COL_COUNT + " = ? WHERE " + HISTORY_COL_NAME + " = ?", new Object[] { e.getCount(), e.getName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
case FAVORITES_ENTRY:
db.execSQL("UPDATE " + FAVORITES_TABLE_NAME + " SET " + HISTORY_COL_COUNT + " = ? WHERE " + HISTORY_COL_NAME + " = ?", new Object[] { e.getCount(), e.getName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
} finally {
db.close();
}
return true;
}
return false;
}
public boolean add(HistoryEntry e, int type){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
switch (type) {
case DOWNLOAD_ENTRY:
db.execSQL("INSERT INTO " + DOWNLOADS_TABLE_NAME + " VALUES (?, ?)", new Object[] { e.getName(), e.getCount()}); //$NON-NLS-1$ //$NON-NLS-2$
case FAVORITES_ENTRY:
db.execSQL("INSERT INTO " + FAVORITES_TABLE_NAME + " VALUES (?, ?)", new Object[] { e.getName(), e.getCount()}); //$NON-NLS-1$ //$NON-NLS-2$
}
} finally {
db.close();
}
return true;
}
return false;
}
public long getCount(String name, int type) {
SQLiteAPI.SQLiteConnection db = openConnection(true);
long count = 0;
if(db != null){
try {
SQLiteAPI.SQLiteCursor query;
switch (type) {
case DOWNLOAD_ENTRY:
query = db.rawQuery(
"SELECT " + HISTORY_COL_COUNT + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
DOWNLOADS_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + "='" + name + "'", null); //$NON-NLS-1$//$NON-NLS-2$
case FAVORITES_ENTRY:
query = db.rawQuery(
"SELECT " + HISTORY_COL_COUNT + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
FAVORITES_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + "='" + name + "'", null); //$NON-NLS-1$//$NON-NLS-2$
default:
query = db.rawQuery("not supported", null); //$NON-NLS-1$//$NON-NLS-2$
}
if (query.moveToFirst()) {
do {
count = query.getInt(0);
} while (query.moveToNext());
}
query.close();
} finally {
db.close();
}
}
return count;
}
public List<HistoryEntry> getEntries(int type){
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
SQLiteAPI.SQLiteConnection db = openConnection(true);
if(db != null){
try {
SQLiteAPI.SQLiteCursor query;
switch (type) {
case DOWNLOAD_ENTRY:
query = db.rawQuery(
"SELECT " + HISTORY_COL_NAME + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
DOWNLOADS_TABLE_NAME + " ORDER BY " + HISTORY_COL_COUNT + " DESC", null); //$NON-NLS-1$//$NON-NLS-2$
case FAVORITES_ENTRY:
query = db.rawQuery(
"SELECT " + HISTORY_COL_NAME + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
FAVORITES_TABLE_NAME + " ORDER BY " + HISTORY_COL_COUNT + " DESC", null); //$NON-NLS-1$//$NON-NLS-2$
default:
query = db.rawQuery("not supported", null); //$NON-NLS-1$//$NON-NLS-2$
}
if (query.moveToFirst()) {
do {
HistoryEntry e = new HistoryEntry(query.getString(0), query.getInt(1));
entries.add(e);
} while (query.moveToNext());
}
query.close();
} finally {
db.close();
}
}
return entries;
}
}

View file

@ -1,170 +0,0 @@
package net.osmand.plus.helpers;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.api.SQLiteAPI;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Denis
* on 23.01.15.
*/
public class DownloadFrequencyHelper {
private static final String DB_NAME = "download_history"; //$NON-NLS-1$
private static final int DB_VERSION = 1;
private static final String HISTORY_TABLE_NAME = "history"; //$NON-NLS-1$
private static final String HISTORY_COL_NAME = "name"; //$NON-NLS-1$
private static final String HISTORY_COL_COUNT = "count"; //$NON-NLS-1$
private static final String HISTORY_TABLE_CREATE = "CREATE TABLE " + HISTORY_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
HISTORY_COL_NAME + " TEXT, " + HISTORY_COL_COUNT + " long);"; //$NON-NLS-1$ //$NON-NLS-2$
private OsmandApplication app;
public static class HistoryEntry {
long count;
String name;
public HistoryEntry(String name, long count){
this.count = count;
this.name = name;
}
public String getName() {
return name;
}
public long getCount() {
return count;
}
}
public DownloadFrequencyHelper(OsmandApplication context) {
app = context;
}
private SQLiteAPI.SQLiteConnection openConnection(boolean readonly) {
SQLiteAPI.SQLiteConnection conn = app.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly);
if (conn.getVersion() == 0 || DB_VERSION != conn.getVersion()) {
if (readonly) {
conn.close();
conn = app.getSQLiteAPI().getOrCreateDatabase(DB_NAME, true);
}
if (conn.getVersion() == 0) {
conn.setVersion(DB_VERSION);
onCreate(conn);
} else {
onUpgrade(conn, conn.getVersion(), DB_VERSION);
}
}
return conn;
}
public void onCreate(SQLiteAPI.SQLiteConnection db) {
db.execSQL(HISTORY_TABLE_CREATE);
}
public void onUpgrade(SQLiteAPI.SQLiteConnection db, int oldVersion, int newVersion) {
}
public boolean remove(HistoryEntry e){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + " = ?", new Object[] { e.getName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} finally {
db.close();
}
return true;
}
return false;
}
public boolean removeAll(){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); //$NON-NLS-1$
} finally {
db.close();
}
return true;
}
return false;
}
public boolean update(HistoryEntry e){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
db.execSQL(
"UPDATE " + HISTORY_TABLE_NAME + " SET " + HISTORY_COL_COUNT + " = ? WHERE " + HISTORY_COL_NAME + " = ?", new Object[] { e.getCount(), e.getName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} finally {
db.close();
}
return true;
}
return false;
}
public boolean add(HistoryEntry e){
SQLiteAPI.SQLiteConnection db = openConnection(false);
if(db != null){
try {
db.execSQL(
"INSERT INTO " + HISTORY_TABLE_NAME + " VALUES (?, ?)", new Object[] { e.getName(), e.getCount()}); //$NON-NLS-1$ //$NON-NLS-2$
} finally {
db.close();
}
return true;
}
return false;
}
public long getCount(String name) {
SQLiteAPI.SQLiteConnection db = openConnection(true);
long count = 0;
if(db != null){
try {
SQLiteAPI.SQLiteCursor query = db.rawQuery(
"SELECT " + HISTORY_COL_COUNT + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
HISTORY_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + "='" + name + "'", null); //$NON-NLS-1$//$NON-NLS-2$
if (query.moveToFirst()) {
do {
count = query.getInt(0);
} while (query.moveToNext());
}
query.close();
} finally {
db.close();
}
}
return count;
}
public List<HistoryEntry> getEntries(){
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
SQLiteAPI.SQLiteConnection db = openConnection(true);
if(db != null){
try {
SQLiteAPI.SQLiteCursor query = db.rawQuery(
"SELECT " + HISTORY_COL_NAME + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
HISTORY_TABLE_NAME + " ORDER BY " + HISTORY_COL_COUNT + " DESC", null); //$NON-NLS-1$//$NON-NLS-2$
if (query.moveToFirst()) {
do {
HistoryEntry e = new HistoryEntry(query.getString(0), query.getInt(1));
entries.add(e);
} while (query.moveToNext());
}
query.close();
} finally {
db.close();
}
}
return entries;
}
}