Implement Flash Notifications UI for Settings app.

Bug: 237628564
Test: make RunSettingsRoboTests ROBOTEST_FILTER=CameraFlashNotificationPreferenceControllerTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ColorSelectorLayoutTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=FlashNotificationsPreferenceControllerTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=FlashNotificationsPreferenceFragmentTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=FlashNotificationsPreviewPreferenceControllerTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=FlashNotificationsPreviewPreferenceTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=FlashNotificationsUtilTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ScreenFlashNotificationColorDialogFragmentTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ScreenFlashNotificationColorTest
Change-Id: I0987590ddfcfd0873ec419db263f6a7eade81844
Signed-off-by: yw.bae <yw.bae@samsung.corp-partner.google.com>
Signed-off-by: Angela Wang <angelala@google.com>
This commit is contained in:
yw.bae
2023-01-06 22:49:14 +09:00
committed by Thomas Lin
parent 8750e58ed6
commit f763c25c1e
68 changed files with 4058 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.FlashNotificationsUtil.SETTING_KEY_CAMERA_FLASH_NOTIFICATION;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
/**
* Controller for Camera flash notification.
*/
public class CameraFlashNotificationPreferenceController extends TogglePreferenceController {
public CameraFlashNotificationPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return FlashNotificationsUtil.isTorchAvailable(mContext)
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY_CAMERA_FLASH_NOTIFICATION, OFF) != OFF;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.System.putInt(mContext.getContentResolver(),
SETTING_KEY_CAMERA_FLASH_NOTIFICATION, (isChecked ? ON : OFF));
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_accessibility;
}
}

View File

@@ -0,0 +1,163 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.android.settings.R;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Layout for Screen flash notification color picker.
*/
public class ColorSelectorLayout extends LinearLayout {
// Holds the checked id. The selection is empty by default
private int mCheckedId = -1;
// Tracks children radio buttons checked state
private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
private ColorSelectorLayout.OnCheckedChangeListener mOnCheckedChangeListener;
private final List<Integer> mRadioButtonResourceIds = Arrays.asList(
R.id.color_radio_button_00,
R.id.color_radio_button_01,
R.id.color_radio_button_02,
R.id.color_radio_button_03,
R.id.color_radio_button_04,
R.id.color_radio_button_05,
R.id.color_radio_button_06,
R.id.color_radio_button_07,
R.id.color_radio_button_08,
R.id.color_radio_button_09,
R.id.color_radio_button_10,
R.id.color_radio_button_11
);
private List<Integer> mColorList;
public ColorSelectorLayout(Context context) {
super(context);
mChildOnCheckedChangeListener = new CheckedStateTracker();
inflate(mContext, R.layout.layout_color_selector, this);
init();
mColorList = Arrays.stream(mContext.getResources()
.getIntArray(R.array.screen_flash_notification_preset_opacity_colors))
.boxed()
.collect(Collectors.toList());
}
public ColorSelectorLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mChildOnCheckedChangeListener = new CheckedStateTracker();
inflate(mContext, R.layout.layout_color_selector, this);
init();
mColorList = Arrays.stream(mContext.getResources()
.getIntArray(R.array.screen_flash_notification_preset_opacity_colors))
.boxed()
.collect(Collectors.toList());
}
private void init() {
for (int resId : mRadioButtonResourceIds) {
RadioButton radioButton = findViewById(resId);
if (radioButton != null) {
radioButton.setOnCheckedChangeListener(mChildOnCheckedChangeListener);
}
}
}
void setOnCheckedChangeListener(ColorSelectorLayout.OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
void setCheckedColor(@ColorInt int color) {
int resId = getResId(mColorList.indexOf(color));
if (resId != NO_ID && resId == mCheckedId) return;
if (mCheckedId != NO_ID) {
setCheckedStateForView(mCheckedId, false);
}
if (resId != NO_ID) {
setCheckedStateForView(resId, true);
}
setCheckedId(resId);
}
int getCheckedColor(int defaultColor) {
int checkedItemIndex = mRadioButtonResourceIds.indexOf(mCheckedId);
if (checkedItemIndex < 0 || checkedItemIndex >= mColorList.size()) {
return defaultColor;
} else {
return mColorList.get(checkedItemIndex);
}
}
private int getResId(int index) {
if (index < 0 || index >= mRadioButtonResourceIds.size()) {
return NO_ID;
} else {
return mRadioButtonResourceIds.get(index);
}
}
private void setCheckedId(int resId) {
mCheckedId = resId;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this);
}
}
private void setCheckedStateForView(int viewId, boolean checked) {
final View checkedView = findViewById(viewId);
if (checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
interface OnCheckedChangeListener {
void onCheckedChanged(ColorSelectorLayout group);
}
private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
return;
}
if (mCheckedId != NO_ID) {
setCheckedStateForView(mCheckedId, false);
}
int id = buttonView.getId();
setCheckedId(id);
}
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
/**
* Controller for flash notifications.
*/
public class FlashNotificationsPreferenceController extends BasePreferenceController {
public FlashNotificationsPreferenceController(Context context, String key) {
super(context, key);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
final int res;
switch (FlashNotificationsUtil.getFlashNotificationsState(mContext)) {
case FlashNotificationsUtil.State.CAMERA:
res = R.string.flash_notifications_summary_on_camera;
break;
case FlashNotificationsUtil.State.SCREEN:
res = R.string.flash_notifications_summary_on_screen;
break;
case FlashNotificationsUtil.State.CAMERA_SCREEN:
res = R.string.flash_notifications_summary_on_camera_and_screen;
break;
case FlashNotificationsUtil.State.OFF:
default:
res = R.string.flash_notifications_summary_off;
break;
}
return mContext.getString(res);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
/**
* Fragment for flash notifications.
*/
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class FlashNotificationsPreferenceFragment extends DashboardFragment {
@Override
protected int getPreferenceScreenResId() {
return R.xml.flash_notifications_settings;
}
@Override
protected String getLogTag() {
return "FlashNotificationsPreferenceFragment";
}
@Override
public int getMetricsCategory() {
// TODO: Flash notifications have to add SettingsEnums.
return 0;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(ScreenFlashNotificationPreferenceController.class).setParentFragment(this);
}
@Override
public int getHelpResource() {
return R.string.help_url_flash_notifications;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.flash_notifications_settings);
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.Context;
import android.util.AttributeSet;
import androidx.preference.Preference;
import com.android.settings.R;
/**
* Preference for Flash notifications preview.
*/
public class FlashNotificationsPreviewPreference extends Preference {
public FlashNotificationsPreviewPreference(Context context) {
super(context);
init();
}
public FlashNotificationsPreviewPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FlashNotificationsPreviewPreference(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public FlashNotificationsPreviewPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
setLayoutResource(R.layout.flash_notification_preview_preference);
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_START_PREVIEW;
import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE;
import static com.android.settings.accessibility.FlashNotificationsUtil.SETTING_KEY_CAMERA_FLASH_NOTIFICATION;
import static com.android.settings.accessibility.FlashNotificationsUtil.SETTING_KEY_SCREEN_FLASH_NOTIFICATION;
import static com.android.settings.accessibility.FlashNotificationsUtil.TYPE_SHORT_PREVIEW;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
/**
* Controller for flash notifications preview.
*/
public class FlashNotificationsPreviewPreferenceController extends
BasePreferenceController implements LifecycleEventObserver {
private Preference mPreference;
private final ContentResolver mContentResolver;
@VisibleForTesting
final ContentObserver mContentObserver = new ContentObserver(
new Handler(Looper.getMainLooper())) {
@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
onSettingChanged();
}
};
public FlashNotificationsPreviewPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mContentResolver = context.getContentResolver();
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
onSettingChanged();
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (getPreferenceKey().equals(preference.getKey())) {
Intent intent = new Intent(ACTION_FLASH_NOTIFICATION_START_PREVIEW);
intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_SHORT_PREVIEW);
mContext.sendBroadcast(intent);
return true;
}
return super.handlePreferenceTreeClick(preference);
}
@Override
public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
@NonNull Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_RESUME) {
mContentResolver.registerContentObserver(
Settings.System.getUriFor(SETTING_KEY_CAMERA_FLASH_NOTIFICATION),
/* notifyForDescendants= */ false, mContentObserver);
mContentResolver.registerContentObserver(
Settings.System.getUriFor(SETTING_KEY_SCREEN_FLASH_NOTIFICATION),
/* notifyForDescendants= */ false, mContentObserver);
} else if (event == Lifecycle.Event.ON_PAUSE) {
mContentResolver.unregisterContentObserver(mContentObserver);
}
}
private void onSettingChanged() {
if (mPreference == null) return;
mPreference.setEnabled(FlashNotificationsUtil.getFlashNotificationsState(mContext)
!= FlashNotificationsUtil.State.OFF);
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
class FlashNotificationsUtil {
static final String LOG_TAG = "FlashNotificationsUtil";
static final String ACTION_FLASH_NOTIFICATION_START_PREVIEW =
"com.android.internal.intent.action.FLASH_NOTIFICATION_START_PREVIEW";
static final String ACTION_FLASH_NOTIFICATION_STOP_PREVIEW =
"com.android.internal.intent.action.FLASH_NOTIFICATION_STOP_PREVIEW";
static final String EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR =
"com.android.internal.intent.extra.FLASH_NOTIFICATION_PREVIEW_COLOR";
static final String EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE =
"com.android.internal.intent.extra.FLASH_NOTIFICATION_PREVIEW_TYPE";
static final String SETTING_KEY_CAMERA_FLASH_NOTIFICATION =
"camera_flash_notification";
static final String SETTING_KEY_SCREEN_FLASH_NOTIFICATION =
"screen_flash_notification";
static final String SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR =
"screen_flash_notification_color_global";
static final int TYPE_SHORT_PREVIEW = 0;
static final int TYPE_LONG_PREVIEW = 1;
static final int DEFAULT_SCREEN_FLASH_COLOR =
ScreenFlashNotificationColor.YELLOW.mColorInt;
@Retention(RetentionPolicy.SOURCE)
@IntDef({
FlashNotificationsUtil.State.OFF,
FlashNotificationsUtil.State.CAMERA,
FlashNotificationsUtil.State.SCREEN,
FlashNotificationsUtil.State.CAMERA_SCREEN,
})
@interface State {
int OFF = 0;
int CAMERA = 1;
int SCREEN = 2;
int CAMERA_SCREEN = 3;
}
static boolean isTorchAvailable(@NonNull Context context) {
// TODO This is duplicated logic of FlashNotificationsController.getCameraId.
final CameraManager cameraManager = context.getSystemService(CameraManager.class);
if (cameraManager == null) return false;
try {
final String[] ids = cameraManager.getCameraIdList();
for (String id : ids) {
final CameraCharacteristics c = cameraManager.getCameraCharacteristics(id);
final Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
if (flashAvailable == null) continue;
final Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
if (lensFacing == null) continue;
if (flashAvailable && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
return true;
}
}
} catch (CameraAccessException ignored) {
Log.w(LOG_TAG, "Failed to get valid camera for camera flash notification.");
}
return false;
}
static ScreenFlashNotificationColor getScreenColor(@ColorInt int colorInt)
throws ScreenColorNotFoundException {
colorInt |= ScreenFlashNotificationColor.ALPHA_MASK;
for (ScreenFlashNotificationColor color : ScreenFlashNotificationColor.values()) {
if (colorInt == color.mOpaqueColorInt) {
return color;
}
}
throw new ScreenColorNotFoundException();
}
@NonNull
static String getColorDescriptionText(@NonNull Context context, @ColorInt int color) {
try {
return context.getString(getScreenColor(color).mStringRes);
} catch (ScreenColorNotFoundException e) {
return "";
}
}
@State
static int getFlashNotificationsState(Context context) {
if (context == null) {
return State.OFF;
}
final boolean isTorchAvailable = FlashNotificationsUtil.isTorchAvailable(context);
final boolean isCameraFlashEnabled = Settings.System.getInt(context.getContentResolver(),
SETTING_KEY_CAMERA_FLASH_NOTIFICATION, State.OFF) != State.OFF;
final boolean isScreenFlashEnabled = Settings.System.getInt(context.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION, State.OFF) != State.OFF;
return ((isTorchAvailable && isCameraFlashEnabled) ? State.CAMERA : State.OFF)
| (isScreenFlashEnabled ? State.SCREEN : State.OFF);
}
static class ScreenColorNotFoundException extends Exception {
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import androidx.annotation.ColorInt;
import androidx.annotation.StringRes;
import com.android.settings.R;
enum ScreenFlashNotificationColor {
BLUE(0x4D0000FE, R.string.screen_flash_color_blue),
AZURE(0x660080FF, R.string.screen_flash_color_azure),
CYAN(0x4D00FFFF, R.string.screen_flash_color_cyan),
SPRING_GREEN(0x6600FF7F, R.string.screen_flash_color_spring_green),
GREEN(0x6600FF01, R.string.screen_flash_color_green),
CHARTREUSE_GREEN(0x6680FF00, R.string.screen_flash_color_chartreuse_green),
YELLOW(0x66FFFF00, R.string.screen_flash_color_yellow),
ORANGE(0x66FF7F00, R.string.screen_flash_color_orange),
RED(0x66FE0000, R.string.screen_flash_color_red),
ROSE(0x4DFF017E, R.string.screen_flash_color_rose),
MAGENTA(0x4DFF00FE, R.string.screen_flash_color_magenta),
VIOLET(0x667F00FF, R.string.screen_flash_color_violet);
static final int ALPHA_MASK = 0xFF000000;
final int mColorInt;
final int mOpaqueColorInt;
final int mStringRes;
ScreenFlashNotificationColor(@ColorInt int colorInt, @StringRes int stringRes) {
this.mColorInt = colorInt;
this.mStringRes = stringRes;
this.mOpaqueColorInt = colorInt | ALPHA_MASK;
}
}

View File

@@ -0,0 +1,179 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_START_PREVIEW;
import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_STOP_PREVIEW;
import static com.android.settings.accessibility.FlashNotificationsUtil.DEFAULT_SCREEN_FLASH_COLOR;
import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR;
import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE;
import static com.android.settings.accessibility.FlashNotificationsUtil.TYPE_LONG_PREVIEW;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
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.
*/
public class ScreenFlashNotificationColorDialogFragment extends DialogFragment implements
ColorSelectorLayout.OnCheckedChangeListener {
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;
@ColorInt
private int mCurrentColor = Color.TRANSPARENT;
private Consumer<Integer> mConsumer;
private Timer mTimer = null;
private Boolean mIsPreview = false;
static ScreenFlashNotificationColorDialogFragment getInstance(int initialColor,
Consumer<Integer> colorConsumer) {
final ScreenFlashNotificationColorDialogFragment result =
new ScreenFlashNotificationColorDialogFragment();
result.mCurrentColor = initialColor;
result.mConsumer = colorConsumer != null ? colorConsumer : i -> {
};
return result;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final View dialogView = getLayoutInflater().inflate(R.layout.layout_color_selector_dialog,
null);
final ColorSelectorLayout colorSelectorLayout = dialogView.findViewById(
R.id.color_selector_preference);
if (colorSelectorLayout != null) {
colorSelectorLayout.setOnCheckedChangeListener(this);
colorSelectorLayout.setCheckedColor(mCurrentColor);
}
final AlertDialog createdDialog = new AlertDialog.Builder(getContext())
.setView(dialogView)
.setTitle(R.string.screen_flash_notification_color_title)
.setNeutralButton(R.string.flash_notifications_preview, null)
.setNegativeButton(R.string.color_selector_dialog_cancel, (dialog, which) -> {
})
.setPositiveButton(R.string.color_selector_dialog_done, (dialog, which) -> {
mCurrentColor = colorSelectorLayout.getCheckedColor(DEFAULT_SCREEN_FLASH_COLOR);
mConsumer.accept(mCurrentColor);
})
.create();
createdDialog.setOnShowListener(
dialogInterface -> createdDialog.getButton(AlertDialog.BUTTON_NEUTRAL)
.setOnClickListener(v -> showColor()));
return createdDialog;
}
@Override
public void onPause() {
super.onPause();
cancelPreviewTask();
}
@Override
public void onCheckedChanged(ColorSelectorLayout group) {
mCurrentColor = group.getCheckedColor(DEFAULT_SCREEN_FLASH_COLOR);
if (mIsPreview) {
showColor();
}
}
private void showColor() {
int startDelay = 0;
synchronized (this) {
if (mTimer != null) mTimer.cancel();
mTimer = new Timer();
if (mIsPreview) {
mTimer.schedule(getStopTask(), 0);
startDelay = BETWEEN_STOP_AND_START_DELAY_MS;
}
mTimer.schedule(getStartTask(), startDelay);
mTimer.schedule(getStopTask(),
startDelay + PREVIEW_LONG_TIME_MS + MARGIN_FOR_STOP_DELAY_MS);
}
}
private TimerTask getStartTask() {
return new TimerTask() {
@Override
public void run() {
synchronized (this) {
startPreviewLocked();
}
}
};
}
private TimerTask getStopTask() {
return new TimerTask() {
@Override
public void run() {
synchronized (this) {
stopPreviewLocked();
}
}
};
}
private void cancelPreviewTask() {
synchronized (this) {
if (mTimer != null) mTimer.cancel();
stopPreviewLocked();
}
}
private void startPreviewLocked() {
if (getContext() == null) return;
mIsPreview = true;
Intent intent = new Intent(ACTION_FLASH_NOTIFICATION_START_PREVIEW);
intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_LONG_PREVIEW);
intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR, mCurrentColor);
getContext().sendBroadcast(intent);
}
private void stopPreviewLocked() {
if (getContext() == null) return;
Intent stopIntent = new Intent(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW);
getContext().sendBroadcast(stopIntent);
mIsPreview = false;
}
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.FlashNotificationsUtil.DEFAULT_SCREEN_FLASH_COLOR;
import static com.android.settings.accessibility.FlashNotificationsUtil.SETTING_KEY_SCREEN_FLASH_NOTIFICATION;
import static com.android.settings.accessibility.FlashNotificationsUtil.SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR;
import android.content.Context;
import android.graphics.Color;
import android.provider.Settings;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import java.util.function.Consumer;
/**
* Controller for Screen flash notification.
*/
public class ScreenFlashNotificationPreferenceController extends TogglePreferenceController {
private Fragment mParentFragment;
private Preference mPreference;
public ScreenFlashNotificationPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
public void setParentFragment(Fragment parentFragment) {
this.mParentFragment = parentFragment;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION, OFF) != OFF;
}
@Override
public boolean setChecked(boolean isChecked) {
if (isChecked) checkAndSetInitialColor();
return Settings.System.putInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION, (isChecked ? ON : OFF));
}
@Override
public int getSliceHighlightMenuRes() {
return R.string.menu_key_accessibility;
}
@Override
public CharSequence getSummary() {
return FlashNotificationsUtil.getColorDescriptionText(mContext,
Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR, DEFAULT_SCREEN_FLASH_COLOR));
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
refreshColorSummary();
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (getPreferenceKey().equals(preference.getKey()) && mParentFragment != null) {
final int initialColor = Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR,
DEFAULT_SCREEN_FLASH_COLOR);
final Consumer<Integer> consumer = color -> {
Settings.System.putInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR, color);
refreshColorSummary();
};
ScreenFlashNotificationColorDialogFragment
.getInstance(initialColor, consumer)
.show(mParentFragment.getParentFragmentManager(),
ScreenFlashNotificationColorDialogFragment.class.getSimpleName());
return true;
}
return super.handlePreferenceTreeClick(preference);
}
private void checkAndSetInitialColor() {
if (Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR, Color.TRANSPARENT)
== Color.TRANSPARENT) {
Settings.System.putInt(mContext.getContentResolver(),
SETTING_KEY_SCREEN_FLASH_NOTIFICATION_COLOR, DEFAULT_SCREEN_FLASH_COLOR);
}
}
private void refreshColorSummary() {
if (mPreference != null) mPreference.setSummary(getSummary());
}
}