net.grandcentrix.tray.core.TrayItem Java Examples

The following examples show how to use net.grandcentrix.tray.core.TrayItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VolumeKeyService.java    From skipTrackLongPressVolume with MIT License 6 votes vote down vote up
@Override
public void onTrayPreferenceChanged(Collection<TrayItem> items) {
    boolean permission = preferences.getBoolean(PREF_PERMISSION, false);
    boolean serviceEnabled = preferences.getBoolean(PREF_ENABLED, false);

    debugEnabled = preferences.getBoolean(PREF_DEBUG, false);
    prefScreenOn = preferences.getBoolean(PREF_SCREEN_ON, false);
    prefNoMedia = preferences.getBoolean(PREF_NO_MEDIA, false);

    if(serviceEnabled && permission) {
        mediaSessionManager.setOnVolumeKeyLongPressListener(this, mHandler);
        Log.d("cilenco", "Registered VolumeKeyListener");
    } else {
        mediaSessionManager.setOnVolumeKeyLongPressListener(null, null);
        Log.d("cilenco", "Unregistered VolumeKeyListener");
    }
}
 
Example #2
Source File: ImportTrayPreferences.java    From tray with Apache License 2.0 6 votes vote down vote up
/**
 * 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: {}
}
 
Example #3
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 6 votes vote down vote up
public void testUpdateChanges() throws Exception {
    assertTrue(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());
    assertNotNull(itemA.updateTime());

    Thread.sleep(10);
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_B));
    final List<TrayItem> list2 = mProviderHelper
            .queryProvider(getUri(MODULE_A, KEY_A));
    assertNotNull(list2);
    assertEquals(1, list2.size());
    TrayItem itemB = list2.get(0);
    //Log.v("", "diff: " + (itemA.updateTime().getTime() - itemB.updateTime().getTime()));
    assertNotSame(itemA.updateTime().getTime(), itemB.updateTime().getTime());
}
 
Example #4
Source File: ChangedListenerTest.java    From tray with Apache License 2.0 6 votes vote down vote up
public void testUnregister() throws Exception {
    final OnTrayPreferenceChangeListener listener = new OnTrayPreferenceChangeListener() {
        @Override
        public void onTrayPreferenceChanged(final Collection<TrayItem> items) {

        }
    };
    final ContentProviderStorage userStorage = checkChangeListener(true, listener);
    assertEquals(1, userStorage.mListeners.size());

    // unregister null should do nothing
    userStorage.unregisterOnTrayPreferenceChangeListener(null);
    assertEquals(1, userStorage.mListeners.size());

    userStorage.unregisterOnTrayPreferenceChangeListener(listener);
    assertEquals(0, userStorage.mListeners.size());
    assertNull(userStorage.mObserver);
    assertNull(userStorage.mObserverThread);

}
 
Example #5
Source File: ContentProviderStorage.java    From tray with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #6
Source File: TrayProviderHelper.java    From tray with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: TrayProviderHelper.java    From tray with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #8
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testCursorToTrayItem() throws Exception {

        final MatrixCursor matrixCursor = new MatrixCursor(new String[]{
                TrayContract.Preferences.Columns.KEY,
                TrayContract.Preferences.Columns.VALUE,
                TrayContract.Preferences.Columns.MODULE,
                TrayContract.Preferences.Columns.CREATED,
                TrayContract.Preferences.Columns.UPDATED,
                TrayContract.Preferences.Columns.MIGRATED_KEY
        });
        final Date created = new Date();
        final Date updated = new Date();
        matrixCursor.addRow(new Object[]{
                "key",
                "value",
                "module",
                created.getTime(),
                updated.getTime(),
                "migratedKey"
        });
        assertTrue(matrixCursor.moveToFirst());

        final TrayItem item = TrayProviderHelper.cursorToTrayItem(matrixCursor);
        assertEquals("key", item.key());
        assertEquals("value", item.value());
        assertEquals("migratedKey", item.migratedKey());
        assertEquals("module", item.module());
        assertEquals(updated, item.updateTime());
        assertEquals(created, item.created());
    }
 
Example #9
Source File: TrayItemTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testToString() throws Exception {
    SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy", Locale.US);
    final Date created = new Date();
    final Date updated = new Date();
    final TrayItem item = new TrayItem("module", "key", "migratedKey", "value", created, updated
    );
    final String string = item.toString();
    assertTrue(string.contains(item.key()));
    assertTrue(string.contains(item.value()));
    assertTrue(string.contains(item.migratedKey()));
    assertTrue(string.contains(item.module()));
    assertTrue(string.contains(sf.format(item.updateTime())));
    assertTrue(string.contains(sf.format(item.created())));
}
 
Example #10
Source File: TrayItemTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testValues() throws Exception {
    final Date created = new Date();
    final Date updated = new Date();
    final TrayItem item = new TrayItem("module", "key", "migratedKey", "value", created, updated
    );
    assertEquals("key", item.key());
    assertEquals("value", item.value());
    assertEquals("migratedKey", item.migratedKey());
    assertEquals("module", item.module());
    assertEquals(updated, item.updateTime());
    assertEquals(created, item.created());
}
 
Example #11
Source File: ChangedListenerTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testApiLevel15OnChange() throws Exception {
    final CountDownLatch latch = new CountDownLatch(
            2); // first time called in checkChangeListener() second time in this test
    final ArrayList<TrayItem> changed = new ArrayList<>();
    final OnTrayPreferenceChangeListener listener = new OnTrayPreferenceChangeListener() {

        @Override
        public void onTrayPreferenceChanged(final Collection<TrayItem> items) {
            changed.addAll(items);
            latch.countDown();
        }
    };
    final ContentProviderStorage storage = checkChangeListener(true, listener);

    storage.put("some", "value");
    storage.put("foo", "bar");

    new HandlerThread("change") {
        @Override
        protected void onLooperPrepared() {
            super.onLooperPrepared();
            storage.mObserver.onChange(false);
        }
    }.start();

    latch.await(3000, TimeUnit.MILLISECONDS);
    assertEquals(2, changed.size());
}
 
Example #12
Source File: ContentProviderStorageTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testGetDevice() throws Exception {
    final ContentProviderStorage storage = new ContentProviderStorage(getProviderMockContext(),
            "testGet_Device", TrayStorage.Type.DEVICE);
    assertNull(storage.get("test"));

    assertTrue(storage.put("test", "foo"));
    final TrayItem item = storage.get("test");
    assertNotNull(item);
    assertEquals("test", item.key());
    assertEquals("foo", item.value());
}
 
Example #13
Source File: ContentProviderStorageTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testGetUser() throws Exception {
    final ContentProviderStorage storage = new ContentProviderStorage(getProviderMockContext(),
            "testGet_User", TrayStorage.Type.USER);
    assertNull(storage.get("test"));

    assertTrue(storage.put("test", "foo"));
    final TrayItem item = storage.get("test");
    assertNotNull(item);
    assertEquals("test", item.key());
    assertEquals("foo", item.value());
}
 
Example #14
Source File: ContentProviderStorageTest.java    From tray with Apache License 2.0 5 votes vote down vote up
private void checkReadDataWithUndefined(final ContentProviderStorage original)
        throws Exception {
    final ContentProviderStorage undefined = new ContentProviderStorage(
            getProviderMockContext(), original.getModuleName(), TrayStorage.Type.UNDEFINED);

    assertEquals(TrayStorage.Type.UNDEFINED, undefined.getType());
    assertEquals(original.getAll().size(), undefined.getAll().size());
    final TrayItem item = undefined.get(TEST_KEY);
    assertNotNull(item);
    assertEquals(original.get(TEST_KEY).value(), item.value());
    assertEquals(original.getVersion(), undefined.getVersion());
    assertEquals(original.getModuleName(), undefined.getModuleName());
}
 
Example #15
Source File: ContentProviderStorageTest.java    From tray with Apache License 2.0 5 votes vote down vote up
private void checkVersionAfterClear(final ContentProviderStorage storage) throws Exception {
    assertTrue(storage.put("key", "value"));
    final TrayItem key = storage.get("key");
    assertNotNull(key);
    assertEquals("value", key.value());
    storage.setVersion(1);
    assertEquals(1, storage.getVersion());

    storage.clear();
    assertNull(storage.get("key"));
    assertEquals(0, storage.getAll().size());
    assertEquals(1, storage.getVersion());
}
 
Example #16
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #17
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
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);

}
 
Example #18
Source File: TrayTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testGetAll() throws Exception {
    final TestTrayModulePreferences module2 =
            new TestTrayModulePreferences(getProviderMockContext(), "module2");

    mTrayModulePref.put("test", "test");
    module2.put("test", "test");
    mTrayModulePref.put("test2", "test");
    module2.put("test2", "test");
    assertUserDatabaseSize(4);

    final Collection<TrayItem> all = mTray.getAll();
    assertEquals(4, all.size());
}
 
Example #19
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testGetAll() throws Exception {
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_A));
    final List<TrayItem> all = mProviderHelper.getAll();
    assertEquals(1, all.size());
    assertEquals(STRING_A, all.get(0).value());
    assertEquals(KEY_A, all.get(0).key());
}
 
Example #20
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testGetAllMultiple() throws Exception {
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_B, STRING_B));
    assertTrue(mProviderHelper.persist(MODULE_B, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_B, KEY_B, STRING_B));
    final List<TrayItem> all = mProviderHelper.getAll();
    assertEquals(4, all.size());
}
 
Example #21
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testQueryAll() throws Exception {
    buildQueryDatabase();
    final List<TrayItem> list = mProviderHelper
            .queryProvider(mTrayUri.get());
    assertNotNull(list);
    assertEquals(4, list.size());
}
 
Example #22
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testQueryModule() throws Exception {
    buildQueryDatabase();
    final List<TrayItem> list = mProviderHelper
            .queryProvider(getUri(MODULE_A));
    assertNotNull(list);
    assertEquals(2, list.size());
    assertNotSame(list.get(0).value(), list.get(1).value());
}
 
Example #23
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testQuerySingle() throws Exception {
    buildQueryDatabase();
    final List<TrayItem> list = mProviderHelper
            .queryProvider(getUri(MODULE_A, KEY_A));
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals(STRING_A, list.get(0).value());
}
 
Example #24
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testReadParsedProperties() throws Exception {
    assertTrue(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);

    assertEquals(STRING_A, itemA.value());
    assertEquals(KEY_A, itemA.key());
    assertEquals(MODULE_A, itemA.module());
}
 
Example #25
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
public void testUpdateEqualsCreatedAtFirst() throws Exception {
    assertTrue(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());
    assertNotNull(itemA.updateTime());
    assertEquals(itemA.updateTime(), itemA.created());
}
 
Example #26
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 5 votes vote down vote up
private void specialCharTest(final String module, final String key) {
    assertTrue(mProviderHelper.persist(module, key, STRING_A));
    assertUserDatabaseSize(1);

    final List<TrayItem> list = mProviderHelper.queryProviderSafe(getUri(module));
    assertEquals(1, list.size());
    assertEquals(module, list.get(0).module());
    assertEquals(key, list.get(0).key());

    mProviderHelper.clear();
    assertUserDatabaseSize(0);
}
 
Example #27
Source File: TrayProviderHelper.java    From tray with Apache License 2.0 5 votes vote down vote up
/**
 * sends a query for TrayItems to the provider, doesn't throw when the database access couldn't
 * be established
 *
 * @param uri path to data
 * @return list of items, empty when an error occured
 */
@NonNull
public List<TrayItem> queryProviderSafe(@NonNull final Uri uri) {
    try {
        return queryProvider(uri);
    } catch (TrayException e) {
        return new ArrayList<>();
    }
}
 
Example #28
Source File: CaptchaDecoratorService.java    From nevo-decorators-sms-captchas with GNU General Public License v3.0 5 votes vote down vote up
private void onSettingsChanged(Collection<TrayItem> trayItems) {
    for (TrayItem item : trayItems) {
        switch (item.key()) {
            case Settings.SETTING_CAPTCHA_IDENTIFY_PATTERN:
                mSettings.setCaptchaIdentifyPattern(item.value());
                initCaptchaUtils();
                break;
            case Settings.SETTING_CAPTCHA_PARSE_PATTERN:
                mSettings.setCaptchaParsePattern(item.value());
                initCaptchaUtils();
                break;
            case Settings.SETTING_CAPTCHA_USE_DEFAULT_PATTERN :
                mSettings.setCaptchaUseDefaultPattern(Boolean.parseBoolean(item.value()));
                initCaptchaUtils();
                break;
            case Settings.SETTING_CAPTCHA_HIDE_ON_LOCKED :
                mSettings.setCaptchaHideOnLocked(Boolean.parseBoolean(item.value()));
                break;
            case Settings.SETTING_CAPTCHA_OVERRIDE_DEFAULT_ACTION :
                mSettings.setCaptchaOverrideDefaultAction(Boolean.parseBoolean(item.value()));
                break;
            case Settings.SETTING_CAPTCHA_POST_COPY_ACTION :
                mSettings.setCaptchaPostCopyAction(Integer.parseInt(Objects.requireNonNull(item.value())));
                break;

        }

        Log.i(TAG ,"Settings Updated " + item.key() + "=" + item.value());
    }
}
 
Example #29
Source File: SampleActivity.java    From tray with Apache License 2.0 5 votes vote down vote up
private List<String> getNiceString(final Collection<TrayItem> items) {
    return Observable.from(items)
            .map(new Func1<TrayItem, String>() {
                @Override
                public String call(final TrayItem trayItem) {
                    return "key: '" + trayItem.key() + "' value '" + trayItem.value() + "'";
                }
            })
            .toList().toBlocking().first();
}
 
Example #30
Source File: ContentProviderStorage.java    From tray with Apache License 2.0 5 votes vote down vote up
@Override
public void onChange(final boolean selfChange, Uri uri) {
    if (uri == null) {
        // for sdk version 15 and below we cannot detect which exact data was changed. This will
        // return all data for this module
        uri = mTrayUri.builder().setModule(getModuleName()).build();
    }

    // query only the changed items
    final List<TrayItem> trayItems = mProviderHelper.queryProviderSafe(uri);

    // clone to get around ConcurrentModificationException
    final Set<Map.Entry<OnTrayPreferenceChangeListener, Handler>> entries
            = new HashSet<>(mListeners.entrySet());

    // notify all registered listeners
    for (final Map.Entry<OnTrayPreferenceChangeListener, Handler> entry : entries) {
        final OnTrayPreferenceChangeListener listener = entry.getKey();
        final Handler handler = entry.getValue();
        if (handler != null) {
            // call the listener on the thread where the listener was registered
            handler.post(new Runnable() {
                @Override
                public void run() {
                    listener.onTrayPreferenceChanged(trayItems);
                }
            });
        } else {
            listener.onTrayPreferenceChanged(trayItems);
        }
    }
}