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 com.trianguloy.urlchecker.utilities.GenericPref;
import com.trianguloy.urlchecker.utilities.JavaUtilities;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -12,77 +13,82 @@ import java.util.List;
*/
public class LastOpened {
/**
* How many apps to remember
*/
private static final int N = 5;
/* ------------------- data ------------------- */
/**
* The preferences
* Maximum 'preference' between two apps
*/
private final List<GenericPref.Str> list = new ArrayList<>(N);
private static final String PREFIX = "opened";
private static final int MAX = 3;
/**
* The prefix for the savedPrefs
*/
private static final String PREFIX = "opened %s %s";
private final Context cntx;
/* ------------------- public ------------------- */
/**
* Initializes this utility
*
* @param cntx base context
*/
public LastOpened(Context cntx) {
for (int i = 0; i < N; i++) {
GenericPref.Str gp = new GenericPref.Str(PREFIX + i, null);
gp.init(cntx);
list.add(gp);
}
// debug
// System.out.println(list);
this.cntx = cntx;
}
/**
* Sorts an existing list with the last opened
*
* @param packs list to sort
* Sorts an existing list of [packages] with the preferred order
*/
public void sort(List<String> packs) {
// check if a priority app is in the list
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);
}
}
public void sort(List<String> packages) {
Collections.sort(packages, this::comparePrefer);
}
/**
* Marks a package as used, updating the priority list
*
* @param pack packagename of the used app
* Marks the [prefer] package as preferred over [others].
*/
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
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());
}
list.get(N - 2).set(null);
/* ------------------- private ------------------- */
/**
* Marks that [prefer] package is preferred over [other] as much as [amount] more
*/
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;
}
// check intermediate ones, and swap with previous
for (int i = N - 2; i >= 0; i--) {
if (pack.equals(list.get(i).get())) {
String prev = list.get(i).get();
list.get(i).set(list.get(i + 1).get());
list.get(i + 1).set(prev);
return;
}
// update preference (we subtract because negative means preferred)
GenericPref<Integer> pref = getPref(prefer, other);
pref.set(JavaUtilities.clamp(-MAX, pref.get() - amount, MAX));
}
/**
* 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
list.get(0).set(pack);
// get preference
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
*/
private void openUrl(int index) {
// get
if (index < 0 || index >= packages.size()) return;
String chosen = packages.get(index);
// update chosen
String chosed = packages.get(index);
lastOpened.usedPackage(chosed);
// update as preferred over the rest
lastOpened.prefer(chosen, packages);
// open
Intent intent = new Intent(getActivity().getIntent());
@ -245,12 +246,10 @@ class OpenDialog extends AModuleDialog implements View.OnClickListener, PopupMen
// preserve original VIEW intent
intent.setData(Uri.parse(getUrl()));
intent.setComponent(null);
intent.setPackage(chosed);
intent.setPackage(chosen);
} else {
// replace with new VIEW intent
intent = UrlUtilities.getViewIntent(getUrl(), chosed);
intent = UrlUtilities.getViewIntent(getUrl(), chosen);
}
if (ctabs && !intent.hasExtra(CTabs.EXTRA)) {