Home | History | Annotate | Download | only in widget
      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 package com.android.launcher3.widget;
     17 
     18 import android.content.Context;
     19 import android.support.v7.widget.RecyclerView;
     20 import android.support.v7.widget.RecyclerView.Adapter;
     21 import android.util.Log;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.View.OnClickListener;
     25 import android.view.View.OnLongClickListener;
     26 import android.view.ViewGroup;
     27 
     28 import com.android.launcher3.IconCache;
     29 import com.android.launcher3.R;
     30 import com.android.launcher3.WidgetPreviewLoader;
     31 import com.android.launcher3.model.WidgetItem;
     32 import com.android.launcher3.util.LabelComparator;
     33 import com.android.launcher3.util.PackageUserKey;
     34 
     35 import java.util.ArrayList;
     36 import java.util.Collections;
     37 import java.util.Comparator;
     38 import java.util.Iterator;
     39 import java.util.List;
     40 
     41 /**
     42  * List view adapter for the widget tray.
     43  *
     44  * <p>Memory vs. Performance:
     45  * The less number of types of views are inserted into a {@link RecyclerView}, the more recycling
     46  * happens and less memory is consumed. {@link #getItemViewType} was not overridden as there is
     47  * only a single type of view.
     48  */
     49 public class WidgetsListAdapter extends Adapter<WidgetsRowViewHolder> {
     50 
     51     private static final String TAG = "WidgetsListAdapter";
     52     private static final boolean DEBUG = false;
     53 
     54     private final WidgetPreviewLoader mWidgetPreviewLoader;
     55     private final LayoutInflater mLayoutInflater;
     56 
     57     private final OnClickListener mIconClickListener;
     58     private final OnLongClickListener mIconLongClickListener;
     59     private final int mIndent;
     60     private ArrayList<WidgetListRowEntry> mEntries = new ArrayList<>();
     61     private final WidgetsDiffReporter mDiffReporter;
     62 
     63     private boolean mApplyBitmapDeferred;
     64 
     65     public WidgetsListAdapter(Context context, LayoutInflater layoutInflater,
     66             WidgetPreviewLoader widgetPreviewLoader, IconCache iconCache,
     67             OnClickListener iconClickListener, OnLongClickListener iconLongClickListener) {
     68         mLayoutInflater = layoutInflater;
     69         mWidgetPreviewLoader = widgetPreviewLoader;
     70         mIconClickListener = iconClickListener;
     71         mIconLongClickListener = iconLongClickListener;
     72         mIndent = context.getResources().getDimensionPixelSize(R.dimen.widget_section_indent);
     73         mDiffReporter = new WidgetsDiffReporter(iconCache, this);
     74     }
     75 
     76     /**
     77      * Defers applying bitmap on all the {@link WidgetCell} in the {@param rv}
     78      *
     79      * @see WidgetCell#setApplyBitmapDeferred(boolean)
     80      */
     81     public void setApplyBitmapDeferred(boolean isDeferred, RecyclerView rv) {
     82         mApplyBitmapDeferred = isDeferred;
     83 
     84         for (int i = rv.getChildCount() - 1; i >= 0; i--) {
     85             WidgetsRowViewHolder holder = (WidgetsRowViewHolder)
     86                     rv.getChildViewHolder(rv.getChildAt(i));
     87             for (int j = holder.cellContainer.getChildCount() - 1; j >= 0; j--) {
     88                 View v = holder.cellContainer.getChildAt(j);
     89                 if (v instanceof WidgetCell) {
     90                     ((WidgetCell) v).setApplyBitmapDeferred(mApplyBitmapDeferred);
     91                 }
     92             }
     93         }
     94     }
     95 
     96     /**
     97      * Update the widget list.
     98      */
     99     public void setWidgets(ArrayList<WidgetListRowEntry> tempEntries) {
    100         WidgetListRowEntryComparator rowComparator = new WidgetListRowEntryComparator();
    101         Collections.sort(tempEntries, rowComparator);
    102         mDiffReporter.process(mEntries, tempEntries, rowComparator);
    103     }
    104 
    105     @Override
    106     public int getItemCount() {
    107         return mEntries.size();
    108     }
    109 
    110     public String getSectionName(int pos) {
    111         return mEntries.get(pos).titleSectionName;
    112     }
    113 
    114     @Override
    115     public void onBindViewHolder(WidgetsRowViewHolder holder, int pos) {
    116         WidgetListRowEntry entry = mEntries.get(pos);
    117         List<WidgetItem> infoList = entry.widgets;
    118 
    119         ViewGroup row = holder.cellContainer;
    120         if (DEBUG) {
    121             Log.d(TAG, String.format(
    122                     "onBindViewHolder [pos=%d, widget#=%d, row.getChildCount=%d]",
    123                     pos, infoList.size(), row.getChildCount()));
    124         }
    125 
    126         // Add more views.
    127         // if there are too many, hide them.
    128         int expectedChildCount = infoList.size() + Math.max(0, infoList.size() - 1);
    129         int childCount = row.getChildCount();
    130 
    131         if (expectedChildCount > childCount) {
    132             for (int i = childCount ; i < expectedChildCount; i++) {
    133                 if ((i & 1) == 1) {
    134                     // Add a divider for odd index
    135                     mLayoutInflater.inflate(R.layout.widget_list_divider, row);
    136                 } else {
    137                     // Add cell for even index
    138                     WidgetCell widget = (WidgetCell) mLayoutInflater.inflate(
    139                             R.layout.widget_cell, row, false);
    140 
    141                     // set up touch.
    142                     widget.setOnClickListener(mIconClickListener);
    143                     widget.setOnLongClickListener(mIconLongClickListener);
    144                     row.addView(widget);
    145                 }
    146             }
    147         } else if (expectedChildCount < childCount) {
    148             for (int i = expectedChildCount ; i < childCount; i++) {
    149                 row.getChildAt(i).setVisibility(View.GONE);
    150             }
    151         }
    152 
    153         // Bind the views in the application info section.
    154         holder.title.applyFromPackageItemInfo(entry.pkgItem);
    155 
    156         // Bind the view in the widget horizontal tray region.
    157         for (int i=0; i < infoList.size(); i++) {
    158             WidgetCell widget = (WidgetCell) row.getChildAt(2*i);
    159             widget.applyFromCellItem(infoList.get(i), mWidgetPreviewLoader);
    160             widget.setApplyBitmapDeferred(mApplyBitmapDeferred);
    161             widget.ensurePreview();
    162             widget.setVisibility(View.VISIBLE);
    163 
    164             if (i > 0) {
    165                 row.getChildAt(2*i - 1).setVisibility(View.VISIBLE);
    166             }
    167         }
    168     }
    169 
    170     @Override
    171     public WidgetsRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    172         if (DEBUG) {
    173             Log.v(TAG, "\nonCreateViewHolder");
    174         }
    175 
    176         ViewGroup container = (ViewGroup) mLayoutInflater.inflate(
    177                 R.layout.widgets_list_row_view, parent, false);
    178 
    179         // if the end padding is 0, then container view (horizontal scroll view) doesn't respect
    180         // the end of the linear layout width + the start padding and doesn't allow scrolling.
    181         container.findViewById(R.id.widgets_cell_list).setPaddingRelative(mIndent, 0, 1, 0);
    182 
    183         return new WidgetsRowViewHolder(container);
    184     }
    185 
    186     @Override
    187     public void onViewRecycled(WidgetsRowViewHolder holder) {
    188         int total = holder.cellContainer.getChildCount();
    189         for (int i = 0; i < total; i+=2) {
    190             WidgetCell widget = (WidgetCell) holder.cellContainer.getChildAt(i);
    191             widget.clear();
    192         }
    193     }
    194 
    195     public boolean onFailedToRecycleView(WidgetsRowViewHolder holder) {
    196         // If child views are animating, then the RecyclerView may choose not to recycle the view,
    197         // causing extraneous onCreateViewHolder() calls.  It is safe in this case to continue
    198         // recycling this view, and take care in onViewRecycled() to cancel any existing
    199         // animations.
    200         return true;
    201     }
    202 
    203     @Override
    204     public long getItemId(int pos) {
    205         return pos;
    206     }
    207 
    208     /**
    209      * Comparator for sorting WidgetListRowEntry based on package title
    210      */
    211     public static class WidgetListRowEntryComparator implements Comparator<WidgetListRowEntry> {
    212 
    213         private final LabelComparator mComparator = new LabelComparator();
    214 
    215         @Override
    216         public int compare(WidgetListRowEntry a, WidgetListRowEntry b) {
    217             return mComparator.compare(a.pkgItem.title.toString(), b.pkgItem.title.toString());
    218         }
    219     }
    220 }
    221