1 /* 2 * Copyright (C) 2017 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.settings.widget; 18 19 import android.content.Context; 20 import android.support.annotation.StringRes; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceViewHolder; 23 import android.text.TextUtils; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.Button; 27 28 import com.android.settings.R; 29 30 public class ActionButtonPreference extends Preference { 31 32 private final ButtonInfo mButton1Info = new ButtonInfo(); 33 private final ButtonInfo mButton2Info = new ButtonInfo(); 34 35 public ActionButtonPreference(Context context, AttributeSet attrs, 36 int defStyleAttr, int defStyleRes) { 37 super(context, attrs, defStyleAttr, defStyleRes); 38 init(); 39 } 40 41 public ActionButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) { 42 super(context, attrs, defStyleAttr); 43 init(); 44 } 45 46 public ActionButtonPreference(Context context, AttributeSet attrs) { 47 super(context, attrs); 48 init(); 49 } 50 51 public ActionButtonPreference(Context context) { 52 super(context); 53 init(); 54 } 55 56 private void init() { 57 setLayoutResource(R.layout.two_action_buttons); 58 setSelectable(false); 59 } 60 61 @Override 62 public void onBindViewHolder(PreferenceViewHolder holder) { 63 super.onBindViewHolder(holder); 64 holder.setDividerAllowedAbove(false); 65 holder.setDividerAllowedBelow(false); 66 mButton1Info.mPositiveButton = (Button) holder.findViewById(R.id.button1_positive); 67 mButton1Info.mNegativeButton = (Button) holder.findViewById(R.id.button1_negative); 68 mButton2Info.mPositiveButton = (Button) holder.findViewById(R.id.button2_positive); 69 mButton2Info.mNegativeButton = (Button) holder.findViewById(R.id.button2_negative); 70 71 mButton1Info.setUpButton(); 72 mButton2Info.setUpButton(); 73 } 74 75 public ActionButtonPreference setButton1Text(@StringRes int textResId) { 76 final String newText = getContext().getString(textResId); 77 if (!TextUtils.equals(newText, mButton1Info.mText)) { 78 mButton1Info.mText = newText; 79 notifyChanged(); 80 } 81 return this; 82 } 83 84 public ActionButtonPreference setButton1Enabled(boolean isEnabled) { 85 if (isEnabled != mButton1Info.mIsEnabled) { 86 mButton1Info.mIsEnabled = isEnabled; 87 notifyChanged(); 88 } 89 return this; 90 } 91 92 public ActionButtonPreference setButton2Text(@StringRes int textResId) { 93 final String newText = getContext().getString(textResId); 94 if (!TextUtils.equals(newText, mButton2Info.mText)) { 95 mButton2Info.mText = newText; 96 notifyChanged(); 97 } 98 return this; 99 } 100 101 public ActionButtonPreference setButton2Enabled(boolean isEnabled) { 102 if (isEnabled != mButton2Info.mIsEnabled) { 103 mButton2Info.mIsEnabled = isEnabled; 104 notifyChanged(); 105 } 106 return this; 107 } 108 109 public ActionButtonPreference setButton1OnClickListener(View.OnClickListener listener) { 110 if (listener != mButton1Info.mListener) { 111 mButton1Info.mListener = listener; 112 notifyChanged(); 113 } 114 return this; 115 } 116 117 public ActionButtonPreference setButton2OnClickListener(View.OnClickListener listener) { 118 if (listener != mButton2Info.mListener) { 119 mButton2Info.mListener = listener; 120 notifyChanged(); 121 } 122 return this; 123 } 124 125 public ActionButtonPreference setButton1Positive(boolean isPositive) { 126 if (isPositive != mButton1Info.mIsPositive) { 127 mButton1Info.mIsPositive = isPositive; 128 notifyChanged(); 129 } 130 return this; 131 } 132 133 public ActionButtonPreference setButton2Positive(boolean isPositive) { 134 if (isPositive != mButton2Info.mIsPositive) { 135 mButton2Info.mIsPositive = isPositive; 136 notifyChanged(); 137 } 138 return this; 139 } 140 public ActionButtonPreference setButton1Visible(boolean isPositive) { 141 if (isPositive != mButton1Info.mIsVisible) { 142 mButton1Info.mIsVisible = isPositive; 143 notifyChanged(); 144 } 145 return this; 146 } 147 148 public ActionButtonPreference setButton2Visible(boolean isPositive) { 149 if (isPositive != mButton2Info.mIsVisible) { 150 mButton2Info.mIsVisible = isPositive; 151 notifyChanged(); 152 } 153 return this; 154 } 155 156 static class ButtonInfo { 157 private Button mPositiveButton; 158 private Button mNegativeButton; 159 private CharSequence mText; 160 private View.OnClickListener mListener; 161 private boolean mIsPositive = true; 162 private boolean mIsEnabled = true; 163 private boolean mIsVisible = true; 164 165 void setUpButton() { 166 setUpButton(mPositiveButton); 167 setUpButton(mNegativeButton); 168 if (!mIsVisible) { 169 mPositiveButton.setVisibility(View.INVISIBLE); 170 mNegativeButton.setVisibility(View.INVISIBLE); 171 } else if (mIsPositive) { 172 mPositiveButton.setVisibility(View.VISIBLE); 173 mNegativeButton.setVisibility(View.INVISIBLE); 174 } else { 175 mPositiveButton.setVisibility(View.INVISIBLE); 176 mNegativeButton.setVisibility(View.VISIBLE); 177 } 178 } 179 180 private void setUpButton(Button button) { 181 button.setText(mText); 182 button.setOnClickListener(mListener); 183 button.setEnabled(mIsEnabled); 184 } 185 } 186 }