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.widget.FrameLayout;
20 
21 import androidx.preference.Preference;
22 
23 import com.android.car.ui.preference.CarUiPreference;
24 
25 import java.util.function.Consumer;
26 
27 /**
28  * Class representing an action item for an {@link MultiActionPreference}
29  */
30 public abstract class BaseActionItem {
31     protected CarUiPreference mPreference;
32     protected boolean mIsRestricted = false;
33     protected Consumer<Preference> mRestrictedOnClickListener;
34 
35     private boolean mIsEnabled = true;
36     private boolean mIsVisible = true;
37     private ActionItemInfoChangeListener mActionItemInfoChangeListener;
38 
BaseActionItem(ActionItemInfoChangeListener actionItemInfoChangeListener)39     public BaseActionItem(ActionItemInfoChangeListener actionItemInfoChangeListener) {
40         mActionItemInfoChangeListener = actionItemInfoChangeListener;
41     }
42 
43     /**
44      * Get the enabled state.
45      */
isEnabled()46     public boolean isEnabled() {
47         return mIsEnabled;
48     }
49 
50     /**
51      * Set the enabled state.
52      */
setEnabled(boolean enabled)53     public void setEnabled(boolean enabled) {
54         if (enabled != mIsEnabled) {
55             mIsEnabled = enabled;
56             update();
57         }
58     }
59 
60     /**
61      * Get the visibility state.
62      */
isVisible()63     public boolean isVisible() {
64         return mIsVisible;
65     }
66 
67     /**
68      * Set the visibility state.
69      */
setVisible(boolean visible)70     public void setVisible(boolean visible) {
71         if (visible != mIsVisible) {
72             mIsVisible = visible;
73             update();
74         }
75     }
76 
77     /**
78      * Sets the preference associated with the action item.
79      */
setPreference(Preference preference)80     public BaseActionItem setPreference(Preference preference) {
81         mPreference = (CarUiPreference) preference;
82         return this;
83     }
84 
85     /**
86      * Set action item as restricted. Made public so restricted status can be independent of the
87      * preference.
88      */
setRestricted(boolean isRestricted)89     public BaseActionItem setRestricted(boolean isRestricted) {
90         mIsRestricted = isRestricted;
91         return this;
92     }
93 
94     /**
95      * Set the Consumer that should run when the action item is clicked while disabled and
96      * restricted.
97      */
setRestrictedOnClickListener( Consumer<Preference> restrictedOnClickListener)98     public BaseActionItem setRestrictedOnClickListener(
99             Consumer<Preference> restrictedOnClickListener) {
100         mRestrictedOnClickListener = restrictedOnClickListener;
101         return this;
102     }
103 
104     /**
105      * Notify listener that action item has been changed.
106      */
update()107     protected void update() {
108         ActionItemInfoChangeListener listener = mActionItemInfoChangeListener;
109         if (listener != null) {
110             listener.onActionItemChange(this);
111         }
112     }
113 
bindViewHolder(FrameLayout frameLayout)114     abstract void bindViewHolder(FrameLayout frameLayout);
getLayoutResource()115     abstract int getLayoutResource();
116 
117     /**
118      * Listener that is notified when an action item has been changed.
119      */
120     interface ActionItemInfoChangeListener {
onActionItemChange(BaseActionItem baseActionItem)121         void onActionItemChange(BaseActionItem baseActionItem);
122     }
123 }
124