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