1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.chromium.latency.walt; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.os.Bundle; 22 import android.support.annotation.NonNull; 23 import android.support.v7.preference.DialogPreference; 24 import android.support.v7.preference.PreferenceDialogFragmentCompat; 25 import android.util.AttributeSet; 26 import android.view.View; 27 28 public class NumberPickerPreference extends DialogPreference { 29 private int currentValue; 30 private int maxValue; 31 private int minValue; 32 33 private static final int DEFAULT_value = 0; 34 private static final int DEFAULT_maxValue = 0; 35 private static final int DEFAULT_minValue = 0; 36 37 private final String defaultSummary; 38 NumberPickerPreference(Context context, AttributeSet attrs)39 public NumberPickerPreference(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 42 defaultSummary = getSummary().toString(); 43 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumberPickerPreference); 44 45 try { 46 maxValue = a.getInt(R.styleable.NumberPickerPreference_maxValue, DEFAULT_maxValue); 47 minValue = a.getInt(R.styleable.NumberPickerPreference_minValue, DEFAULT_minValue); 48 } finally { 49 a.recycle(); 50 } 51 52 setDialogLayoutResource(R.layout.numberpicker_dialog); 53 setPositiveButtonText(android.R.string.ok); 54 setNegativeButtonText(android.R.string.cancel); 55 56 setDialogIcon(null); 57 58 } 59 getValue()60 public int getValue() { 61 return currentValue; 62 } 63 setValue(int value)64 public void setValue(int value) { 65 currentValue = value; 66 persistInt(currentValue); 67 setSummary(String.format(defaultSummary, getValue())); 68 } 69 70 @Override onGetDefaultValue(TypedArray a, int index)71 protected Object onGetDefaultValue(TypedArray a, int index) { 72 return a.getInt(index, DEFAULT_value); 73 } 74 75 @Override onSetInitialValue(boolean restorePersistedValue, Object defaultValue)76 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { 77 setValue(restorePersistedValue ? getPersistedInt(currentValue) : (Integer) defaultValue); 78 } 79 80 public static class NumberPickerPreferenceDialogFragmentCompat 81 extends PreferenceDialogFragmentCompat { 82 private static final String SAVE_STATE_VALUE = "NumberPickerPreferenceDialogFragment.value"; 83 private CustomNumberPicker picker; 84 private int currentValue = 1; 85 NumberPickerPreferenceDialogFragmentCompat()86 public NumberPickerPreferenceDialogFragmentCompat() { 87 } 88 newInstance(String key)89 public static NumberPickerPreferenceDialogFragmentCompat newInstance(String key) { 90 NumberPickerPreferenceDialogFragmentCompat fragment = 91 new NumberPickerPreferenceDialogFragmentCompat(); 92 Bundle b = new Bundle(1); 93 b.putString(ARG_KEY, key); 94 fragment.setArguments(b); 95 return fragment; 96 } 97 onCreate(Bundle savedInstanceState)98 public void onCreate(Bundle savedInstanceState) { 99 super.onCreate(savedInstanceState); 100 if (savedInstanceState == null) { 101 currentValue = getNumberPickerPreference().getValue(); 102 } else { 103 currentValue = savedInstanceState.getInt(SAVE_STATE_VALUE); 104 } 105 } 106 onSaveInstanceState(@onNull Bundle outState)107 public void onSaveInstanceState(@NonNull Bundle outState) { 108 outState.putInt(SAVE_STATE_VALUE, currentValue); 109 } 110 getNumberPickerPreference()111 private NumberPickerPreference getNumberPickerPreference() { 112 return (NumberPickerPreference) this.getPreference(); 113 } 114 115 @Override onBindDialogView(View view)116 protected void onBindDialogView(View view) { 117 super.onBindDialogView(view); 118 picker = (CustomNumberPicker) view.findViewById(R.id.numpicker_pref); 119 picker.setMaxValue(getNumberPickerPreference().maxValue); 120 picker.setMinValue(getNumberPickerPreference().minValue); 121 picker.setValue(currentValue); 122 } 123 124 @Override onDialogClosed(boolean b)125 public void onDialogClosed(boolean b) { 126 if (b) { 127 int value = picker.getValue(); 128 if(getPreference().callChangeListener(value)) { 129 getNumberPickerPreference().setValue(value); 130 } 131 } 132 } 133 } 134 } 135