1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 19:52:17 -05:00

Made ColorPickerDialog a real dialog by subclassing AlertDialog

This way it's easy to save/restore the dialog's state, e.g. on orientation changes.
This commit is contained in:
cketti 2012-06-09 01:28:43 +02:00
parent 9b77aad8b7
commit fb1bd9f913
2 changed files with 85 additions and 36 deletions

View File

@ -10,23 +10,27 @@
package com.fsck.k9.activity;
import com.fsck.k9.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.*;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.fsck.k9.view.ColorPickerBox;
public class ColorPickerDialog {
public class ColorPickerDialog extends AlertDialog {
private static final String TAG = ColorPickerDialog.class.getSimpleName();
private static final String BUNDLE_KEY_PARENT_BUNDLE = "parent";
private static final String BUNDLE_KEY_COLOR_OLD = "color_old";
private static final String BUNDLE_KEY_COLOR_NEW = "color_new";
public interface OnColorChangedListener {
void colorChanged(int color);
}
AlertDialog dialog;
OnColorChangedListener listener;
View viewHue;
ColorPickerBox viewBox;
@ -45,13 +49,10 @@ public class ColorPickerDialog {
float sizeUiPx; // diset di constructor
public ColorPickerDialog(Context context, OnColorChangedListener listener, int color) {
super(context);
this.listener = listener;
this.colorOld = color;
this.colorNew = color;
Color.colorToHSV(color, tmp01);
hue = tmp01[0];
sat = tmp01[1];
val = tmp01[2];
initColor(color);
onedp = context.getResources().getDimension(R.dimen.colorpicker_onedp);
sizeUiPx = sizeUiDp * onedp;
@ -65,11 +66,7 @@ public class ColorPickerDialog {
viewColorNew = view.findViewById(R.id.colorpicker_colorNew);
viewSpyglass = (ImageView) view.findViewById(R.id.colorpicker_spyglass);
placeArrow();
placeSpyglass();
viewBox.setHue(hue);
viewColorOld.setBackgroundColor(color);
viewColorNew.setBackgroundColor(color);
updateView();
viewHue.setOnTouchListener(new View.OnTouchListener() {
@Override
@ -125,25 +122,69 @@ public class ColorPickerDialog {
}
});
dialog = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(R.string.okay_action, new DialogInterface.OnClickListener() {
this.setView(view);
this.setButton(BUTTON_POSITIVE, context.getString(R.string.okay_action),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (ColorPickerDialog.this.listener != null) {
ColorPickerDialog.this.listener.colorChanged(colorNew);
}
}
})
.setNegativeButton(R.string.cancel_action, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (ColorPickerDialog.this.listener != null) {
}
}
})
.create();
});
this.setButton(BUTTON_NEGATIVE, context.getString(R.string.cancel_action), (OnClickListener) null);
}
private void updateView() {
placeArrow();
placeSpyglass();
viewBox.setHue(hue);
viewColorOld.setBackgroundColor(colorOld);
viewColorNew.setBackgroundColor(colorNew);
}
private void initColor(int color) {
colorNew = color;
colorOld = color;
Color.colorToHSV(color, tmp01);
hue = tmp01[0];
sat = tmp01[1];
val = tmp01[2];
}
public void setColor(int color) {
initColor(color);
updateView();
}
@Override
public Bundle onSaveInstanceState() {
Bundle parentBundle = super.onSaveInstanceState();
Bundle savedInstanceState = new Bundle();
savedInstanceState.putBundle(BUNDLE_KEY_PARENT_BUNDLE, parentBundle);
savedInstanceState.putInt(BUNDLE_KEY_COLOR_OLD, colorOld);
savedInstanceState.putInt(BUNDLE_KEY_COLOR_NEW, colorNew);
return savedInstanceState;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
Bundle parentBundle = savedInstanceState.getBundle(BUNDLE_KEY_PARENT_BUNDLE);
super.onRestoreInstanceState(parentBundle);
int color = savedInstanceState.getInt(BUNDLE_KEY_COLOR_NEW);
// Sets colorOld, colorNew to color and initializes hue, sat, val from color
initColor(color);
// Now restore the real colorOld value
colorOld = savedInstanceState.getInt(BUNDLE_KEY_COLOR_OLD);
updateView();
}
@SuppressWarnings("deprecation")
@ -174,12 +215,4 @@ public class ColorPickerDialog {
tmp01[2] = val;
return Color.HSVToColor(tmp01);
}
public void show() {
dialog.show();
}
public Dialog create() {
return dialog;
}
}

View File

@ -833,7 +833,7 @@ public class AccountSettings extends K9PreferenceActivity {
mAccount.setChipColor(color);
}
},
mAccount.getChipColor()).create();
mAccount.getChipColor());
break;
}
@ -844,7 +844,7 @@ public class AccountSettings extends K9PreferenceActivity {
mAccount.getNotificationSetting().setLedColor(color);
}
},
mAccount.getNotificationSetting().getLedColor()).create();
mAccount.getNotificationSetting().getLedColor());
break;
}
@ -853,6 +853,22 @@ public class AccountSettings extends K9PreferenceActivity {
return dialog;
}
@Override
public void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_COLOR_PICKER_ACCOUNT: {
ColorPickerDialog colorPicker = (ColorPickerDialog) dialog;
colorPicker.setColor(mAccount.getChipColor());
break;
}
case DIALOG_COLOR_PICKER_LED: {
ColorPickerDialog colorPicker = (ColorPickerDialog) dialog;
colorPicker.setColor(mAccount.getNotificationSetting().getLedColor());
break;
}
}
}
public void onChooseAutoExpandFolder() {
Intent selectIntent = new Intent(this, ChooseFolder.class);
selectIntent.putExtra(ChooseFolder.EXTRA_ACCOUNT, mAccount.getUuid());