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 android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.TextView;
24 
25 import androidx.annotation.ColorInt;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceViewHolder;
28 
29 import com.android.settings.R;
30 import com.android.settingslib.Utils;
31 
32 /**
33  * Preference for Flash notifications preview.
34  */
35 public class FlashNotificationsPreviewPreference extends Preference {
36     private Drawable mBackgroundEnabled;
37     private Drawable mBackgroundDisabled;
38     @ColorInt
39     private int mTextColorDisabled;
40 
FlashNotificationsPreviewPreference(Context context)41     public FlashNotificationsPreviewPreference(Context context) {
42         super(context);
43         init();
44     }
45 
FlashNotificationsPreviewPreference(Context context, AttributeSet attrs)46     public FlashNotificationsPreviewPreference(Context context, AttributeSet attrs) {
47         super(context, attrs);
48         init();
49     }
50 
FlashNotificationsPreviewPreference(Context context, AttributeSet attrs, int defStyleAttr)51     public FlashNotificationsPreviewPreference(Context context, AttributeSet attrs,
52             int defStyleAttr) {
53         super(context, attrs, defStyleAttr);
54         init();
55     }
56 
FlashNotificationsPreviewPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)57     public FlashNotificationsPreviewPreference(Context context, AttributeSet attrs,
58             int defStyleAttr, int defStyleRes) {
59         super(context, attrs, defStyleAttr, defStyleRes);
60         init();
61     }
62 
init()63     private void init() {
64         setLayoutResource(R.layout.flash_notification_preview_preference);
65         mBackgroundEnabled = getContext().getDrawable(
66                 com.android.settingslib.widget.mainswitch.R.drawable.settingslib_switch_bar_bg_on);
67         mBackgroundDisabled = getContext().getDrawable(R.drawable.switch_bar_bg_disabled);
68         mTextColorDisabled = Utils.getColorAttrDefaultColor(getContext(),
69                 android.R.attr.textColorPrimary);
70     }
71 
72     @Override
onBindViewHolder(PreferenceViewHolder holder)73     public void onBindViewHolder(PreferenceViewHolder holder) {
74         super.onBindViewHolder(holder);
75 
76         final boolean enabled = isEnabled();
77         final View frame = holder.findViewById(R.id.frame);
78         if (frame != null) {
79             frame.setBackground(enabled ? mBackgroundEnabled : mBackgroundDisabled);
80         }
81         final TextView title = (TextView) holder.findViewById(android.R.id.title);
82         if (title != null) {
83             @ColorInt final int textColorEnabled = title.getCurrentTextColor();
84             title.setAlpha(enabled ? 1f : 0.38f);
85             title.setTextColor(enabled ? textColorEnabled : mTextColorDisabled);
86         }
87     }
88 
89     @Override
setEnabled(boolean enabled)90     public void setEnabled(boolean enabled) {
91         super.setEnabled(enabled);
92         notifyChanged();
93     }
94 }
95