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.util.AttributeSet;
21 
22 import androidx.annotation.Nullable;
23 import androidx.preference.Preference;
24 import androidx.slice.core.SliceActionImpl;
25 
26 import com.android.tv.twopanelsettings.R;
27 
28 /**
29  * A preference that represents a slice provided by another app.
30  */
31 public class SlicePreference extends Preference implements HasSliceAction, HasSliceUri {
32 
33     private static final String TAG = "SlicePreference";
34 
35     private String mUri;
36     private int mActionId;
37     private SliceActionImpl mAction;
38     private SliceActionImpl mFollowUpAction;
39 
SlicePreference(Context context)40     public SlicePreference(Context context) {
41         super(context);
42         init(null);
43     }
44 
SlicePreference(Context context, AttributeSet attrs)45     public SlicePreference(Context context, AttributeSet attrs) {
46         super(context, attrs);
47         init(attrs);
48     }
49 
init(@ullable AttributeSet attrs)50     private void init(@Nullable AttributeSet attrs) {
51         if (attrs != null) {
52             initStyleAttributes(attrs);
53         }
54     }
55 
initStyleAttributes(AttributeSet attrs)56     private void initStyleAttributes(AttributeSet attrs) {
57         final TypedArray a = getContext().obtainStyledAttributes(
58                 attrs, R.styleable.SlicePreference);
59         for (int i = a.getIndexCount() - 1; i >= 0; i--) {
60             int attr = a.getIndex(i);
61             if (attr == R.styleable.SlicePreference_uri) {
62                 mUri = a.getString(attr);
63                 break;
64             }
65         }
66     }
67 
setUri(String uri)68     public void setUri(String uri) {
69         mUri = uri;
70     }
71 
getUri()72     public String getUri() {
73         return mUri;
74     }
75 
76     @Override
getActionId()77     public int getActionId() {
78         return mActionId;
79     }
80 
81     @Override
setActionId(int actionId)82     public void setActionId(int actionId) {
83         mActionId = actionId;
84     }
85 
setSliceAction(SliceActionImpl action)86     public void setSliceAction(SliceActionImpl action) {
87         mAction = action;
88     }
89 
90     @Override
getFollowupSliceAction()91     public SliceActionImpl getFollowupSliceAction() {
92         return mFollowUpAction;
93     }
94 
95     @Override
setFollowupSliceAction(SliceActionImpl sliceAction)96     public void setFollowupSliceAction(SliceActionImpl sliceAction) {
97         mFollowUpAction = sliceAction;
98     }
99 
100     @Override
getSliceAction()101     public SliceActionImpl getSliceAction() {
102         return mAction;
103     }
104 }
105