Flash Notifications color dialog crashs if configuration changes

Root Cause:
When configuration changes, it will recreate fragment then call no args constructor which lead to null pointer exception for error passing parameters in getInstance().

Solution:
* Use setArguments() to pass parameters.
* Move settings key update action into fragment.
* Obsever settings key changes to update summary accordingly.

Bug: 349049233
Test: atest ScreenFlashNotificationColorDialogFragmentTest ScreenFlashNotificationPreferenceControllerTest
Flag: EXEMPT bugfix
Change-Id: Ided0853cf9a989e2f288a85c1aa709f71f7e1cd6
This commit is contained in:
jasonwshsu
2024-08-02 19:11:47 +08:00
parent 93b093033a
commit e08c1a5f92
4 changed files with 97 additions and 38 deletions

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();
}
}
}
}