Home | History | Annotate | Download | only in util
      1 package com.android.tv.util;
      2 
      3 import android.content.Context;
      4 import android.util.SparseArray;
      5 import android.view.LayoutInflater;
      6 import android.view.View;
      7 import android.view.ViewGroup;
      8 
      9 import java.util.ArrayList;
     10 
     11 /**
     12  * A cache for the views.
     13  */
     14 public class ViewCache {
     15     private final static SparseArray<ArrayList<View>> mViews = new SparseArray();
     16 
     17     private static ViewCache sViewCache;
     18 
     19     private ViewCache() { }
     20 
     21     /**
     22      * Returns an instance of the view cache.
     23      */
     24     public static ViewCache getInstance() {
     25         if (sViewCache == null) {
     26             sViewCache = new ViewCache();
     27         }
     28         return sViewCache;
     29     }
     30 
     31     /**
     32      * Returns if the view cache is empty.
     33      */
     34     public boolean isEmpty() {
     35         return mViews.size() == 0;
     36     }
     37 
     38     /**
     39      * Stores a view into this view cache.
     40      */
     41     public void putView(int resId, View view) {
     42         ArrayList<View> views = mViews.get(resId);
     43         if (views == null) {
     44             views = new ArrayList();
     45             mViews.put(resId, views);
     46         }
     47         views.add(view);
     48     }
     49 
     50     /**
     51      * Stores multi specific views into the view cache.
     52      */
     53     public void putView(Context context, int resId, ViewGroup fakeParent, int num) {
     54         LayoutInflater inflater =
     55                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     56         ArrayList<View> views = mViews.get(resId);
     57         if (views == null) {
     58             views = new ArrayList<>();
     59             mViews.put(resId, views);
     60         }
     61         for (int i = 0; i < num; i++) {
     62             View view = inflater.inflate(resId, fakeParent, false);
     63             views.add(view);
     64         }
     65     }
     66 
     67     /**
     68      * Returns the view for specific resource id.
     69      */
     70     public View getView(int resId) {
     71         ArrayList<View> views = mViews.get(resId);
     72         if (views != null && !views.isEmpty()) {
     73             View view = views.remove(views.size() - 1);
     74             if (views.isEmpty()) {
     75                 mViews.remove(resId);
     76             }
     77             return view;
     78         } else {
     79             return null;
     80         }
     81     }
     82 
     83     /**
     84      * Returns the view if exists, or create a new view for the specific resource id.
     85      */
     86     public View getOrCreateView(LayoutInflater inflater, int resId, ViewGroup container) {
     87         View view = getView(resId);
     88         if (view == null) {
     89             view = inflater.inflate(resId, container, false);
     90         }
     91         return view;
     92     }
     93 
     94     /**
     95      * Clears the view cache.
     96      */
     97     public void clear() {
     98         mViews.clear();
     99     }
    100 }
    101