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 static int sDefaultMaxLine = 0; 27 28 private final String mCheckedTitle; 29 private final String mUncheckedTitle; 30 private final String mDescription; 31 private final int mMaxLine; 32 private boolean mChecked; 33 private TextView mTextView; 34 private CompoundButton mCompoundButton; 35 CompoundButtonItem(String title, String description)36 public CompoundButtonItem(String title, String description) { 37 this(title, title, description); 38 } 39 CompoundButtonItem(String checkedTitle, String uncheckedTitle, String description)40 public CompoundButtonItem(String checkedTitle, String uncheckedTitle, String description) { 41 mCheckedTitle = checkedTitle; 42 mUncheckedTitle = uncheckedTitle; 43 mDescription = description; 44 mMaxLine = 0; 45 } 46 CompoundButtonItem(String checkedTitle, String uncheckedTitle, String description, int maxLine)47 public CompoundButtonItem(String checkedTitle, String uncheckedTitle, String description, 48 int maxLine) { 49 mCheckedTitle = checkedTitle; 50 mUncheckedTitle = uncheckedTitle; 51 mDescription = description; 52 mMaxLine = maxLine; 53 } 54 getCompoundButtonId()55 protected abstract int getCompoundButtonId(); 56 getTitleViewId()57 protected int getTitleViewId() { 58 return R.id.title; 59 } 60 getDescriptionViewId()61 protected int getDescriptionViewId() { 62 return R.id.description; 63 } 64 65 @Override onBind(View view)66 protected void onBind(View view) { 67 super.onBind(view); 68 mCompoundButton = (CompoundButton) view.findViewById(getCompoundButtonId()); 69 mTextView = (TextView) view.findViewById(getTitleViewId()); 70 TextView descriptionView = (TextView) view.findViewById(getDescriptionViewId()); 71 if (mDescription != null) { 72 if (mMaxLine != 0) { 73 descriptionView.setMaxLines(mMaxLine); 74 } else { 75 if (sDefaultMaxLine == 0) { 76 sDefaultMaxLine = view.getContext().getResources() 77 .getInteger(R.integer.option_item_description_max_lines); 78 } 79 descriptionView.setMaxLines(sDefaultMaxLine); 80 } 81 descriptionView.setVisibility(View.VISIBLE); 82 descriptionView.setText(mDescription); 83 } else { 84 descriptionView.setVisibility(View.GONE); 85 } 86 } 87 88 @Override onUnbind()89 protected void onUnbind() { 90 super.onUnbind(); 91 mTextView = null; 92 mCompoundButton = null; 93 } 94 95 @Override onUpdate()96 protected void onUpdate() { 97 super.onUpdate(); 98 updateInternal(); 99 } 100 setChecked(boolean checked)101 public void setChecked(boolean checked) { 102 if (mChecked != checked) { 103 mChecked = checked; 104 updateInternal(); 105 } 106 } 107 isChecked()108 public boolean isChecked() { 109 return mChecked; 110 } 111 updateInternal()112 private void updateInternal() { 113 if (isBound()) { 114 mTextView.setText(mChecked ? mCheckedTitle : mUncheckedTitle); 115 mCompoundButton.setChecked(mChecked); 116 } 117 } 118 } 119