Search in sources :

Example 16 with TrayItem

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;
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) Uri(android.net.Uri) Nullable(android.support.annotation.Nullable)

Example 17 with TrayItem

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;
}
Also used : TrayException(net.grandcentrix.tray.core.TrayException) TrayItem(net.grandcentrix.tray.core.TrayItem) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull)

Example 18 with TrayItem

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);
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) Date(java.util.Date) NonNull(android.support.annotation.NonNull)

Example 19 with TrayItem

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;
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) Date(java.util.Date)

Example 20 with TrayItem

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());
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem)

Aggregations

TrayItem (net.grandcentrix.tray.core.TrayItem)26 Date (java.util.Date)5 Uri (android.net.Uri)3 ArrayList (java.util.ArrayList)3 NonNull (android.support.annotation.NonNull)2 TrayException (net.grandcentrix.tray.core.TrayException)2 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 MatrixCursor (android.database.MatrixCursor)1 HandlerThread (android.os.HandlerThread)1 Nullable (android.support.annotation.Nullable)1 MockContentProvider (android.test.mock.MockContentProvider)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OnTrayPreferenceChangeListener (net.grandcentrix.tray.core.OnTrayPreferenceChangeListener)1 TrayRuntimeException (net.grandcentrix.tray.core.TrayRuntimeException)1 WrongTypeException (net.grandcentrix.tray.core.WrongTypeException)1 TestTrayModulePreferences (net.grandcentrix.tray.mock.TestTrayModulePreferences)1