diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2018-04-10 23:27:31 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2018-04-10 23:27:31 +0000 |
commit | 3e98abd1a11c903340496b6563a21fd9ca921411 (patch) | |
tree | 7b31dcd02a359b27f32a7476c3ed94bd262f6d82 /java/com/android/dialer/speeddial/SpeedDialAdapter.java | |
parent | 2bdb59f0392e54be3dc2c57c32ce126e1e4af7cf (diff) | |
parent | 2bee0528c1f42b698a606f24da4fa652ceb8d322 (diff) |
Merge "Wire up SpeedDial fragment with SpeedDialUiItemLoader."
Diffstat (limited to 'java/com/android/dialer/speeddial/SpeedDialAdapter.java')
-rw-r--r-- | java/com/android/dialer/speeddial/SpeedDialAdapter.java | 118 |
1 files changed, 84 insertions, 34 deletions
diff --git a/java/com/android/dialer/speeddial/SpeedDialAdapter.java b/java/com/android/dialer/speeddial/SpeedDialAdapter.java index 5f7b68e5c..3312397c7 100644 --- a/java/com/android/dialer/speeddial/SpeedDialAdapter.java +++ b/java/com/android/dialer/speeddial/SpeedDialAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,30 @@ package com.android.dialer.speeddial; +import android.annotation.TargetApi; import android.content.Context; +import android.os.Build.VERSION_CODES; +import android.support.annotation.IntDef; +import android.support.annotation.NonNull; import android.support.annotation.VisibleForTesting; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.GridLayoutManager.SpanSizeLookup; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.LayoutManager; +import android.support.v7.widget.RecyclerView.ViewHolder; +import android.util.ArrayMap; import android.view.LayoutInflater; import android.view.ViewGroup; import com.android.dialer.common.Assert; import com.android.dialer.speeddial.FavoritesViewHolder.FavoriteContactsListener; import com.android.dialer.speeddial.HeaderViewHolder.SpeedDialHeaderListener; -import com.android.dialer.speeddial.SpeedDialCursor.RowType; import com.android.dialer.speeddial.SuggestionViewHolder.SuggestedContactsListener; +import com.android.dialer.speeddial.loader.SpeedDialUiItem; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * RecyclerView adapter for {@link SpeedDialFragment}. @@ -42,14 +53,26 @@ import com.android.dialer.speeddial.SuggestionViewHolder.SuggestedContactsListen * <li>Suggested contacts * </ol> */ -final class SpeedDialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { +@SuppressWarnings("AndroidApiChecker") +@TargetApi(VERSION_CODES.N) +public final class SpeedDialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + + @Retention(RetentionPolicy.SOURCE) + @IntDef({RowType.STARRED_HEADER, RowType.SUGGESTION_HEADER, RowType.STARRED, RowType.SUGGESTION}) + @interface RowType { + int STARRED_HEADER = 0; + int SUGGESTION_HEADER = 1; + int STARRED = 2; + int SUGGESTION = 3; + } private final Context context; private final FavoriteContactsListener favoritesListener; private final SuggestedContactsListener suggestedListener; private final SpeedDialHeaderListener headerListener; - private SpeedDialCursor cursor; + private final Map<Integer, Integer> positionToRowTypeMap = new ArrayMap<>(); + private List<SpeedDialUiItem> speedDialUiItems; public SpeedDialAdapter( Context context, @@ -64,39 +87,45 @@ final class SpeedDialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde @Override public int getItemViewType(int position) { - return cursor.getRowType(position); + return positionToRowTypeMap.get(position); } + @NonNull @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(context); - if (viewType == RowType.STARRED) { - return new FavoritesViewHolder( - inflater.inflate(R.layout.favorite_item_layout, parent, false), favoritesListener); - } else if (viewType == RowType.SUGGESTION) { - return new SuggestionViewHolder( - inflater.inflate(R.layout.suggestion_row_layout, parent, false), suggestedListener); - } else if (viewType == RowType.HEADER) { - return new HeaderViewHolder( - inflater.inflate(R.layout.speed_dial_header_layout, parent, false), headerListener); - } else { - throw Assert.createIllegalStateFailException("Invalid viewType: " + viewType); + switch (viewType) { + case RowType.STARRED: + return new FavoritesViewHolder( + inflater.inflate(R.layout.favorite_item_layout, parent, false), favoritesListener); + case RowType.SUGGESTION: + return new SuggestionViewHolder( + inflater.inflate(R.layout.suggestion_row_layout, parent, false), suggestedListener); + case RowType.STARRED_HEADER: + case RowType.SUGGESTION_HEADER: + return new HeaderViewHolder( + inflater.inflate(R.layout.speed_dial_header_layout, parent, false), headerListener); + default: + throw Assert.createIllegalStateFailException("Invalid viewType: " + viewType); } } @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { - cursor.moveToPosition(position); - switch (cursor.getRowType(position)) { - case RowType.HEADER: - ((HeaderViewHolder) holder).setHeaderText(cursor.getHeader()); - ((HeaderViewHolder) holder).showAddButton(cursor.hasFavorites() && position == 0); - break; + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + switch (getItemViewType(position)) { + case RowType.STARRED_HEADER: + ((HeaderViewHolder) holder).setHeaderText(R.string.favorites_header); + ((HeaderViewHolder) holder).showAddButton(true); + return; + case RowType.SUGGESTION_HEADER: + ((HeaderViewHolder) holder).setHeaderText(R.string.suggestions_header); + ((HeaderViewHolder) holder).showAddButton(false); + return; case RowType.STARRED: - ((FavoritesViewHolder) holder).bind(context, cursor); + ((FavoritesViewHolder) holder).bind(context, speedDialUiItems.get(position - 1)); break; case RowType.SUGGESTION: - ((SuggestionViewHolder) holder).bind(context, cursor); + ((SuggestionViewHolder) holder).bind(context, speedDialUiItems.get(position - 2)); break; default: throw Assert.createIllegalStateFailException("Invalid view holder: " + holder); @@ -105,15 +134,35 @@ final class SpeedDialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde @Override public int getItemCount() { - return cursor == null || cursor.isClosed() ? 0 : cursor.getCount(); + return positionToRowTypeMap.size(); } - public void setCursor(SpeedDialCursor cursor) { - this.cursor = cursor; - notifyDataSetChanged(); + public void setSpeedDialUiItems(List<SpeedDialUiItem> immutableSpeedDialUiItems) { + speedDialUiItems = new ArrayList<>(); + speedDialUiItems.addAll(immutableSpeedDialUiItems); + speedDialUiItems.sort((o1, o2) -> Boolean.compare(o2.isStarred(), o1.isStarred())); + positionToRowTypeMap.clear(); + if (speedDialUiItems.isEmpty()) { + return; + } + + // Show the add favorites even if there are no favorite contacts + positionToRowTypeMap.put(0, RowType.STARRED_HEADER); + int positionOfSuggestionHeader = 1; + for (int i = 0; i < speedDialUiItems.size(); i++) { + if (speedDialUiItems.get(i).isStarred()) { + positionToRowTypeMap.put(i + 1, RowType.STARRED); // +1 for the header + positionOfSuggestionHeader++; + } else { + positionToRowTypeMap.put(i + 2, RowType.SUGGESTION); // +2 for both headers + } + } + if (!speedDialUiItems.get(speedDialUiItems.size() - 1).isStarred()) { + positionToRowTypeMap.put(positionOfSuggestionHeader, RowType.SUGGESTION_HEADER); + } } - LayoutManager getLayoutManager(Context context) { + /* package-private */ LayoutManager getLayoutManager(Context context) { GridLayoutManager layoutManager = new GridLayoutManager(context, 3 /* spanCount */); layoutManager.setSpanSizeLookup( new SpanSizeLookup() { @@ -127,15 +176,16 @@ final class SpeedDialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde @VisibleForTesting int getSpanSize(int position) { - switch (cursor.getRowType(position)) { + switch (getItemViewType(position)) { case RowType.SUGGESTION: - case RowType.HEADER: + case RowType.STARRED_HEADER: + case RowType.SUGGESTION_HEADER: return 3; // span the whole screen case RowType.STARRED: return 1; // span 1/3 of the screen default: throw Assert.createIllegalStateFailException( - "Invalid row type: " + cursor.getRowType(position)); + "Invalid row type: " + positionToRowTypeMap.get(position)); } } } |