Fix formatting in SearchHistoryHelper
This commit is contained in:
parent
2fe7debf05
commit
7c6d09f5f2
1 changed files with 108 additions and 111 deletions
|
@ -18,25 +18,26 @@ import java.util.Map;
|
|||
public class SearchHistoryHelper {
|
||||
|
||||
private static final int HISTORY_LIMIT = 1500;
|
||||
|
||||
private OsmandApplication context;
|
||||
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) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
private static final int[] DEF_INTERVALS_MIN = new int[] {
|
||||
5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24
|
||||
private static final int[] DEF_INTERVALS_MIN = new int[]{
|
||||
5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24
|
||||
};
|
||||
|
||||
private static class HistoryEntryComparator implements Comparator<HistoryEntry> {
|
||||
|
@ -48,7 +49,7 @@ public class SearchHistoryHelper {
|
|||
double r = rhs.getRank(time);
|
||||
return -Double.compare(l, r);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static class HistoryEntry {
|
||||
double lat;
|
||||
|
@ -58,14 +59,14 @@ public class SearchHistoryHelper {
|
|||
private int[] intervals = new int[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.lon = lon;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private double rankFunction(double cf, double timeDiff) {
|
||||
if(timeDiff <= 0) {
|
||||
if (timeDiff <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return cf / timeDiff;
|
||||
|
@ -79,7 +80,7 @@ public class SearchHistoryHelper {
|
|||
for (int k = 0; k < intervals.length; k++) {
|
||||
double ntimeDiff = intervals[k] * 60 * 1000;
|
||||
double nvl = intervalValues[k];
|
||||
if(ntimeDiff < timeDiff || nvl <= vl){
|
||||
if (ntimeDiff < timeDiff || nvl <= vl) {
|
||||
continue;
|
||||
}
|
||||
rnk += rankFunction(nvl - vl, baseTimeDiff + (ntimeDiff - timeDiff) / 2 + timeDiff);
|
||||
|
@ -100,6 +101,7 @@ public class SearchHistoryHelper {
|
|||
public double getLat() {
|
||||
return lat;
|
||||
}
|
||||
|
||||
public double getLon() {
|
||||
return lon;
|
||||
}
|
||||
|
@ -107,7 +109,7 @@ public class SearchHistoryHelper {
|
|||
public void markAsAccessed(long time) {
|
||||
int[] nintervals = new int[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];
|
||||
nintervalValues[k] = getUsageLastTime(time, 0, 0, nintervals[k]) + 1;
|
||||
}
|
||||
|
@ -116,7 +118,7 @@ public class SearchHistoryHelper {
|
|||
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 timeInPast = time - mins * 60 * 1000;
|
||||
if (this.lastAccessedTime <= timeInPast) {
|
||||
|
@ -139,7 +141,7 @@ public class SearchHistoryHelper {
|
|||
}
|
||||
|
||||
public void setFrequency(String intervalsString, String values) {
|
||||
if(Algorithms.isEmpty(intervalsString) || Algorithms.isEmpty(values)) {
|
||||
if (Algorithms.isEmpty(intervalsString) || Algorithms.isEmpty(values)) {
|
||||
markAsAccessed(this.lastAccessedTime);
|
||||
return;
|
||||
}
|
||||
|
@ -148,7 +150,7 @@ public class SearchHistoryHelper {
|
|||
intervals = new int[ints.length];
|
||||
intervalValues = new double[ints.length];
|
||||
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]);
|
||||
intervalValues[i] = Double.parseDouble(vsl[i]);
|
||||
}
|
||||
|
@ -163,8 +165,8 @@ public class SearchHistoryHelper {
|
|||
|
||||
public String getIntervalsValues() {
|
||||
StringBuilder s = new StringBuilder();
|
||||
for(int i = 0; i < intervalValues.length; i++) {
|
||||
if(i > 0) {
|
||||
for (int i = 0; i < intervalValues.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(",");
|
||||
}
|
||||
s.append(intervalValues[i]);
|
||||
|
@ -174,8 +176,8 @@ public class SearchHistoryHelper {
|
|||
|
||||
public String getIntervals() {
|
||||
StringBuilder s = new StringBuilder();
|
||||
for(int i = 0; i < intervals.length; i++) {
|
||||
if(i > 0) {
|
||||
for (int i = 0; i < intervals.length; i++) {
|
||||
if (i > 0) {
|
||||
s.append(",");
|
||||
}
|
||||
s.append(intervals[i]);
|
||||
|
@ -190,10 +192,10 @@ public class SearchHistoryHelper {
|
|||
}
|
||||
|
||||
public List<HistoryEntry> getHistoryEntries() {
|
||||
if(loadedEntries == null){
|
||||
if (loadedEntries == null) {
|
||||
checkLoadedEntries();
|
||||
}
|
||||
return new ArrayList<SearchHistoryHelper.HistoryEntry>(loadedEntries);
|
||||
return new ArrayList<>(loadedEntries);
|
||||
}
|
||||
|
||||
private HistoryItemDBHelper checkLoadedEntries() {
|
||||
|
@ -201,7 +203,7 @@ public class SearchHistoryHelper {
|
|||
if (loadedEntries == null) {
|
||||
loadedEntries = helper.getEntries();
|
||||
Collections.sort(loadedEntries, new HistoryEntryComparator());
|
||||
for(HistoryEntry he : loadedEntries) {
|
||||
for (HistoryEntry he : loadedEntries) {
|
||||
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();
|
||||
if(mp.containsKey(model.getName())) {
|
||||
if (mp.containsKey(model.getName())) {
|
||||
model = mp.get(model.getName());
|
||||
model.markAsAccessed(System.currentTimeMillis());
|
||||
helper.update(model);
|
||||
|
@ -237,8 +239,8 @@ public class SearchHistoryHelper {
|
|||
helper.add(model);
|
||||
}
|
||||
Collections.sort(loadedEntries, new HistoryEntryComparator());
|
||||
if(loadedEntries.size() > HISTORY_LIMIT){
|
||||
if(helper.remove(loadedEntries.get(loadedEntries.size() - 1))){
|
||||
if (loadedEntries.size() > HISTORY_LIMIT) {
|
||||
if (helper.remove(loadedEntries.get(loadedEntries.size() - 1))) {
|
||||
loadedEntries.remove(loadedEntries.size() - 1);
|
||||
}
|
||||
}
|
||||
|
@ -247,24 +249,24 @@ public class SearchHistoryHelper {
|
|||
|
||||
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 String HISTORY_TABLE_NAME = "history_recents"; //$NON-NLS-1$
|
||||
private static final String HISTORY_COL_NAME = "name"; //$NON-NLS-1$
|
||||
private static final String HISTORY_COL_TIME = "time"; //$NON-NLS-1$
|
||||
private static final String HISTORY_COL_FREQ_INTERVALS = "freq_intervals"; //$NON-NLS-1$
|
||||
private static final String HISTORY_COL_FREQ_VALUES = "freq_values"; //$NON-NLS-1$
|
||||
private static final String HISTORY_COL_LAT = "latitude"; //$NON-NLS-1$
|
||||
private static final String HISTORY_COL_LON = "longitude"; //$NON-NLS-1$
|
||||
private static final String HISTORY_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
|
||||
HISTORY_COL_NAME + " TEXT, " +
|
||||
HISTORY_COL_TIME + " long, " +
|
||||
HISTORY_COL_FREQ_INTERVALS + " TEXT, " +
|
||||
HISTORY_COL_FREQ_VALUES + " TEXT, " +
|
||||
HISTORY_COL_LAT + " double, " +HISTORY_COL_LON + " double);"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
private static final String HISTORY_TABLE_NAME = "history_recents";
|
||||
private static final String HISTORY_COL_NAME = "name";
|
||||
private static final String HISTORY_COL_TIME = "time";
|
||||
private static final String HISTORY_COL_FREQ_INTERVALS = "freq_intervals";
|
||||
private static final String HISTORY_COL_FREQ_VALUES = "freq_values";
|
||||
private static final String HISTORY_COL_LAT = "latitude";
|
||||
private static final String HISTORY_COL_LON = "longitude";
|
||||
private static final String HISTORY_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE_NAME + " (" +
|
||||
HISTORY_COL_NAME + " TEXT, " +
|
||||
HISTORY_COL_TIME + " long, " +
|
||||
HISTORY_COL_FREQ_INTERVALS + " TEXT, " +
|
||||
HISTORY_COL_FREQ_VALUES + " TEXT, " +
|
||||
HISTORY_COL_LAT + " double, " + HISTORY_COL_LON + " double);";
|
||||
|
||||
|
||||
public HistoryItemDBHelper() {
|
||||
HistoryItemDBHelper() {
|
||||
}
|
||||
|
||||
private SQLiteConnection openConnection(boolean readonly) {
|
||||
|
@ -280,7 +282,6 @@ public class SearchHistoryHelper {
|
|||
onUpgrade(conn, conn.getVersion(), DB_VERSION);
|
||||
}
|
||||
conn.setVersion(DB_VERSION);
|
||||
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
@ -290,17 +291,17 @@ public class SearchHistoryHelper {
|
|||
}
|
||||
|
||||
public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) {
|
||||
if(newVersion == 2) {
|
||||
if (newVersion == 2) {
|
||||
db.execSQL(HISTORY_TABLE_CREATE);
|
||||
for(HistoryEntry he : getLegacyEntries(db)) {
|
||||
for (HistoryEntry he : getLegacyEntries(db)) {
|
||||
insert(he, db);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean remove(HistoryEntry e){
|
||||
public boolean remove(HistoryEntry e) {
|
||||
SQLiteConnection db = openConnection(false);
|
||||
if(db != null){
|
||||
if (db != null) {
|
||||
try {
|
||||
removeQuery(e.getSerializedName(), db);
|
||||
} finally {
|
||||
|
@ -313,14 +314,14 @@ public class SearchHistoryHelper {
|
|||
|
||||
private void removeQuery(String name, SQLiteConnection db) {
|
||||
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);
|
||||
if(db != null){
|
||||
if (db != null) {
|
||||
try {
|
||||
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); //$NON-NLS-1$
|
||||
db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
|
@ -329,17 +330,17 @@ public class SearchHistoryHelper {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean update(HistoryEntry e){
|
||||
public boolean update(HistoryEntry e) {
|
||||
SQLiteConnection db = openConnection(false);
|
||||
if(db != null){
|
||||
if (db != null) {
|
||||
try {
|
||||
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_VALUES + "= ? WHERE " +
|
||||
", " + HISTORY_COL_FREQ_VALUES + "= ? WHERE " +
|
||||
HISTORY_COL_NAME + " = ?",
|
||||
new Object[] { e.getLastAccessTime(), e.getIntervals(), e.getIntervalsValues(),
|
||||
e.getSerializedName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
new Object[]{e.getLastAccessTime(), e.getIntervals(), e.getIntervalsValues(),
|
||||
e.getSerializedName()});
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
|
@ -348,9 +349,9 @@ public class SearchHistoryHelper {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean add(HistoryEntry e){
|
||||
public boolean add(HistoryEntry e) {
|
||||
SQLiteConnection db = openConnection(false);
|
||||
if(db != null){
|
||||
if (db != null) {
|
||||
try {
|
||||
insert(e, db);
|
||||
} finally {
|
||||
|
@ -364,16 +365,16 @@ public class SearchHistoryHelper {
|
|||
private void insert(HistoryEntry e, SQLiteConnection db) {
|
||||
db.execSQL(
|
||||
"INSERT INTO " + HISTORY_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?)",
|
||||
new Object[] { e.getSerializedName(), e.getLastAccessTime(),
|
||||
e.getIntervals(), e.getIntervalsValues(), e.getLat(), e.getLon() }); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
new Object[]{e.getSerializedName(), e.getLastAccessTime(),
|
||||
e.getIntervals(), e.getIntervalsValues(), e.getLat(), e.getLon()});
|
||||
}
|
||||
|
||||
public List<HistoryEntry> getLegacyEntries(SQLiteConnection db){
|
||||
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
|
||||
List<HistoryEntry> getLegacyEntries(SQLiteConnection db) {
|
||||
List<HistoryEntry> entries = new ArrayList<>();
|
||||
if (db != null) {
|
||||
// LEGACY QUERY !!
|
||||
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()) {
|
||||
do {
|
||||
String name = query.getString(0);
|
||||
|
@ -401,23 +402,23 @@ public class SearchHistoryHelper {
|
|||
entries.add(e);
|
||||
} while (query.moveToNext());
|
||||
}
|
||||
if(query != null) {
|
||||
if (query != null) {
|
||||
query.close();
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
public List<HistoryEntry> getEntries(){
|
||||
List<HistoryEntry> entries = new ArrayList<HistoryEntry>();
|
||||
public List<HistoryEntry> getEntries() {
|
||||
List<HistoryEntry> entries = new ArrayList<>();
|
||||
SQLiteConnection db = openConnection(true);
|
||||
if(db != null){
|
||||
if (db != null) {
|
||||
try {
|
||||
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 +
|
||||
" FROM " + HISTORY_TABLE_NAME , null); //$NON-NLS-1$//$NON-NLS-2$
|
||||
Map<PointDescription, HistoryEntry> st = new HashMap<PointDescription, HistoryEntry>();
|
||||
" FROM " + HISTORY_TABLE_NAME, null);
|
||||
Map<PointDescription, HistoryEntry> st = new HashMap<>();
|
||||
if (query != null && query.moveToFirst()) {
|
||||
boolean reinsert = false;
|
||||
do {
|
||||
|
@ -428,24 +429,24 @@ public class SearchHistoryHelper {
|
|||
long time = query.getLong(3);
|
||||
e.setLastAccessTime(time);
|
||||
e.setFrequency(query.getString(4), query.getString(5));
|
||||
if(st.containsKey(p)) {
|
||||
if (st.containsKey(p)) {
|
||||
reinsert = true;
|
||||
}
|
||||
entries.add(e);
|
||||
st.put(p, e);
|
||||
} while (query.moveToNext());
|
||||
if(reinsert) {
|
||||
if (reinsert) {
|
||||
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.addAll(st.values());
|
||||
for(HistoryEntry he : entries) {
|
||||
for (HistoryEntry he : entries) {
|
||||
insert(he, db);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if(query != null) {
|
||||
if (query != null) {
|
||||
query.close();
|
||||
}
|
||||
} finally {
|
||||
|
@ -461,8 +462,4 @@ public class SearchHistoryHelper {
|
|||
addNewItemToHistory(new HistoryEntry(latitude, longitude, pointDescription));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue