use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.
the class ContentProviderStorage method get.
@Override
@Nullable
public TrayItem get(@NonNull final String key) {
final Uri uri = mTrayUri.builder().setType(getType()).setModule(getModuleName()).setKey(key).build();
final List<TrayItem> prefs = mProviderHelper.queryProviderSafe(uri);
final int size = prefs.size();
if (size > 1) {
TrayLog.w("found more than one item for key '" + key + "' in module " + getModuleName() + ". " + "This can be caused by using the same name for a device and user specific preference.");
for (int i = 0; i < prefs.size(); i++) {
final TrayItem pref = prefs.get(i);
TrayLog.d("item #" + i + " " + pref);
}
}
return size > 0 ? prefs.get(0) : null;
}
use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.
the class TrayProviderHelper method queryProvider.
/**
* sends a query for TrayItems to the provider
*
* @param uri path to data
* @return list of items
* @throws TrayException when something is wrong with the provider/database
*/
@NonNull
public List<TrayItem> queryProvider(@NonNull final Uri uri) throws TrayException {
final Cursor cursor;
try {
cursor = mContext.getContentResolver().query(uri, null, null, null, null);
} catch (Throwable e) {
throw new TrayException("Hard error accessing the ContentProvider", e);
}
// Return Preference if found
if (cursor == null) {
// When running in here, please check if your ContentProvider has the correct authority
throw new TrayException("could not access stored data with uri " + uri);
}
final ArrayList<TrayItem> list = new ArrayList<>();
for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) {
final TrayItem trayItem = cursorToTrayItem(cursor);
list.add(trayItem);
}
cursor.close();
return list;
}
use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.
the class TrayProviderHelper method cursorToTrayItem.
/**
* converts a {@link Cursor} to a {@link TrayItem}
* <p>
* This is not a secondary constructor in {@link TrayItem} because the columns are a
* implementation detail of the provider package
*
* @param cursor (size > 1)
* @return a {@link TrayItem} filled with data
*/
@NonNull
static TrayItem cursorToTrayItem(final Cursor cursor) {
final String module = cursor.getString(cursor.getColumnIndexOrThrow(TrayContract.Preferences.Columns.MODULE));
final String key = cursor.getString(cursor.getColumnIndexOrThrow(TrayContract.Preferences.Columns.KEY));
final String migratedKey = cursor.getString(cursor.getColumnIndexOrThrow(TrayContract.Preferences.Columns.MIGRATED_KEY));
final String value = cursor.getString(cursor.getColumnIndexOrThrow(TrayContract.Preferences.Columns.VALUE));
final Date created = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(TrayContract.Preferences.Columns.CREATED)));
final Date updated = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(TrayContract.Preferences.Columns.UPDATED)));
return new TrayItem(module, key, migratedKey, value, created, updated);
}
use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.
the class MockTrayStorage method put.
@Override
public boolean put(@NonNull final String key, @Nullable final String migrationKey, final Object data) {
final TrayItem saved = this.mData.get(key);
final String value = String.valueOf(data);
final Date now = new Date();
final TrayItem item;
if (saved == null) {
item = new TrayItem(getModuleName(), key, migrationKey, value, now, now);
} else {
final Date created = saved.created();
item = new TrayItem(getModuleName(), key, migrationKey, value, created, now);
}
this.mData.put(key, item);
return true;
}
use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.
the class ContentProviderStorageTest method testDeviceAndUserWithSameName.
public void testDeviceAndUserWithSameName() throws Exception {
final ContentProviderStorage userStorage = new ContentProviderStorage(getProviderMockContext(), "sameName", TrayStorage.Type.USER);
userStorage.setVersion(100);
final ContentProviderStorage deviceStorage = new ContentProviderStorage(getProviderMockContext(), "sameName", TrayStorage.Type.DEVICE);
deviceStorage.setVersion(200);
assertEquals(100, userStorage.getVersion());
assertEquals(200, deviceStorage.getVersion());
// put
assertTrue(userStorage.put(TEST_KEY, "A"));
assertTrue(userStorage.put(TEST_KEY2, "a"));
assertUserDatabaseSize(2);
assertTrue(deviceStorage.put(TEST_KEY, "B"));
assertTrue(deviceStorage.put(TEST_KEY2, "b"));
assertDeviceDatabaseSize(2);
// get
final TrayItem itemA = userStorage.get(TEST_KEY);
assertNotNull(itemA);
assertEquals("A", itemA.value());
final TrayItem itemB = deviceStorage.get(TEST_KEY);
assertNotNull(itemB);
assertEquals("B", itemB.value());
// remove
userStorage.remove(TEST_KEY);
assertNull(userStorage.get(TEST_KEY));
deviceStorage.remove(TEST_KEY);
assertNull(deviceStorage.get(TEST_KEY));
assertUserDatabaseSize(1);
assertDeviceDatabaseSize(1);
// fill up again
assertTrue(userStorage.put(TEST_KEY, "A"));
assertTrue(deviceStorage.put(TEST_KEY, "B"));
assertUserDatabaseSize(2);
assertDeviceDatabaseSize(2);
/**
* undefined
*/
final ContentProviderStorage undefinedStorage = new ContentProviderStorage(getProviderMockContext(), "sameName", TrayStorage.Type.UNDEFINED);
// get returns now two items!!!! order is not defined
final TrayItem trayItem1 = undefinedStorage.get(TEST_KEY);
assertNotNull(trayItem1);
assertTrue(trayItem1.value().equals("A") || trayItem1.value().equals("B"));
final TrayItem trayItem2 = undefinedStorage.get(TEST_KEY);
assertNotNull(trayItem2);
assertTrue(trayItem2.value().equals("A") || trayItem2.value().equals("B"));
// remove removes both items
undefinedStorage.remove(TEST_KEY);
assertUserDatabaseSize(1);
assertDeviceDatabaseSize(1);
undefinedStorage.clear();
assertUserDatabaseSize(0);
assertDeviceDatabaseSize(0);
assertNull(undefinedStorage.get(TEST_KEY));
assertEquals(100, userStorage.getVersion());
assertEquals(200, deviceStorage.getVersion());
// undefinedStorage.getVersion() would return random 100 or 200. more likely 100 because it was inserted before
assertTrue(undefinedStorage.wipe());
assertEquals(0, userStorage.getVersion());
assertEquals(0, deviceStorage.getVersion());
}
Aggregations