1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.accessibility; 18 19 import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_START_PREVIEW; 20 import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_STOP_PREVIEW; 21 import static com.android.settings.accessibility.FlashNotificationsUtil.DEFAULT_SCREEN_FLASH_COLOR; 22 import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR; 23 import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE; 24 import static com.android.settings.accessibility.FlashNotificationsUtil.TYPE_LONG_PREVIEW; 25 26 import android.app.Dialog; 27 import android.content.Intent; 28 import android.graphics.Color; 29 import android.os.Bundle; 30 import android.os.UserHandle; 31 import android.view.View; 32 33 import androidx.annotation.ColorInt; 34 import androidx.annotation.NonNull; 35 import androidx.annotation.Nullable; 36 import androidx.appcompat.app.AlertDialog; 37 import androidx.fragment.app.DialogFragment; 38 39 import com.android.settings.R; 40 41 import java.util.Timer; 42 import java.util.TimerTask; 43 import java.util.function.Consumer; 44 45 46 /** 47 * DialogFragment for Screen flash notification color picker. 48 */ 49 public class ScreenFlashNotificationColorDialogFragment extends DialogFragment implements 50 ColorSelectorLayout.OnCheckedChangeListener { 51 52 private static final int PREVIEW_LONG_TIME_MS = 5000; 53 private static final int BETWEEN_STOP_AND_START_DELAY_MS = 250; 54 private static final int MARGIN_FOR_STOP_DELAY_MS = 100; 55 56 @ColorInt 57 private int mCurrentColor = Color.TRANSPARENT; 58 private Consumer<Integer> mConsumer; 59 60 private Timer mTimer = null; 61 private Boolean mIsPreview = false; 62 getInstance(int initialColor, Consumer<Integer> colorConsumer)63 static ScreenFlashNotificationColorDialogFragment getInstance(int initialColor, 64 Consumer<Integer> colorConsumer) { 65 final ScreenFlashNotificationColorDialogFragment result = 66 new ScreenFlashNotificationColorDialogFragment(); 67 result.mCurrentColor = initialColor; 68 result.mConsumer = colorConsumer != null ? colorConsumer : i -> { 69 }; 70 return result; 71 } 72 73 @NonNull 74 @Override onCreateDialog(@ullable Bundle savedInstanceState)75 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { 76 final View dialogView = getLayoutInflater().inflate(R.layout.layout_color_selector_dialog, 77 null); 78 79 final ColorSelectorLayout colorSelectorLayout = dialogView.findViewById( 80 R.id.color_selector_preference); 81 if (colorSelectorLayout != null) { 82 colorSelectorLayout.setOnCheckedChangeListener(this); 83 colorSelectorLayout.setCheckedColor(mCurrentColor); 84 } 85 86 final AlertDialog createdDialog = new AlertDialog.Builder(getContext()) 87 .setView(dialogView) 88 .setTitle(R.string.screen_flash_notification_color_title) 89 .setNeutralButton(R.string.flash_notifications_preview, null) 90 .setNegativeButton(R.string.color_selector_dialog_cancel, (dialog, which) -> { 91 }) 92 .setPositiveButton(R.string.color_selector_dialog_done, (dialog, which) -> { 93 mCurrentColor = colorSelectorLayout.getCheckedColor(DEFAULT_SCREEN_FLASH_COLOR); 94 mConsumer.accept(mCurrentColor); 95 }) 96 .create(); 97 createdDialog.setOnShowListener( 98 dialogInterface -> createdDialog.getButton(AlertDialog.BUTTON_NEUTRAL) 99 .setOnClickListener(v -> showColor())); 100 101 return createdDialog; 102 } 103 104 @Override onPause()105 public void onPause() { 106 super.onPause(); 107 cancelPreviewTask(); 108 } 109 110 @Override onCheckedChanged(ColorSelectorLayout group)111 public void onCheckedChanged(ColorSelectorLayout group) { 112 mCurrentColor = group.getCheckedColor(DEFAULT_SCREEN_FLASH_COLOR); 113 if (mIsPreview) { 114 showColor(); 115 } 116 } 117 showColor()118 private void showColor() { 119 int startDelay = 0; 120 121 synchronized (this) { 122 if (mTimer != null) mTimer.cancel(); 123 124 mTimer = createTimer(); 125 if (mIsPreview) { 126 mTimer.schedule(getStopTask(), 0); 127 startDelay = BETWEEN_STOP_AND_START_DELAY_MS; 128 } 129 mTimer.schedule(getStartTask(), startDelay); 130 mTimer.schedule(getStopTask(), 131 startDelay + PREVIEW_LONG_TIME_MS + MARGIN_FOR_STOP_DELAY_MS); 132 } 133 } 134 getStartTask()135 private TimerTask getStartTask() { 136 return new TimerTask() { 137 @Override 138 public void run() { 139 synchronized (this) { 140 startPreviewLocked(); 141 } 142 } 143 }; 144 } 145 146 private TimerTask getStopTask() { 147 return new TimerTask() { 148 @Override 149 public void run() { 150 synchronized (this) { 151 stopPreviewLocked(); 152 } 153 } 154 }; 155 } 156 157 private void cancelPreviewTask() { 158 synchronized (this) { 159 if (mTimer != null) mTimer.cancel(); 160 stopPreviewLocked(); 161 } 162 } 163 164 private void startPreviewLocked() { 165 if (getContext() == null) return; 166 167 mIsPreview = true; 168 Intent intent = new Intent(ACTION_FLASH_NOTIFICATION_START_PREVIEW); 169 intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_LONG_PREVIEW); 170 intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR, mCurrentColor); 171 getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM); 172 } 173 174 private void stopPreviewLocked() { 175 if (getContext() == null) return; 176 177 Intent stopIntent = new Intent(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW); 178 getContext().sendBroadcastAsUser(stopIntent, UserHandle.SYSTEM); 179 mIsPreview = false; 180 } 181 182 Timer createTimer() { 183 return new Timer(); 184 } 185 } 186