1 /* 2 * Copyright (C) 2018 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 android.content.Context; 20 import android.content.res.TypedArray; 21 import android.util.AttributeSet; 22 import android.view.View; 23 24 import androidx.annotation.NonNull; 25 import androidx.preference.PreferenceViewHolder; 26 27 import com.android.car.ui.R; 28 import com.android.car.ui.utils.CarUiUtils; 29 30 /** 31 * A preference which can perform two actions. The secondary action is shown by default. 32 * {@link #showAction(boolean)} may be used to manually set the visibility of the action. 33 */ 34 public class CarUiTwoActionPreference extends CarUiPreference { 35 36 private boolean mIsActionShown; 37 CarUiTwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)38 public CarUiTwoActionPreference(Context context, AttributeSet attrs, 39 int defStyleAttr, int defStyleRes) { 40 super(context, attrs, defStyleAttr, defStyleRes); 41 init(attrs); 42 } 43 CarUiTwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr)44 public CarUiTwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr) { 45 super(context, attrs, defStyleAttr); 46 init(attrs); 47 } 48 CarUiTwoActionPreference(Context context, AttributeSet attrs)49 public CarUiTwoActionPreference(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 init(attrs); 52 } 53 CarUiTwoActionPreference(Context context)54 public CarUiTwoActionPreference(Context context) { 55 super(context); 56 init(/* attrs= */ null); 57 } 58 59 /** 60 * Sets the custom two action preference layout and attributes. 61 * Check {@link #setLayoutResource} for layout requirements. 62 */ init(AttributeSet attrs)63 private void init(AttributeSet attrs) { 64 setLayoutResource(R.layout.car_ui_two_action_preference); 65 TypedArray preferenceAttributes = getContext().obtainStyledAttributes(attrs, 66 R.styleable.CarUiTwoActionPreference); 67 mIsActionShown = preferenceAttributes.getBoolean( 68 R.styleable.CarUiTwoActionPreference_actionShown, true); 69 setShowChevron(false); 70 preferenceAttributes.recycle(); 71 } 72 73 /** 74 * Sets whether the secondary action is visible in the preference. 75 * 76 * @param isShown {@code true} if the secondary action should be shown. 77 */ showAction(boolean isShown)78 public void showAction(boolean isShown) { 79 mIsActionShown = isShown; 80 notifyChanged(); 81 } 82 83 /** Returns {@code true} if action is shown. */ isActionShown()84 public boolean isActionShown() { 85 return mIsActionShown; 86 } 87 88 @Override onBindViewHolder(PreferenceViewHolder holder)89 public void onBindViewHolder(PreferenceViewHolder holder) { 90 super.onBindViewHolder(holder); 91 View actionContainer = CarUiUtils.findViewByRefId(holder.itemView, 92 R.id.action_widget_container); 93 View widgetFrame = CarUiUtils.findViewByRefId(holder.itemView, android.R.id.widget_frame); 94 if (mIsActionShown) { 95 actionContainer.setVisibility(View.VISIBLE); 96 onBindWidgetFrame(widgetFrame); 97 } else { 98 actionContainer.setVisibility(View.GONE); 99 } 100 } 101 102 /** 103 * Binds the created View for the second action. 104 * 105 * <p>This is a good place to set properties on any custom view. 106 * 107 * @param widgetFrame The widget frame which controls the 2nd action. 108 */ onBindWidgetFrame(@onNull View widgetFrame)109 protected void onBindWidgetFrame(@NonNull View widgetFrame) { 110 } 111 } 112