/* * Copyright � 2016 TIBCO Software,Inc.All rights reserved. * http://community.jaspersoft.com/project/jaspermobile-android * * Unless you have purchased a commercial license agreement from TIBCO Jaspersoft, * the following license terms apply: * * This program is part of TIBCO Jaspersoft Mobile for Android. * * TIBCO Jaspersoft Mobile is free software:you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation,either version 3of the License,or * (at your option)any later version. * * TIBCO Jaspersoft Mobile is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY;without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with TIBCO Jaspersoft Mobile for Android.If not,see * <http://www.gnu.org/licenses/lgpl>. */ package com.jaspersoft.android.jaspermobile.widget; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.res.TypedArray; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; import android.preference.Preference; import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.EditText; import com.jaspersoft.android.jaspermobile.R; /** * @author Andrew Tivodar * @since 2.0 */ public class AppCompatEditTextPreference extends Preference implements DialogInterface.OnClickListener { private AlertDialog.Builder mBuilder; private CharSequence mDialogTitle; private Dialog mDialog; private EditText mEditText; private String mText; public AppCompatEditTextPreference(Context context) { super(context); } public AppCompatEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); } public AppCompatEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public AppCompatEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return a.getString(index); } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { setText(restoreValue ? getPersistedString(mText) : (String) defaultValue); } @Override public boolean shouldDisableDependents() { return TextUtils.isEmpty(mText) || super.shouldDisableDependents(); } public void setDialogTitle(CharSequence dialogTitle) { mDialogTitle = dialogTitle; } public void setDialogTitle(int dialogTitleResId) { setDialogTitle(getContext().getString(dialogTitleResId)); } public CharSequence getDialogTitle() { return mDialogTitle; } @Override protected void onClick() { if (mDialog != null && mDialog.isShowing()) return; showDialog(null); } protected void showDialog(Bundle state) { Context context = getContext(); mBuilder = new AlertDialog.Builder(context) .setTitle(mDialogTitle) .setPositiveButton(R.string.ok, this) .setNegativeButton(R.string.cancel, this); View contentView = onCreateDialogView(); mBuilder.setView(contentView); // Create the dialog final Dialog dialog = mDialog = mBuilder.create(); if (state != null) { dialog.onRestoreInstanceState(state); } requestInputMethod(dialog); dialog.show(); } private void requestInputMethod(Dialog dialog) { Window window = dialog.getWindow(); window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } protected View onCreateDialogView() { LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext()); View dialogMessageView = inflater.inflate(R.layout.dialog_edit_preference, null); mEditText = (EditText) dialogMessageView.findViewById(R.id.edit_preference_input); mEditText.setText(mText); return dialogMessageView; } public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_POSITIVE) { String value = mEditText.getText().toString(); if (callChangeListener(value)) { setText(value); } } } public void setText(String text) { final boolean wasBlocking = shouldDisableDependents(); mText = text; persistString(text); final boolean isBlocking = shouldDisableDependents(); if (isBlocking != wasBlocking) { notifyDependencyChange(isBlocking); } } public Dialog getDialog() { return mDialog; } @Override protected Parcelable onSaveInstanceState() { final Parcelable superState = super.onSaveInstanceState(); if (mDialog == null || !mDialog.isShowing()) { return superState; } final SavedState myState = new SavedState(superState); myState.isDialogShowing = true; myState.dialogBundle = mDialog.onSaveInstanceState(); myState.text = mText; return myState; } @Override protected void onRestoreInstanceState(Parcelable state) { if (state == null || !state.getClass().equals(SavedState.class)) { // Didn't save state for us in onSaveInstanceState super.onRestoreInstanceState(state); return; } SavedState myState = (SavedState) state; super.onRestoreInstanceState(myState.getSuperState()); if (myState.isDialogShowing) { showDialog(myState.dialogBundle); } setText(myState.text); } private static class SavedState extends BaseSavedState { boolean isDialogShowing; Bundle dialogBundle; String text; public SavedState(Parcel source) { super(source); isDialogShowing = source.readInt() == 1; dialogBundle = source.readBundle(); text = source.readString(); } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeInt(isDialogShowing ? 1 : 0); dest.writeBundle(dialogBundle); dest.writeString(text); } public SavedState(Parcelable superState) { super(superState); } 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]; } }; } }