1 /*
2  * Copyright (C) 2015 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.ui.sidepanel;
18 
19 import android.view.View;
20 import android.widget.CompoundButton;
21 import android.widget.TextView;
22 
23 import com.android.tv.R;
24 
25 public abstract class CompoundButtonItem extends Item {
26     private final String mCheckedTitle;
27     private final String mUncheckedTitle;
28     private final String mDescription;
29     private boolean mChecked;
30     private TextView mTextView;
31     private CompoundButton mCompoundButton;
32 
CompoundButtonItem(String title, String description)33     public CompoundButtonItem(String title, String description) {
34         this(title, title, description);
35     }
36 
CompoundButtonItem(String checkedTitle, String uncheckedTitle, String description)37     public CompoundButtonItem(String checkedTitle, String uncheckedTitle, String description) {
38         mCheckedTitle = checkedTitle;
39         mUncheckedTitle = uncheckedTitle;
40         mDescription = description;
41     }
42 
getCompoundButtonId()43     protected abstract int getCompoundButtonId();
44 
getTitleViewId()45     protected int getTitleViewId() {
46         return R.id.title;
47     }
48 
getDescriptionViewId()49     protected int getDescriptionViewId() {
50         return R.id.description;
51     }
52 
53     @Override
onBind(View view)54     protected void onBind(View view) {
55         super.onBind(view);
56         mCompoundButton = (CompoundButton) view.findViewById(getCompoundButtonId());
57         mTextView = (TextView) view.findViewById(getTitleViewId());
58         TextView descriptionView = (TextView) view.findViewById(getDescriptionViewId());
59         if (mDescription != null) {
60             descriptionView.setVisibility(View.VISIBLE);
61             descriptionView.setText(mDescription);
62         } else {
63             descriptionView.setVisibility(View.GONE);
64         }
65     }
66 
67     @Override
onUnbind()68     protected void onUnbind() {
69         super.onUnbind();
70         mTextView = null;
71         mCompoundButton = null;
72     }
73 
74     @Override
onUpdate()75     protected void onUpdate() {
76         super.onUpdate();
77         updateInternal();
78     }
79 
setChecked(boolean checked)80     public void setChecked(boolean checked) {
81         if (mChecked != checked) {
82             mChecked = checked;
83             updateInternal();
84         }
85     }
86 
isChecked()87     public boolean isChecked() {
88         return mChecked;
89     }
90 
updateInternal()91     private void updateInternal() {
92         if (isBound()) {
93             mTextView.setText(mChecked ? mCheckedTitle : mUncheckedTitle);
94             mCompoundButton.setChecked(mChecked);
95         }
96     }
97 }
98