1 /* 2 * Copyright (C) 2016 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.example.sampleleanbacklauncher.apps; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.graphics.drawable.Drawable; 25 import androidx.annotation.NonNull; 26 27 import java.util.Objects; 28 29 public class LaunchItem implements Comparable<LaunchItem> { 30 protected final Intent mIntent; 31 private final Drawable mBanner; 32 private final Drawable mIcon; 33 private final CharSequence mLabel; 34 private final long mPriority; 35 LaunchItem(Context context, Intent intent, Drawable icon, CharSequence label)36 LaunchItem(Context context, Intent intent, Drawable icon, CharSequence label) { 37 mIntent = intent; 38 mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 39 final PackageManager packageManager = context.getPackageManager(); 40 mBanner = null; 41 mIcon = icon; 42 mLabel = label; 43 mPriority = 0; 44 } 45 LaunchItem(Context context, ResolveInfo info, long priority)46 LaunchItem(Context context, ResolveInfo info, long priority) { 47 mIntent = Intent.makeMainActivity( 48 new ComponentName(info.activityInfo.packageName, info.activityInfo.name)); 49 mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 50 final PackageManager packageManager = context.getPackageManager(); 51 mBanner = info.activityInfo.loadBanner(packageManager); 52 mIcon = info.loadIcon(packageManager); 53 mLabel = info.loadLabel(packageManager); 54 mPriority = priority; 55 } 56 getIntent()57 public Intent getIntent() { 58 return mIntent; 59 } 60 getBanner()61 public Drawable getBanner() { 62 return mBanner; 63 } 64 getIcon()65 public Drawable getIcon() { 66 return mIcon; 67 } 68 getLabel()69 public CharSequence getLabel() { 70 return mLabel; 71 } 72 73 /** 74 * Returns whether this item should appear identical to the given item. 75 * @param another Item to compare to 76 * @return True if items should appear identical 77 */ areContentsTheSame(LaunchItem another)78 public boolean areContentsTheSame(LaunchItem another) { 79 return Objects.equals(another.getBanner(), getBanner()) 80 && Objects.equals(another.getIcon(), getIcon()) 81 && Objects.equals(another.getLabel(), getLabel()); 82 } 83 84 @Override compareTo(@onNull LaunchItem another)85 public int compareTo(@NonNull LaunchItem another) { 86 long priorityDiff = another.mPriority - mPriority; 87 if (priorityDiff != 0) { 88 return priorityDiff < 0L ? -1 : 1; 89 } else { 90 return getLabel().toString().compareTo(another.getLabel().toString()); 91 } 92 } 93 94 @Override equals(Object o)95 public boolean equals(Object o) { 96 return o instanceof LaunchItem && Objects.equals(mIntent, ((LaunchItem)o).getIntent()); 97 } 98 99 @Override hashCode()100 public int hashCode() { 101 return mIntent.hashCode(); 102 } 103 104 @Override toString()105 public String toString() { 106 return mLabel + " -- " + mIntent.getComponent().toString(); 107 } 108 toDebugString()109 public String toDebugString() { 110 return "Label: " + mLabel 111 + " Intent: " + mIntent 112 + " Banner: " + mBanner 113 + " Icon: " + mIcon 114 + " Priority: " + Long.toString(mPriority); 115 } 116 } 117