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 
17 package com.android.tv.twopanelsettings.slices;
18 
19 import static android.app.slice.Slice.HINT_PARTIAL;
20 
21 import android.content.Context;
22 import android.net.Uri;
23 import android.view.ContextThemeWrapper;
24 
25 import androidx.lifecycle.Observer;
26 import androidx.preference.Preference;
27 import androidx.slice.Slice;
28 import androidx.slice.SliceItem;
29 import androidx.slice.widget.ListContent;
30 import androidx.slice.widget.SliceContent;
31 
32 import java.util.List;
33 
34 /**
35  * Helper class to handle the updates for embedded slice preferences.
36  */
37 public class EmbeddedSlicePreferenceHelper implements Observer<Slice> {
38     private final Preference mPreference;
39     private final Context mContext;
40     SlicePreferenceListener mListener;
41     Preference mNewPref;
42     private String mUri;
43     private Slice mSlice;
44 
EmbeddedSlicePreferenceHelper(Preference preference, String uri)45     EmbeddedSlicePreferenceHelper(Preference preference, String uri) {
46         mPreference = preference;
47         mUri = uri;
48         mContext = mPreference.getContext();
49     }
50 
onAttached()51     void onAttached() {
52         getSliceLiveData().observeForever(this);
53     }
54 
onDetached()55     void onDetached() {
56         getSliceLiveData().removeObserver(this);
57     }
58 
getSliceLiveData()59     private PreferenceSliceLiveData.SliceLiveDataImpl getSliceLiveData() {
60         return ContextSingleton.getInstance()
61                 .getSliceLiveData(mContext, Uri.parse(mUri));
62     }
63 
64     @Override
onChanged(Slice slice)65     public void onChanged(Slice slice) {
66         mSlice = slice;
67         if (slice == null || slice.getHints() == null || slice.getHints().contains(HINT_PARTIAL)) {
68             updateVisibility(false);
69             return;
70         }
71         update();
72     }
73 
updateVisibility(boolean visible)74     private void updateVisibility(boolean visible) {
75         mPreference.setVisible(visible);
76         if (mListener != null) {
77             mListener.onChangeVisibility();
78         }
79     }
80 
update()81     private void update() {
82         ListContent mListContent = new ListContent(mSlice);
83         List<SliceContent> items = mListContent.getRowItems();
84         if (items == null || items.size() == 0) {
85             updateVisibility(false);
86             return;
87         }
88         SliceItem embeddedItem = SlicePreferencesUtil.getEmbeddedItem(items);
89         mNewPref = SlicePreferencesUtil.getPreference(embeddedItem,
90                 (ContextThemeWrapper) mContext, null);
91         if (mNewPref == null) {
92             updateVisibility(false);
93             return;
94         }
95         updateVisibility(true);
96         if (mPreference instanceof EmbeddedSlicePreference) {
97             ((EmbeddedSlicePreference) mPreference).update();
98         } else if (mPreference instanceof EmbeddedSliceSwitchPreference) {
99             ((EmbeddedSliceSwitchPreference) mPreference).update();
100         }
101     }
102 
103     /**
104      * Implement this if the container needs to do something when embedded slice preference change
105      * visibility.
106      **/
107     public interface SlicePreferenceListener {
108         /**
109          * Callback when the preference change visibility
110          */
onChangeVisibility()111         void onChangeVisibility();
112     }
113 }
114