Persistence { @Nullable public DataMap getDataMap(String key) { if (preferences.contains(key)) { final String rawB64Data = preferences.getString(key, null); byte[] rawData = Base64.decode(rawB64Data, Base64.DEFAULT); try { return DataMap.fromByteArray(rawData); } catch (IllegalArgumentException ex) { } } return null; } Persistence(); @Nullable DataMap getDataMap(String key); void putDataMap(String key, DataMap dataMap); String getString(String key, String defaultValue); void putString(String key, String value); boolean getBoolean(String key, boolean defaultValue); void putBoolean(String key, boolean value); long whenDataUpdated(); Set<String> getSetOf(String key); void addToSet(String key, String value); void removeFromSet(String key, String value); static void storeDataMap(String key, DataMap dataMap); static Set<String> setOf(String key); static final String COMPLICATION_PROVIDER_PREFERENCES_FILE_KEY; }
@Test public void getDataMapTest() { Persistence persistence = new Persistence(); DataMap map = new DataMap(); map.putByteArray("test-key", new byte[]{9, 42, 127, -5}); DataMap notExisting = persistence.getDataMap("not-there"); Persistence.storeDataMap("data-map", map); DataMap restoredMap = persistence.getDataMap("data-map"); byte[] restoredMapContents = restoredMap.getByteArray("test-key"); assertNull(notExisting); assertNotNull(restoredMap); assertTrue(restoredMap.containsKey("test-key")); assertThat(restoredMapContents.length, is(4)); assertThat(restoredMapContents[0], is((byte)9)); assertThat(restoredMapContents[1], is((byte)42)); assertThat(restoredMapContents[2], is((byte)127)); assertThat(restoredMapContents[3], is((byte)-5)); }