• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.net.Uri
19 import android.view.ContextThemeWrapper
20 import androidx.lifecycle.Observer
21 import androidx.preference.Preference
22 import com.android.tv.twopanelsettings.slices.SlicePreferencesUtil.getEmbeddedItem
23 import com.android.tv.twopanelsettings.slices.SlicePreferencesUtil.getPreference
24 import com.android.tv.twopanelsettings.slices.compat.Slice
25 import com.android.tv.twopanelsettings.slices.compat.widget.ListContent
26 import com.android.tv.twopanelsettings.slices.compat.widget.SliceContent
27 
28 /**
29  * Helper class to handle the updates for embedded slice preferences.
30  */
31 class EmbeddedSlicePreferenceHelper internal constructor(
32     private val mPreference: Preference,
33     private val mUri: String
34 ) :
35     Observer<Slice?> {
36     private val mContext = mPreference.context
37     var mListener: SlicePreferenceListener? = null
38     var mNewPref: Preference? = null
39     private var mSlice: Slice? = null
40 
onAttachednull41     fun onAttached() {
42         sliceLiveData.observeForever(this)
43     }
44 
onDetachednull45     fun onDetached() {
46         sliceLiveData.removeObserver(this)
47     }
48 
49     private val sliceLiveData: PreferenceSliceLiveData.SliceLiveDataImpl
50         get() = ContextSingleton.getInstance()
51             .getSliceLiveData(mContext, Uri.parse(mUri))
52 
onChangednull53     override fun onChanged(slice: Slice?) {
54         mSlice = slice
55         if (slice == null || slice.hints == null || slice.hints.contains(android.app.slice.Slice.HINT_PARTIAL)) {
56             updateVisibility(false)
57             return
58         }
59         update()
60     }
61 
updateVisibilitynull62     private fun updateVisibility(visible: Boolean) {
63         mPreference.isVisible = visible
64         if (mListener != null) {
65             mListener!!.onChangeVisibility()
66         }
67     }
68 
updatenull69     private fun update() {
70         val mListContent = ListContent(
71             mSlice!!
72         )
73         val items: List<SliceContent> = mListContent.rowItems
74         if (items == null || items.size == 0) {
75             updateVisibility(false)
76             return
77         }
78         val embeddedItem = getEmbeddedItem(items)
79         // TODO(b/174691340): Refactor this class and integrate the functionality to TsPreference.
80         // TODO: Redesign TvSettings project structure so class in twopanelsettings lib can access
81         //  FlavorUtils
82         // For now, put true here so IconNeedsToBeProcessed will be respected.
83         mNewPref = getPreference(
84             embeddedItem,
85             (mContext as ContextThemeWrapper), null, true, null
86         )
87         if (mNewPref == null) {
88             updateVisibility(false)
89             return
90         }
91         updateVisibility(true)
92         if (mPreference is EmbeddedSlicePreference) {
93             mPreference.update()
94         } else if (mPreference is EmbeddedSliceSwitchPreference) {
95             mPreference.update()
96         }
97     }
98 
99     /**
100      * Implement this if the container needs to do something when embedded slice preference change
101      * visibility.
102      */
103     interface SlicePreferenceListener {
104         /**
105          * Callback when the preference change visibility
106          */
onChangeVisibilitynull107         fun onChangeVisibility()
108     }
109 }