1 /* 2 * Copyright (C) 2020 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.util.AttributeSet 20 import com.android.tv.twopanelsettings.slices.EmbeddedSlicePreferenceHelper.SlicePreferenceListener 21 22 /** 23 * An embedded slice preference which would be embedded in common TvSettings preference 24 * items, but will automatically update its status and communicates with external apps through 25 * slice api. 26 */ 27 open class EmbeddedSlicePreference : SlicePreference, 28 HasCustomContentDescription { 29 private val mHelper: EmbeddedSlicePreferenceHelper 30 private var mContentDescription: String? = null 31 32 constructor(context: Context?, uri: String?) : super(context) { 33 setUri(uri) 34 mHelper = EmbeddedSlicePreferenceHelper(this, getUri()) 35 } 36 37 constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { 38 mHelper = EmbeddedSlicePreferenceHelper(this, uri) 39 } 40 onAttachednull41 override fun onAttached() { 42 super.onAttached() 43 mHelper.onAttached() 44 } 45 onDetachednull46 override fun onDetached() { 47 super.onDetached() 48 mHelper.onDetached() 49 } 50 addListenernull51 fun addListener(listener: SlicePreferenceListener?) { 52 mHelper.mListener = listener 53 } 54 removeListenernull55 fun removeListener(listener: SlicePreferenceListener?) { 56 mHelper.mListener = null 57 } 58 updatenull59 fun update() { 60 isEnabled = mHelper.mNewPref!!.isEnabled 61 title = mHelper.mNewPref!!.title 62 summary = mHelper.mNewPref!!.summary 63 icon = mHelper.mNewPref!!.icon 64 extras.putAll(mHelper.mNewPref!!.extras) 65 if (mHelper.mNewPref is HasSliceAction 66 && (mHelper.mNewPref as HasSliceAction).sliceAction != null 67 ) { 68 sliceAction = (mHelper.mNewPref as HasSliceAction).sliceAction 69 } 70 } 71 72 73 /** 74 * Sets the accessibility content description that will be read to the TalkBack users when they 75 * focus on this preference. 76 */ setContentDescriptionnull77 override fun setContentDescription(contentDescription: String) { 78 this.mContentDescription = contentDescription 79 } 80 getContentDescriptionnull81 override fun getContentDescription(): String { 82 return mContentDescription!! 83 } 84 85 companion object { 86 private const val TAG = "EmbeddedSlicePreference" 87 } 88 } 89