AllAppsGridAdapter.java revision 2494c3f1681e71af06556ef47de16f018811f7e3
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 */
16package com.android.launcher3.allapps;
17
18import android.content.Context;
19import android.content.Intent;
20import android.content.pm.PackageManager;
21import android.content.pm.ResolveInfo;
22import android.content.res.Resources;
23import android.graphics.Canvas;
24import android.graphics.Paint;
25import android.graphics.PointF;
26import android.graphics.Rect;
27import android.graphics.drawable.Drawable;
28import android.support.v4.view.accessibility.AccessibilityRecordCompat;
29import android.support.v4.view.accessibility.AccessibilityEventCompat;
30import android.net.Uri;
31import android.support.v7.widget.GridLayoutManager;
32import android.support.v7.widget.RecyclerView;
33import android.view.Gravity;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.view.ViewConfiguration;
37import android.view.ViewGroup;
38import android.view.accessibility.AccessibilityEvent;
39import android.widget.TextView;
40import com.android.launcher3.AppInfo;
41import com.android.launcher3.BubbleTextView;
42import com.android.launcher3.Launcher;
43import com.android.launcher3.LauncherAppState;
44import com.android.launcher3.R;
45import com.android.launcher3.Utilities;
46import com.android.launcher3.util.Thunk;
47
48import java.util.HashMap;
49import java.util.List;
50
51
52/**
53 * The grid view adapter of all the apps.
54 */
55public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.ViewHolder> {
56
57    public static final String TAG = "AppsGridAdapter";
58    private static final boolean DEBUG = false;
59
60    // A section break in the grid
61    public static final int SECTION_BREAK_VIEW_TYPE = 0;
62    // A normal icon
63    public static final int ICON_VIEW_TYPE = 1;
64    // A prediction icon
65    public static final int PREDICTION_ICON_VIEW_TYPE = 2;
66    // The message shown when there are no filtered results
67    public static final int EMPTY_SEARCH_VIEW_TYPE = 3;
68    // A divider that separates the apps list and the search market button
69    public static final int SEARCH_MARKET_DIVIDER_VIEW_TYPE = 4;
70    // The message to continue to a market search when there are no filtered results
71    public static final int SEARCH_MARKET_VIEW_TYPE = 5;
72
73    public interface BindViewCallback {
74        public void onBindView(ViewHolder holder);
75    }
76
77    /**
78     * ViewHolder for each icon.
79     */
80    public static class ViewHolder extends RecyclerView.ViewHolder {
81        public View mContent;
82
83        public ViewHolder(View v) {
84            super(v);
85            mContent = v;
86        }
87    }
88
89    /**
90     * A subclass of GridLayoutManager that overrides accessibility values during app search.
91     */
92    public class AppsGridLayoutManager extends GridLayoutManager {
93
94        public AppsGridLayoutManager(Context context) {
95            super(context, 1, GridLayoutManager.VERTICAL, false);
96        }
97
98        @Override
99        public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
100            super.onInitializeAccessibilityEvent(event);
101
102            // Ensure that we only report the number apps for accessibility not including other
103            // adapter views
104            final AccessibilityRecordCompat record = AccessibilityEventCompat
105                    .asRecord(event);
106            record.setItemCount(mApps.getNumFilteredApps());
107        }
108
109        @Override
110        public int getRowCountForAccessibility(RecyclerView.Recycler recycler,
111                RecyclerView.State state) {
112            if (mApps.hasNoFilteredResults()) {
113                // Disregard the no-search-results text as a list item for accessibility
114                return 0;
115            } else {
116                return super.getRowCountForAccessibility(recycler, state);
117            }
118        }
119    }
120
121    /**
122     * Helper class to size the grid items.
123     */
124    public class GridSpanSizer extends GridLayoutManager.SpanSizeLookup {
125
126        public GridSpanSizer() {
127            super();
128            setSpanIndexCacheEnabled(true);
129        }
130
131        @Override
132        public int getSpanSize(int position) {
133            switch (mApps.getAdapterItems().get(position).viewType) {
134                case AllAppsGridAdapter.ICON_VIEW_TYPE:
135                case AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE:
136                    return 1;
137                default:
138                    // Section breaks span the full width
139                    return mAppsPerRow;
140            }
141        }
142    }
143
144    /**
145     * Helper class to draw the section headers
146     */
147    public class GridItemDecoration extends RecyclerView.ItemDecoration {
148
149        private static final boolean DEBUG_SECTION_MARGIN = false;
150        private static final boolean FADE_OUT_SECTIONS = false;
151
152        private HashMap<String, PointF> mCachedSectionBounds = new HashMap<>();
153        private Rect mTmpBounds = new Rect();
154
155        @Override
156        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
157            if (mApps.hasFilter() || mAppsPerRow == 0) {
158                return;
159            }
160
161            if (DEBUG_SECTION_MARGIN) {
162                Paint p = new Paint();
163                p.setColor(0x33ff0000);
164                c.drawRect(mBackgroundPadding.left, 0, mBackgroundPadding.left + mSectionNamesMargin,
165                        parent.getMeasuredHeight(), p);
166            }
167
168            List<AlphabeticalAppsList.AdapterItem> items = mApps.getAdapterItems();
169            boolean hasDrawnPredictedAppsDivider = false;
170            boolean showSectionNames = mSectionNamesMargin > 0;
171            int childCount = parent.getChildCount();
172            int lastSectionTop = 0;
173            int lastSectionHeight = 0;
174            for (int i = 0; i < childCount; i++) {
175                View child = parent.getChildAt(i);
176                ViewHolder holder = (ViewHolder) parent.getChildViewHolder(child);
177                if (!isValidHolderAndChild(holder, child, items)) {
178                    continue;
179                }
180
181                if (shouldDrawItemDivider(holder, items) && !hasDrawnPredictedAppsDivider) {
182                    // Draw the divider under the predicted apps
183                    int top = child.getTop() + child.getHeight() + mPredictionBarDividerOffset;
184                    c.drawLine(mBackgroundPadding.left, top,
185                            parent.getWidth() - mBackgroundPadding.right, top,
186                            mPredictedAppsDividerPaint);
187                    hasDrawnPredictedAppsDivider = true;
188
189                } else if (showSectionNames && shouldDrawItemSection(holder, i, items)) {
190                    // At this point, we only draw sections for each section break;
191                    int viewTopOffset = (2 * child.getPaddingTop());
192                    int pos = holder.getPosition();
193                    AlphabeticalAppsList.AdapterItem item = items.get(pos);
194                    AlphabeticalAppsList.SectionInfo sectionInfo = item.sectionInfo;
195
196                    // Draw all the sections for this index
197                    String lastSectionName = item.sectionName;
198                    for (int j = item.sectionAppIndex; j < sectionInfo.numApps; j++, pos++) {
199                        AlphabeticalAppsList.AdapterItem nextItem = items.get(pos);
200                        String sectionName = nextItem.sectionName;
201                        if (nextItem.sectionInfo != sectionInfo) {
202                            break;
203                        }
204                        if (j > item.sectionAppIndex && sectionName.equals(lastSectionName)) {
205                            continue;
206                        }
207
208
209                        // Find the section name bounds
210                        PointF sectionBounds = getAndCacheSectionBounds(sectionName);
211
212                        // Calculate where to draw the section
213                        int sectionBaseline = (int) (viewTopOffset + sectionBounds.y);
214                        int x = mIsRtl ?
215                                parent.getWidth() - mBackgroundPadding.left - mSectionNamesMargin :
216                                        mBackgroundPadding.left;
217                        x += (int) ((mSectionNamesMargin - sectionBounds.x) / 2f);
218                        int y = child.getTop() + sectionBaseline;
219
220                        // Determine whether this is the last row with apps in that section, if
221                        // so, then fix the section to the row allowing it to scroll past the
222                        // baseline, otherwise, bound it to the baseline so it's in the viewport
223                        int appIndexInSection = items.get(pos).sectionAppIndex;
224                        int nextRowPos = Math.min(items.size() - 1,
225                                pos + mAppsPerRow - (appIndexInSection % mAppsPerRow));
226                        AlphabeticalAppsList.AdapterItem nextRowItem = items.get(nextRowPos);
227                        boolean fixedToRow = !sectionName.equals(nextRowItem.sectionName);
228                        if (!fixedToRow) {
229                            y = Math.max(sectionBaseline, y);
230                        }
231
232                        // In addition, if it overlaps with the last section that was drawn, then
233                        // offset it so that it does not overlap
234                        if (lastSectionHeight > 0 && y <= (lastSectionTop + lastSectionHeight)) {
235                            y += lastSectionTop - y + lastSectionHeight;
236                        }
237
238                        // Draw the section header
239                        if (FADE_OUT_SECTIONS) {
240                            int alpha = 255;
241                            if (fixedToRow) {
242                                alpha = Math.min(255,
243                                        (int) (255 * (Math.max(0, y) / (float) sectionBaseline)));
244                            }
245                            mSectionTextPaint.setAlpha(alpha);
246                        }
247                        c.drawText(sectionName, x, y, mSectionTextPaint);
248
249                        lastSectionTop = y;
250                        lastSectionHeight = (int) (sectionBounds.y + mSectionHeaderOffset);
251                        lastSectionName = sectionName;
252                    }
253                    i += (sectionInfo.numApps - item.sectionAppIndex);
254                }
255            }
256        }
257
258        @Override
259        public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
260                RecyclerView.State state) {
261            // Do nothing
262        }
263
264        /**
265         * Given a section name, return the bounds of the given section name.
266         */
267        private PointF getAndCacheSectionBounds(String sectionName) {
268            PointF bounds = mCachedSectionBounds.get(sectionName);
269            if (bounds == null) {
270                mSectionTextPaint.getTextBounds(sectionName, 0, sectionName.length(), mTmpBounds);
271                bounds = new PointF(mSectionTextPaint.measureText(sectionName), mTmpBounds.height());
272                mCachedSectionBounds.put(sectionName, bounds);
273            }
274            return bounds;
275        }
276
277        /**
278         * Returns whether we consider this a valid view holder for us to draw a divider or section for.
279         */
280        private boolean isValidHolderAndChild(ViewHolder holder, View child,
281                List<AlphabeticalAppsList.AdapterItem> items) {
282            // Ensure item is not already removed
283            GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams)
284                    child.getLayoutParams();
285            if (lp.isItemRemoved()) {
286                return false;
287            }
288            // Ensure we have a valid holder
289            if (holder == null) {
290                return false;
291            }
292            // Ensure we have a holder position
293            int pos = holder.getPosition();
294            if (pos < 0 || pos >= items.size()) {
295                return false;
296            }
297            return true;
298        }
299
300        /**
301         * Returns whether to draw the divider for a given child.
302         */
303        private boolean shouldDrawItemDivider(ViewHolder holder,
304                List<AlphabeticalAppsList.AdapterItem> items) {
305            int pos = holder.getPosition();
306            return items.get(pos).viewType == AllAppsGridAdapter.PREDICTION_ICON_VIEW_TYPE;
307        }
308
309        /**
310         * Returns whether to draw the section for the given child.
311         */
312        private boolean shouldDrawItemSection(ViewHolder holder, int childIndex,
313                List<AlphabeticalAppsList.AdapterItem> items) {
314            int pos = holder.getPosition();
315            AlphabeticalAppsList.AdapterItem item = items.get(pos);
316
317            // Ensure it's an icon
318            if (item.viewType != AllAppsGridAdapter.ICON_VIEW_TYPE) {
319                return false;
320            }
321            // Draw the section header for the first item in each section
322            return (childIndex == 0) ||
323                    (items.get(pos - 1).viewType == AllAppsGridAdapter.SECTION_BREAK_VIEW_TYPE);
324        }
325    }
326
327    private final Launcher mLauncher;
328    private final LayoutInflater mLayoutInflater;
329    private final AlphabeticalAppsList mApps;
330    private final GridLayoutManager mGridLayoutMgr;
331    private final GridSpanSizer mGridSizer;
332    private final GridItemDecoration mItemDecoration;
333    private final View.OnTouchListener mTouchListener;
334    private final View.OnClickListener mIconClickListener;
335    private final View.OnLongClickListener mIconLongClickListener;
336
337    private final Rect mBackgroundPadding = new Rect();
338    private final boolean mIsRtl;
339
340    // Section drawing
341    private final int mSectionNamesMargin;
342    private final int mSectionHeaderOffset;
343    private final Paint mSectionTextPaint;
344    private final Paint mPredictedAppsDividerPaint;
345
346    private final int mPredictionBarDividerOffset;
347    private int mAppsPerRow;
348    private BindViewCallback mBindViewCallback;
349    private AllAppsSearchBarController mSearchController;
350
351    // The text to show when there are no search results and no market search handler.
352    private String mEmptySearchMessage;
353    // The name of the market app which handles searches, to be used in the format str
354    // below when updating the search-market view.  Only needs to be loaded once.
355    private String mMarketAppName;
356    // The text to show when there is a market app which can handle a specific query, updated
357    // each time the search query changes.
358    private String mMarketSearchMessage;
359    // The intent to send off to the market app, updated each time the search query changes.
360    private Intent mMarketSearchIntent;
361
362    public AllAppsGridAdapter(Launcher launcher, AlphabeticalAppsList apps,
363            View.OnTouchListener touchListener, View.OnClickListener iconClickListener,
364            View.OnLongClickListener iconLongClickListener) {
365        Resources res = launcher.getResources();
366        mLauncher = launcher;
367        mApps = apps;
368        mEmptySearchMessage = res.getString(R.string.all_apps_loading_message);
369        mGridSizer = new GridSpanSizer();
370        mGridLayoutMgr = new AppsGridLayoutManager(launcher);
371        mGridLayoutMgr.setSpanSizeLookup(mGridSizer);
372        mItemDecoration = new GridItemDecoration();
373        mLayoutInflater = LayoutInflater.from(launcher);
374        mTouchListener = touchListener;
375        mIconClickListener = iconClickListener;
376        mIconLongClickListener = iconLongClickListener;
377        mSectionNamesMargin = res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
378        mSectionHeaderOffset = res.getDimensionPixelSize(R.dimen.all_apps_grid_section_y_offset);
379        mIsRtl = Utilities.isRtl(res);
380
381        mSectionTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
382        mSectionTextPaint.setTextSize(res.getDimensionPixelSize(
383                R.dimen.all_apps_grid_section_text_size));
384        mSectionTextPaint.setColor(res.getColor(R.color.all_apps_grid_section_text_color));
385
386        mPredictedAppsDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
387        mPredictedAppsDividerPaint.setStrokeWidth(Utilities.pxFromDp(1f, res.getDisplayMetrics()));
388        mPredictedAppsDividerPaint.setColor(0x1E000000);
389        mPredictionBarDividerOffset =
390                (-res.getDimensionPixelSize(R.dimen.all_apps_prediction_icon_bottom_padding) +
391                        res.getDimensionPixelSize(R.dimen.all_apps_icon_top_bottom_padding)) / 2;
392    }
393
394    /**
395     * Sets the number of apps per row.
396     */
397    public void setNumAppsPerRow(int appsPerRow) {
398        mAppsPerRow = appsPerRow;
399        mGridLayoutMgr.setSpanCount(appsPerRow);
400    }
401
402    public void setSearchController(AllAppsSearchBarController searchController) {
403        mSearchController = searchController;
404
405        // Resolve the market app handling additional searches
406        PackageManager pm = mLauncher.getPackageManager();
407        ResolveInfo marketInfo = pm.resolveActivity(mSearchController.createMarketSearchIntent(""),
408                PackageManager.MATCH_DEFAULT_ONLY);
409        if (marketInfo != null) {
410            mMarketAppName = marketInfo.loadLabel(pm).toString();
411        }
412    }
413
414    /**
415     * Sets the last search query that was made, used to show when there are no results and to also
416     * seed the intent for searching the market.
417     */
418    public void setLastSearchQuery(String query) {
419        Resources res = mLauncher.getResources();
420        String formatStr = res.getString(R.string.all_apps_no_search_results);
421        mEmptySearchMessage = String.format(formatStr, query);
422        if (mMarketAppName != null) {
423            mMarketSearchMessage = String.format(res.getString(R.string.all_apps_search_market_message),
424                    mMarketAppName);
425            mMarketSearchIntent = mSearchController.createMarketSearchIntent(query);
426        }
427    }
428
429    /**
430     * Sets the callback for when views are bound.
431     */
432    public void setBindViewCallback(BindViewCallback cb) {
433        mBindViewCallback = cb;
434    }
435
436    /**
437     * Notifies the adapter of the background padding so that it can draw things correctly in the
438     * item decorator.
439     */
440    public void updateBackgroundPadding(Rect padding) {
441        mBackgroundPadding.set(padding);
442    }
443
444    /**
445     * Returns the grid layout manager.
446     */
447    public GridLayoutManager getLayoutManager() {
448        return mGridLayoutMgr;
449    }
450
451    /**
452     * Returns the item decoration for the recycler view.
453     */
454    public RecyclerView.ItemDecoration getItemDecoration() {
455        // We don't draw any headers when we are uncomfortably dense
456        return mItemDecoration;
457    }
458
459    @Override
460    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
461        switch (viewType) {
462            case SECTION_BREAK_VIEW_TYPE:
463                return new ViewHolder(new View(parent.getContext()));
464            case ICON_VIEW_TYPE: {
465                BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(
466                        R.layout.all_apps_icon, parent, false);
467                icon.setOnTouchListener(mTouchListener);
468                icon.setOnClickListener(mIconClickListener);
469                icon.setOnLongClickListener(mIconLongClickListener);
470                icon.setLongPressTimeout(ViewConfiguration.get(parent.getContext())
471                        .getLongPressTimeout());
472                icon.setFocusable(true);
473                return new ViewHolder(icon);
474            }
475            case PREDICTION_ICON_VIEW_TYPE: {
476                BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(
477                        R.layout.all_apps_prediction_bar_icon, parent, false);
478                icon.setOnTouchListener(mTouchListener);
479                icon.setOnClickListener(mIconClickListener);
480                icon.setOnLongClickListener(mIconLongClickListener);
481                icon.setLongPressTimeout(ViewConfiguration.get(parent.getContext())
482                        .getLongPressTimeout());
483                icon.setFocusable(true);
484                return new ViewHolder(icon);
485            }
486            case EMPTY_SEARCH_VIEW_TYPE:
487                return new ViewHolder(mLayoutInflater.inflate(R.layout.all_apps_empty_search,
488                        parent, false));
489            case SEARCH_MARKET_DIVIDER_VIEW_TYPE:
490                return new ViewHolder(mLayoutInflater.inflate(R.layout.all_apps_search_market_divider,
491                        parent, false));
492            case SEARCH_MARKET_VIEW_TYPE:
493                View searchMarketView = mLayoutInflater.inflate(R.layout.all_apps_search_market,
494                        parent, false);
495                searchMarketView.setOnClickListener(new View.OnClickListener() {
496                    @Override
497                    public void onClick(View v) {
498                        mLauncher.startActivitySafely(v, mMarketSearchIntent, null);
499                    }
500                });
501                return new ViewHolder(searchMarketView);
502            default:
503                throw new RuntimeException("Unexpected view type");
504        }
505    }
506
507    @Override
508    public void onBindViewHolder(ViewHolder holder, int position) {
509        switch (holder.getItemViewType()) {
510            case ICON_VIEW_TYPE: {
511                AppInfo info = mApps.getAdapterItems().get(position).appInfo;
512                BubbleTextView icon = (BubbleTextView) holder.mContent;
513                icon.applyFromApplicationInfo(info);
514                break;
515            }
516            case PREDICTION_ICON_VIEW_TYPE: {
517                AppInfo info = mApps.getAdapterItems().get(position).appInfo;
518                BubbleTextView icon = (BubbleTextView) holder.mContent;
519                icon.applyFromApplicationInfo(info);
520                break;
521            }
522            case EMPTY_SEARCH_VIEW_TYPE:
523                TextView emptyViewText = (TextView) holder.mContent;
524                emptyViewText.setText(mEmptySearchMessage);
525                emptyViewText.setGravity(mApps.hasNoFilteredResults() ? Gravity.CENTER :
526                        Gravity.START | Gravity.CENTER_VERTICAL);
527                break;
528            case SEARCH_MARKET_VIEW_TYPE:
529                TextView searchView = (TextView) holder.mContent;
530                if (mMarketSearchIntent != null) {
531                    searchView.setVisibility(View.VISIBLE);
532                    searchView.setContentDescription(mMarketSearchMessage);
533                    searchView.setGravity(mApps.hasNoFilteredResults() ? Gravity.CENTER :
534                            Gravity.START | Gravity.CENTER_VERTICAL);
535                    searchView.setText(mMarketSearchMessage);
536                } else {
537                    searchView.setVisibility(View.GONE);
538                }
539                break;
540        }
541        if (mBindViewCallback != null) {
542            mBindViewCallback.onBindView(holder);
543        }
544    }
545
546    @Override
547    public boolean onFailedToRecycleView(ViewHolder holder) {
548        // Always recycle and we will reset the view when it is bound
549        return true;
550    }
551
552    @Override
553    public int getItemCount() {
554        return mApps.getAdapterItems().size();
555    }
556
557    @Override
558    public int getItemViewType(int position) {
559        AlphabeticalAppsList.AdapterItem item = mApps.getAdapterItems().get(position);
560        return item.viewType;
561    }
562}
563