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