Reduce ControlButton memory usage

This commit is contained in:
SerpentSpirale 2021-09-07 20:58:07 +02:00 committed by SerpentSpirale
parent 978f029958
commit bb52f8cd1c

View File

@ -33,10 +33,12 @@ public class ControlData {
// Internal usage only
public boolean isHideable;
private static WeakReference<ExpressionBuilder> builder;
private static WeakReference<Field> expression;
private static WeakReference<ExpressionBuilder> builder = new WeakReference<>(null);
private static WeakReference<Field> expression = new WeakReference<>(null);
private static WeakReference<ArrayMap<String , String>> conversionMap = new WeakReference<>(null);
static {
bypassExpressionBuilder();
buildConversionMap();
}
@ -178,28 +180,14 @@ public class ControlData {
public float insertDynamicPos(String dynamicPos) {
// Values in the map below may be always changed
Map<String, String> keyValueMap = new ArrayMap<>();
keyValueMap.put("top", "0");
keyValueMap.put("left", "0");
keyValueMap.put("right", Float.toString(CallbackBridge.physicalWidth - getWidth()));
keyValueMap.put("bottom", Float.toString(CallbackBridge.physicalHeight - getHeight()));
keyValueMap.put("width", Float.toString(getWidth()));
keyValueMap.put("height", Float.toString(getHeight()));
keyValueMap.put("screen_width", Integer.toString(CallbackBridge.physicalWidth));
keyValueMap.put("screen_height", Integer.toString(CallbackBridge.physicalHeight));
keyValueMap.put("margin", Integer.toString((int) Tools.dpToPx(2)));
keyValueMap.put("preferred_scale", Float.toString(LauncherPreferences.PREF_BUTTONSIZE));
// Insert value to ${variable}
String insertedPos = JSONUtils.insertSingleJSONValue(dynamicPos, keyValueMap);
String insertedPos = JSONUtils.insertSingleJSONValue(dynamicPos, fillConversionMap());
// Calculate, because the dynamic position contains some math equations
return calculate(insertedPos);
}
private static float calculate(String math) {
if(builder.get() == null) bypassExpressionBuilder();
setExpression(math);
return (float) builder.get().build().evaluate();
}
@ -269,10 +257,49 @@ public class ControlData {
* @param stringExpression the expression to set.
*/
private static void setExpression(String stringExpression){
if(builder.get() == null) bypassExpressionBuilder();
try {
expression.get().set(builder.get(), (String) stringExpression);
}catch (IllegalAccessException e){}
}
/**
* Build a shared conversion map without the view dependent values
* You need to set the view dependent values before using it.
*/
private static void buildConversionMap() {
// Values in the map below may be always changed
ArrayMap<String, String> keyValueMap = new ArrayMap<>(10);
keyValueMap.put("top", "0");
keyValueMap.put("left", "0");
keyValueMap.put("right", "DUMMY_RIGHT");
keyValueMap.put("bottom", "DUMMY_BOTTOM");
keyValueMap.put("width", "DUMMY_WIDTH");
keyValueMap.put("height", "DUMMY_HEIGHT");
keyValueMap.put("screen_width", Integer.toString(CallbackBridge.physicalWidth));
keyValueMap.put("screen_height", Integer.toString(CallbackBridge.physicalHeight));
keyValueMap.put("margin", Integer.toString((int) Tools.dpToPx(2)));
keyValueMap.put("preferred_scale", Float.toString(LauncherPreferences.PREF_BUTTONSIZE));
conversionMap = new WeakReference<>(keyValueMap);
}
/**
* Fill the conversionMap with
*/
private Map<String, String> fillConversionMap(){
ArrayMap<String, String> valueMap = conversionMap.get();
if (valueMap == null){
buildConversionMap();
valueMap = conversionMap.get();
}
valueMap.put("right", Float.toString(CallbackBridge.physicalWidth - getWidth()));
valueMap.put("bottom", Float.toString(CallbackBridge.physicalHeight - getHeight()));
valueMap.put("width", Float.toString(getWidth()));
valueMap.put("height", Float.toString(getHeight()));
return valueMap;
}
}