Merge "Flash Notifications color dialog crashs if configuration changes" into main

This commit is contained in:
Jason Hsu
2024-08-10 08:03:32 +00:00
committed by Android (Google) Code Review
4 changed files with 97 additions and 38 deletions

View File

@@ -28,11 +28,13 @@ import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
@@ -40,8 +42,6 @@ import com.android.settings.R;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Consumer;
/**
* DialogFragment for Screen flash notification color picker.
@@ -49,29 +49,33 @@ import java.util.function.Consumer;
public class ScreenFlashNotificationColorDialogFragment extends DialogFragment implements
ColorSelectorLayout.OnCheckedChangeListener {
private static final int DEFAULT_COLOR = Color.TRANSPARENT;
private static final int PREVIEW_LONG_TIME_MS = 5000;
private static final int BETWEEN_STOP_AND_START_DELAY_MS = 250;
private static final int MARGIN_FOR_STOP_DELAY_MS = 100;
@VisibleForTesting
static final String EXTRA_COLOR = "extra_color";
@ColorInt
private int mCurrentColor = Color.TRANSPARENT;
private Consumer<Integer> mConsumer;
private int mCurrentColor = DEFAULT_COLOR;
private Timer mTimer = null;
private Boolean mIsPreview = false;
static ScreenFlashNotificationColorDialogFragment getInstance(int initialColor,
Consumer<Integer> colorConsumer) {
static ScreenFlashNotificationColorDialogFragment getInstance(int initialColor) {
final ScreenFlashNotificationColorDialogFragment result =
new ScreenFlashNotificationColorDialogFragment();
result.mCurrentColor = initialColor;
result.mConsumer = colorConsumer != null ? colorConsumer : i -> {};
Bundle bundle = new Bundle();
bundle.putInt(EXTRA_COLOR, initialColor);
result.setArguments(bundle);
return result;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
mCurrentColor = getArguments().getInt(EXTRA_COLOR, DEFAULT_COLOR);
final View dialogView = getLayoutInflater().inflate(R.layout.layout_color_selector_dialog,
null);
@@ -90,7 +94,8 @@ public class ScreenFlashNotificationColorDialogFragment extends DialogFragment i
})
.setPositiveButton(R.string.color_selector_dialog_save, (dialog, which) -> {
mCurrentColor = colorSelectorLayout.getCheckedColor(DEFAULT_SCREEN_FLASH_COLOR);
mConsumer.accept(mCurrentColor);
Settings.System.putInt(getContext().getContentResolver(),
Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, mCurrentColor);
})
.create();
createdDialog.setOnShowListener(

View File

@@ -21,10 +21,17 @@ import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.FlashNotificationsUtil.DEFAULT_SCREEN_FLASH_COLOR;
import android.content.Context;
import android.database.ContentObserver;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -32,24 +39,37 @@ import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import java.util.function.Consumer;
/**
* Controller for Screen flash notification.
*/
public class ScreenFlashNotificationPreferenceController extends TogglePreferenceController {
public class ScreenFlashNotificationPreferenceController extends
TogglePreferenceController implements DefaultLifecycleObserver {
private final FlashNotificationColorContentObserver mFlashNotificationColorContentObserver;
private Fragment mParentFragment;
private Preference mPreference;
public ScreenFlashNotificationPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mFlashNotificationColorContentObserver = new FlashNotificationColorContentObserver(
new Handler(mContext.getMainLooper()));
}
public void setParentFragment(Fragment parentFragment) {
this.mParentFragment = parentFragment;
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
mFlashNotificationColorContentObserver.register(mContext);
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
mFlashNotificationColorContentObserver.unregister(mContext);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
@@ -100,14 +120,8 @@ public class ScreenFlashNotificationPreferenceController extends TogglePreferenc
Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR,
DEFAULT_SCREEN_FLASH_COLOR);
final Consumer<Integer> consumer = color -> {
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR, color);
refreshColorSummary();
};
ScreenFlashNotificationColorDialogFragment
.getInstance(initialColor, consumer)
.getInstance(initialColor)
.show(mParentFragment.getParentFragmentManager(),
ScreenFlashNotificationColorDialogFragment.class.getSimpleName());
return true;
@@ -128,4 +142,37 @@ public class ScreenFlashNotificationPreferenceController extends TogglePreferenc
private void refreshColorSummary() {
if (mPreference != null) mPreference.setSummary(getSummary());
}
private final class FlashNotificationColorContentObserver extends ContentObserver {
private final Uri mColorUri = Settings.System.getUriFor(
Settings.System.SCREEN_FLASH_NOTIFICATION_COLOR);
FlashNotificationColorContentObserver(Handler handler) {
super(handler);
}
/**
* Register this observer to given {@link Context}, to be called from lifecycle
* {@code onStart} method.
*/
public void register(@NonNull Context context) {
context.getContentResolver().registerContentObserver(
mColorUri, /* notifyForDescendants= */ false, this);
}
/**
* Unregister this observer from given {@link Context}, to be called from lifecycle
* {@code onStop} method.
*/
public void unregister(@NonNull Context context) {
context.getContentResolver().unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
if (mColorUri.equals(uri)) {
refreshColorSummary();
}
}
}
}