diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java index cc031aeff6..92d0b539c9 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java @@ -80,6 +80,10 @@ public void loadLayout(String jsonPath) throws IOException, JsonSyntaxException } public void loadLayout(CustomControls controlLayout) { + boolean sanitizedModified = false; + if(controlLayout != null) { + sanitizedModified = LayoutSanitizer.sanitizeLayout(controlLayout); + } if(mActionRow == null){ mActionRow = new ActionRow(getContext()); addView(mActionRow); @@ -118,7 +122,7 @@ public void loadLayout(CustomControls controlLayout) { mLayout.scaledAt = LauncherPreferences.PREF_BUTTONSIZE; - setModified(false); + setModified(sanitizedModified); mButtons = null; getButtonChildren(); // Force refresh } // loadLayout diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutSanitizer.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutSanitizer.java new file mode 100644 index 0000000000..18a8afe384 --- /dev/null +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/LayoutSanitizer.java @@ -0,0 +1,52 @@ +package net.kdt.pojavlaunch.customcontrols; + +import java.util.Iterator; +import java.util.List; + +public class LayoutSanitizer { + + // Maybe add more conditions here later? + private static boolean isInvalidFormula(String formula) { + return formula.contains("Infinity"); + } + + private static boolean isSaneData(ControlData controlData) { + if(controlData.getWidth() == 0 || controlData.getHeight() == 0) return false; + if(isInvalidFormula(controlData.dynamicX) || isInvalidFormula(controlData.dynamicY)) return false; + return true; + } + + private static ControlData getControlData(Object dataEntry) { + if(dataEntry instanceof ControlData) { + return (ControlData) dataEntry; + }else if(dataEntry instanceof ControlDrawerData) { + return ((ControlDrawerData) dataEntry).properties; + }else throw new RuntimeException("Encountered wrong type during ControlData sanitization"); + } + + private static boolean sanitizeList(List controlDataList) { + boolean madeChanges = false; + Iterator iterator = controlDataList.iterator(); + while(iterator.hasNext()) { + ControlData controlData = getControlData(iterator.next()); + if(!isSaneData(controlData)) { + madeChanges = true; + iterator.remove(); + } + } + return madeChanges; + } + + /** + * Check all buttons in a control layout and ensure they're sane (contain values valid enough + * to be displayed properly). Removes any buttons deemed not sane. + * @param controls the original control layout. + * @return whether the sanitization process made any changes to the layout + */ + public static boolean sanitizeLayout(CustomControls controls) { + boolean madeChanges = sanitizeList(controls.mControlDataList); + if(sanitizeList(controls.mDrawerDataList)) madeChanges = true; + if(sanitizeList(controls.mJoystickDataList)) madeChanges = true; + return madeChanges; + } +}