From effb434cf70b88aff3d45ba4d7831a6284fbbe24 Mon Sep 17 00:00:00 2001 From: Forrest Guice Date: Tue, 27 Jun 2023 12:45:13 -0700 Subject: [PATCH] applyExtra Fixes a bug where widget actions fail to apply the correct extra type when using data %substitutions. --- .../settings/WidgetActions.java | 116 ++++++++++-------- 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/settings/WidgetActions.java b/app/src/main/java/com/forrestguice/suntimeswidget/settings/WidgetActions.java index 16984f1c2..d0c2c4abc 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/settings/WidgetActions.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/settings/WidgetActions.java @@ -478,75 +478,91 @@ public static void applyExtras(Context context, Intent intent, @Nullable String } String[] extras = extraString.split("&"); - for (String extra : extras) + for (String extra : extras) { + applyExtra(context, intent, extra, data); + } + } + + public static void applyExtra(Context context, Intent intent, String extra, @Nullable SuntimesData data) + { + String[] pair = extra.split("="); + if (pair.length == 2) { - String[] pair = extra.split("="); - if (pair.length == 2) - { - String key = pair[0]; - String value = pair[1]; + String key = pair[0]; + String value = pair[1]; - char c = value.charAt(0); - boolean isNumeric = (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7'|| c == '8' || c == '9'); - if (isNumeric) + char c = value.charAt(0); + boolean isNumeric = (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7'|| c == '8' || c == '9'); + if (isNumeric) + { + if (value.endsWith("L") || value.endsWith("l")) { - if (value.endsWith("L") || value.endsWith("l")) - { - try { - intent.putExtra(key, Long.parseLong(value.substring(0, value.length()-1))); // long - Log.i(TAG, "applyExtras: applied " + extra + " (long)"); + try { + intent.putExtra(key, Long.parseLong(value.substring(0, value.length()-1))); // long + Log.i(TAG, "applyExtras: applied " + extra + " (long)"); - } catch (NumberFormatException e) { - intent.putExtra(key, value); // string - Log.w(TAG, "applyExtras: fallback " + extra + " (long)"); - } + } catch (NumberFormatException e) { + intent.putExtra(key, value); // string + Log.w(TAG, "applyExtras: fallback " + extra + " (long)"); + } - } else if (value.endsWith("D") || value.endsWith("d")) { - try { - intent.putExtra(key, Double.parseDouble(value)); // double - Log.i(TAG, "applyExtras: applied " + extra + " (double)"); + } else if (value.endsWith("D") || value.endsWith("d")) { + try { + intent.putExtra(key, Double.parseDouble(value)); // double + Log.i(TAG, "applyExtras: applied " + extra + " (double)"); - } catch (NumberFormatException e) { - intent.putExtra(key, value); // string - Log.w(TAG, "applyExtras: fallback " + extra + " (double)"); - } + } catch (NumberFormatException e) { + intent.putExtra(key, value); // string + Log.w(TAG, "applyExtras: fallback " + extra + " (double)"); + } - } else if (value.endsWith("F") || value.endsWith("f")) { - try { - intent.putExtra(key, Float.parseFloat(value)); // float - Log.i(TAG, "applyExtras: applied " + extra + " (float)"); + } else if (value.endsWith("F") || value.endsWith("f")) { + try { + intent.putExtra(key, Float.parseFloat(value)); // float + Log.i(TAG, "applyExtras: applied " + extra + " (float)"); - } catch (NumberFormatException e) { - intent.putExtra(key, value); // string - Log.w(TAG, "applyExtras: fallback " + extra + " (float)"); - } + } catch (NumberFormatException e) { + intent.putExtra(key, value); // string + Log.w(TAG, "applyExtras: fallback " + extra + " (float)"); + } - } else { - try { - intent.putExtra(key, Integer.parseInt(value)); // int - Log.i(TAG, "applyExtras: applied " + extra + " (int)"); + } else { + try { + intent.putExtra(key, Integer.parseInt(value)); // int + Log.i(TAG, "applyExtras: applied " + extra + " (int)"); - } catch (NumberFormatException e) { - intent.putExtra(key, value); // string - Log.w(TAG, "applyExtras: fallback " + extra + " (int)"); - } + } catch (NumberFormatException e) { + intent.putExtra(key, value); // string + Log.w(TAG, "applyExtras: fallback " + extra + " (int)"); } + } + + } else { + String lowerCase = value.toLowerCase(Locale.getDefault()); + if (lowerCase.equals("true") || lowerCase.equals("false")) { + intent.putExtra(key, lowerCase.equals("true")); // boolean + Log.i(TAG, "applyExtras: applied " + extra + " (boolean)"); } else { - String lowerCase = value.toLowerCase(Locale.getDefault()); - if (lowerCase.equals("true") || lowerCase.equals("false")) { - intent.putExtra(key, lowerCase.equals("true")); // boolean - Log.i(TAG, "applyExtras: applied " + extra + " (boolean)"); + if (value.contains("%")) + { + String v = displayStringForPattern(context, value, data); + if (!v.contains("%")) { + applyExtra(context, intent, key + "=" + v, data); // recursive call + } else { + intent.putExtra(key, v); + Log.w(TAG, "applyExtras: applied " + extra + " (String) (incomplete substitution)"); + } } else { - intent.putExtra(key, displayStringForPattern(context, value, data)); // string (may contain % patterns) + intent.putExtra(key, value); Log.i(TAG, "applyExtras: applied " + extra + " (String)"); } } - - } else { - Log.w(TAG, "applyExtras: skipping " + extra); } + + } else { + Log.w(TAG, "applyExtras: skipping " + extra); } }