Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed May 22, 2015
2 parents 5ee8039 + 136cf97 commit d975bb9
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.EditTextPreference;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -47,6 +48,8 @@ public MaterialEditTextPreference(Context context, AttributeSet attrs) {
mColor = DialogUtils.resolveColor(context, R.attr.colorAccent, fallback);

mEditText = new AppCompatEditText(context, attrs);
// Give it an ID so it can be saved/restored
mEditText.setId(android.R.id.edit);
mEditText.setEnabled(true);
}

Expand All @@ -56,22 +59,26 @@ public MaterialEditTextPreference(Context context) {

@Override
protected void onAddEditTextToDialogView(@NonNull View dialogView, @NonNull EditText editText) {
if (editText.getParent() != null)
((ViewGroup) mEditText.getParent()).removeView(editText);
((ViewGroup) dialogView).addView(editText, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

@Override
protected void onBindDialogView(@NonNull View view) {
mEditText.setText("");
if (getText() != null)
mEditText.setText(getText());
ViewParent oldParent = mEditText.getParent();
EditText editText = mEditText;
editText.setText(getText());

// Initialize cursor to end of text
if (editText.getText().length() > 0) {
editText.setSelection(editText.length());
}

ViewParent oldParent = editText.getParent();
if (oldParent != view) {
if (oldParent != null)
((ViewGroup) oldParent).removeView(mEditText);
onAddEditTextToDialogView(view, mEditText);
if (oldParent != null) {
((ViewGroup) oldParent).removeView(editText);
}
onAddEditTextToDialogView(view, editText);
}
}

Expand Down Expand Up @@ -103,14 +110,7 @@ protected void showDialog(Bundle state) {
.positiveText(getPositiveButtonText())
.negativeText(getNegativeButtonText())
.callback(callback)
.dismissListener(this)
.showListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if (mEditText.getText().length() > 0)
mEditText.setSelection(mEditText.length());
}
});
.dismissListener(this);

View layout = LayoutInflater.from(getContext()).inflate(R.layout.md_stub_inputpref, null);
onBindDialogView(layout);
Expand Down Expand Up @@ -172,4 +172,67 @@ public void onActivityDestroy() {
if (mDialog != null && mDialog.isShowing())
mDialog.dismiss();
}

@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
Dialog dialog = getDialog();
if (dialog == null || !dialog.isShowing()) {
return superState;
}

final SavedState myState = new SavedState(superState);
myState.isDialogShowing = true;
myState.dialogBundle = dialog.onSaveInstanceState();
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);
}
}

// From DialogPreference
private static class SavedState extends BaseSavedState {
boolean isDialogShowing;
Bundle dialogBundle;

public SavedState(Parcel source) {
super(source);
isDialogShowing = source.readInt() == 1;
dialogBundle = source.readBundle();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(isDialogShowing ? 1 : 0);
dest.writeBundle(dialogBundle);
}

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];
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.text.TextUtils;
Expand Down Expand Up @@ -122,4 +124,67 @@ public void setValue(String value) {
notifyChanged();
}
}

@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
Dialog dialog = getDialog();
if (dialog == null || !dialog.isShowing()) {
return superState;
}

final SavedState myState = new SavedState(superState);
myState.isDialogShowing = true;
myState.dialogBundle = dialog.onSaveInstanceState();
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);
}
}

// From DialogPreference
private static class SavedState extends BaseSavedState {
boolean isDialogShowing;
Bundle dialogBundle;

public SavedState(Parcel source) {
super(source);
isDialogShowing = source.readInt() == 1;
dialogBundle = source.readBundle();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(isDialogShowing ? 1 : 0);
dest.writeBundle(dialogBundle);
}

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];
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
Expand Down Expand Up @@ -119,4 +121,67 @@ public void onActivityDestroy() {
if (mDialog != null && mDialog.isShowing())
mDialog.dismiss();
}

@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
Dialog dialog = getDialog();
if (dialog == null || !dialog.isShowing()) {
return superState;
}

final SavedState myState = new SavedState(superState);
myState.isDialogShowing = true;
myState.dialogBundle = dialog.onSaveInstanceState();
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);
}
}

// From DialogPreference
private static class SavedState extends BaseSavedState {
boolean isDialogShowing;
Bundle dialogBundle;

public SavedState(Parcel source) {
super(source);
isDialogShowing = source.readInt() == 1;
dialogBundle = source.readBundle();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(isDialogShowing ? 1 : 0);
dest.writeBundle(dialogBundle);
}

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];
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preference_activity_custom);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();

if (getFragmentManager().findFragmentById(R.id.content_frame) == null) {
getFragmentManager().beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
}
}

@Override
Expand Down

0 comments on commit d975bb9

Please sign in to comment.