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.content.ComponentName; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 import android.util.Pair; 23 import android.view.View; 24 25 import androidx.annotation.Nullable; 26 27 import java.util.function.Consumer; 28 29 /** 30 * Meta data of an app including the display name, the component name, the icon drawable, and an 31 * intent to either open the app or the media center (for media services). 32 */ 33 34 public final class AppMetaData { 35 // The display name of the app 36 @Nullable 37 private final String mDisplayName; 38 // The component name of the app 39 private final ComponentName mComponentName; 40 private final Drawable mIcon; 41 private final boolean mIsDistractionOptimized; 42 private final boolean mIsMirroring; 43 private final Consumer<Context> mLaunchCallback; 44 private final Consumer<Pair<Context, View>> mAlternateLaunchCallback; 45 46 /** 47 * AppMetaData 48 * 49 * @param displayName the name to display in the launcher 50 * @param componentName the component name 51 * @param icon the application's icon 52 * @param isDistractionOptimized whether mainLaunchIntent is safe for driving 53 * @param launchCallback action to execute to launch this app 54 * @param alternateLaunchCallback temporary alternative action to execute (e.g.: for media apps 55 * this allows opening their own UI). 56 */ AppMetaData( CharSequence displayName, ComponentName componentName, Drawable icon, boolean isDistractionOptimized, boolean isMirroring, Consumer<Context> launchCallback, Consumer<Pair<Context, View>> alternateLaunchCallback)57 AppMetaData( 58 CharSequence displayName, 59 ComponentName componentName, 60 Drawable icon, 61 boolean isDistractionOptimized, 62 boolean isMirroring, 63 Consumer<Context> launchCallback, 64 Consumer<Pair<Context, View>> alternateLaunchCallback) { 65 mDisplayName = displayName == null ? "" : displayName.toString(); 66 mComponentName = componentName; 67 mIcon = icon; 68 mIsDistractionOptimized = isDistractionOptimized; 69 mIsMirroring = isMirroring; 70 mLaunchCallback = launchCallback; 71 mAlternateLaunchCallback = alternateLaunchCallback; 72 } 73 getDisplayName()74 public String getDisplayName() { 75 return mDisplayName; 76 } 77 getClassName()78 public String getClassName() { 79 return getComponentName().getClassName(); 80 } 81 getPackageName()82 public String getPackageName() { 83 return getComponentName().getPackageName(); 84 } 85 getComponentName()86 public ComponentName getComponentName() { 87 return mComponentName; 88 } 89 getLaunchCallback()90 public Consumer<Context> getLaunchCallback() { 91 return mLaunchCallback; 92 } 93 getAlternateLaunchCallback()94 public Consumer<Pair<Context, View>> getAlternateLaunchCallback() { 95 return mAlternateLaunchCallback; 96 } 97 getIcon()98 public Drawable getIcon() { 99 return mIcon; 100 } 101 getIsDistractionOptimized()102 public boolean getIsDistractionOptimized() { 103 return mIsDistractionOptimized; 104 } 105 getIsMirroring()106 boolean getIsMirroring() { 107 return mIsMirroring; 108 } 109 110 /** 111 * The equality of two AppMetaData is determined by whether the component names are the same. 112 * 113 * @param o Object that this AppMetaData object is compared against 114 * @return {@code true} when two AppMetaData have the same component name 115 */ 116 @Override equals(Object o)117 public boolean equals(Object o) { 118 if (!(o instanceof AppMetaData)) { 119 return false; 120 } else { 121 return ((AppMetaData) o).getComponentName().equals(mComponentName) 122 && ((AppMetaData) o).getIsMirroring() == mIsMirroring; 123 } 124 } 125 126 @Override hashCode()127 public int hashCode() { 128 return mComponentName.hashCode(); 129 } 130 } 131