|
|
|
/** |
|
* Utils class which allows to write more easy access of the {@link SharedPreferences} |
|
* |
|
* @author Jens Hohl |
|
* |
|
*/ |
|
public final class SharedPreferencesUtil |
|
{ |
|
|
|
private final String SHARED_PREFERENCES_NAME = "yourSharedPreferencesFileName.xml"; |
|
|
|
private SharedPreferencesUtil() |
|
{ |
|
} |
|
|
|
|
|
/** |
|
* Returns the Name of the Shared Preferences File |
|
* |
|
* @return |
|
*/ |
|
public static String getSharedPreferencesFileName() |
|
{ |
|
return SHARED_PREFERENCES_NAME; |
|
} |
|
|
|
|
|
public static SharedPreferences getPreferences(final Context aContext) |
|
{ |
|
MorePreconditions.checkNotNull(aContext, "context"); |
|
return aContext.getSharedPreferences(SHARED_PREFERENCES_NAME, 0); |
|
} |
|
|
|
|
|
public static Editor getPreferencesEditor(final Context aContext) |
|
{ |
|
MorePreconditions.checkNotNull(aContext, "context"); |
|
final CustomEditor editor = new CustomEditor(aContext, aContext.getSharedPreferences( |
|
SHARED_PREFERENCES_NAME, 0).edit()); |
|
return editor; |
|
} |
|
|
|
/** |
|
* Enhanced {@link Editor} which allows to request the {@link BackupAgent} transparently |
|
* |
|
* @author Jens hohl |
|
* |
|
*/ |
|
private final static class CustomEditor implements Editor |
|
{ |
|
|
|
private final Editor delegate; |
|
|
|
private final Context context; |
|
|
|
|
|
/** |
|
* |
|
* @param aContext |
|
* @param editor |
|
*/ |
|
private CustomEditor(@Nonnull final Context aContext, @Nonnull final Editor editor) |
|
{ |
|
MorePreconditions.checkNotNull(aContext, "Context"); |
|
MorePreconditions.checkNotNull(editor, "Editor"); |
|
// |
|
context = aContext; |
|
delegate = editor; |
|
} |
|
|
|
|
|
@Override |
|
public void apply() |
|
{ |
|
delegate.apply(); |
|
final android.app.backup.BackupManager manager = new android.app.backup.BackupManager(context); |
|
manager.dataChanged(); |
|
|
|
} |
|
|
|
|
|
@Override |
|
public Editor clear() |
|
{ |
|
return delegate.clear(); |
|
} |
|
|
|
|
|
@Override |
|
public boolean commit() |
|
{ |
|
final boolean returnValue = delegate.commit(); |
|
final android.app.backup.BackupManager manager = new android.app.backup.BackupManager(context); |
|
manager.dataChanged(); |
|
|
|
return returnValue; |
|
} |
|
|
|
|
|
@Override |
|
public Editor putBoolean(final String key, final boolean value) |
|
{ |
|
return delegate.putBoolean(key, value); |
|
} |
|
|
|
|
|
@Override |
|
public Editor putFloat(final String key, final float value) |
|
{ |
|
return delegate.putFloat(key, value); |
|
} |
|
|
|
|
|
@Override |
|
public Editor putInt(final String key, final int value) |
|
{ |
|
return delegate.putInt(key, value); |
|
} |
|
|
|
|
|
@Override |
|
public Editor putLong(final String key, final long value) |
|
{ |
|
return delegate.putLong(key, value); |
|
} |
|
|
|
|
|
@Override |
|
public Editor putString(final String key, final String value) |
|
{ |
|
return delegate.putString(key, value); |
|
} |
|
|
|
|
|
@Override |
|
public Editor putStringSet(final String key, final Set<String> set) |
|
{ |
|
return delegate.putStringSet(key, set); |
|
} |
|
|
|
|
|
@Override |
|
public Editor remove(final String key) |
|
{ |
|
return delegate.remove(key); |
|
} |
|
|
|
} |
|
|
|
} |