Skip to content

Commit

Permalink
per-domain sorting (disabled by default)
Browse files Browse the repository at this point in the history
related to #111
  • Loading branch information
TrianguloY committed Oct 14, 2022
1 parent 29504a1 commit 0787bef
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,6 +15,10 @@
*/
public class LastOpened {

public static GenericPref.Bool PERDOMAIN_PREF() {
return new GenericPref.Bool("lastOpen_perDomain", false);
}

/* ------------------- data ------------------- */

/**
Expand All @@ -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 ------------------- */
Expand All @@ -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<String> packages) {
Collections.sort(packages, this::comparePrefer);
public void sort(List<String> 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<String> others) {
public void prefer(String prefer, List<String> others, String url) {
for (String other : others) {
prefer(prefer, other, 1);
prefer(prefer, other, 1, url);
}
}

Expand All @@ -56,39 +64,62 @@ public void prefer(String prefer, List<String> 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<Integer> pref = getPref(prefer, other);
GenericPref<Integer> pref = getPref(prefer, other, url);
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) {
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<Integer> getPref(String left, String right) {
return new GenericPref.Int(String.format(PREFIX, left, right), 0).init(cntx);
private GenericPref<Integer> 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<String> 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 "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -340,11 +340,14 @@ class OpenConfig extends AModuleConfig {
private final GenericPref.Bool closeSharePref = OpenModule.CLOSESHARE_PREF();
private final GenericPref.Enumeration<CTabs.Config> 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
Expand All @@ -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));
}
}

6 changes: 6 additions & 0 deletions app/src/main/res/layout/config_open.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mOpen_closeshareSetting" />
<CheckBox
android:id="@+id/perDomain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/smallPadding"
android:text="@string/mOpen_perDomain" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Nota: si editas los patrones, nuevos patrones incorporados en futuras actualizac
<string name="mOpen_ctabsOff">Desactivado por defecto</string>
<string name="mOpen_ctabsEn">Siempre activado</string>
<string name="mOpen_ctabsDis">Siempre desactivado</string>
<string name="mOpen_perDomain">Ordenar diferentes dominios de forma independiente</string>
<string name="mOpen_with">Abrir con %s</string>
<string name="mOpen_open">Abrir</string>
<string name="mOpen_share">Compartir</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Note: if you edit the patterns, new built-in patterns from app updates will not
<string name="mOpen_ctabsOff">Disabled by default</string>
<string name="mOpen_ctabsEn">Always on</string>
<string name="mOpen_ctabsDis">Always off</string>
<string name="mOpen_perDomain">Sort different domains independently</string>
<string name="mOpen_with">Open with %s</string>
<string name="mOpen_open">Open</string>
<string name="mOpen_share">Share</string>
Expand Down

0 comments on commit 0787bef

Please sign in to comment.