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; package com.fsck.k9.activity;
import com.fsck.k9.R; import com.fsck.k9.R;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog;
import android.content.*; import android.content.*;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.*; import android.view.*;
import android.widget.*; import android.widget.*;
import com.fsck.k9.view.ColorPickerBox; 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 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 { public interface OnColorChangedListener {
void colorChanged(int color); void colorChanged(int color);
} }
AlertDialog dialog;
OnColorChangedListener listener; OnColorChangedListener listener;
View viewHue; View viewHue;
ColorPickerBox viewBox; ColorPickerBox viewBox;
@ -45,13 +49,10 @@ public class ColorPickerDialog {
float sizeUiPx; // diset di constructor float sizeUiPx; // diset di constructor
public ColorPickerDialog(Context context, OnColorChangedListener listener, int color) { public ColorPickerDialog(Context context, OnColorChangedListener listener, int color) {
super(context);
this.listener = listener; this.listener = listener;
this.colorOld = color;
this.colorNew = color; initColor(color);
Color.colorToHSV(color, tmp01);
hue = tmp01[0];
sat = tmp01[1];
val = tmp01[2];
onedp = context.getResources().getDimension(R.dimen.colorpicker_onedp); onedp = context.getResources().getDimension(R.dimen.colorpicker_onedp);
sizeUiPx = sizeUiDp * onedp; sizeUiPx = sizeUiDp * onedp;
@ -65,11 +66,7 @@ public class ColorPickerDialog {
viewColorNew = view.findViewById(R.id.colorpicker_colorNew); viewColorNew = view.findViewById(R.id.colorpicker_colorNew);
viewSpyglass = (ImageView) view.findViewById(R.id.colorpicker_spyglass); viewSpyglass = (ImageView) view.findViewById(R.id.colorpicker_spyglass);
placeArrow(); updateView();
placeSpyglass();
viewBox.setHue(hue);
viewColorOld.setBackgroundColor(color);
viewColorNew.setBackgroundColor(color);
viewHue.setOnTouchListener(new View.OnTouchListener() { viewHue.setOnTouchListener(new View.OnTouchListener() {
@Override @Override
@ -125,25 +122,69 @@ public class ColorPickerDialog {
} }
}); });
dialog = new AlertDialog.Builder(context) this.setView(view);
.setView(view) this.setButton(BUTTON_POSITIVE, context.getString(R.string.okay_action),
.setPositiveButton(R.string.okay_action, new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (ColorPickerDialog.this.listener != null) { if (ColorPickerDialog.this.listener != null) {
ColorPickerDialog.this.listener.colorChanged(colorNew); 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") @SuppressWarnings("deprecation")
@ -174,12 +215,4 @@ public class ColorPickerDialog {
tmp01[2] = val; tmp01[2] = val;
return Color.HSVToColor(tmp01); 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.setChipColor(color);
} }
}, },
mAccount.getChipColor()).create(); mAccount.getChipColor());
break; break;
} }
@ -844,7 +844,7 @@ public class AccountSettings extends K9PreferenceActivity {
mAccount.getNotificationSetting().setLedColor(color); mAccount.getNotificationSetting().setLedColor(color);
} }
}, },
mAccount.getNotificationSetting().getLedColor()).create(); mAccount.getNotificationSetting().getLedColor());
break; break;
} }
@ -853,6 +853,22 @@ public class AccountSettings extends K9PreferenceActivity {
return dialog; 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() { public void onChooseAutoExpandFolder() {
Intent selectIntent = new Intent(this, ChooseFolder.class); Intent selectIntent = new Intent(this, ChooseFolder.class);
selectIntent.putExtra(ChooseFolder.EXTRA_ACCOUNT, mAccount.getUuid()); selectIntent.putExtra(ChooseFolder.EXTRA_ACCOUNT, mAccount.getUuid());