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.View; 22 import android.widget.ImageView; 23 import android.widget.TextView; 24 import android.widget.Toast; 25 26 import androidx.recyclerview.widget.RecyclerView; 27 28 /** 29 * App item view holder that contains the app icon and name. 30 */ 31 public class AppItemViewHolder extends RecyclerView.ViewHolder { 32 private final Context mContext; 33 private View mAppItem; 34 private ImageView mAppIconView; 35 private TextView mAppNameView; 36 AppItemViewHolder(View view, Context context)37 AppItemViewHolder(View view, Context context) { 38 super(view); 39 mContext = context; 40 mAppItem = view.findViewById(R.id.app_item); 41 mAppIconView = mAppItem.findViewById(R.id.app_icon); 42 mAppNameView = mAppItem.findViewById(R.id.app_name); 43 } 44 45 /** 46 * Binds the grid app item view with the app meta data. 47 * 48 * @param app Pass {@code null} will empty out the view. 49 */ bind(@ullable AppMetaData app, boolean isDistractionOptimizationRequired)50 public void bind(@Nullable AppMetaData app, boolean isDistractionOptimizationRequired) { 51 // Empty out the view 52 mAppIconView.setImageDrawable(null); 53 mAppNameView.setText(null); 54 55 if (app == null) { 56 return; 57 } 58 59 mAppNameView.setText(app.getDisplayName()); 60 mAppIconView.setImageDrawable(app.getIcon()); 61 boolean isLaunchable = 62 !isDistractionOptimizationRequired || app.getIsDistractionOptimized(); 63 mAppIconView.setAlpha(mContext.getResources().getFloat( 64 isLaunchable ? R.dimen.app_icon_opacity : R.dimen.app_icon_opacity_unavailable)); 65 66 if (isLaunchable) { 67 mAppItem.setOnClickListener(v -> app.getLaunchCallback().accept(mContext)); 68 boolean hasLongClickCallback = (app.getAlternateLaunchCallback() != null); 69 mAppItem.setLongClickable(hasLongClickCallback); 70 if (hasLongClickCallback) { 71 // Note setOnLongClickListener implicitly sets view to be long clickable 72 mAppItem.setOnLongClickListener(v -> { 73 app.getAlternateLaunchCallback().accept(mContext); 74 return true; 75 }); 76 } 77 } else { 78 String warningText = mContext.getResources() 79 .getString(R.string.driving_toast_text, app.getDisplayName()); 80 mAppItem.setOnClickListener( 81 v -> Toast.makeText(mContext, warningText, Toast.LENGTH_LONG).show()); 82 } 83 } 84 } 85