Preferences are imported to Cpp project

This commit is contained in:
serso 2016-02-13 23:51:35 +01:00
parent 7dfb6dc45b
commit e917e7f955
17 changed files with 1363 additions and 1 deletions

View File

@ -77,7 +77,6 @@ dependencies {
compile 'org.solovyev.android:android-common-other:1.1.18@aar' compile 'org.solovyev.android:android-common-other:1.1.18@aar'
compile 'org.solovyev.android:android-common-views:1.1.18@aar' compile 'org.solovyev.android:android-common-views:1.1.18@aar'
compile 'org.solovyev.android:android-common-menus:1.1.18@aar' compile 'org.solovyev.android:android-common-menus:1.1.18@aar'
compile 'org.solovyev.android:android-common-preferences:1.1.18@aar'
compile(project(':jscl')) { compile(project(':jscl')) {
exclude(module: 'xercesImpl') exclude(module: 'xercesImpl')
} }

View File

@ -0,0 +1,240 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.solovyev.common.Objects;
import org.solovyev.common.text.Mapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Base class for creating preferences with dialogs
*
* @param <T>
*/
public abstract class AbstractDialogPreference<T> extends DialogPreference {
@Nonnull
protected final static String localNameSpace = "http://schemas.android.com/apk/res-auto";
@Nonnull
protected final static String androidns = "http://schemas.android.com/apk/res/android";
@Nonnull
private static final String TAG = AbstractDialogPreference.class.getSimpleName();
@Nonnull
private final Context context;
@Nullable
private final String defaultStringValue;
private final boolean needValueText;
@Nonnull
private final Mapper<T> mapper;
@Nullable
private TextView valueTextView;
@Nullable
private String valueText;
@Nullable
private View preferenceView;
@Nullable
private String description;
@Nullable
private T value;
@Nullable
private T defaultValue;
public AbstractDialogPreference(Context context, AttributeSet attrs, @Nullable String defaultStringValue, boolean needValueText, @Nonnull Mapper<T> mapper) {
super(context, attrs);
this.context = context;
this.defaultStringValue = defaultStringValue;
this.needValueText = needValueText;
this.mapper = mapper;
final String defaultValueFromAttrs = attrs.getAttributeValue(androidns, "defaultValue");
if (defaultValueFromAttrs != null) {
defaultValue = getMapper().parseValue(defaultValueFromAttrs);
} else if (defaultStringValue != null) {
defaultValue = getMapper().parseValue(defaultStringValue);
} else {
throw new IllegalArgumentException();
}
description = attrs.getAttributeValue(androidns, "dialogMessage");
valueText = attrs.getAttributeValue(androidns, "text");
}
@Nullable
protected View getPreferenceView() {
return preferenceView;
}
@Nullable
public T getValue() {
return value;
}
public void setValue(@Nullable T value) {
this.value = value;
}
@Override
@Nonnull
protected final LinearLayout onCreateDialogView() {
if (shouldPersist()) {
value = getPersistedValue();
}
final LinearLayout result = new LinearLayout(context);
result.setOrientation(LinearLayout.VERTICAL);
result.setPadding(6, 6, 6, 6);
if (description != null) {
final TextView splashText = new TextView(context);
splashText.setText(description);
result.addView(splashText);
}
if (needValueText) {
valueTextView = new TextView(context);
valueTextView.setGravity(Gravity.CENTER_HORIZONTAL);
valueTextView.setTextSize(32);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
result.addView(valueTextView, params);
}
preferenceView = createPreferenceView(context);
initPreferenceView(preferenceView, value);
final LinearLayout.LayoutParams params = getParams();
if (params != null) {
result.addView(preferenceView, params);
} else {
result.addView(preferenceView);
}
return result;
}
@Nullable
protected abstract LinearLayout.LayoutParams getParams();
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
super.onSetInitialValue(restore, defaultValue);
if (restore) {
if (shouldPersist()) {
value = getPersistedValue();
} else {
value = this.defaultValue;
}
} else {
value = (T) defaultValue;
if (shouldPersist()) {
persist(this.value);
}
}
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
if (this.preferenceView != null) {
initPreferenceView(this.preferenceView, value);
}
}
/**
* Creates view which is responsible for changing preference value (for example, Spinner or EditText)
*
* @param context context
* @return view which changes the preference value
*/
@Nonnull
protected abstract View createPreferenceView(@Nonnull Context context);
/**
* @param v view to be filled with initial data (the one which has been created with {@link #createPreferenceView} method)
* @param value current preference value
*/
protected abstract void initPreferenceView(@Nonnull View v, @Nullable T value);
@Nullable
private T getPersistedValue() {
String persistedString = getPersistedString(defaultStringValue);
if (Objects.areEqual(persistedString, defaultStringValue)) {
return defaultValue;
} else {
return getMapper().parseValue(persistedString);
}
}
protected void persistValue(@Nullable T value) {
this.value = value;
if (!callChangeListener(value)) {
return;
}
if (!shouldPersist()) {
return;
}
persist(value);
}
private void persist(@Nullable T value) {
if (value != null) {
final String toBePersistedString = getMapper().formatValue(value);
if (toBePersistedString != null) {
if (callChangeListener(toBePersistedString)) {
persistString(toBePersistedString);
}
}
}
}
@Nullable
public String getValueText() {
return valueText;
}
protected void updateValueText(@Nonnull String text) {
if (valueTextView != null) {
valueTextView.setText(text);
}
}
@Nonnull
private Mapper<T> getMapper() {
return this.mapper;
}
}

View File

@ -0,0 +1,130 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Base class for {@link Preference} implementation. Contains preference key and default value
*
* @param <T> type of preference
*/
public abstract class AbstractPreference<T> implements Preference<T> {
@Nonnull
private final String key;
private final T defaultValue;
protected AbstractPreference(@Nonnull String key, @Nullable T defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}
@Nonnull
public String getKey() {
return key;
}
public T getDefaultValue() {
return defaultValue;
}
@Override
public final T getPreference(@Nonnull SharedPreferences preferences) {
if (isSet(preferences)) {
return getPersistedValue(preferences);
} else {
return this.defaultValue;
}
}
@Override
public T getPreferenceNoError(@Nonnull SharedPreferences preferences) {
if (isSet(preferences)) {
try {
return getPersistedValue(preferences);
} catch (RuntimeException e) {
return this.defaultValue;
}
} else {
return this.defaultValue;
}
}
@Override
public void putDefault(@Nonnull SharedPreferences preferences) {
putPreference(preferences, this.defaultValue);
}
@Override
public void putPreference(@Nonnull SharedPreferences preferences, @Nullable T value) {
if (value != null) {
final SharedPreferences.Editor editor = preferences.edit();
putPersistedValue(editor, value);
editor.commit();
}
}
@Override
public boolean isSet(@Nonnull SharedPreferences preferences) {
return preferences.contains(this.key);
}
@Override
public final boolean tryPutDefault(@Nonnull SharedPreferences preferences) {
final boolean result;
if (isSet(preferences)) {
result = false;
} else {
putDefault(preferences);
result = true;
}
return result;
}
@Override
public final boolean isSameKey(@Nonnull String key) {
return this.key.equals(key);
}
/**
* @param preferences preferences container
* @return preference value from preferences with key defined by {@link #getKey()} method
*/
@Nullable
protected abstract T getPersistedValue(@Nonnull SharedPreferences preferences);
/**
* Method saved preference to preferences container editor
*
* @param editor editor in which value must be saved
* @param value value to be saved
*/
protected abstract void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull T value);
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class BooleanPreference extends AbstractPreference<Boolean> {
private BooleanPreference(@Nonnull String key, @Nullable Boolean defaultValue) {
super(key, defaultValue);
}
@Nonnull
public static BooleanPreference of(@Nonnull String key, @Nullable Boolean defaultValue) {
return new BooleanPreference(key, defaultValue);
}
@Override
protected Boolean getPersistedValue(@Nonnull SharedPreferences preferences) {
return preferences.getBoolean(getKey(), false);
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Boolean value) {
editor.putBoolean(getKey(), value);
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import org.solovyev.common.text.Mapper;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public abstract class CollectionSetPreference<C extends Collection<T>, T> extends AbstractPreference<C> {
@Nonnull
private final Mapper<T> mapper;
protected CollectionSetPreference(@Nonnull String id, @Nonnull C defaultValue, @Nonnull Mapper<T> mapper) {
super(id, defaultValue);
this.mapper = mapper;
}
@Override
protected C getPersistedValue(@Nonnull SharedPreferences preferences) {
final Set<String> stringValues = preferences.getStringSet(getKey(), null);
final C result = createCollection(stringValues.size());
for (String stringValue : stringValues) {
result.add(mapper.parseValue(stringValue));
}
return result;
}
@Nonnull
protected abstract C createCollection(int size);
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull C values) {
final Set<String> result = new HashSet<String>(values.size());
for (T value : values) {
result.add(mapper.formatValue(value));
}
editor.putStringSet(getKey(), result);
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import org.solovyev.common.text.ListMapper;
import org.solovyev.common.text.Mapper;
import org.solovyev.common.text.StringMapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
public class CollectionToStringPreference<C extends Collection<T>, T> extends AbstractPreference<C> {
@Nonnull
private final Mapper<C> mapper;
private CollectionToStringPreference(@Nonnull String key, @Nullable C defaultValue, @Nonnull Mapper<C> mapper) {
super(key, defaultValue);
this.mapper = mapper;
}
@Nonnull
public static <T> CollectionToStringPreference<List<T>, T> forList(@Nonnull String key, @Nullable List<T> defaultValue, @Nonnull Mapper<List<T>> mapper) {
return new CollectionToStringPreference<List<T>, T>(key, defaultValue, mapper);
}
@Nonnull
public static <T> CollectionToStringPreference<List<T>, T> forTypedList(@Nonnull String key, @Nullable List<T> defaultValue, @Nonnull Mapper<T> mapper) {
return new CollectionToStringPreference<List<T>, T>(key, defaultValue, ListMapper.newInstance(mapper));
}
@Nonnull
public static CollectionToStringPreference<List<String>, String> forStringList(@Nonnull String key, @Nullable List<String> defaultValue) {
return new CollectionToStringPreference<List<String>, String>(key, defaultValue, ListMapper.newInstance(StringMapper.getInstance()));
}
@Override
protected C getPersistedValue(@Nonnull SharedPreferences preferences) {
return mapper.parseValue(preferences.getString(getKey(), null));
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull C values) {
editor.putString(getKey(), mapper.formatValue(values));
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class FloatPreference extends AbstractPreference<Float> {
private FloatPreference(@Nonnull String key, @Nullable Float defaultValue) {
super(key, defaultValue);
}
@Nonnull
public static FloatPreference of(@Nonnull String key, @Nullable Float defaultValue) {
return new FloatPreference(key, defaultValue);
}
@Override
protected Float getPersistedValue(@Nonnull SharedPreferences preferences) {
return preferences.getFloat(getKey(), -1f);
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Float value) {
editor.putFloat(getKey(), value);
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import org.solovyev.common.text.EnumMapper;
import org.solovyev.common.text.Mapper;
import org.solovyev.common.text.StringMapper;
import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;
public class HashSetPreference<T> extends CollectionSetPreference<Set<T>, T> {
private HashSetPreference(@Nonnull String id, @Nonnull Set<T> defaultValue, @Nonnull Mapper<T> mapper) {
super(id, defaultValue, mapper);
}
@Nonnull
public static HashSetPreference<String> ofStrings(@Nonnull String key, @Nonnull Set<String> defaultValue) {
return new HashSetPreference<String>(key, defaultValue, StringMapper.getInstance());
}
@Nonnull
public static <T> HashSetPreference<T> ofTypedValues(@Nonnull String key, @Nonnull Set<T> defaultValue, @Nonnull Mapper<T> parser) {
return new HashSetPreference<T>(key, defaultValue, parser);
}
@Nonnull
public static <T extends Enum> HashSetPreference<T> ofEnums(@Nonnull String id, @Nonnull Set<T> defaultValue, @Nonnull Class<T> enumType) {
return new HashSetPreference<T>(id, defaultValue, EnumMapper.of(enumType));
}
@Nonnull
@Override
protected Set<T> createCollection(int size) {
return new HashSet<T>(size);
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class IntegerPreference extends AbstractPreference<Integer> {
private IntegerPreference(@Nonnull String key, @Nullable Integer defaultValue) {
super(key, defaultValue);
}
@Nonnull
public static IntegerPreference of(@Nonnull String key, @Nullable Integer defaultValue) {
return new IntegerPreference(key, defaultValue);
}
@Override
protected Integer getPersistedValue(@Nonnull SharedPreferences preferences) {
return preferences.getInt(getKey(), -1);
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Integer value) {
editor.putInt(getKey(), value);
}
}

View File

@ -0,0 +1,155 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import org.solovyev.common.text.Mapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Locale;
import java.util.StringTokenizer;
public class LocalePreference implements Preference<Locale> {
@Nonnull
private final StringPreference<Locale> stringPreference;
private LocalePreference(@Nonnull String id, @Nullable Locale defaultValue, @Nonnull Mapper<Locale> localeMapper) {
this.stringPreference = new StringPreference<Locale>(id, defaultValue, localeMapper);
}
private LocalePreference(@Nonnull String id, @Nullable Locale defaultValue) {
this.stringPreference = new StringPreference<Locale>(id, defaultValue, DefaultLocaleMapper.getInstance());
}
@Nonnull
public static LocalePreference of(@Nonnull String id, @Nullable Locale defaultValue, @Nonnull Mapper<Locale> localeMapper) {
return new LocalePreference(id, defaultValue, localeMapper);
}
@Nonnull
public static LocalePreference of(@Nonnull String id, @Nullable Locale defaultValue) {
return new LocalePreference(id, defaultValue);
}
@Override
@Nonnull
public String getKey() {
return stringPreference.getKey();
}
@Override
public Locale getDefaultValue() {
return stringPreference.getDefaultValue();
}
@Override
public Locale getPreference(@Nonnull SharedPreferences preferences) {
return stringPreference.getPreference(preferences);
}
@Override
public Locale getPreferenceNoError(@Nonnull SharedPreferences preferences) {
return stringPreference.getPreferenceNoError(preferences);
}
@Override
public void putDefault(@Nonnull SharedPreferences preferences) {
stringPreference.putDefault(preferences);
}
@Override
public void putPreference(@Nonnull SharedPreferences preferences, @Nullable Locale value) {
stringPreference.putPreference(preferences, value);
}
@Override
public boolean isSet(@Nonnull SharedPreferences preferences) {
return stringPreference.isSet(preferences);
}
@Override
public boolean tryPutDefault(@Nonnull SharedPreferences preferences) {
return stringPreference.tryPutDefault(preferences);
}
@Override
public boolean isSameKey(@Nonnull String key) {
return stringPreference.isSameKey(key);
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
private static final class DefaultLocaleMapper implements Mapper<Locale> {
@Nonnull
private static final String delimiter = ";";
@Nonnull
private static Mapper<Locale> instance = new DefaultLocaleMapper();
private DefaultLocaleMapper() {
}
@Nonnull
public static Mapper<Locale> getInstance() {
return instance;
}
@Override
public String formatValue(@Nullable Locale locale) throws IllegalArgumentException {
assert locale != null;
return locale.getLanguage() + delimiter + locale.getCountry() + delimiter + locale.getVariant();
}
@Override
public Locale parseValue(@Nullable String s) throws IllegalArgumentException {
final StringTokenizer st = new StringTokenizer(s, delimiter, false);
final String language = st.nextToken();
final String country;
if (st.hasMoreTokens()) {
country = st.nextToken();
} else {
country = "";
}
final String variant;
if (st.hasMoreTokens()) {
variant = st.nextToken();
} else {
variant = "";
}
return new Locale(language, country, variant);
}
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class LongPreference extends AbstractPreference<Long> {
private LongPreference(@Nonnull String key, @Nullable Long defaultValue) {
super(key, defaultValue);
}
@Nonnull
public static LongPreference of(@Nonnull String key, @Nullable Long defaultValue) {
return new LongPreference(key, defaultValue);
}
@Override
protected Long getPersistedValue(@Nonnull SharedPreferences preferences) {
return preferences.getLong(getKey(), -1);
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Long value) {
editor.putLong(getKey(), value);
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import org.solovyev.common.interval.Interval;
import org.solovyev.common.text.Mapper;
import org.solovyev.common.text.NumberIntervalMapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public final class NumberIntervalPreference<N extends Number & Comparable<N>> extends AbstractPreference<Interval<N>> {
@Nonnull
private final Mapper<Interval<N>> mapper;
private NumberIntervalPreference(@Nonnull String key, @Nullable Interval<N> defaultValue, @Nonnull Class<N> clazz) {
super(key, defaultValue);
this.mapper = NumberIntervalMapper.of(clazz);
}
@Nonnull
public static <N extends Number & Comparable<N>> NumberIntervalPreference<N> of(@Nonnull String key, @Nullable Interval<N> defaultValue, @Nonnull Class<N> clazz) {
return new NumberIntervalPreference<N>(key, defaultValue, clazz);
}
@Override
protected Interval<N> getPersistedValue(@Nonnull SharedPreferences preferences) {
final String result = preferences.getString(getKey(), null);
if (result == null) {
return null;
} else {
return mapper.parseValue(result);
}
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull Interval<N> value) {
editor.putString(getKey(), mapper.formatValue(value));
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import org.solovyev.common.text.Mapper;
import org.solovyev.common.text.NumberMapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class NumberToStringPreference<N extends Number> extends AbstractPreference<N> {
@Nonnull
private final Mapper<N> mapper;
private NumberToStringPreference(@Nonnull String key, @Nullable N defaultValue, @Nonnull Class<N> clazz) {
super(key, defaultValue);
this.mapper = NumberMapper.of(clazz);
}
@Nonnull
public static <N extends Number> NumberToStringPreference<N> of(@Nonnull String key, @Nullable N defaultValue, @Nonnull Class<N> clazz) {
return new NumberToStringPreference<N>(key, defaultValue, clazz);
}
@Override
protected N getPersistedValue(@Nonnull SharedPreferences preferences) {
return mapper.parseValue(preferences.getString(getKey(), "0"));
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull N value) {
editor.putString(getKey(), mapper.formatValue(value));
}
}

View File

@ -0,0 +1,102 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Class for working with android preferences: can save and load preferences, convert them to custom java objects
* and use default value;
*
* @param <T> type of java object preference
*/
public interface Preference<T> {
/**
* Method returns key of preference used in android: the key with which current preference is saved in persistence
*
* @return android preference key
*/
@Nonnull
String getKey();
/**
* @return default preference value, may be null
*/
T getDefaultValue();
/**
* NOTE: this method can throw runtime exceptions if errors occurred while extracting preferences values
*
* @param preferences application preferences
* @return value from preference, default value if no value in preference was found
*/
T getPreference(@Nonnull SharedPreferences preferences);
/**
* NOTE: this method SHOULD not throw any runtime exceptions BUT return default value if any error occurred
*
* @param preferences application preferences
* @return value from preference, default value if no value in preference was found or error occurred
*/
T getPreferenceNoError(@Nonnull SharedPreferences preferences);
/**
* Method puts (saves) preference represented by <code>value</code> in <code>preferences</code> container
*
* @param preferences preferences container
* @param value value to be saved
*/
void putPreference(@Nonnull SharedPreferences preferences, @Nullable T value);
/**
* Method saves default value in <code>preferences</code> container.
* Should behave exactly as <code>p.putPreference(preferences, p.getDefaultValue())</code>
*
* @param preferences preferences container
*/
void putDefault(@Nonnull SharedPreferences preferences);
/**
* @param preferences preferences container
* @return true if any value is saved in preferences container, false - otherwise
*/
boolean isSet(@Nonnull SharedPreferences preferences);
/**
* Method applies default value to preference only if explicit value is not set
*
* @param preferences preferences container
* @return true if default values have been applied, false otherwise
*/
boolean tryPutDefault(@Nonnull SharedPreferences preferences);
/**
* @param key preference key
* @return true if current preferences has the same key
*/
boolean isSameKey(@Nonnull String key);
}

View File

@ -0,0 +1,114 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import org.solovyev.common.text.NumberMapper;
import javax.annotation.Nonnull;
/* The following code was written by Matthew Wiggins
* and is released under the APACHE 2.0 license
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
public class SeekBarPreference extends AbstractDialogPreference<Integer> implements SeekBar.OnSeekBarChangeListener {
private int max = 0;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs, "50", true, NumberMapper.of(Integer.class));
max = attrs.getAttributeIntValue(androidns, "max", 100);
}
@Override
protected LinearLayout.LayoutParams getParams() {
return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
@Nonnull
@Override
protected View createPreferenceView(@Nonnull Context context) {
final SeekBar result = new SeekBar(context);
result.setOnSeekBarChangeListener(this);
return result;
}
@Override
protected void initPreferenceView(@Nonnull View v, Integer value) {
((SeekBar) v).setMax(max);
if (value != null) {
((SeekBar) v).setProgress(value);
setValueText(value);
}
}
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
setValueText(value);
persistValue(value);
}
private void setValueText(int value) {
String t = String.valueOf(value);
final String valueText = getValueText();
updateValueText(valueText == null ? t : t.concat(valueText));
}
public void onStartTrackingTouch(SeekBar seek) {
}
public void onStopTrackingTouch(SeekBar seek) {
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getProgress() {
final Integer value = getValue();
return value == null ? 0 : value;
}
public void setProgress(int progress) {
setValue(progress);
final View preferenceView = getPreferenceView();
if (preferenceView != null) {
((SeekBar) preferenceView).setProgress(progress);
}
}
}

View File

@ -0,0 +1,73 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.prefs;
import android.content.SharedPreferences;
import org.solovyev.common.text.EnumMapper;
import org.solovyev.common.text.Mapper;
import org.solovyev.common.text.StringMapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* {@link Preference} implementation which uses {@link String} way of storing object in persistence.
* This class provides methods for mapping real java objects to String and vice versa.
*
* @param <T>
*/
public final class StringPreference<T> extends AbstractPreference<T> {
@Nonnull
private final Mapper<T> mapper;
public StringPreference(@Nonnull String key, @Nullable T defaultValue, @Nonnull Mapper<T> mapper) {
super(key, defaultValue);
this.mapper = mapper;
}
@Nonnull
public static StringPreference<String> of(@Nonnull String key, @Nullable String defaultValue) {
return new StringPreference<String>(key, defaultValue, StringMapper.getInstance());
}
@Nonnull
public static <T> StringPreference<T> ofTypedValue(@Nonnull String key, @Nullable String defaultValue, @Nonnull Mapper<T> mapper) {
return new StringPreference<T>(key, mapper.parseValue(defaultValue), mapper);
}
@Nonnull
public static <T extends Enum> StringPreference<T> ofEnum(@Nonnull String key, @Nullable T defaultValue, @Nonnull Class<T> enumType) {
return new StringPreference<T>(key, defaultValue, EnumMapper.of(enumType));
}
@Override
protected T getPersistedValue(@Nonnull SharedPreferences preferences) {
return mapper.parseValue(preferences.getString(getKey(), null));
}
@Override
protected void putPersistedValue(@Nonnull SharedPreferences.Editor editor, @Nonnull T value) {
editor.putString(getKey(), mapper.formatValue(value));
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2013 serso aka se.solovyev
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Contact details
~
~ Email: se.solovyev@gmail.com
~ Site: http://se.solovyev.org
-->
<resources>
<declare-styleable name="NumberRangeSeekBar">
<attr name="boundaries" format="string" />
<attr name="step" format="string" />
</declare-styleable>
</resources>