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 android.content.Context;
20 import android.util.AttributeSet;
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 public class EmbeddedSlicePreference extends SlicePreference {
28     private static final String TAG = "EmbeddedSlicePreference";
29     private final EmbeddedSlicePreferenceHelper mHelper;
30 
EmbeddedSlicePreference(Context context, String uri)31     public EmbeddedSlicePreference(Context context, String uri) {
32         super(context);
33         mHelper = new EmbeddedSlicePreferenceHelper(this, getUri());
34     }
35 
EmbeddedSlicePreference(Context context, AttributeSet attrs)36     public EmbeddedSlicePreference(Context context, AttributeSet attrs) {
37         super(context, attrs);
38         mHelper = new EmbeddedSlicePreferenceHelper(this, getUri());
39     }
40 
41     @Override
onAttached()42     public void onAttached() {
43         super.onAttached();
44         mHelper.onAttached();
45     }
46 
47     @Override
onDetached()48     public void onDetached() {
49         super.onDetached();
50         mHelper.onDetached();
51     }
52 
addListener(EmbeddedSlicePreferenceHelper.SlicePreferenceListener listener)53     public void addListener(EmbeddedSlicePreferenceHelper.SlicePreferenceListener listener) {
54         mHelper.mListener = listener;
55     }
56 
removeListener(EmbeddedSlicePreferenceHelper.SlicePreferenceListener listener)57     public void removeListener(EmbeddedSlicePreferenceHelper.SlicePreferenceListener listener) {
58         mHelper.mListener = null;
59     }
60 
update()61     void update() {
62         setTitle(mHelper.mNewPref.getTitle());
63         setSummary(mHelper.mNewPref.getSummary());
64         setIcon(mHelper.mNewPref.getIcon());
65         if (mHelper.mNewPref instanceof HasSliceAction) {
66             setIntent(((HasSliceAction) mHelper.mNewPref).getSliceAction().getAction().getIntent());
67         }
68     }
69 }
70