2016-03-10 12:02:29 -08:00
|
|
|
package com.android.launcher3.model;
|
|
|
|
|
|
|
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.Process;
|
|
|
|
|
import android.os.UserHandle;
|
2016-03-10 12:02:29 -08:00
|
|
|
|
|
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
|
|
|
import com.android.launcher3.LauncherAppWidgetProviderInfo;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
2017-01-19 10:27:54 -08:00
|
|
|
import com.android.launcher3.compat.ShortcutConfigActivityInfo;
|
2016-03-10 12:02:29 -08:00
|
|
|
import com.android.launcher3.util.ComponentKey;
|
|
|
|
|
|
|
|
|
|
import java.text.Collator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An wrapper over various items displayed in a widget picker,
|
|
|
|
|
* {@link LauncherAppWidgetProviderInfo} & {@link ActivityInfo}. This provides easier access to
|
|
|
|
|
* common attributes like spanX and spanY.
|
|
|
|
|
*/
|
|
|
|
|
public class WidgetItem extends ComponentKey implements Comparable<WidgetItem> {
|
|
|
|
|
|
2016-12-15 15:53:17 -08:00
|
|
|
private static UserHandle sMyUserHandle;
|
2016-03-10 12:02:29 -08:00
|
|
|
private static Collator sCollator;
|
|
|
|
|
|
|
|
|
|
public final LauncherAppWidgetProviderInfo widgetInfo;
|
2017-01-19 10:27:54 -08:00
|
|
|
public final ShortcutConfigActivityInfo activityInfo;
|
2016-03-10 12:02:29 -08:00
|
|
|
|
|
|
|
|
public final String label;
|
|
|
|
|
public final int spanX, spanY;
|
|
|
|
|
|
2017-01-11 10:48:34 -08:00
|
|
|
public WidgetItem(LauncherAppWidgetProviderInfo info, PackageManager pm,
|
|
|
|
|
InvariantDeviceProfile idp) {
|
2016-12-16 15:04:51 -08:00
|
|
|
super(info.provider, info.getProfile());
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2016-12-16 15:04:51 -08:00
|
|
|
label = Utilities.trim(info.getLabel(pm));
|
2016-03-10 12:02:29 -08:00
|
|
|
widgetInfo = info;
|
|
|
|
|
activityInfo = null;
|
|
|
|
|
|
2017-01-11 10:48:34 -08:00
|
|
|
spanX = Math.min(info.spanX, idp.numColumns);
|
|
|
|
|
spanY = Math.min(info.spanY, idp.numRows);
|
2016-03-10 12:02:29 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-19 10:27:54 -08:00
|
|
|
public WidgetItem(ShortcutConfigActivityInfo info) {
|
|
|
|
|
super(info.getComponent(), info.getUser());
|
|
|
|
|
label = Utilities.trim(info.getLabel());
|
2016-03-10 12:02:29 -08:00
|
|
|
widgetInfo = null;
|
2017-01-19 10:27:54 -08:00
|
|
|
activityInfo = info;
|
2016-03-10 12:02:29 -08:00
|
|
|
spanX = spanY = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int compareTo(WidgetItem another) {
|
|
|
|
|
if (sMyUserHandle == null) {
|
|
|
|
|
// Delay these object creation until required.
|
2016-12-15 15:53:17 -08:00
|
|
|
sMyUserHandle = Process.myUserHandle();
|
2016-03-10 12:02:29 -08:00
|
|
|
sCollator = Collator.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Independent of how the labels compare, if only one of the two widget info belongs to
|
|
|
|
|
// work profile, put that one in the back.
|
|
|
|
|
boolean thisWorkProfile = !sMyUserHandle.equals(user);
|
|
|
|
|
boolean otherWorkProfile = !sMyUserHandle.equals(another.user);
|
|
|
|
|
if (thisWorkProfile ^ otherWorkProfile) {
|
|
|
|
|
return thisWorkProfile ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 17:01:24 -07:00
|
|
|
int labelCompare = sCollator.compare(label, another.label);
|
|
|
|
|
if (labelCompare != 0) {
|
|
|
|
|
return labelCompare;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the label is same, put the smaller widget before the larger widget. If the area is
|
|
|
|
|
// also same, put the widget with smaller height before.
|
|
|
|
|
int thisArea = spanX * spanY;
|
|
|
|
|
int otherArea = another.spanX * another.spanY;
|
|
|
|
|
return thisArea == otherArea
|
|
|
|
|
? Integer.compare(spanY, another.spanY)
|
|
|
|
|
: Integer.compare(thisArea, otherArea);
|
2016-03-10 12:02:29 -08:00
|
|
|
}
|
|
|
|
|
}
|