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