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 
17 package com.android.settings.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 
23 import com.android.settings.R;
24 import com.android.settingslib.RestrictedPreference;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.PreferenceViewHolder;
28 
29 /**
30  * A preference with a plus button on the side representing an "add" action. The plus button will
31  * only be visible when a non-null click listener is registered.
32  */
33 public class AddPreference extends RestrictedPreference implements View.OnClickListener {
34 
35     private OnAddClickListener mListener;
36     private View mWidgetFrame;
37     private View mAddWidget;
38 
AddPreference(Context context, AttributeSet attrs)39     public AddPreference(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
43     @VisibleForTesting
getAddWidgetResId()44     int getAddWidgetResId() {
45         return R.id.add_preference_widget;
46     }
47 
48     /** Sets a listener for clicks on the plus button. Passing null will cause the button to be
49      * hidden. */
setOnAddClickListener(OnAddClickListener listener)50     public void setOnAddClickListener(OnAddClickListener listener) {
51         mListener = listener;
52        if (mWidgetFrame != null) {
53            mWidgetFrame.setVisibility(shouldHideSecondTarget() ? View.GONE : View.VISIBLE);
54        }
55     }
56 
setAddWidgetEnabled(boolean enabled)57     public void setAddWidgetEnabled(boolean enabled) {
58         if (mAddWidget != null) {
59             mAddWidget.setEnabled(enabled);
60         }
61     }
62 
63     @Override
getSecondTargetResId()64     protected int getSecondTargetResId() {
65         return R.layout.preference_widget_add;
66     }
67 
68     @Override
shouldHideSecondTarget()69     protected boolean shouldHideSecondTarget() {
70         return mListener == null;
71     }
72 
73     @Override
onBindViewHolder(PreferenceViewHolder holder)74     public void onBindViewHolder(PreferenceViewHolder holder) {
75         super.onBindViewHolder(holder);
76         mWidgetFrame = holder.findViewById(android.R.id.widget_frame);
77         mAddWidget = holder.findViewById(getAddWidgetResId());
78         mAddWidget.setEnabled(true);
79         mAddWidget.setOnClickListener(this);
80     }
81 
82     @Override
onClick(View view)83     public void onClick(View view) {
84         if (view.getId() == getAddWidgetResId() && mListener != null) {
85             mListener.onAddClick(this);
86         }
87     }
88 
89     public interface OnAddClickListener {
onAddClick(AddPreference p)90         void onAddClick(AddPreference p);
91     }
92 }
93