From 0787befbe7866b293b0820f11df8c2e215a781a9 Mon Sep 17 00:00:00 2001 From: TrianguloY Date: Fri, 14 Oct 2022 11:38:54 +0200 Subject: [PATCH] per-domain sorting (disabled by default) related to #111 --- .../modules/companions/LastOpened.java | 55 +++++++++++++++---- .../urlchecker/modules/list/OpenModule.java | 8 ++- app/src/main/res/layout/config_open.xml | 6 ++ app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/trianguloy/urlchecker/modules/companions/LastOpened.java b/app/src/main/java/com/trianguloy/urlchecker/modules/companions/LastOpened.java index d79ff21c..93336a4e 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/modules/companions/LastOpened.java +++ b/app/src/main/java/com/trianguloy/urlchecker/modules/companions/LastOpened.java @@ -5,6 +5,8 @@ import com.trianguloy.urlchecker.utilities.GenericPref; import com.trianguloy.urlchecker.utilities.JavaUtilities; +import java.net.URL; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -13,6 +15,10 @@ */ public class LastOpened { + public static GenericPref.Bool PERDOMAIN_PREF() { + return new GenericPref.Bool("lastOpen_perDomain", false); + } + /* ------------------- data ------------------- */ /** @@ -24,6 +30,7 @@ public class LastOpened { * The prefix for the savedPrefs */ private static final String PREFIX = "opened %s %s"; + private final GenericPref.Bool perDomainPref = PERDOMAIN_PREF(); private final Context cntx; /* ------------------- public ------------------- */ @@ -33,21 +40,22 @@ public class LastOpened { */ public LastOpened(Context cntx) { this.cntx = cntx; + perDomainPref.init(cntx); } /** * Sorts an existing list of [packages] with the preferred order */ - public void sort(List packages) { - Collections.sort(packages, this::comparePrefer); + public void sort(List packages, String url) { + Collections.sort(packages, (from, another) -> comparePrefer(from, another, url)); } /** * Marks the [prefer] package as preferred over [others]. */ - public void prefer(String prefer, List others) { + public void prefer(String prefer, List others, String url) { for (String other : others) { - prefer(prefer, other, 1); + prefer(prefer, other, 1, url); } } @@ -56,18 +64,18 @@ public void prefer(String prefer, List others) { /** * Marks that [prefer] package is preferred over [other] as much as [amount] more */ - private void prefer(String prefer, String other, int amount) { + private void prefer(String prefer, String other, int amount, String url) { // 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); + prefer(other, prefer, -amount, url); return; } // update preference (we subtract because negative means preferred) - GenericPref pref = getPref(prefer, other); + GenericPref pref = getPref(prefer, other, url); pref.set(JavaUtilities.clamp(-MAX, pref.get() - amount, MAX)); } @@ -75,20 +83,43 @@ private void prefer(String prefer, String other, int amount) { * Returns the current preference between these two packages. * Equivalent result as [from].compareTo([another]) */ - private int comparePrefer(String from, String another) { + private int comparePrefer(String from, String another, String url) { // switch order if not lexicographically sorted if (from.compareTo(another) > 0) { - return -comparePrefer(another, from); + return -comparePrefer(another, from, url); } // get preference - return getPref(from, another).get(); + return getPref(from, another, url).get(); } /** * The preference between two packages. ([left] must be lexicographically less than [right]) */ - private GenericPref getPref(String left, String right) { - return new GenericPref.Int(String.format(PREFIX, left, right), 0).init(cntx); + private GenericPref getPref(String left, String right, String url) { + String prefName = String.format(PREFIX, left, right); + if (perDomainPref.get()) { + prefName = getDomain(url) + " " + prefName; + } + + return new GenericPref.Int(prefName, 0).init(cntx); + } + + /** + * Get top level domain and first subdomain (if any) from a given url + * a.b.c.d => c.d + * a.b.c => b.c + * a.b => a.b + * a => a + */ + private String getDomain(String url) { + try { + List domainParts = Arrays.asList(new URL(url).getHost().split("\\.")); + return String.join(".", domainParts.size() <= 1 ? domainParts : domainParts.subList(domainParts.size() - 2, domainParts.size())); + } catch (Exception e) { + // can't get + e.printStackTrace(); + return ""; + } } } diff --git a/app/src/main/java/com/trianguloy/urlchecker/modules/list/OpenModule.java b/app/src/main/java/com/trianguloy/urlchecker/modules/list/OpenModule.java index db0eaa6d..55619640 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/modules/list/OpenModule.java +++ b/app/src/main/java/com/trianguloy/urlchecker/modules/list/OpenModule.java @@ -223,7 +223,7 @@ private void updateSpinner() { } // sort - lastOpened.sort(packages); + lastOpened.sort(packages, getUrl()); // set btn_open.setText(getActivity().getString(R.string.mOpen_with, PackageUtilities.getPackageName(packages.get(0), getActivity()))); @@ -253,7 +253,7 @@ private void openUrl(int index) { String chosen = packages.get(index); // update as preferred over the rest - lastOpened.prefer(chosen, packages); + lastOpened.prefer(chosen, packages, getUrl()); // open Intent intent = new Intent(getActivity().getIntent()); @@ -340,11 +340,14 @@ class OpenConfig extends AModuleConfig { private final GenericPref.Bool closeSharePref = OpenModule.CLOSESHARE_PREF(); private final GenericPref.Enumeration ctabsPref = CTabs.PREF(); + private final GenericPref.Bool perDomainPref = LastOpened.PERDOMAIN_PREF(); + public OpenConfig(ConfigActivity activity) { super(activity); ctabsPref.init(activity); closeOpenPref.init(activity); closeSharePref.init(activity); + perDomainPref.init(activity); } @Override @@ -366,6 +369,7 @@ public void onInitialize(View views) { } else { views.findViewById(R.id.ctabs_parent).setVisibility(View.GONE); } + perDomainPref.attachToCheckBox(views.findViewById(R.id.perDomain)); } } diff --git a/app/src/main/res/layout/config_open.xml b/app/src/main/res/layout/config_open.xml index de122a68..4310b52a 100644 --- a/app/src/main/res/layout/config_open.xml +++ b/app/src/main/res/layout/config_open.xml @@ -43,5 +43,11 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/mOpen_closeshareSetting" /> + \ No newline at end of file diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index f90cb98d..23319d45 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -65,6 +65,7 @@ Nota: si editas los patrones, nuevos patrones incorporados en futuras actualizac Desactivado por defecto Siempre activado Siempre desactivado + Ordenar diferentes dominios de forma independiente Abrir con %s Abrir Compartir diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e0a238e2..bd126c43 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -66,6 +66,7 @@ Note: if you edit the patterns, new built-in patterns from app updates will not Disabled by default Always on Always off + Sort different domains independently Open with %s Open Share