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

move flags-static logic to flags class

This commit is contained in:
TrianguloY 2023-04-23 13:28:16 +02:00
parent 406db9bd3a
commit ea0c83cc4e
3 changed files with 67 additions and 62 deletions

View File

@ -1,7 +1,12 @@
package com.trianguloy.urlchecker.modules.companions;
import static com.trianguloy.urlchecker.utilities.JavaUtils.valueOrDefault;
import android.app.Activity;
import android.content.Intent;
import com.trianguloy.urlchecker.modules.AModuleDialog;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -203,4 +208,62 @@ public class Flags {
}
return hex;
}
// ------------------- store/load flags -------------------
// this handles the store and load of the flags, if something wants to get the flags
// it should always use these methods.
private static final String DATA_FLAGS = "flagsEditor.flags";
private static final String REGEX = "(0x)?[a-fA-F\\d]{1,8}";
private static final int BASE = 16;
/**
* parses a text as an hexadecimal flags string.
* Returns null if invalid
*/
public static Integer toInteger(String text) {
if (text != null && text.matches(REGEX)) {
return Integer.parseInt(text.replaceAll("^0x", ""), BASE);
} else {
return null;
}
}
/**
* Converts an int flags to string
*/
public static String toHexString(int flags) {
return "0x" + Integer.toString(flags, BASE);
}
/**
* Retrieves the flags from GlobalData, if it is not defined it will return null
*/
public static Integer getGlobalFlagsNullable(AModuleDialog instance) {
return toInteger(instance.getData(DATA_FLAGS));
}
/**
* Loads the flags from GlobalData, if none were found it gets the flags from the intent that
* started this activity
*/
public static int getGlobalFlagsNonNull(AModuleDialog instance, Activity cntx) {
return getGlobalFlagsOrDefault(instance, cntx.getIntent().getFlags());
}
/**
* Loads the flags from GlobalData, if none were found it gets the flags from default
* Can be used by other modules
*/
public static int getGlobalFlagsOrDefault(AModuleDialog instance, int defaultFlags) {
return valueOrDefault(toInteger(instance.getData(DATA_FLAGS)), defaultFlags);
}
/**
* Stores the flags in GlobalData
*/
public static void setGlobalFlags(Flags flags, AModuleDialog instance) {
instance.putData(DATA_FLAGS, flags == null ? null : toHexString(flags.getFlagsAsInt()));
}
}

View File

@ -81,8 +81,6 @@ public class FlagsModule extends AModuleData {
class FlagsDialog extends AModuleDialog {
public static final String DATA_FLAGS = "flagsEditor.flags";
private final Flags defaultFlags;
private final Flags currentFlags;
@ -231,7 +229,7 @@ class FlagsDialog extends AModuleDialog {
fillWithFlags(hiddenFlagsSet, hiddenFlagsVG);
// Update global
setGlobalFlags(currentFlags);
Flags.setGlobalFlags(currentFlags, this);
updateMoreIndicator();
}
@ -260,7 +258,7 @@ class FlagsDialog extends AModuleDialog {
String flag = (String) v.getTag(R.id.text);
currentFlags.setFlag(flag, isChecked);
// Update global
setGlobalFlags(currentFlags);
Flags.setGlobalFlags(currentFlags, this);
// To update debug module view of GlobalData
setUrl(new UrlData(getUrl()).dontTriggerOwn().asMinorUpdate());
@ -323,63 +321,6 @@ class FlagsDialog extends AModuleDialog {
AndroidUtils.setRoundedColor(color, preferenceIndicator);
}
// ------------------- store/load flags -------------------
// this handles the store and load of the flags, if something wants to get the flags
// it should always use these methods.
private static final int BASE = 16;
protected static final String REGEX = "(0x)?[a-fA-F\\d]{1,8}";
/**
* parses a text as an hexadecimal flags string.
* Returns null if invalid
*/
public static Integer toInteger(String text) {
if (text != null && text.matches(REGEX)) {
return Integer.parseInt(text.replaceAll("^0x", ""), BASE);
} else {
return null;
}
}
/**
* Converts an int flags to string
*/
public static String toHexString(int flags) {
return "0x" + Integer.toString(flags, BASE);
}
/**
* Retrieves the flags from GlobalData, if it is not defined it will return null
* Intended for use in other modules
*/
public static Integer getGlobalFlagsNullable(AModuleDialog instance) {
return toInteger(instance.getData(DATA_FLAGS));
}
/**
* Loads the flags from GlobalData, if none were found it gets the flags from the intent that
* started this activity
*/
private int getGlobalFlagsNonNull() {
return getGlobalFlagsOrDefault(this, getActivity().getIntent().getFlags());
}
/**
* Loads the flags from GlobalData, if none were found it gets the flags from default
* Can be used by other modules
*/
public static int getGlobalFlagsOrDefault(AModuleDialog instance, int defaultFlags) {
return valueOrDefault(toInteger(instance.getData(DATA_FLAGS)), defaultFlags);
}
/**
* Stores the flags in GlobalData
*/
private void setGlobalFlags(Flags flags) {
putData(DATA_FLAGS, flags == null ? null : toHexString(flags.getFlagsAsInt()));
}
}
class FlagsConfig extends AModuleConfig {

View File

@ -17,6 +17,7 @@ import com.trianguloy.urlchecker.modules.AModuleConfig;
import com.trianguloy.urlchecker.modules.AModuleData;
import com.trianguloy.urlchecker.modules.AModuleDialog;
import com.trianguloy.urlchecker.modules.companions.CTabs;
import com.trianguloy.urlchecker.modules.companions.Flags;
import com.trianguloy.urlchecker.modules.companions.LastOpened;
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.AndroidUtils;
@ -251,7 +252,7 @@ class OpenDialog extends AModuleDialog {
}
// Get flags from global data (probably set by flags module, if active)
Integer flags = FlagsDialog.getGlobalFlagsNullable(this);
Integer flags = Flags.getGlobalFlagsNullable(this);
if (flags != null) {
intent.setFlags(flags);
}