0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

new preferred computation

This commit is contained in:
TrianguloY 2022-08-27 20:49:53 +02:00
parent 1e650cbc34
commit a10a280d95
2 changed files with 63 additions and 58 deletions

View File

@ -3,8 +3,9 @@ package com.trianguloy.urlchecker.modules.companions;
import android.content.Context; import android.content.Context;
import com.trianguloy.urlchecker.utilities.GenericPref; import com.trianguloy.urlchecker.utilities.GenericPref;
import com.trianguloy.urlchecker.utilities.JavaUtilities;
import java.util.ArrayList; import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -12,77 +13,82 @@ import java.util.List;
*/ */
public class LastOpened { public class LastOpened {
/** /* ------------------- data ------------------- */
* How many apps to remember
*/
private static final int N = 5;
/** /**
* The preferences * Maximum 'preference' between two apps
*/ */
private final List<GenericPref.Str> list = new ArrayList<>(N); private static final int MAX = 3;
private static final String PREFIX = "opened";
/**
* The prefix for the savedPrefs
*/
private static final String PREFIX = "opened %s %s";
private final Context cntx;
/* ------------------- public ------------------- */
/** /**
* Initializes this utility * Initializes this utility
*
* @param cntx base context
*/ */
public LastOpened(Context cntx) { public LastOpened(Context cntx) {
for (int i = 0; i < N; i++) { this.cntx = cntx;
GenericPref.Str gp = new GenericPref.Str(PREFIX + i, null);
gp.init(cntx);
list.add(gp);
}
// debug
// System.out.println(list);
} }
/** /**
* Sorts an existing list with the last opened * Sorts an existing list of [packages] with the preferred order
*
* @param packs list to sort
*/ */
public void sort(List<String> packs) { public void sort(List<String> packages) {
// check if a priority app is in the list Collections.sort(packages, this::comparePrefer);
for (int i = 0; i < N; i++) {
final String pack = list.get(i).get();
// and if it is, move to front
if (packs.contains(pack)) {
packs.remove(pack);
packs.add(0, pack);
}
}
} }
/** /**
* Marks a package as used, updating the priority list * Marks the [prefer] package as preferred over [others].
*
* @param pack packagename of the used app
*/ */
public void usedPackage(String pack) { public void prefer(String prefer, List<String> others) {
for (String other : others) {
prefer(prefer, other, 1);
}
}
// check if already the most used, and move all one below /* ------------------- private ------------------- */
if (pack.equals(list.get(N - 1).get())) {
for (int i = 0; i < N - 3; ++i) { /**
list.get(i).set(list.get(i + 1).get()); * Marks that [prefer] package is preferred over [other] as much as [amount] more
} */
list.get(N - 2).set(null); private void prefer(String prefer, String other, int amount) {
// skip prefer over ourselves, it's useless
if (prefer.equals(other)) return;
// switch order if not lexicographically sorted
if (prefer.compareTo(other) > 0) {
prefer(other, prefer, -amount);
return; return;
} }
// check intermediate ones, and swap with previous // update preference (we subtract because negative means preferred)
for (int i = N - 2; i >= 0; i--) { GenericPref<Integer> pref = getPref(prefer, other);
if (pack.equals(list.get(i).get())) { pref.set(JavaUtilities.clamp(-MAX, pref.get() - amount, MAX));
String prev = list.get(i).get(); }
list.get(i).set(list.get(i + 1).get());
list.get(i + 1).set(prev); /**
return; * Returns the current preference between these two packages.
} * Equivalent result as [from].compareTo([another])
*/
private int comparePrefer(String from, String another) {
// switch order if not lexicographically sorted
if (from.compareTo(another) > 0) {
return -comparePrefer(another, from);
} }
// if not in list, set as last // get preference
list.get(0).set(pack); return getPref(from, another).get();
}
/**
* The preference between two packages. ([left] must be lexicographically less than [right])
*/
private GenericPref<Integer> getPref(String left, String right) {
return new GenericPref.Int(String.format(PREFIX, left, right), 0).init(cntx);
} }
} }

View File

@ -233,11 +233,12 @@ class OpenDialog extends AModuleDialog implements View.OnClickListener, PopupMen
* @param index index from the packages list of the app to use * @param index index from the packages list of the app to use
*/ */
private void openUrl(int index) { private void openUrl(int index) {
// get
if (index < 0 || index >= packages.size()) return; if (index < 0 || index >= packages.size()) return;
String chosen = packages.get(index);
// update chosen // update as preferred over the rest
String chosed = packages.get(index); lastOpened.prefer(chosen, packages);
lastOpened.usedPackage(chosed);
// open // open
Intent intent = new Intent(getActivity().getIntent()); Intent intent = new Intent(getActivity().getIntent());
@ -245,12 +246,10 @@ class OpenDialog extends AModuleDialog implements View.OnClickListener, PopupMen
// preserve original VIEW intent // preserve original VIEW intent
intent.setData(Uri.parse(getUrl())); intent.setData(Uri.parse(getUrl()));
intent.setComponent(null); intent.setComponent(null);
intent.setPackage(chosed); intent.setPackage(chosen);
} else { } else {
// replace with new VIEW intent // replace with new VIEW intent
intent = UrlUtilities.getViewIntent(getUrl(), chosed); intent = UrlUtilities.getViewIntent(getUrl(), chosen);
} }
if (ctabs && !intent.hasExtra(CTabs.EXTRA)) { if (ctabs && !intent.hasExtra(CTabs.EXTRA)) {