Home | History | Annotate | Download | only in sidepanel
      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.support.annotation.UiThread;
     20 import android.view.View;
     21 import android.view.ViewGroup;
     22 
     23 @UiThread
     24 public abstract class Item {
     25     private View mItemView;
     26     private boolean mEnabled = true;
     27     private boolean mClickable = true;
     28 
     29     public void setEnabled(boolean enabled) {
     30         if (mEnabled != enabled) {
     31             mEnabled = enabled;
     32             if (mItemView != null) {
     33                 setEnabledInternal(mItemView, enabled);
     34             }
     35         }
     36     }
     37 
     38     /** Sets the item to be clickable or not. */
     39     public void setClickable(boolean clickable) {
     40         mClickable = clickable;
     41         if (mItemView != null) {
     42             mItemView.setClickable(clickable);
     43         }
     44     }
     45 
     46     /** Returns whether this item is enabled. */
     47     public boolean isEnabled() {
     48         return mEnabled;
     49     }
     50 
     51     public final void notifyUpdated() {
     52         if (mItemView != null) {
     53             onUpdate();
     54         }
     55     }
     56 
     57     protected abstract int getResourceId();
     58 
     59     protected void onBind(View view) {
     60         mItemView = view;
     61     }
     62 
     63     protected void onUnbind() {
     64         mItemView = null;
     65     }
     66 
     67     /**
     68      * Called after onBind is called and when {@link #notifyUpdated} is called. {@link
     69      * #notifyUpdated} is usually called by {@link SideFragment#notifyItemChanged} and {@link
     70      * SideFragment#notifyItemsChanged}.
     71      */
     72     protected void onUpdate() {
     73         setEnabledInternal(mItemView, mEnabled);
     74         mItemView.setClickable(mClickable);
     75     }
     76 
     77     protected abstract void onSelected();
     78 
     79     protected void onFocused() {}
     80 
     81     /** Returns true if the item is bound, i.e., onBind is called. */
     82     protected boolean isBound() {
     83         return mItemView != null;
     84     }
     85 
     86     private void setEnabledInternal(View view, boolean enabled) {
     87         view.setEnabled(enabled);
     88         if (view instanceof ViewGroup) {
     89             ViewGroup parent = (ViewGroup) view;
     90             int childCount = parent.getChildCount();
     91             for (int i = 0; i < childCount; ++i) {
     92                 setEnabledInternal(parent.getChildAt(i), enabled);
     93             }
     94         }
     95     }
     96 }
     97