From e3f6353062162295a9e4025d3ab3e47787fff840 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Wed, 1 Jan 2020 08:11:37 -0600 Subject: [PATCH] Preserve widgets from Loop 1.7.11 and lower versions --- .../uhabits/core/preferences/WidgetPreferences.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/preferences/WidgetPreferences.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/preferences/WidgetPreferences.java index ae2588105..018652d89 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/preferences/WidgetPreferences.java +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/preferences/WidgetPreferences.java @@ -38,7 +38,17 @@ public class WidgetPreferences { } public long[] getHabitIdsFromWidgetId(int widgetId) { - long habitIds[] = storage.getLongArray(getHabitIdKey(widgetId)); + long habitIds[]; + String habitIdKey = getHabitIdKey(widgetId); + try { + habitIds = storage.getLongArray(habitIdKey); + } catch (ClassCastException e) { + // Up to Loop 1.7.11, this preference was not an array, but a single + // long. Trying to read the old preference causes a cast exception. + habitIds = new long[1]; + habitIds[0] = storage.getLong(habitIdKey, -1); + storage.putLongArray(habitIdKey, habitIds); + } if (habitIds.length == 0) throw new HabitNotFoundException(); return habitIds; }