From ea0c83cc4e1613471b8153f4eb3e4a3f1f5d6508 Mon Sep 17 00:00:00 2001 From: TrianguloY Date: Sun, 23 Apr 2023 13:28:16 +0200 Subject: [PATCH] move flags-static logic to flags class --- .../urlchecker/modules/companions/Flags.java | 63 +++++++++++++++++++ .../urlchecker/modules/list/FlagsModule.java | 63 +------------------ .../urlchecker/modules/list/OpenModule.java | 3 +- 3 files changed, 67 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Flags.java b/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Flags.java index 610771d..2caaeaf 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Flags.java +++ b/app/src/main/java/com/trianguloy/urlchecker/modules/companions/Flags.java @@ -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())); + } } diff --git a/app/src/main/java/com/trianguloy/urlchecker/modules/list/FlagsModule.java b/app/src/main/java/com/trianguloy/urlchecker/modules/list/FlagsModule.java index 8068431..57cd9cc 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/modules/list/FlagsModule.java +++ b/app/src/main/java/com/trianguloy/urlchecker/modules/list/FlagsModule.java @@ -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 { 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 3b0b8f8..34ad1c1 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 @@ -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); }