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.permissioncontroller.permission.ui.auto;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.widget.FrameLayout;
23 
24 import androidx.annotation.AttrRes;
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.annotation.StyleRes;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceViewHolder;
30 
31 import com.android.permissioncontroller.R;
32 
33 /** {@link Preference} with the widget layout as a separate target. */
34 public class AutoTwoTargetPreference extends Preference {
35 
36     private OnSecondTargetClickListener mListener;
37     private boolean mIsDividerVisible = true;
38 
AutoTwoTargetPreference(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)39     public AutoTwoTargetPreference(@NonNull Context context, @Nullable AttributeSet attrs,
40             @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
41         super(context, attrs, defStyleAttr, defStyleRes);
42         init();
43     }
44 
AutoTwoTargetPreference(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)45     public AutoTwoTargetPreference(@NonNull Context context, @Nullable AttributeSet attrs,
46             @AttrRes int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48         init();
49     }
50 
AutoTwoTargetPreference(@onNull Context context, @Nullable AttributeSet attrs)51     public AutoTwoTargetPreference(@NonNull Context context, @Nullable AttributeSet attrs) {
52         super(context, attrs);
53         init();
54     }
55 
AutoTwoTargetPreference(@onNull Context context)56     public AutoTwoTargetPreference(@NonNull Context context) {
57         super(context);
58         init();
59     }
60 
init()61     private void init() {
62         setLayoutResource(R.layout.car_two_target_preference);
63     }
64 
65     /** Set the listener for second target click. */
setOnSecondTargetClickListener(@ullable OnSecondTargetClickListener listener)66     public void setOnSecondTargetClickListener(@Nullable OnSecondTargetClickListener listener) {
67         mListener = listener;
68         notifyChanged();
69     }
70 
71     /** Sets the visibility of the divider. */
setDividerVisible(boolean visible)72     public void setDividerVisible(boolean visible) {
73         mIsDividerVisible = visible;
74         notifyChanged();
75     }
76 
77     @Override
onBindViewHolder(@onNull PreferenceViewHolder holder)78     public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
79         super.onBindViewHolder(holder);
80 
81         View actionContainer = holder.findViewById(R.id.action_widget_container);
82         View divider = holder.findViewById(R.id.two_target_divider);
83         FrameLayout widgetFrame = (FrameLayout) holder.findViewById(android.R.id.widget_frame);
84         if (mListener != null) {
85             actionContainer.setVisibility(View.VISIBLE);
86             divider.setVisibility(mIsDividerVisible ? View.VISIBLE : View.GONE);
87             widgetFrame.setVisibility(View.VISIBLE);
88             widgetFrame.setOnClickListener(v -> mListener.onSecondTargetClick(this));
89         } else {
90             actionContainer.setVisibility(View.GONE);
91         }
92     }
93 
94     /**
95      * Listener for second target click.
96      */
97     public interface OnSecondTargetClickListener {
98 
99         /**
100          * Callback when the second target is clicked.
101          *
102          * @param preference the {@link AutoTwoTargetPreference} that was clicked
103          */
onSecondTargetClick(@onNull AutoTwoTargetPreference preference)104         void onSecondTargetClick(@NonNull AutoTwoTargetPreference preference);
105     }
106 }
107