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 java.util.function.Consumer; 24 25 /** 26 * Class representing an action item for an {@link MultiActionPreference} 27 */ 28 public abstract class BaseActionItem { 29 protected Preference mPreference; 30 protected boolean mIsPreferenceRestricted = false; 31 protected Consumer<Preference> mRestrictedOnClickListener; 32 33 private boolean mIsEnabled = true; 34 private boolean mIsVisible = true; 35 private ActionItemInfoChangeListener mActionItemInfoChangeListener; 36 BaseActionItem(ActionItemInfoChangeListener actionItemInfoChangeListener)37 public BaseActionItem(ActionItemInfoChangeListener actionItemInfoChangeListener) { 38 mActionItemInfoChangeListener = actionItemInfoChangeListener; 39 } 40 41 /** 42 * Get the enabled state. 43 */ isEnabled()44 public boolean isEnabled() { 45 return mIsEnabled; 46 } 47 48 /** 49 * Set the enabled state. 50 */ setEnabled(boolean enabled)51 public void setEnabled(boolean enabled) { 52 if (enabled != mIsEnabled) { 53 mIsEnabled = enabled; 54 update(); 55 } 56 } 57 58 /** 59 * Get the visibility state. 60 */ isVisible()61 public boolean isVisible() { 62 return mIsVisible; 63 } 64 65 /** 66 * Set the visibility state. 67 */ setVisible(boolean visible)68 public void setVisible(boolean visible) { 69 if (visible != mIsVisible) { 70 mIsVisible = visible; 71 update(); 72 } 73 } 74 setPreference(Preference preference)75 BaseActionItem setPreference(Preference preference) { 76 mPreference = preference; 77 return this; 78 } 79 setPreferenceRestricted(boolean isPreferenceRestricted)80 BaseActionItem setPreferenceRestricted(boolean isPreferenceRestricted) { 81 mIsPreferenceRestricted = isPreferenceRestricted; 82 return this; 83 } 84 setRestrictedOnClickListener(Consumer<Preference> restrictedOnClickListener)85 BaseActionItem setRestrictedOnClickListener(Consumer<Preference> restrictedOnClickListener) { 86 mRestrictedOnClickListener = restrictedOnClickListener; 87 return this; 88 } 89 90 /** 91 * Notify listener that action item has been changed. 92 */ update()93 protected void update() { 94 ActionItemInfoChangeListener listener = mActionItemInfoChangeListener; 95 if (listener != null) { 96 listener.onActionItemChange(this); 97 } 98 } 99 bindViewHolder(FrameLayout frameLayout)100 abstract void bindViewHolder(FrameLayout frameLayout); getLayoutResource()101 abstract int getLayoutResource(); 102 103 /** 104 * Listener that is notified when an action item has been changed. 105 */ 106 interface ActionItemInfoChangeListener { onActionItemChange(BaseActionItem baseActionItem)107 void onActionItemChange(BaseActionItem baseActionItem); 108 } 109 } 110