Search in sources :

Example 11 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class TrayProviderHelperTest method testCreatedTime.

public void testCreatedTime() throws Exception {
    final long start = System.currentTimeMillis();
    mProviderHelper.persist(MODULE_A, KEY_A, STRING_A);
    final List<TrayItem> list = mProviderHelper.queryProvider(getUri(MODULE_A, KEY_A));
    assertNotNull(list);
    assertEquals(1, list.size());
    TrayItem itemA = list.get(0);
    assertNotNull(itemA.created());
    assertEqualsWithin(start, itemA.created().getTime(), 50l);
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem)

Example 12 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class TrayProviderHelperTest method testCreatedTimeDoesNotChange.

public void testCreatedTimeDoesNotChange() throws Exception {
    testCreatedTime();
    final TrayItem insertedItem = mProviderHelper.getAll().get(0);
    final long createdTime = insertedItem.created().getTime();
    Thread.sleep(50);
    // save again
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_B));
    final long updatedCreatedTime = mProviderHelper.getAll().get(0).created().getTime();
    assertEquals(createdTime, updatedCreatedTime);
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem)

Example 13 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class ImportTrayPreferences method importSharedPreferencesWithLogging.

/**
     * logging wrapper for:
     * example how to import shared preferences
     */
private void importSharedPreferencesWithLogging() {
    final SharedPreferences sharedPreferences = getContext().getSharedPreferences(SampleActivity.SHARED_PREF_NAME, Context.MODE_MULTI_PROCESS);
    final HashMap<String, ?> allBefore = new HashMap<>(sharedPreferences.getAll());
    Log.v(TAG, allBefore.size() + " items in sharedPreferences: " + allBefore.toString());
    // 2 items in sharedPreferences: {userToken=cf26535a-6949-4728-b595-c6d80c094eff, gcmToken=2ca7e9a0-9114-4d55-8d2d-870b8d49fafe}
    importSharedPreferences();
    final ArrayList<TrayItem> all = new ArrayList<>(getAll());
    Log.v(TAG, "imported " + all.size() + " items: " + all.toString());
    // imported 2 items: [
    // {key: gcm:token, value: 2ca7e9a0-9114-4d55-8d2d-870b8d49fafe, module: imported, created: 14:06:04 05.06.2015, updated: 14:06:04 05.06.2015, migratedKey: gcmToken},
    // {key: user:token, value: cf26535a-6949-4728-b595-c6d80c094eff, module: imported, created: 14:06:04 05.06.2015, updated: 14:06:04 05.06.2015, migratedKey: userToken}
    // ]
    final HashMap<String, ?> allAfter = new HashMap<>(sharedPreferences.getAll());
    Log.v(TAG, allAfter.size() + " items in sharedPreferences: " + allAfter.toString());
// 0 items in sharedPreferences: {}
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) SharedPreferences(android.content.SharedPreferences) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 14 with TrayItem

use of net.grandcentrix.tray.core.TrayItem in project tray by grandcentrix.

the class ContentProviderStorage method getVersion.

@Override
public int getVersion() throws TrayException {
    final Uri internalUri = mTrayUri.builder().setInternal(true).setType(getType()).setModule(getModuleName()).setKey(VERSION).build();
    final List<TrayItem> trayItems = mProviderHelper.queryProvider(internalUri);
    if (trayItems.size() == 0) {
        // fallback, not found
        return 0;
    }
    return Integer.valueOf(trayItems.get(0).value());
}
Also used : TrayItem(net.grandcentrix.tray.core.TrayItem) Uri(android.net.Uri)

Example 15 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)

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