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