From 29ec1ef5bc398ad012f2421f730868950c9c92ba Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 30 Jun 2019 10:25:50 +0200 Subject: [PATCH] Dismiss color picker on pause Signed-off-by: M66B --- .../java/eu/faircode/email/ActivityView.java | 2 +- .../faircode/email/ColorPickerDialogEx.java | 35 +++++++++++++++++++ .../eu/faircode/email/FragmentAccount.java | 2 +- .../eu/faircode/email/FragmentCompose.java | 2 +- .../eu/faircode/email/FragmentIdentity.java | 2 +- .../eu/faircode/email/FragmentMessages.java | 2 +- .../java/eu/faircode/email/FragmentRule.java | 2 +- 7 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/ColorPickerDialogEx.java diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index f0fa05a515..316b899a74 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -1092,7 +1092,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB private void onColor(final Intent intent) { int color = intent.getIntExtra("color", -1); int[] colors = getResources().getIntArray(R.array.colorPicker); - ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); + ColorPickerDialog colorPickerDialog = new ColorPickerDialogEx(this); colorPickerDialog.initialize(R.string.title_flag_color, colors, color, 4, colors.length); colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { @Override diff --git a/app/src/main/java/eu/faircode/email/ColorPickerDialogEx.java b/app/src/main/java/eu/faircode/email/ColorPickerDialogEx.java new file mode 100644 index 0000000000..ac5b21e00d --- /dev/null +++ b/app/src/main/java/eu/faircode/email/ColorPickerDialogEx.java @@ -0,0 +1,35 @@ +package eu.faircode.email; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentManager; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.OnLifecycleEvent; + +import com.android.colorpicker.ColorPickerDialog; + +public class ColorPickerDialogEx extends ColorPickerDialog implements LifecycleObserver { + private LifecycleOwner owner; + + private ColorPickerDialogEx() { + } + + ColorPickerDialogEx(LifecycleOwner owner) { + super(); + this.owner = owner; + } + + @Override + public void show(@NonNull FragmentManager manager, @Nullable String tag) { + owner.getLifecycle().addObserver(this); + super.show(manager, tag); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + public void onOwnerPause() { + dismiss(); + this.owner = null; + } +} diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index e732ea8e6e..d19971f91a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -291,7 +291,7 @@ public class FragmentAccount extends FragmentBase { @Override public void onClick(View v) { int[] colors = getContext().getResources().getIntArray(R.array.colorPicker); - ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); + ColorPickerDialog colorPickerDialog = new ColorPickerDialogEx(getViewLifecycleOwner()); colorPickerDialog.initialize(R.string.title_account_color, colors, color, 4, colors.length); colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 0eddd9b716..4977ccae7d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -3220,7 +3220,7 @@ public class FragmentCompose extends FragmentBase { int color = (spans.length > 0 ? spans[0].getForegroundColor() : Color.TRANSPARENT); int[] colors = getContext().getResources().getIntArray(R.array.colorPicker); - ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); + ColorPickerDialog colorPickerDialog = new ColorPickerDialogEx(getViewLifecycleOwner()); colorPickerDialog.initialize(R.string.title_style_color, colors, color, 4, colors.length); colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index b46e35bd8d..5fddf6e2f1 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -262,7 +262,7 @@ public class FragmentIdentity extends FragmentBase { @Override public void onClick(View v) { int[] colors = getContext().getResources().getIntArray(R.array.colorPicker); - ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); + ColorPickerDialog colorPickerDialog = new ColorPickerDialogEx(getViewLifecycleOwner()); colorPickerDialog.initialize(R.string.title_account_color, colors, color, 4, colors.length); colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index eb372374c9..28f164e434 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -1806,7 +1806,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private void onActionFlagColorSelection(int color, final Long id) { int[] colors = getResources().getIntArray(R.array.colorPicker); - ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); + ColorPickerDialog colorPickerDialog = new ColorPickerDialogEx(getViewLifecycleOwner()); colorPickerDialog.initialize(R.string.title_flag_color, colors, color, 4, colors.length); colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index d763fd7cb5..df5b7f0126 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -296,7 +296,7 @@ public class FragmentRule extends FragmentBase { @Override public void onClick(View v) { int[] colors = getContext().getResources().getIntArray(R.array.colorPicker); - ColorPickerDialog colorPickerDialog = new ColorPickerDialog(); + ColorPickerDialog colorPickerDialog = new ColorPickerDialogEx(getViewLifecycleOwner()); colorPickerDialog.initialize(R.string.title_flag_color, colors, color, 4, colors.length); colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() { @Override