mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-09 11:47:04 +01:00
Fix import of some hashset preference values from xml
This commit is contained in:
parent
be9cc348d1
commit
9fcd463bfc
@ -183,9 +183,9 @@ public class GBApplication extends Application {
|
||||
final Intent startActivity = new Intent(context, ControlCenterv2.class);
|
||||
final PendingIntent pendingIntent = PendingIntentUtils.getActivity(context, 1337, startActivity, PendingIntent.FLAG_CANCEL_CURRENT, false);
|
||||
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pendingIntent);
|
||||
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1500, pendingIntent);
|
||||
|
||||
System.exit(0);
|
||||
Runtime.getRuntime().exit(0);
|
||||
}
|
||||
|
||||
public GBApplication() {
|
||||
|
@ -117,9 +117,11 @@ public class BackupRestoreProgressActivity extends AbstractGBActivity {
|
||||
.setTitle(R.string.backup_restore_restart_title)
|
||||
.setMessage(message.toString())
|
||||
.setOnCancelListener((dialog -> {
|
||||
finish();
|
||||
GBApplication.restart();
|
||||
}))
|
||||
.setPositiveButton(R.string.ok, (dialog, which) -> {
|
||||
finish();
|
||||
GBApplication.restart();
|
||||
}).show();
|
||||
break;
|
||||
|
@ -21,6 +21,8 @@ package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Xml;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
|
||||
@ -35,8 +37,14 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.garmin.GarminPreferences;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceConst;
|
||||
|
||||
@Deprecated // use JsonBackupPreferences
|
||||
public class ImportExportSharedPreferences {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ImportExportSharedPreferences.class);
|
||||
|
||||
private static final String BOOLEAN = Boolean.class.getSimpleName();
|
||||
private static final String FLOAT = Float.class.getSimpleName();
|
||||
@ -114,26 +122,46 @@ public class ImportExportSharedPreferences {
|
||||
} else if (STRING.equals(name)) {
|
||||
editor.putString(key, text);
|
||||
} else if (HASHSET.equals(name)) {
|
||||
// FIXME: We can only deserialize values that are guaranteed to not contain commas,
|
||||
// spaces at the end or start, square brackets
|
||||
switch (key) {
|
||||
case GBPrefs.PACKAGE_BLACKLIST:
|
||||
Set<String> apps_blacklist = new HashSet<>();
|
||||
text = text.replace("[", "").replace("]", "");
|
||||
for (int z = 0; z < text.split(",").length; z++) {
|
||||
apps_blacklist.add(text.split(",")[z].trim());
|
||||
}
|
||||
GBApplication.setAppsNotifBlackList(apps_blacklist, editor);
|
||||
GBApplication.setAppsNotifBlackList(stringToSet(text), editor);
|
||||
break;
|
||||
case GBPrefs.PACKAGE_PEBBLEMSG_BLACKLIST:
|
||||
Set<String> apps_pebble_blacklist = new HashSet<>();
|
||||
text = text.replace("[", "").replace("]", "");
|
||||
for (int z = 0; z < text.split(",").length; z++) {
|
||||
apps_pebble_blacklist.add(text.split(",")[z].trim());
|
||||
}
|
||||
GBApplication.setAppsPebbleBlackList(apps_pebble_blacklist, editor);
|
||||
GBApplication.setAppsPebbleBlackList(stringToSet(text), editor);
|
||||
break;
|
||||
// @array/device_action_values
|
||||
case DeviceSettingsPreferenceConst.PREF_DEVICE_ACTION_FELL_SLEEP_SELECTIONS:
|
||||
case DeviceSettingsPreferenceConst.PREF_DEVICE_ACTION_START_NON_WEAR_SELECTIONS:
|
||||
case DeviceSettingsPreferenceConst.PREF_DEVICE_ACTION_WOKE_UP_SELECTIONS:
|
||||
// GarminCapability enum
|
||||
case GarminPreferences.PREF_GARMIN_CAPABILITIES:
|
||||
// mac addresses
|
||||
case "dashboard_devices_multiselect":
|
||||
case GBPrefs.LAST_DEVICE_ADDRESSES:
|
||||
// display items
|
||||
case "bip_display_items":
|
||||
case "cor_display_items":
|
||||
case "mi2_display_items":
|
||||
case "miband3_display_items":
|
||||
case HuamiConst.PREF_DISPLAY_ITEMS:
|
||||
// @array/pref_amazfitneo_sounds_values
|
||||
case DeviceSettingsPreferenceConst.PREF_SOUNDS:
|
||||
// TestFeature enum
|
||||
case TestDeviceConst.PREF_TEST_FEATURES:
|
||||
// Ignored due to unsafe values
|
||||
//case GBPrefs.CALENDAR_BLACKLIST: // user-controlled names
|
||||
//case LoyaltyCardsSettingsConst.LOYALTY_CARDS_SYNC_GROUPS: // user-controlled names
|
||||
//case MiBandConst.PREF_MIBAND_ALARMS: // unknown potential values
|
||||
//case "casio_features_current_values": // unknown potential values
|
||||
editor.putStringSet(key, stringToSet(text));
|
||||
break;
|
||||
default:
|
||||
LOG.warn("Unknown hashset preference {}, will not import", key);
|
||||
}
|
||||
} else if (!PREFERENCES.equals(name)) {
|
||||
throw new Exception("Unknown type " + name);
|
||||
throw new Exception("Unknown type " + name + " for pref " + key);
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
@ -144,4 +172,15 @@ public class ImportExportSharedPreferences {
|
||||
}
|
||||
return editor.commit();
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<String> stringToSet(final String text) {
|
||||
final Set<String> ret = new HashSet<>();
|
||||
final String[] split = text.replace("[", "")
|
||||
.replace("]", "")
|
||||
.split(",");
|
||||
for (final String s : split) {
|
||||
ret.add(s.trim());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;
|
||||
|
||||
public class ImportExportSharedPreferencesTest extends TestBase {
|
||||
@Test
|
||||
public void testExportImport() throws Exception {
|
||||
final SharedPreferences sharedPreferences = getContext().getSharedPreferences("testExportImport", Context.MODE_PRIVATE);
|
||||
|
||||
assertEquals(0, sharedPreferences.getAll().size());
|
||||
|
||||
final HashSet<String> EXPECTED_SET = new HashSet<>(Arrays.asList(
|
||||
"item 1",
|
||||
"item 2"
|
||||
));
|
||||
|
||||
sharedPreferences.edit()
|
||||
.putString("a_string", "Hello")
|
||||
.putBoolean("a_boolean", true)
|
||||
.putLong("a_long", 42)
|
||||
.putInt("an_int", 1337)
|
||||
.putFloat("a_float", 7f)
|
||||
.putStringSet("bip_display_items", EXPECTED_SET).commit();
|
||||
|
||||
assertEquals(6, sharedPreferences.getAll().size());
|
||||
|
||||
final File file = File.createTempFile("gb-ImportExportSharedPreferencesTest", "xml");
|
||||
file.deleteOnExit();
|
||||
ImportExportSharedPreferences.exportToFile(sharedPreferences, file);
|
||||
|
||||
sharedPreferences.edit()
|
||||
.putInt("an_int", 3)
|
||||
.putString("this extra string", "should be gone after import")
|
||||
.commit();
|
||||
|
||||
assertEquals(7, sharedPreferences.getAll().size());
|
||||
ImportExportSharedPreferences.importFromFile(sharedPreferences, file);
|
||||
|
||||
assertEquals(6, sharedPreferences.getAll().size());
|
||||
assertEquals("Hello", sharedPreferences.getString("a_string", null));
|
||||
assertTrue(sharedPreferences.getBoolean("a_boolean", false));
|
||||
assertEquals(42, sharedPreferences.getLong("a_long", 0));
|
||||
assertEquals(1337, sharedPreferences.getInt("an_int", 0));
|
||||
assertEquals(7f, sharedPreferences.getFloat("a_float", 0), 0.01);
|
||||
|
||||
final Set<String> actualSet = sharedPreferences.getStringSet("bip_display_items", Collections.emptySet());
|
||||
assertEquals(EXPECTED_SET.size(), actualSet.size());
|
||||
assertTrue(EXPECTED_SET.containsAll(actualSet));
|
||||
assertTrue(actualSet.containsAll(EXPECTED_SET));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user