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