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 package com.android.tv.twopanelsettings.slices; 17 18 import android.content.Context; 19 import android.content.res.TypedArray; 20 import android.text.TextUtils; 21 import android.util.AttributeSet; 22 import android.view.View; 23 24 import androidx.annotation.Nullable; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceViewHolder; 27 import androidx.slice.core.SliceActionImpl; 28 29 import com.android.tv.twopanelsettings.R; 30 31 /** 32 * A preference that represents a slice provided by another app. 33 */ 34 public class SlicePreference extends Preference implements HasSliceAction, HasSliceUri, 35 HasCustomContentDescription { 36 37 private static final String TAG = "SlicePreference"; 38 private static final String SEPARATOR = ","; 39 40 private String mUri; 41 private int mActionId; 42 private SliceActionImpl mAction; 43 private SliceActionImpl mFollowUpAction; 44 private String mContentDescription; 45 SlicePreference(Context context)46 public SlicePreference(Context context) { 47 super(context); 48 init(null); 49 } 50 SlicePreference(Context context, AttributeSet attrs)51 public SlicePreference(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 init(attrs); 54 } 55 56 @Override onBindViewHolder(PreferenceViewHolder holder)57 public void onBindViewHolder(PreferenceViewHolder holder) { 58 super.onBindViewHolder(holder); 59 if (!TextUtils.isEmpty(mContentDescription)) { 60 holder.itemView.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE); 61 holder.itemView.setContentDescription(mContentDescription); 62 } 63 } 64 init(@ullable AttributeSet attrs)65 protected void init(@Nullable AttributeSet attrs) { 66 if (attrs != null) { 67 initStyleAttributes(attrs); 68 } 69 } 70 initStyleAttributes(AttributeSet attrs)71 private void initStyleAttributes(AttributeSet attrs) { 72 final TypedArray a = getContext().obtainStyledAttributes( 73 attrs, R.styleable.SlicePreference); 74 for (int i = a.getIndexCount() - 1; i >= 0; i--) { 75 int attr = a.getIndex(i); 76 if (attr == R.styleable.SlicePreference_uri) { 77 mUri = a.getString(attr); 78 break; 79 } 80 } 81 } 82 setUri(String uri)83 public void setUri(String uri) { 84 mUri = uri; 85 } 86 getUri()87 public String getUri() { 88 return mUri; 89 } 90 91 @Override getActionId()92 public int getActionId() { 93 return mActionId; 94 } 95 96 @Override setActionId(int actionId)97 public void setActionId(int actionId) { 98 mActionId = actionId; 99 } 100 setSliceAction(SliceActionImpl action)101 public void setSliceAction(SliceActionImpl action) { 102 mAction = action; 103 } 104 105 @Override getFollowupSliceAction()106 public SliceActionImpl getFollowupSliceAction() { 107 return mFollowUpAction; 108 } 109 110 @Override setFollowupSliceAction(SliceActionImpl sliceAction)111 public void setFollowupSliceAction(SliceActionImpl sliceAction) { 112 mFollowUpAction = sliceAction; 113 } 114 115 @Override getSliceAction()116 public SliceActionImpl getSliceAction() { 117 return mAction; 118 } 119 120 /** 121 * Sets the accessibility content description that will be read to the TalkBack users when they 122 * select this preference. 123 */ setContentDescription(String contentDescription)124 public void setContentDescription(String contentDescription) { 125 this.mContentDescription = contentDescription; 126 } 127 getContentDescription()128 public String getContentDescription() { 129 return mContentDescription; 130 } 131 } 132