1 package com.android.launcher3.model; 2 3 import android.content.pm.ActivityInfo; 4 import android.content.pm.PackageManager; 5 import android.os.Process; 6 import android.os.UserHandle; 7 8 import com.android.launcher3.InvariantDeviceProfile; 9 import com.android.launcher3.LauncherAppWidgetProviderInfo; 10 import com.android.launcher3.Utilities; 11 import com.android.launcher3.compat.ShortcutConfigActivityInfo; 12 import com.android.launcher3.util.ComponentKey; 13 14 import java.text.Collator; 15 16 /** 17 * An wrapper over various items displayed in a widget picker, 18 * {@link LauncherAppWidgetProviderInfo} & {@link ActivityInfo}. This provides easier access to 19 * common attributes like spanX and spanY. 20 */ 21 public class WidgetItem extends ComponentKey implements Comparable<WidgetItem> { 22 23 private static UserHandle sMyUserHandle; 24 private static Collator sCollator; 25 26 public final LauncherAppWidgetProviderInfo widgetInfo; 27 public final ShortcutConfigActivityInfo activityInfo; 28 29 public final String label; 30 public final int spanX, spanY; 31 WidgetItem(LauncherAppWidgetProviderInfo info, PackageManager pm, InvariantDeviceProfile idp)32 public WidgetItem(LauncherAppWidgetProviderInfo info, PackageManager pm, 33 InvariantDeviceProfile idp) { 34 super(info.provider, info.getProfile()); 35 36 label = Utilities.trim(info.getLabel(pm)); 37 widgetInfo = info; 38 activityInfo = null; 39 40 spanX = Math.min(info.spanX, idp.numColumns); 41 spanY = Math.min(info.spanY, idp.numRows); 42 } 43 WidgetItem(ShortcutConfigActivityInfo info)44 public WidgetItem(ShortcutConfigActivityInfo info) { 45 super(info.getComponent(), info.getUser()); 46 label = Utilities.trim(info.getLabel()); 47 widgetInfo = null; 48 activityInfo = info; 49 spanX = spanY = 1; 50 } 51 52 @Override compareTo(WidgetItem another)53 public int compareTo(WidgetItem another) { 54 if (sMyUserHandle == null) { 55 // Delay these object creation until required. 56 sMyUserHandle = Process.myUserHandle(); 57 sCollator = Collator.getInstance(); 58 } 59 60 // Independent of how the labels compare, if only one of the two widget info belongs to 61 // work profile, put that one in the back. 62 boolean thisWorkProfile = !sMyUserHandle.equals(user); 63 boolean otherWorkProfile = !sMyUserHandle.equals(another.user); 64 if (thisWorkProfile ^ otherWorkProfile) { 65 return thisWorkProfile ? 1 : -1; 66 } 67 68 int labelCompare = sCollator.compare(label, another.label); 69 if (labelCompare != 0) { 70 return labelCompare; 71 } 72 73 // If the label is same, put the smaller widget before the larger widget. If the area is 74 // also same, put the widget with smaller height before. 75 int thisArea = spanX * spanY; 76 int otherArea = another.spanX * another.spanY; 77 return thisArea == otherArea 78 ? Integer.compare(spanY, another.spanY) 79 : Integer.compare(thisArea, otherArea); 80 } 81 } 82