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