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.tv.twopanelsettings.slices;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.AttributeSet;
22 import android.view.View;
23 
24 import androidx.preference.CheckBoxPreference;
25 import androidx.preference.PreferenceViewHolder;
26 import androidx.slice.core.SliceActionImpl;
27 
28 /**
29  * Slice version of CheckboxPreference.
30  */
31 public class SliceCheckboxPreference extends CheckBoxPreference implements HasSliceAction,
32         HasCustomContentDescription {
33     private int mActionId;
34     private SliceActionImpl mAction;
35     private SliceActionImpl mFollowupSliceAction;
36     private String mContentDescription;
37 
SliceCheckboxPreference(Context context, SliceActionImpl action)38     public SliceCheckboxPreference(Context context, SliceActionImpl action) {
39         super(context);
40         mAction = action;
41         update();
42     }
43 
SliceCheckboxPreference(Context context, AttributeSet attrs, SliceActionImpl action)44     public SliceCheckboxPreference(Context context, AttributeSet attrs, SliceActionImpl action) {
45         super(context, attrs);
46         mAction = action;
47         update();
48     }
49 
50     @Override
onBindViewHolder(PreferenceViewHolder holder)51     public void onBindViewHolder(PreferenceViewHolder holder) {
52         super.onBindViewHolder(holder);
53         if (!TextUtils.isEmpty(mContentDescription)) {
54             holder.itemView.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE);
55             holder.itemView.setContentDescription(mContentDescription);
56         }
57     }
58 
59     @Override
getActionId()60     public int getActionId() {
61         return mActionId;
62     }
63 
64     @Override
setActionId(int actionId)65     public void setActionId(int actionId) {
66         mActionId = actionId;
67     }
68 
69     @Override
getSliceAction()70     public SliceActionImpl getSliceAction() {
71         return mAction;
72     }
73 
74     @Override
setSliceAction(SliceActionImpl sliceAction)75     public void setSliceAction(SliceActionImpl sliceAction) {
76         mAction = sliceAction;
77     }
78 
79     @Override
getFollowupSliceAction()80     public SliceActionImpl getFollowupSliceAction() {
81         return mFollowupSliceAction;
82     }
83 
84     @Override
setFollowupSliceAction(SliceActionImpl sliceAction)85     public void setFollowupSliceAction(SliceActionImpl sliceAction) {
86         mFollowupSliceAction = sliceAction;
87     }
88 
update()89     private void update() {
90         this.setChecked(mAction.isChecked());
91     }
92 
93     /**
94      * Sets the accessibility content description that will be read to the TalkBack users when they
95      * select this preference.
96      */
setContentDescription(String contentDescription)97     public void setContentDescription(String contentDescription) {
98         this.mContentDescription = contentDescription;
99     }
100 
getContentDescription()101     public String getContentDescription() {
102         return mContentDescription;
103     }
104 }
105