• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.settings.common;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import androidx.annotation.Nullable;
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceViewHolder;
25 
26 import java.util.function.Consumer;
27 
28 /**
29  * Extends {@link ColoredSwitchPreference} to give a disabled look while it is clickable.
30  */
31 public class ClickableWhileDisabledSwitchPreference extends ColoredSwitchPreference {
32 
33     private Consumer<Preference> mClickListener;
34 
ClickableWhileDisabledSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)35     public ClickableWhileDisabledSwitchPreference(Context context, AttributeSet attrs,
36             int defStyleAttr, int defStyleRes) {
37         super(context, attrs, defStyleAttr, defStyleRes);
38     }
39 
ClickableWhileDisabledSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)40     public ClickableWhileDisabledSwitchPreference(Context context, AttributeSet attrs,
41             int defStyleAttr) {
42         super(context, attrs, defStyleAttr);
43     }
44 
ClickableWhileDisabledSwitchPreference(Context context, AttributeSet attrs)45     public ClickableWhileDisabledSwitchPreference(Context context, AttributeSet attrs) {
46         super(context, attrs);
47     }
48 
ClickableWhileDisabledSwitchPreference(Context context)49     public ClickableWhileDisabledSwitchPreference(Context context) {
50         super(context);
51     }
52 
53     @Override
onBindViewHolder(PreferenceViewHolder holder)54     public void onBindViewHolder(PreferenceViewHolder holder) {
55         super.onBindViewHolder(holder);
56         holder.itemView.setAllowClickWhenDisabled(true);
57     }
58 
59     @Override
60     @SuppressWarnings("RestrictTo")
performClick()61     public void performClick() {
62         if (!isEnabled()) {
63             if (mClickListener != null) {
64                 mClickListener.accept(this);
65             }
66         } else {
67             super.performClick();
68         }
69     }
70 
71     /**
72      * Sets the click listener for when the preference is disabled.
73      * @param listener Listener to call when the preference is disabled and clicked.
74      */
setDisabledClickListener(@ullable Consumer<Preference> listener)75     public void setDisabledClickListener(@Nullable Consumer<Preference> listener) {
76         mClickListener = listener;
77     }
78 }
79