/*
* Copyright (C) 2015 Arles. All rights reserved.
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
*/
package com.arles.swissmanager.utils;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;
import com.arles.swissmanager.R;
/**
* A {@link android.preference.Preference} that displays a number picker as a dialog.
*/
public class NumberPickerPreference extends DialogPreference {
private final int DEFAULT_MAX_VALUE = 10;
private final int DEFAULT_MIN_VALUE = 0;
private int mMin, mMax, mDefault;
private String mMaxExternalKey, mMinExternalKey;
private NumberPicker mNumberPicker;
public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// TypedArray dialogType = context.obtainStyledAttributes(attrs,
// com.android.internal.R.styleable.DialogPreference, 0, 0);
TypedArray dialogType = context.obtainStyledAttributes(attrs, new int[]{Resources.getSystem().getIdentifier("DialogPreference", "styleable", "android")}, 0, 0);
TypedArray numberPickerType = context.obtainStyledAttributes(attrs,
R.styleable.NumberPickerPreference, 0, 0);
mMaxExternalKey = numberPickerType.getString(R.styleable.NumberPickerPreference_maxExternal);
mMinExternalKey = numberPickerType.getString(R.styleable.NumberPickerPreference_minExternal);
mMax = numberPickerType.getInt(R.styleable.NumberPickerPreference_max, DEFAULT_MAX_VALUE);
mMin = numberPickerType.getInt(R.styleable.NumberPickerPreference_min, DEFAULT_MIN_VALUE);
//mDefault = dialogType.getInt(com.android.internal.R.styleable.Preference_defaultValue, mMin);
//mDefault = dialogType.getInt(Resources.getSystem().getIdentifier("Preference_defaultValue", "styleable", "android"), mMin);
mDefault = numberPickerType.getInt(R.styleable.NumberPickerPreference_default_value, mMin);
dialogType.recycle();
numberPickerType.recycle();
}
@Override
protected View onCreateDialogView() {
int max = mMax;
int min = mMin;
// External values
if (mMaxExternalKey != null) {
max = getSharedPreferences().getInt(mMaxExternalKey, mMax);
}
if (mMinExternalKey != null) {
min = getSharedPreferences().getInt(mMinExternalKey, mMin);
}
LayoutInflater inflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.number_picker_dialog, null);
mNumberPicker = (NumberPicker) view.findViewById(R.id.number_picker);
// Initialize state
mNumberPicker.setMaxValue(max);
mNumberPicker.setMinValue(min);
mNumberPicker.setValue(getPersistedInt(mDefault));
mNumberPicker.setWrapSelectorWheel(false);
// No keyboard popup
EditText textInput = (EditText) mNumberPicker.findViewById(Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));
//EditText textInput = (EditText) mNumberPicker.findViewById(com.android.internal.R.id.numberpicker_input);
textInput.setCursorVisible(false);
textInput.setFocusable(false);
textInput.setFocusableInTouchMode(false);
return view;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
persistInt(mNumberPicker.getValue());
}
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
// Check whether this Preference is persistent (continually saved)
if (isPersistent()) {
// No need to save instance state since it's persistent,
// use superclass state
return superState;
}
// Create instance of custom BaseSavedState
final SavedState myState = new SavedState(superState);
// Set the state's value with the class member that holds current
// setting value
myState.value = mDefault;
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
// Check whether we saved the state in onSaveInstanceState
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save the state, so call superclass
super.onRestoreInstanceState(state);
return;
}
// Cast state to custom BaseSavedState and pass to superclass
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
// Set this Preference's widget to reflect the restored state
mNumberPicker.setValue(myState.value);
}
private static class SavedState extends BaseSavedState {
// Member that holds the setting's value
// Change this data type to match the type saved by your Preference
int value;
public SavedState(Parcelable superState) {
super(superState);
}
public SavedState(Parcel source) {
super(source);
// Get the current preference's value
value = source.readInt(); // Change this to read the appropriate data type
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Write the preference's value
dest.writeInt(value); // Change this to write the appropriate data type
}
// Standard creator object using an instance of this class
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}