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 android.content.Context; 20 import android.content.res.TypedArray; 21 import android.util.AttributeSet; 22 23 import androidx.annotation.CallSuper; 24 import androidx.annotation.LayoutRes; 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.annotation.StyleableRes; 28 29 import com.android.car.ui.R; 30 31 /** 32 * A base class for several types of preferences, that all have a main click action along 33 * with a secondary action. 34 */ 35 public abstract class CarUiTwoActionBasePreference extends CarUiPreference { 36 37 protected boolean mSecondaryActionEnabled = true; 38 protected boolean mSecondaryActionVisible = true; 39 CarUiTwoActionBasePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40 public CarUiTwoActionBasePreference(Context context, 41 AttributeSet attrs, 42 int defStyleAttr, int defStyleRes) { 43 super(context, attrs, defStyleAttr, defStyleRes); 44 init(attrs); 45 } 46 CarUiTwoActionBasePreference(Context context, AttributeSet attrs, int defStyleAttr)47 public CarUiTwoActionBasePreference(Context context, AttributeSet attrs, int defStyleAttr) { 48 super(context, attrs, defStyleAttr); 49 init(attrs); 50 } 51 CarUiTwoActionBasePreference(Context context, AttributeSet attrs)52 public CarUiTwoActionBasePreference(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 init(attrs); 55 } 56 CarUiTwoActionBasePreference(Context context)57 public CarUiTwoActionBasePreference(Context context) { 58 super(context); 59 init(null); 60 } 61 62 @CallSuper init(@ullable AttributeSet attrs)63 protected void init(@Nullable AttributeSet attrs) { 64 setShowChevron(false); 65 66 TypedArray a = getContext() 67 .obtainStyledAttributes(attrs, R.styleable.CarUiTwoActionBasePreference); 68 try { 69 disallowResourceIds(a, 70 R.styleable.CarUiTwoActionBasePreference_layout, 71 R.styleable.CarUiTwoActionBasePreference_android_layout, 72 R.styleable.CarUiTwoActionBasePreference_widgetLayout, 73 R.styleable.CarUiTwoActionBasePreference_android_widgetLayout); 74 } finally { 75 a.recycle(); 76 } 77 78 a = getContext().obtainStyledAttributes(attrs, 79 R.styleable.CarUiTwoActionPreference); 80 81 try { 82 mSecondaryActionVisible = a.getBoolean( 83 R.styleable.CarUiTwoActionPreference_actionShown, true); 84 mSecondaryActionEnabled = a.getBoolean( 85 R.styleable.CarUiTwoActionPreference_actionEnabled, true); 86 } finally { 87 a.recycle(); 88 } 89 } 90 91 /** 92 * Returns whether or not the secondary action is enabled. 93 */ isSecondaryActionEnabled()94 public boolean isSecondaryActionEnabled() { 95 return mSecondaryActionEnabled && isEnabled(); 96 } 97 98 /** 99 * Sets whether or not the secondary action is enabled. This is secondary to the overall 100 * {@link #setEnabled(boolean)} of the preference 101 */ setSecondaryActionEnabled(boolean enabled)102 public void setSecondaryActionEnabled(boolean enabled) { 103 mSecondaryActionEnabled = enabled; 104 notifyChanged(); 105 } 106 107 /** 108 * Returns whether or not the secondary action is visible. 109 */ isSecondaryActionVisible()110 public boolean isSecondaryActionVisible() { 111 return mSecondaryActionVisible; 112 } 113 114 /** 115 * Sets whether or not the secondary action is visible. 116 */ setSecondaryActionVisible(boolean visible)117 public void setSecondaryActionVisible(boolean visible) { 118 mSecondaryActionVisible = visible; 119 notifyChanged(); 120 } 121 122 /** 123 * Like {@link #onClick()}, but for the secondary action. 124 */ performSecondaryActionClick()125 public void performSecondaryActionClick() { 126 if (mSecondaryActionEnabled && mSecondaryActionVisible) { 127 performSecondaryActionClickInternal(); 128 } 129 } 130 performSecondaryActionClickInternal()131 protected abstract void performSecondaryActionClickInternal(); 132 setLayoutResourceInternal(@ayoutRes int layoutResId)133 protected void setLayoutResourceInternal(@LayoutRes int layoutResId) { 134 super.setLayoutResource(layoutResId); 135 } 136 137 @Override setLayoutResource(@ayoutRes int layoutResId)138 public void setLayoutResource(@LayoutRes int layoutResId) { 139 throw new UnsupportedOperationException(); 140 } 141 142 @Override setWidgetLayoutResource(@ayoutRes int widgetLayoutResId)143 public void setWidgetLayoutResource(@LayoutRes int widgetLayoutResId) { 144 throw new UnsupportedOperationException(); 145 } 146 disallowResourceIds(@onNull TypedArray a, @StyleableRes int ...indices)147 private static void disallowResourceIds(@NonNull TypedArray a, @StyleableRes int ...indices) { 148 for (int index : indices) { 149 if (a.hasValue(index)) { 150 throw new AssertionError("Setting this attribute is not allowed: " 151 + a.getResources().getResourceName(index)); 152 } 153 } 154 } 155 } 156