Skip to content

Commit

Permalink
Merge pull request #2310 from marunjar/history_mode
Browse files Browse the repository at this point in the history
make history mode an enum
  • Loading branch information
marunjar authored Aug 9, 2024
2 parents 63888e5 + fa080b8 commit 7a7846c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 52 deletions.
42 changes: 18 additions & 24 deletions app/src/main/java/fr/neamar/kiss/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -40,9 +41,11 @@
import fr.neamar.kiss.dataprovider.simpleprovider.SettingsProvider;
import fr.neamar.kiss.dataprovider.simpleprovider.TagsProvider;
import fr.neamar.kiss.db.DBHelper;
import fr.neamar.kiss.db.HistoryMode;
import fr.neamar.kiss.db.ShortcutRecord;
import fr.neamar.kiss.db.ValuedHistoryRecord;
import fr.neamar.kiss.pojo.AppPojo;
import fr.neamar.kiss.pojo.NameComparator;
import fr.neamar.kiss.pojo.Pojo;
import fr.neamar.kiss.pojo.ShortcutPojo;
import fr.neamar.kiss.searcher.Searcher;
Expand Down Expand Up @@ -396,7 +399,7 @@ public List<Pojo> getHistory(Context context, int itemCount, Set<String> itemsTo
int extendedItemCount = itemCount + itemsToExcludeById.size();

// Read history
String historyMode = getHistoryMode();
HistoryMode historyMode = getHistoryMode();
List<ValuedHistoryRecord> ids = DBHelper.getHistory(context, extendedItemCount, historyMode);

// Find associated items
Expand All @@ -413,16 +416,20 @@ public List<Pojo> getHistory(Context context, int itemCount, Set<String> itemsTo
continue;
}

pojo.relevance = size - i;
if (historyMode == HistoryMode.ALPHABETICALLY) {
pojo.relevance = 0;
} else {
pojo.relevance = size - i;
}
history.add(pojo);
}

// Break if maximum number of items have been retrieved
if (history.size() >= itemCount) {
break;
}
if (historyMode == HistoryMode.ALPHABETICALLY) {
Collections.sort(history, new NameComparator());
}

return history;
// return only needed items
return history.subList(0, Math.min(itemCount, history.size()));
}

/**
Expand All @@ -431,8 +438,8 @@ public List<Pojo> getHistory(Context context, int itemCount, Set<String> itemsTo
* @param pojos which needs to have relevance set
* @param historyMode
*/
public void applyRelevanceFromHistory(List<? extends Pojo> pojos, String historyMode) {
if ("alphabetically".equals(historyMode)) {
public void applyRelevanceFromHistory(List<? extends Pojo> pojos, HistoryMode historyMode) {
if (HistoryMode.ALPHABETICALLY == historyMode) {
// "alphabetically" is special case because relevance needs to be set for all pojos instead of these from history.
// This is done by setting all relevance to zero which results in order by name from used comparator.
for (Pojo pojo : pojos) {
Expand Down Expand Up @@ -460,28 +467,15 @@ public void applyRelevanceFromHistory(List<? extends Pojo> pojos, String history
/**
* @return history mode from settings: Recency vs Frecency vs Frequency vs Adaptive vs Alphabetically
*/
public String getHistoryMode() {
public HistoryMode getHistoryMode() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString("history-mode", "recency");
return HistoryMode.valueById(prefs.getString("history-mode", "recency"));
}

public int getHistoryLength() {
return DBHelper.getHistoryLength(this.context);
}

/**
* Query database for item and return its name
*
* @param id globally unique ID, usually starts with provider scheme, e.g. "app://" or "contact://"
* @return name of item (i.e. app name)
*/
public String getItemName(String id) {
// Ask all providers if they know this id
Pojo pojo = getPojo(id);

return (pojo != null) ? pojo.getName() : "???";
}

@Nullable
public Pojo getItemById(String id) {
return getPojo(id);
Expand Down
34 changes: 15 additions & 19 deletions app/src/main/java/fr/neamar/kiss/db/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static Cursor getHistoryByAdaptive(SQLiteDatabase db, int hours, int lim
* Get the history items used closest to this time of day, for each day old an item is it has
* one less hour of time weight. So we limit the number of days of history to 24 days in the
* WHERE clause, this should also help with speed on large histories.
*
* <p>
* This is done by taking the max of a triangle waveform whose period is 24 hours, amplitude
* is half the milliseconds in a day and begins at currentTimeMillis() - timestamp, then offset
* by the time difference / 48 to diminish older history items by an hour for every day old. 48
Expand Down Expand Up @@ -171,44 +171,38 @@ private static Cursor getHistoryByTime(SQLiteDatabase db, int limit) {
* @param limit max number of items to retrieve
* @return records with number of use
*/
public static List<ValuedHistoryRecord> getHistory(Context context, int limit, String historyMode) {
public static List<ValuedHistoryRecord> getHistory(Context context, int limit, HistoryMode historyMode) {
List<ValuedHistoryRecord> records;

SQLiteDatabase db = getDatabase(context);

Cursor cursor;
switch (historyMode) {
case "frecency":
case FRECENCY:
cursor = getHistoryByFrecency(db, limit);
break;
case "frequency":
case FREQUENCY:
cursor = getHistoryByFrequency(db, limit);
break;
case "adaptive":
case ADAPTIVE:
cursor = getHistoryByAdaptive(db, 36, limit);
break;
case "time":
case TIME:
cursor = getHistoryByTime(db, limit);
break;
case ALPHABETICALLY:
case RECENCY:
cursor = getHistoryByRecency(db, limit);
break;
default:
cursor = getHistoryByRecency(db, limit);
Log.e(TAG, "Fallback to 'recency' for unknown history mode " + historyMode);
break;
}

records = readCursor(cursor);
cursor.close();

// sort history entries alphabetically
if (historyMode.equals("alphabetically")) {
DataHandler dataHandler = KissApplication.getApplication(context).getDataHandler();

for (ValuedHistoryRecord entry : records) {
entry.name = dataHandler.getItemName(entry.record);
}

Collections.sort(records, (a, b) -> a.name.compareToIgnoreCase(b.name));
}

return records;
}

Expand Down Expand Up @@ -240,7 +234,7 @@ public static int getHistoryLength(Context context) {
* @return records with number of use
*/
public static List<ValuedHistoryRecord> getPreviousResultsForQuery(Context context,
String query) {
String query) {
List<ValuedHistoryRecord> records;
SQLiteDatabase db = getDatabase(context);

Expand Down Expand Up @@ -557,7 +551,8 @@ public static void insertTagsForId(Context context, String tag, String record) {
}


/** Delete
/**
* Delete
*
* @param context android context
* @param record record to delete
Expand All @@ -570,6 +565,7 @@ public static void deleteTagsForId(Context context, String record) {

/**
* Delete all tags
*
* @param context android context
*/
public static void deleteTags(Context context) {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/fr/neamar/kiss/db/HistoryMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.neamar.kiss.db;

import androidx.annotation.NonNull;

public enum HistoryMode {
RECENCY("recency"),
FRECENCY("frecency"),
FREQUENCY("frequency"),
ADAPTIVE("adaptive"),
TIME("time"),
ALPHABETICALLY("alphabetically");

private final String id;

HistoryMode(String id) {
this.id = id;
}

@NonNull
public static HistoryMode valueById(String id) {
for (HistoryMode historyMode : values()) {
if (historyMode.id.equals(id)) {
return historyMode;
}
}
return RECENCY;
}
}
5 changes: 0 additions & 5 deletions app/src/main/java/fr/neamar/kiss/db/ValuedHistoryRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ public class ValuedHistoryRecord {
*/
public String record;

/**
* Name for the record
*/
public String name;

/**
* Context dependant value, e.g. number of access
*/
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/fr/neamar/kiss/pojo/SearchPojoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ public enum SearchPojoType {
URL_QUERY,
CALCULATOR_QUERY,
URI_QUERY
;
}
21 changes: 21 additions & 0 deletions app/src/main/java/fr/neamar/kiss/searcher/HistorySearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fr.neamar.kiss.DataHandler;
import fr.neamar.kiss.KissApplication;
import fr.neamar.kiss.MainActivity;
import fr.neamar.kiss.db.HistoryMode;
import fr.neamar.kiss.db.ShortcutRecord;
import fr.neamar.kiss.pojo.AppPojo;
import fr.neamar.kiss.pojo.Pojo;
Expand Down Expand Up @@ -81,4 +82,24 @@ protected Void doInBackground(Void... voids) {
this.addResults(pojos);
return null;
}

@Override
public boolean addResults(List<? extends Pojo> pojos) {
MainActivity activity = activityWeakReference.get();
if (activity == null) {
return false;
}

DataHandler dataHandler = KissApplication.getApplication(activity).getDataHandler();
if (dataHandler.getHistoryMode() != HistoryMode.ALPHABETICALLY) {
for (Pojo pojo : pojos) {
if (pojo.isDisabled()) {
// Give penalty for disabled items, these should not be preferred
pojo.relevance -= 200;
}
}
}

return super.addResults(pojos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import fr.neamar.kiss.KissApplication;
import fr.neamar.kiss.MainActivity;
import fr.neamar.kiss.db.HistoryMode;
import fr.neamar.kiss.pojo.Pojo;
import fr.neamar.kiss.pojo.PojoWithTags;

Expand Down Expand Up @@ -60,12 +61,12 @@ public boolean addResults(List<? extends Pojo> pojos) {
}

@NonNull
private String getTaggedResultSortMode() {
private HistoryMode getTaggedResultSortMode() {
String sortMode = prefs.getString("tagged-result-sort-mode", "default");
if ("default".equals(sortMode)) {
sortMode = KissApplication.getApplication(activityWeakReference.get()).getDataHandler().getHistoryMode();
return KissApplication.getApplication(activityWeakReference.get()).getDataHandler().getHistoryMode();
}
return sortMode;
return HistoryMode.valueById(sortMode);

}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<item>@string/history_frecency</item>
<item>@string/history_frequency</item>
<item>@string/history_adaptive</item>
<item>@string/history_time</item>
<item>@string/history_alphabetically</item>
</string-array>
<string-array name="taggedResultSortModeValues" translatable="false">
Expand All @@ -48,6 +49,7 @@
<item>frecency</item>
<item>frequency</item>
<item>adaptive</item>
<item>time</item>
<item>alphabetically</item>
</string-array>
<string-array name="defaultSearchProviders" tools:ignore="InconsistentArrays">
Expand Down

0 comments on commit 7a7846c

Please sign in to comment.