1 /*
2  * Copyright (C) 2018 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.car.carlauncher;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 import androidx.recyclerview.widget.RecyclerView;
26 
27 import java.util.Collections;
28 import java.util.List;
29 
30 /**
31  * The adapter that populates the grid view with apps.
32  */
33 final class AppGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
34     public static final int RECENT_APPS_TYPE = 1;
35     public static final int APP_ITEM_TYPE = 2;
36 
37     private final Context mContext;
38     private final int mColumnNumber;
39     private final LayoutInflater mInflater;
40 
41     private List<AppMetaData> mApps;
42     private List<AppMetaData> mMostRecentApps;
43     private boolean mIsDistractionOptimizationRequired;
44 
AppGridAdapter(Context context)45     AppGridAdapter(Context context) {
46         mContext = context;
47         mInflater = LayoutInflater.from(context);
48         mColumnNumber =
49                 mContext.getResources().getInteger(R.integer.car_app_selector_column_number);
50     }
51 
setIsDistractionOptimizationRequired(boolean isDistractionOptimizationRequired)52     void setIsDistractionOptimizationRequired(boolean isDistractionOptimizationRequired) {
53         mIsDistractionOptimizationRequired = isDistractionOptimizationRequired;
54         sortAllApps();
55         notifyDataSetChanged();
56     }
57 
setMostRecentApps(@ullable List<AppMetaData> mostRecentApps)58     void setMostRecentApps(@Nullable List<AppMetaData> mostRecentApps) {
59         mMostRecentApps = mostRecentApps;
60         notifyDataSetChanged();
61     }
62 
setAllApps(@ullable List<AppMetaData> apps)63     void setAllApps(@Nullable List<AppMetaData> apps) {
64         mApps = apps;
65         sortAllApps();
66         notifyDataSetChanged();
67     }
68 
getSpanSizeLookup(int position)69     public int getSpanSizeLookup(int position) {
70         if (position == 0 && hasRecentlyUsedApps()) {
71             return mColumnNumber;
72         }
73         return 1;
74     }
75 
76     @Override
getItemViewType(int position)77     public int getItemViewType(int position) {
78         if (position == 0 && hasRecentlyUsedApps()) {
79             return RECENT_APPS_TYPE;
80         }
81         return APP_ITEM_TYPE;
82     }
83 
84     @Override
onCreateViewHolder(ViewGroup parent, int viewType)85     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
86         if (viewType == RECENT_APPS_TYPE) {
87             View view =
88                     mInflater.inflate(R.layout.recent_apps_row, parent, /* attachToRoot= */ false);
89             return new RecentAppsRowViewHolder(view, mContext);
90         } else {
91             View view = mInflater.inflate(R.layout.app_item, parent, /* attachToRoot= */ false);
92             return new AppItemViewHolder(view, mContext);
93         }
94     }
95 
96     @Override
onBindViewHolder(RecyclerView.ViewHolder holder, int position)97     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
98         switch (holder.getItemViewType()) {
99             case RECENT_APPS_TYPE:
100                 ((RecentAppsRowViewHolder) holder).bind(
101                         mMostRecentApps, mIsDistractionOptimizationRequired);
102                 break;
103             case APP_ITEM_TYPE:
104                 int index = hasRecentlyUsedApps() ? position - 1 : position;
105                 AppMetaData app = mApps.get(index);
106                 ((AppItemViewHolder) holder).bind(app, mIsDistractionOptimizationRequired);
107                 break;
108             default:
109         }
110     }
111 
112     @Override
getItemCount()113     public int getItemCount() {
114         // If there are any most recently launched apps, add a "most recently used apps row item"
115         return (mApps == null ? 0 : mApps.size()) + (hasRecentlyUsedApps() ? 1 : 0);
116     }
117 
hasRecentlyUsedApps()118     private boolean hasRecentlyUsedApps() {
119         return mMostRecentApps != null && mMostRecentApps.size() > 0;
120     }
121 
sortAllApps()122     private void sortAllApps() {
123         if (mApps != null) {
124             Collections.sort(mApps, AppLauncherUtils.ALPHABETICAL_COMPARATOR);
125         }
126     }
127 }