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     /**
     39      * Sets the item to be clickable or not.
     40      */
     41     public void setClickable(boolean clickable) {
     42         mClickable = clickable;
     43         if (mItemView != null) {
     44             mItemView.setClickable(clickable);
     45         }
     46     }
     47 
     48     /**
     49      * Returns whether this item is enabled.
     50      */
     51     public boolean isEnabled() {
     52         return mEnabled;
     53     }
     54 
     55     public final void notifyUpdated() {
     56         if (mItemView != null) {
     57             onUpdate();
     58         }
     59     }
     60 
     61     protected abstract int getResourceId();
     62 
     63     protected void onBind(View view) {
     64         mItemView = view;
     65     }
     66 
     67     protected void onUnbind() {
     68         mItemView = null;
     69     }
     70 
     71     /**
     72      * Called after onBind is called and when {@link #notifyUpdated} is called.
     73      * {@link #notifyUpdated} is usually called by {@link SideFragment#notifyItemChanged} and
     74      * {@link SideFragment#notifyItemsChanged}.
     75      */
     76     protected void onUpdate() {
     77         setEnabledInternal(mItemView, mEnabled);
     78         mItemView.setClickable(mClickable);
     79     }
     80 
     81     protected abstract void onSelected();
     82 
     83     protected void onFocused() {
     84     }
     85 
     86     /**
     87      * Returns true if the item is bound, i.e., onBind is called.
     88      */
     89     protected boolean isBound() {
     90         return mItemView != null;
     91     }
     92 
     93     private void setEnabledInternal(View view, boolean enabled) {
     94         view.setEnabled(enabled);
     95         if (view instanceof ViewGroup) {
     96             ViewGroup parent = (ViewGroup) view;
     97             int childCount = parent.getChildCount();
     98             for (int i = 0; i < childCount; ++i) {
     99                 setEnabledInternal(parent.getChildAt(i), enabled);
    100             }
    101         }
    102     }
    103 }