Fix formatting in SearchHistoryHelper

This commit is contained in:
Alex Sytnyk 2018-09-19 14:23:33 +03:00
parent 2fe7debf05
commit 7c6d09f5f2

View file

@ -16,40 +16,41 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class SearchHistoryHelper { public class SearchHistoryHelper {
private static final int HISTORY_LIMIT = 1500; private static final int HISTORY_LIMIT = 1500;
private OsmandApplication context; private OsmandApplication context;
private List<HistoryEntry> loadedEntries = null; private List<HistoryEntry> loadedEntries = null;
private Map<PointDescription, HistoryEntry> mp = new HashMap<PointDescription, SearchHistoryHelper.HistoryEntry>(); private Map<PointDescription, HistoryEntry> mp = new HashMap<>();
public SearchHistoryHelper(OsmandApplication context) { public SearchHistoryHelper(OsmandApplication context) {
this.context = context; this.context = context;
} }
private static SearchHistoryHelper instance = null; private static SearchHistoryHelper instance = null;
public static SearchHistoryHelper getInstance(OsmandApplication context){
if(instance == null) { public static SearchHistoryHelper getInstance(OsmandApplication context) {
if (instance == null) {
instance = new SearchHistoryHelper(context); instance = new SearchHistoryHelper(context);
} }
return instance; return instance;
} }
private static final int[] DEF_INTERVALS_MIN = new int[]{
private static final int[] DEF_INTERVALS_MIN = new int[] { 5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24
5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24
}; };
private static class HistoryEntryComparator implements Comparator<HistoryEntry> { private static class HistoryEntryComparator implements Comparator<HistoryEntry> {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
@Override @Override
public int compare(HistoryEntry lhs, HistoryEntry rhs) { public int compare(HistoryEntry lhs, HistoryEntry rhs) {
double l = lhs.getRank(time); double l = lhs.getRank(time);
double r = rhs.getRank(time); double r = rhs.getRank(time);
return -Double.compare(l, r); return -Double.compare(l, r);
} }
}; }
public static class HistoryEntry { public static class HistoryEntry {
double lat; double lat;
double lon; double lon;
@ -57,15 +58,15 @@ public class SearchHistoryHelper {
private long lastAccessedTime; private long lastAccessedTime;
private int[] intervals = new int[0]; private int[] intervals = new int[0];
private double[] intervalValues = new double[0]; private double[] intervalValues = new double[0];
public HistoryEntry(double lat, double lon, PointDescription name){ HistoryEntry(double lat, double lon, PointDescription name) {
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
this.name = name; this.name = name;
} }
private double rankFunction(double cf, double timeDiff) { private double rankFunction(double cf, double timeDiff) {
if(timeDiff <= 0) { if (timeDiff <= 0) {
return 0; return 0;
} }
return cf / timeDiff; return cf / timeDiff;
@ -79,7 +80,7 @@ public class SearchHistoryHelper {
for (int k = 0; k < intervals.length; k++) { for (int k = 0; k < intervals.length; k++) {
double ntimeDiff = intervals[k] * 60 * 1000; double ntimeDiff = intervals[k] * 60 * 1000;
double nvl = intervalValues[k]; double nvl = intervalValues[k];
if(ntimeDiff < timeDiff || nvl <= vl){ if (ntimeDiff < timeDiff || nvl <= vl) {
continue; continue;
} }
rnk += rankFunction(nvl - vl, baseTimeDiff + (ntimeDiff - timeDiff) / 2 + timeDiff); rnk += rankFunction(nvl - vl, baseTimeDiff + (ntimeDiff - timeDiff) / 2 + timeDiff);
@ -92,22 +93,23 @@ public class SearchHistoryHelper {
public PointDescription getName() { public PointDescription getName() {
return name; return name;
} }
public String getSerializedName() { public String getSerializedName() {
return PointDescription.serializeToString(name); return PointDescription.serializeToString(name);
} }
public double getLat() { public double getLat() {
return lat; return lat;
} }
public double getLon() { public double getLon() {
return lon; return lon;
} }
public void markAsAccessed(long time) { public void markAsAccessed(long time) {
int[] nintervals = new int[DEF_INTERVALS_MIN.length]; int[] nintervals = new int[DEF_INTERVALS_MIN.length];
double[] nintervalValues = new double[DEF_INTERVALS_MIN.length]; double[] nintervalValues = new double[DEF_INTERVALS_MIN.length];
for(int k = 0; k < nintervals.length; k++) { for (int k = 0; k < nintervals.length; k++) {
nintervals[k] = DEF_INTERVALS_MIN[k]; nintervals[k] = DEF_INTERVALS_MIN[k];
nintervalValues[k] = getUsageLastTime(time, 0, 0, nintervals[k]) + 1; nintervalValues[k] = getUsageLastTime(time, 0, 0, nintervals[k]) + 1;
} }
@ -115,8 +117,8 @@ public class SearchHistoryHelper {
intervalValues = nintervalValues; intervalValues = nintervalValues;
this.lastAccessedTime = time; this.lastAccessedTime = time;
} }
public double getUsageLastTime(long time, int days, int hours, int minutes) { double getUsageLastTime(long time, int days, int hours, int minutes) {
long mins = (minutes + (hours + 24 * days) * 60); long mins = (minutes + (hours + 24 * days) * 60);
long timeInPast = time - mins * 60 * 1000; long timeInPast = time - mins * 60 * 1000;
if (this.lastAccessedTime <= timeInPast) { if (this.lastAccessedTime <= timeInPast) {
@ -139,7 +141,7 @@ public class SearchHistoryHelper {
} }
public void setFrequency(String intervalsString, String values) { public void setFrequency(String intervalsString, String values) {
if(Algorithms.isEmpty(intervalsString) || Algorithms.isEmpty(values)) { if (Algorithms.isEmpty(intervalsString) || Algorithms.isEmpty(values)) {
markAsAccessed(this.lastAccessedTime); markAsAccessed(this.lastAccessedTime);
return; return;
} }
@ -148,7 +150,7 @@ public class SearchHistoryHelper {
intervals = new int[ints.length]; intervals = new int[ints.length];
intervalValues = new double[ints.length]; intervalValues = new double[ints.length];
try { try {
for(int i = 0; i < ints.length && i < vsl.length; i++) { for (int i = 0; i < ints.length && i < vsl.length; i++) {
intervals[i] = Integer.parseInt(ints[i]); intervals[i] = Integer.parseInt(ints[i]);
intervalValues[i] = Double.parseDouble(vsl[i]); intervalValues[i] = Double.parseDouble(vsl[i]);
} }
@ -156,26 +158,26 @@ public class SearchHistoryHelper {
e.printStackTrace(); e.printStackTrace();
} }
} }
public long getLastAccessTime() { public long getLastAccessTime() {
return lastAccessedTime; return lastAccessedTime;
} }
public String getIntervalsValues() { public String getIntervalsValues() {
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
for(int i = 0; i < intervalValues.length; i++) { for (int i = 0; i < intervalValues.length; i++) {
if(i > 0) { if (i > 0) {
s.append(","); s.append(",");
} }
s.append(intervalValues[i]); s.append(intervalValues[i]);
} }
return s.toString(); return s.toString();
} }
public String getIntervals() { public String getIntervals() {
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
for(int i = 0; i < intervals.length; i++) { for (int i = 0; i < intervals.length; i++) {
if(i > 0) { if (i > 0) {
s.append(","); s.append(",");
} }
s.append(intervals[i]); s.append(intervals[i]);
@ -188,20 +190,20 @@ public class SearchHistoryHelper {
} }
} }
public List<HistoryEntry> getHistoryEntries() { public List<HistoryEntry> getHistoryEntries() {
if(loadedEntries == null){ if (loadedEntries == null) {
checkLoadedEntries(); checkLoadedEntries();
} }
return new ArrayList<SearchHistoryHelper.HistoryEntry>(loadedEntries); return new ArrayList<>(loadedEntries);
} }
private HistoryItemDBHelper checkLoadedEntries() { private HistoryItemDBHelper checkLoadedEntries() {
HistoryItemDBHelper helper = new HistoryItemDBHelper(); HistoryItemDBHelper helper = new HistoryItemDBHelper();
if (loadedEntries == null) { if (loadedEntries == null) {
loadedEntries = helper.getEntries(); loadedEntries = helper.getEntries();
Collections.sort(loadedEntries, new HistoryEntryComparator()); Collections.sort(loadedEntries, new HistoryEntryComparator());
for(HistoryEntry he : loadedEntries) { for (HistoryEntry he : loadedEntries) {
mp.put(he.getName(), he); mp.put(he.getName(), he);
} }
} }
@ -224,9 +226,9 @@ public class SearchHistoryHelper {
} }
} }
public void addNewItemToHistory(HistoryEntry model) { private void addNewItemToHistory(HistoryEntry model) {
HistoryItemDBHelper helper = checkLoadedEntries(); HistoryItemDBHelper helper = checkLoadedEntries();
if(mp.containsKey(model.getName())) { if (mp.containsKey(model.getName())) {
model = mp.get(model.getName()); model = mp.get(model.getName());
model.markAsAccessed(System.currentTimeMillis()); model.markAsAccessed(System.currentTimeMillis());
helper.update(model); helper.update(model);
@ -237,8 +239,8 @@ public class SearchHistoryHelper {
helper.add(model); helper.add(model);
} }
Collections.sort(loadedEntries, new HistoryEntryComparator()); Collections.sort(loadedEntries, new HistoryEntryComparator());
if(loadedEntries.size() > HISTORY_LIMIT){ if (loadedEntries.size() > HISTORY_LIMIT) {
if(helper.remove(loadedEntries.get(loadedEntries.size() - 1))){ if (helper.remove(loadedEntries.get(loadedEntries.size() - 1))) {
loadedEntries.remove(loadedEntries.size() - 1); loadedEntries.remove(loadedEntries.size() - 1);
} }
} }
@ -247,26 +249,26 @@ public class SearchHistoryHelper {
private class HistoryItemDBHelper { private class HistoryItemDBHelper {
private static final String DB_NAME = "search_history"; //$NON-NLS-1$ private static final String DB_NAME = "search_history";
private static final int DB_VERSION = 2; private static final int DB_VERSION = 2;
private static final String HISTORY_TABLE_NAME = "history_recents"; //$NON-NLS-1$ private static final String HISTORY_TABLE_NAME = "history_recents";
private static final String HISTORY_COL_NAME = "name"; //$NON-NLS-1$ private static final String HISTORY_COL_NAME = "name";
private static final String HISTORY_COL_TIME = "time"; //$NON-NLS-1$ private static final String HISTORY_COL_TIME = "time";
private static final String HISTORY_COL_FREQ_INTERVALS = "freq_intervals"; //$NON-NLS-1$ private static final String HISTORY_COL_FREQ_INTERVALS = "freq_intervals";
private static final String HISTORY_COL_FREQ_VALUES = "freq_values"; //$NON-NLS-1$ private static final String HISTORY_COL_FREQ_VALUES = "freq_values";
private static final String HISTORY_COL_LAT = "latitude"; //$NON-NLS-1$ private static final String HISTORY_COL_LAT = "latitude";
private static final String HISTORY_COL_LON = "longitude"; //$NON-NLS-1$ private static final String HISTORY_COL_LON = "longitude";
private static final String HISTORY_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ private static final String HISTORY_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE_NAME + " (" +
HISTORY_COL_NAME + " TEXT, " + HISTORY_COL_NAME + " TEXT, " +
HISTORY_COL_TIME + " long, " + HISTORY_COL_TIME + " long, " +
HISTORY_COL_FREQ_INTERVALS + " TEXT, " + HISTORY_COL_FREQ_INTERVALS + " TEXT, " +
HISTORY_COL_FREQ_VALUES + " TEXT, " + HISTORY_COL_FREQ_VALUES + " TEXT, " +
HISTORY_COL_LAT + " double, " +HISTORY_COL_LON + " double);"; //$NON-NLS-1$ //$NON-NLS-2$ HISTORY_COL_LAT + " double, " + HISTORY_COL_LON + " double);";
public HistoryItemDBHelper() { HistoryItemDBHelper() {
} }
private SQLiteConnection openConnection(boolean readonly) { private SQLiteConnection openConnection(boolean readonly) {
SQLiteConnection conn = context.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly); SQLiteConnection conn = context.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly);
if (conn.getVersion() == 0 || DB_VERSION != conn.getVersion()) { if (conn.getVersion() == 0 || DB_VERSION != conn.getVersion()) {
@ -280,7 +282,6 @@ public class SearchHistoryHelper {
onUpgrade(conn, conn.getVersion(), DB_VERSION); onUpgrade(conn, conn.getVersion(), DB_VERSION);
} }
conn.setVersion(DB_VERSION); conn.setVersion(DB_VERSION);
} }
return conn; return conn;
} }
@ -290,17 +291,17 @@ public class SearchHistoryHelper {
} }
public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) {
if(newVersion == 2) { if (newVersion == 2) {
db.execSQL(HISTORY_TABLE_CREATE); db.execSQL(HISTORY_TABLE_CREATE);
for(HistoryEntry he : getLegacyEntries(db)) { for (HistoryEntry he : getLegacyEntries(db)) {
insert(he, db); insert(he, db);
} }
} }
} }
public boolean remove(HistoryEntry e){ public boolean remove(HistoryEntry e) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if(db != null){ if (db != null) {
try { try {
removeQuery(e.getSerializedName(), db); removeQuery(e.getSerializedName(), db);
} finally { } finally {
@ -313,14 +314,14 @@ public class SearchHistoryHelper {
private void removeQuery(String name, SQLiteConnection db) { private void removeQuery(String name, SQLiteConnection db) {
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + " = ?", db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + " = ?",
new Object[] { name }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ new Object[]{name});
} }
public boolean removeAll(){ public boolean removeAll() {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if(db != null){ if (db != null) {
try { try {
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); //$NON-NLS-1$ db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME);
} finally { } finally {
db.close(); db.close();
} }
@ -328,18 +329,18 @@ public class SearchHistoryHelper {
} }
return false; return false;
} }
public boolean update(HistoryEntry e){ public boolean update(HistoryEntry e) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if(db != null){ if (db != null) {
try { try {
db.execSQL( db.execSQL(
"UPDATE " + HISTORY_TABLE_NAME + " SET " + HISTORY_COL_TIME + "= ? "+ "UPDATE " + HISTORY_TABLE_NAME + " SET " + HISTORY_COL_TIME + "= ? " +
", " + HISTORY_COL_FREQ_INTERVALS + " = ? " + ", " + HISTORY_COL_FREQ_INTERVALS + " = ? " +
", " +HISTORY_COL_FREQ_VALUES + "= ? WHERE " + ", " + HISTORY_COL_FREQ_VALUES + "= ? WHERE " +
HISTORY_COL_NAME + " = ?", HISTORY_COL_NAME + " = ?",
new Object[] { e.getLastAccessTime(), e.getIntervals(), e.getIntervalsValues(), new Object[]{e.getLastAccessTime(), e.getIntervals(), e.getIntervalsValues(),
e.getSerializedName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ e.getSerializedName()});
} finally { } finally {
db.close(); db.close();
} }
@ -347,10 +348,10 @@ public class SearchHistoryHelper {
} }
return false; return false;
} }
public boolean add(HistoryEntry e){ public boolean add(HistoryEntry e) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if(db != null){ if (db != null) {
try { try {
insert(e, db); insert(e, db);
} finally { } finally {
@ -363,17 +364,17 @@ public class SearchHistoryHelper {
private void insert(HistoryEntry e, SQLiteConnection db) { private void insert(HistoryEntry e, SQLiteConnection db) {
db.execSQL( db.execSQL(
"INSERT INTO " + HISTORY_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?)", "INSERT INTO " + HISTORY_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?)",
new Object[] { e.getSerializedName(), e.getLastAccessTime(), new Object[]{e.getSerializedName(), e.getLastAccessTime(),
e.getIntervals(), e.getIntervalsValues(), e.getLat(), e.getLon() }); //$NON-NLS-1$ //$NON-NLS-2$ e.getIntervals(), e.getIntervalsValues(), e.getLat(), e.getLon()});
} }
public List<HistoryEntry> getLegacyEntries(SQLiteConnection db){ List<HistoryEntry> getLegacyEntries(SQLiteConnection db) {
List<HistoryEntry> entries = new ArrayList<HistoryEntry>(); List<HistoryEntry> entries = new ArrayList<>();
if (db != null) { if (db != null) {
// LEGACY QUERY !! // LEGACY QUERY !!
SQLiteCursor query = db.rawQuery( SQLiteCursor query = db.rawQuery(
"SELECT name, latitude, longitude, time FROM history ORDER BY time DESC", null); //$NON-NLS-1$//$NON-NLS-2$ "SELECT name, latitude, longitude, time FROM history ORDER BY time DESC", null);
if (query != null && query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
String name = query.getString(0); String name = query.getString(0);
@ -401,51 +402,51 @@ public class SearchHistoryHelper {
entries.add(e); entries.add(e);
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) { if (query != null) {
query.close(); query.close();
} }
} }
return entries; return entries;
} }
public List<HistoryEntry> getEntries(){ public List<HistoryEntry> getEntries() {
List<HistoryEntry> entries = new ArrayList<HistoryEntry>(); List<HistoryEntry> entries = new ArrayList<>();
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
if(db != null){ if (db != null) {
try { try {
SQLiteCursor query = db.rawQuery( SQLiteCursor query = db.rawQuery(
"SELECT " + HISTORY_COL_NAME + ", " + HISTORY_COL_LAT + "," + HISTORY_COL_LON +", " + "SELECT " + HISTORY_COL_NAME + ", " + HISTORY_COL_LAT + "," + HISTORY_COL_LON + ", " +
HISTORY_COL_TIME + ", " + HISTORY_COL_FREQ_INTERVALS + ", " + HISTORY_COL_FREQ_VALUES + HISTORY_COL_TIME + ", " + HISTORY_COL_FREQ_INTERVALS + ", " + HISTORY_COL_FREQ_VALUES +
" FROM " + HISTORY_TABLE_NAME , null); //$NON-NLS-1$//$NON-NLS-2$ " FROM " + HISTORY_TABLE_NAME, null);
Map<PointDescription, HistoryEntry> st = new HashMap<PointDescription, HistoryEntry>(); Map<PointDescription, HistoryEntry> st = new HashMap<>();
if (query != null && query.moveToFirst()) { if (query != null && query.moveToFirst()) {
boolean reinsert = false; boolean reinsert = false;
do { do {
String name = query.getString(0); String name = query.getString(0);
PointDescription p = PointDescription.deserializeFromString(name, new LatLon(query.getDouble(1), query.getDouble(2))); PointDescription p = PointDescription.deserializeFromString(name, new LatLon(query.getDouble(1), query.getDouble(2)));
HistoryEntry e = new HistoryEntry(query.getDouble(1), query.getDouble(2), HistoryEntry e = new HistoryEntry(query.getDouble(1), query.getDouble(2),
p); p);
long time = query.getLong(3); long time = query.getLong(3);
e.setLastAccessTime(time); e.setLastAccessTime(time);
e.setFrequency(query.getString(4), query.getString(5)); e.setFrequency(query.getString(4), query.getString(5));
if(st.containsKey(p)) { if (st.containsKey(p)) {
reinsert = true; reinsert = true;
} }
entries.add(e); entries.add(e);
st.put(p, e); st.put(p, e);
} while (query.moveToNext()); } while (query.moveToNext());
if(reinsert) { if (reinsert) {
System.err.println("Reinsert all values for search history"); System.err.println("Reinsert all values for search history");
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); //$NON-NLS-1$ db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME);
entries.clear(); entries.clear();
entries.addAll(st.values()); entries.addAll(st.values());
for(HistoryEntry he : entries) { for (HistoryEntry he : entries) {
insert(he, db); insert(he, db);
} }
} }
} }
if(query != null) { if (query != null) {
query.close(); query.close();
} }
} finally { } finally {
@ -454,15 +455,11 @@ public class SearchHistoryHelper {
} }
return entries; return entries;
} }
} }
public void addNewItemToHistory(double latitude, double longitude, PointDescription pointDescription) { public void addNewItemToHistory(double latitude, double longitude, PointDescription pointDescription) {
addNewItemToHistory(new HistoryEntry(latitude, longitude, pointDescription)); addNewItemToHistory(new HistoryEntry(latitude, longitude, pointDescription));
}
}
} }