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