1 /* 2 * Copyright (C) 2023 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 boolean mIsDisabledByTos; 44 private final Consumer<Context> mLaunchCallback; 45 private final Consumer<Pair<Context, View>> mAlternateLaunchCallback; 46 47 /** 48 * AppMetaData 49 * 50 * @param displayName the name to display in the launcher 51 * @param componentName the component name 52 * @param icon the application's icon 53 * @param isDistractionOptimized whether mainLaunchIntent is safe for driving 54 * @param isDisabledByTos whether app is disabled by tos 55 * @param launchCallback action to execute to launch this app 56 * @param alternateLaunchCallback temporary alternative action to execute (e.g.: for media apps 57 * this allows opening their own UI). 58 */ AppMetaData( CharSequence displayName, ComponentName componentName, Drawable icon, boolean isDistractionOptimized, boolean isMirroring, boolean isDisabledByTos, Consumer<Context> launchCallback, Consumer<Pair<Context, View>> alternateLaunchCallback)59 public AppMetaData( 60 CharSequence displayName, 61 ComponentName componentName, 62 Drawable icon, 63 boolean isDistractionOptimized, 64 boolean isMirroring, 65 boolean isDisabledByTos, 66 Consumer<Context> launchCallback, 67 Consumer<Pair<Context, View>> alternateLaunchCallback) { 68 mDisplayName = displayName == null ? "" : displayName.toString(); 69 mComponentName = componentName; 70 mIcon = icon; 71 mIsDistractionOptimized = isDistractionOptimized; 72 mIsMirroring = isMirroring; 73 mIsDisabledByTos = isDisabledByTos; 74 mLaunchCallback = launchCallback; 75 mAlternateLaunchCallback = alternateLaunchCallback; 76 } 77 getDisplayName()78 public String getDisplayName() { 79 return mDisplayName; 80 } 81 getClassName()82 public String getClassName() { 83 return getComponentName().getClassName(); 84 } 85 getPackageName()86 public String getPackageName() { 87 return getComponentName().getPackageName(); 88 } 89 getComponentName()90 public ComponentName getComponentName() { 91 return mComponentName; 92 } 93 getLaunchCallback()94 public Consumer<Context> getLaunchCallback() { 95 return mLaunchCallback; 96 } 97 getAlternateLaunchCallback()98 public Consumer<Pair<Context, View>> getAlternateLaunchCallback() { 99 return mAlternateLaunchCallback; 100 } 101 getIcon()102 public Drawable getIcon() { 103 return mIcon; 104 } 105 getIsDistractionOptimized()106 public boolean getIsDistractionOptimized() { 107 return mIsDistractionOptimized; 108 } 109 getIsDisabledByTos()110 public boolean getIsDisabledByTos() { 111 return mIsDisabledByTos; 112 } 113 getIsMirroring()114 public boolean getIsMirroring() { 115 return mIsMirroring; 116 } 117 118 /** 119 * The equality of two AppMetaData is determined by whether the component names are the same. 120 * 121 * @param o Object that this AppMetaData object is compared against 122 * @return {@code true} when two AppMetaData have the same component name 123 */ 124 @Override equals(Object o)125 public boolean equals(Object o) { 126 if (!(o instanceof AppMetaData)) { 127 return false; 128 } else { 129 return ((AppMetaData) o).getComponentName().equals(mComponentName) 130 && ((AppMetaData) o).getIsMirroring() == mIsMirroring 131 && ((AppMetaData) o).getIsDisabledByTos() == mIsDisabledByTos 132 && ((AppMetaData) o).getIsDistractionOptimized() == mIsDistractionOptimized; 133 } 134 } 135 136 @Override hashCode()137 public int hashCode() { 138 return mComponentName.hashCode(); 139 } 140 } 141