Java Code Examples for android.util.Property#getType()

The following examples show how to use android.util.Property#getType() . 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: SettingsHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the value stored in {@link android.content.SharedPreferences} for a
 * given {@link android.util.Property} associated with a VolumePanel.
 * @return The given value, {@code defVal} if none was set, or null is the
 * value could not be retrieved.
 * @throws ClassCastException If a type error occurred between SP and Property.
 */
@SuppressWarnings("unchecked")
public <T, E> E getProperty(Class<T> clazz, Property<T, E> property, E defVal)
        throws ClassCastException {
    Class<E> type = property.getType();
    String name = getName(clazz, property);

    // Handle all types supported by SharedPreferences.
    if (type.equals(Integer.TYPE) || type.equals(Integer.class))
        return (E) Integer.valueOf(mPreferences.getInt(name, (Integer) defVal));
    else if (type.equals(String.class) || type.equals(CharSequence.class))
        return (E) mPreferences.getString(name, ((defVal == null) ? (String) defVal : defVal.toString()));
    else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
        return (E) Boolean.valueOf(mPreferences.getBoolean(name, (Boolean) defVal));
    else if (type.equals(Long.TYPE) || type.equals(Long.class))
        return (E) Long.valueOf(mPreferences.getLong(name, (Long) defVal));
    else if (type.equals(Float.TYPE) || type.equals(Float.class))
        return (E) Float.valueOf(mPreferences.getFloat(name, (Float) defVal));
    else if (type.getClass().isAssignableFrom(Set.class))
        return (E) mPreferences.getStringSet(name, (Set<String>) defVal);

    return defVal;
}
 
Example 2
Source File: SettingsHelper.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the value stored in {@link android.content.SharedPreferences} for a
 * given {@link android.util.Property} associated with a VolumePanel.
 * @return The given value, {@code defVal} if none was set, or null is the
 * value could not be retrieved.
 * @throws ClassCastException If a type error occurred between SP and Property.
 */
@SuppressWarnings("unchecked")
public <T, E> E getProperty(Class<T> clazz, Property<T, E> property, E defVal)
        throws ClassCastException {
    Class<E> type = property.getType();
    String name = getName(clazz, property);

    // Handle all types supported by SharedPreferences.
    if (type.equals(Integer.TYPE) || type.equals(Integer.class))
        return (E) Integer.valueOf(mPreferences.getInt(name, (Integer) defVal));
    else if (type.equals(String.class) || type.equals(CharSequence.class))
        return (E) mPreferences.getString(name, ((defVal == null) ? (String) defVal : defVal.toString()));
    else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
        return (E) Boolean.valueOf(mPreferences.getBoolean(name, (Boolean) defVal));
    else if (type.equals(Long.TYPE) || type.equals(Long.class))
        return (E) Long.valueOf(mPreferences.getLong(name, (Long) defVal));
    else if (type.equals(Float.TYPE) || type.equals(Float.class))
        return (E) Float.valueOf(mPreferences.getFloat(name, (Float) defVal));
    else if (type.getClass().isAssignableFrom(Set.class))
        return (E) mPreferences.getStringSet(name, (Set<String>) defVal);

    return defVal;
}
 
Example 3
Source File: SettingsHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value for a given {@link android.util.Property}. Uses
 * {@link android.content.SharedPreferences.Editor#apply()} to store asynchronously.
 * @return True if the value was successfully applied, false if else.
 * @throws ClassCastException If a type error occurred between SP and Property.
 */
@SuppressWarnings("unchecked")
public <T, E> boolean setProperty(Class<T> clazz, Property<T, E> property, E val)
        throws ClassCastException {
    Class<E> type = property.getType();
    String name = getName(clazz, property);
    SharedPreferences.Editor editor = edit();

    // Handle all types supported by SharedPreferences.
    if (type.equals(Integer.TYPE) || type.equals(Integer.class))
        editor.putInt(name, (Integer) val);
    else if (type.equals(String.class) || type.equals(CharSequence.class))
        editor.putString(name, val.toString());
    else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
        editor.putBoolean(name, (Boolean) val);
    else if (type.equals(Long.TYPE) || type.equals(Long.class))
        editor.putLong(name, (Long) val);
    else if (type.equals(Float.TYPE) || type.equals(Float.class))
        editor.putFloat(name, (Float) val);
    else if (type.getClass().isAssignableFrom(Set.class))
        editor.putStringSet(name, (Set<String>) val);
    else
        editor = null;

    if (null == editor) return false;
    editor.apply();
    return true;
}
 
Example 4
Source File: SettingsHelper.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value for a given {@link android.util.Property}. Uses
 * {@link android.content.SharedPreferences.Editor#apply()} to store asynchronously.
 * @return True if the value was successfully applied, false if else.
 * @throws ClassCastException If a type error occurred between SP and Property.
 */
@SuppressWarnings("unchecked")
public <T, E> boolean setProperty(Class<T> clazz, Property<T, E> property, E val)
        throws ClassCastException {
    Class<E> type = property.getType();
    String name = getName(clazz, property);
    SharedPreferences.Editor editor = edit();

    // Handle all types supported by SharedPreferences.
    if (type.equals(Integer.TYPE) || type.equals(Integer.class))
        editor.putInt(name, (Integer) val);
    else if (type.equals(String.class) || type.equals(CharSequence.class))
        editor.putString(name, val.toString());
    else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
        editor.putBoolean(name, (Boolean) val);
    else if (type.equals(Long.TYPE) || type.equals(Long.class))
        editor.putLong(name, (Long) val);
    else if (type.equals(Float.TYPE) || type.equals(Float.class))
        editor.putFloat(name, (Float) val);
    else if (type.getClass().isAssignableFrom(Set.class))
        editor.putStringSet(name, (Set<String>) val);
    else
        editor = null;

    if (null == editor) return false;
    editor.apply();
    return true;
}