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 import android.widget.LinearLayout; 25 26 import androidx.recyclerview.widget.RecyclerView; 27 28 import java.util.List; 29 30 /** 31 * View holder that contains a row of most recently used apps and a divider. 32 */ 33 public class RecentAppsRowViewHolder extends RecyclerView.ViewHolder { 34 private final Context mContext; 35 private final int mColumnNumber; 36 private final LinearLayout mRecentAppsRow; 37 RecentAppsRowViewHolder(View view, Context context)38 public RecentAppsRowViewHolder(View view, Context context) { 39 super(view); 40 mContext = context; 41 mRecentAppsRow = view.findViewById(R.id.recent_apps_row); 42 mColumnNumber = context.getResources().getInteger(R.integer.car_app_selector_column_number); 43 } 44 45 /** 46 * Binds the most recently used apps row view with the list of most recently used app meta data. 47 * 48 * @param apps Pass {@code null} will empty out the row. 49 */ bind(@ullable List<AppMetaData> apps, boolean isDistractionOptimizationRequired)50 public void bind(@Nullable List<AppMetaData> apps, boolean isDistractionOptimizationRequired) { 51 // Empty out the views 52 mRecentAppsRow.removeAllViews(); 53 mRecentAppsRow.setWeightSum(mColumnNumber); 54 55 if (apps == null) { 56 return; 57 } 58 59 int size = Math.min(mColumnNumber, apps.size()); 60 for (int i = 0; i < size; i++) { 61 View view = 62 LayoutInflater.from(mContext).inflate(R.layout.app_item, mRecentAppsRow, false); 63 64 AppItemViewHolder holder = new AppItemViewHolder(view, mContext); 65 holder.bind(apps.get(i), isDistractionOptimizationRequired); 66 67 LinearLayout.LayoutParams params = 68 (LinearLayout.LayoutParams) view.getLayoutParams(); 69 params.width = 0; 70 params.height = ViewGroup.LayoutParams.WRAP_CONTENT; 71 params.weight = 1; 72 params.bottomMargin = 0; 73 mRecentAppsRow.addView(view); 74 } 75 76 // Add empty views to fill out the entire first row 77 for (int i = size; i < mColumnNumber; i++) { 78 LinearLayout.LayoutParams params = 79 new LinearLayout.LayoutParams( 80 0, ViewGroup.LayoutParams.WRAP_CONTENT, /* weight= */ 1); 81 mRecentAppsRow.addView(new View(mContext), params); 82 } 83 84 } 85 } 86 87