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