Blame client/Android/Studio/freeRDPCore/src/main/java/com/freerdp/freerdpcore/utils/IntEditTextPreference.java

Packit 1fb8d4
/*
Packit 1fb8d4
   EditTextPreference to store/load integer values
Packit 1fb8d4
Packit 1fb8d4
   Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit 1fb8d4
Packit Service 5a9772
   This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
Packit Service 5a9772
   If a copy of the MPL was not distributed with this file, You can obtain one at
Packit Service 5a9772
   http://mozilla.org/MPL/2.0/.
Packit 1fb8d4
*/
Packit 1fb8d4
Packit 1fb8d4
package com.freerdp.freerdpcore.utils;
Packit 1fb8d4
Packit 1fb8d4
import android.content.Context;
Packit 1fb8d4
import android.content.res.TypedArray;
Packit 1fb8d4
import android.preference.EditTextPreference;
Packit 1fb8d4
import android.util.AttributeSet;
Packit 1fb8d4
Packit 1fb8d4
import com.freerdp.freerdpcore.R;
Packit 1fb8d4
Packit Service 5a9772
public class IntEditTextPreference extends EditTextPreference
Packit Service 5a9772
{
Packit Service 5a9772
Packit Service 5a9772
	private int bounds_min, bounds_max, bounds_default;
Packit Service 5a9772
Packit Service 5a9772
	public IntEditTextPreference(Context context)
Packit Service 5a9772
	{
Packit Service 5a9772
		super(context);
Packit Service 5a9772
		init(context, null);
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	public IntEditTextPreference(Context context, AttributeSet attrs)
Packit Service 5a9772
	{
Packit Service 5a9772
		super(context, attrs);
Packit Service 5a9772
		init(context, attrs);
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle)
Packit Service 5a9772
	{
Packit Service 5a9772
		super(context, attrs, defStyle);
Packit Service 5a9772
		init(context, attrs);
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	private void init(Context context, AttributeSet attrs)
Packit Service 5a9772
	{
Packit Service 5a9772
		if (attrs != null)
Packit Service 5a9772
		{
Packit Service 5a9772
			TypedArray array =
Packit Service 5a9772
			    context.obtainStyledAttributes(attrs, R.styleable.IntEditTextPreference, 0, 0);
Packit Service 5a9772
			bounds_min =
Packit Service 5a9772
			    array.getInt(R.styleable.IntEditTextPreference_bounds_min, Integer.MIN_VALUE);
Packit Service 5a9772
			bounds_max =
Packit Service 5a9772
			    array.getInt(R.styleable.IntEditTextPreference_bounds_max, Integer.MAX_VALUE);
Packit Service 5a9772
			bounds_default = array.getInt(R.styleable.IntEditTextPreference_bounds_default, 0);
Packit Service 5a9772
			array.recycle();
Packit Service 5a9772
		}
Packit Service 5a9772
		else
Packit Service 5a9772
		{
Packit Service 5a9772
			bounds_min = Integer.MIN_VALUE;
Packit Service 5a9772
			bounds_max = Integer.MAX_VALUE;
Packit Service 5a9772
			bounds_default = 0;
Packit Service 5a9772
		}
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	public void setBounds(int min, int max, int defaultValue)
Packit Service 5a9772
	{
Packit Service 5a9772
		bounds_min = min;
Packit Service 5a9772
		bounds_max = max;
Packit Service 5a9772
		bounds_default = defaultValue;
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	@Override protected String getPersistedString(String defaultReturnValue)
Packit Service 5a9772
	{
Packit Service 5a9772
		int value = getPersistedInt(-1);
Packit Service 5a9772
		if (value > bounds_max || value < bounds_min)
Packit Service 5a9772
			value = bounds_default;
Packit Service 5a9772
		return String.valueOf(value);
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	@Override protected boolean persistString(String value)
Packit Service 5a9772
	{
Packit Service 5a9772
		return persistInt(Integer.valueOf(value));
Packit Service 5a9772
	}
Packit Service 5a9772
Packit Service 5a9772
	@Override protected void onDialogClosed(boolean positiveResult)
Packit Service 5a9772
	{
Packit Service 5a9772
		if (positiveResult)
Packit Service 5a9772
		{
Packit Service 5a9772
			// prevent exception when an empty value is persisted
Packit Service 5a9772
			if (getEditText().getText().length() == 0)
Packit Service 5a9772
				getEditText().setText("0");
Packit Service 5a9772
Packit Service 5a9772
			// check bounds
Packit Service 5a9772
			int value = Integer.valueOf(getEditText().getText().toString());
Packit Service 5a9772
			if (value > bounds_max || value < bounds_min)
Packit Service 5a9772
				value = bounds_default;
Packit Service 5a9772
			getEditText().setText(String.valueOf(value));
Packit Service 5a9772
		}
Packit Service 5a9772
Packit Service 5a9772
		super.onDialogClosed(positiveResult);
Packit Service 5a9772
	}
Packit 1fb8d4
}