1 /* 2 * Copyright (C) 2020 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.ui.preference; 18 19 import static com.android.car.ui.utils.CarUiUtils.requireViewByRefId; 20 21 import android.content.Context; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.widget.Switch; 25 26 import androidx.annotation.Nullable; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.car.ui.R; 31 32 import java.util.function.Consumer; 33 34 /** 35 * A preference that has a switch that can be toggled independently of pressing the main 36 * body of the preference. 37 */ 38 public class CarUiTwoActionSwitchPreference extends CarUiTwoActionBasePreference { 39 @Nullable 40 protected Consumer<Boolean> mSecondaryActionOnClickListener; 41 private boolean mSecondaryActionChecked; 42 CarUiTwoActionSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)43 public CarUiTwoActionSwitchPreference(Context context, 44 AttributeSet attrs, 45 int defStyleAttr, int defStyleRes) { 46 super(context, attrs, defStyleAttr, defStyleRes); 47 } 48 CarUiTwoActionSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)49 public CarUiTwoActionSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 50 super(context, attrs, defStyleAttr); 51 } 52 CarUiTwoActionSwitchPreference(Context context, AttributeSet attrs)53 public CarUiTwoActionSwitchPreference(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 CarUiTwoActionSwitchPreference(Context context)57 public CarUiTwoActionSwitchPreference(Context context) { 58 super(context); 59 } 60 61 @Override init(@ullable AttributeSet attrs)62 protected void init(@Nullable AttributeSet attrs) { 63 super.init(attrs); 64 65 setLayoutResourceInternal(R.layout.car_ui_preference_two_action_switch); 66 } 67 68 @Override performSecondaryActionClickInternal()69 protected void performSecondaryActionClickInternal() { 70 if (isSecondaryActionEnabled()) { 71 if (isUxRestricted()) { 72 Consumer<Preference> restrictedListener = getOnClickWhileRestrictedListener(); 73 if (restrictedListener != null) { 74 restrictedListener.accept(this); 75 } 76 } else { 77 mSecondaryActionChecked = !mSecondaryActionChecked; 78 notifyChanged(); 79 if (mSecondaryActionOnClickListener != null) { 80 mSecondaryActionOnClickListener.accept(mSecondaryActionChecked); 81 } 82 } 83 } 84 } 85 86 @Override onBindViewHolder(PreferenceViewHolder holder)87 public void onBindViewHolder(PreferenceViewHolder holder) { 88 super.onBindViewHolder(holder); 89 90 View firstActionContainer = requireViewByRefId(holder.itemView, 91 R.id.car_ui_first_action_container); 92 View secondActionContainer = requireViewByRefId(holder.itemView, 93 R.id.car_ui_second_action_container); 94 View secondaryAction = requireViewByRefId(holder.itemView, 95 R.id.car_ui_secondary_action); 96 Switch s = requireViewByRefId(holder.itemView, 97 R.id.car_ui_secondary_action_concrete); 98 99 holder.itemView.setFocusable(false); 100 holder.itemView.setClickable(false); 101 firstActionContainer.setOnClickListener(this::performClick); 102 firstActionContainer.setEnabled(isEnabled() || isUxRestricted()); 103 firstActionContainer.setFocusable(isEnabled() || isUxRestricted()); 104 105 secondActionContainer.setVisibility(mSecondaryActionVisible ? View.VISIBLE : View.GONE); 106 s.setChecked(mSecondaryActionChecked); 107 s.setEnabled(isSecondaryActionEnabled()); 108 109 secondaryAction.setOnClickListener(v -> performSecondaryActionClickInternal()); 110 secondaryAction.setEnabled(isSecondaryActionEnabled() || isUxRestricted()); 111 secondaryAction.setFocusable(isSecondaryActionEnabled() || isUxRestricted()); 112 } 113 114 /** 115 * Sets the checked state of the switch in the secondary action space. 116 * @param checked Whether the switch should be checked or not. 117 */ setSecondaryActionChecked(boolean checked)118 public void setSecondaryActionChecked(boolean checked) { 119 mSecondaryActionChecked = checked; 120 notifyChanged(); 121 } 122 123 /** 124 * Returns the checked state of the switch in the secondary action space. 125 * @return Whether the switch is checked or not. 126 */ isSecondaryActionChecked()127 public boolean isSecondaryActionChecked() { 128 return mSecondaryActionChecked; 129 } 130 131 /** 132 * Sets the on-click listener of the secondary action button. 133 * 134 * The listener is called with the current checked state of the switch. 135 */ setOnSecondaryActionClickListener(@ullable Consumer<Boolean> onClickListener)136 public void setOnSecondaryActionClickListener(@Nullable Consumer<Boolean> onClickListener) { 137 mSecondaryActionOnClickListener = onClickListener; 138 notifyChanged(); 139 } 140 } 141