1 /* 2 * Copyright (C) 2019 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 package com.android.car.developeroptions; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.view.View; 21 import android.view.View.OnClickListener; 22 import android.widget.ImageView; 23 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceViewHolder; 26 27 public class CancellablePreference extends Preference implements OnClickListener { 28 29 private boolean mCancellable; 30 private OnCancelListener mListener; 31 CancellablePreference(Context context)32 public CancellablePreference(Context context) { 33 super(context); 34 setWidgetLayoutResource(R.layout.cancel_pref_widget); 35 } 36 CancellablePreference(Context context, AttributeSet attrs)37 public CancellablePreference(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 setWidgetLayoutResource(R.layout.cancel_pref_widget); 40 } 41 setCancellable(boolean isCancellable)42 public void setCancellable(boolean isCancellable) { 43 mCancellable = isCancellable; 44 notifyChanged(); 45 } 46 setOnCancelListener(OnCancelListener listener)47 public void setOnCancelListener(OnCancelListener listener) { 48 mListener = listener; 49 } 50 51 @Override onBindViewHolder(PreferenceViewHolder view)52 public void onBindViewHolder(PreferenceViewHolder view) { 53 super.onBindViewHolder(view); 54 55 ImageView cancel = (ImageView) view.findViewById(R.id.cancel); 56 cancel.setVisibility(mCancellable ? View.VISIBLE : View.INVISIBLE); 57 cancel.setOnClickListener(this); 58 } 59 60 @Override onClick(View v)61 public void onClick(View v) { 62 if (mListener != null) { 63 mListener.onCancel(this); 64 } 65 } 66 67 public interface OnCancelListener { onCancel(CancellablePreference preference)68 void onCancel(CancellablePreference preference); 69 } 70 71 } 72