summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/model/WidgetItem.java
blob: 452dbe2ad6fc641e96e5603483a2340df33980c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.android.launcher3.model;

import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Process;
import android.os.UserHandle;

import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.Utilities;
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> {

    private static UserHandle sMyUserHandle;
    private static Collator sCollator;

    public final LauncherAppWidgetProviderInfo widgetInfo;
    public final ActivityInfo activityInfo;

    public final String label;
    public final int spanX, spanY;

    public WidgetItem(LauncherAppWidgetProviderInfo info, PackageManager pm) {
        super(info.provider, info.getProfile());

        label = Utilities.trim(info.getLabel(pm));
        widgetInfo = info;
        activityInfo = null;

        InvariantDeviceProfile idv = LauncherAppState.getInstance().getInvariantDeviceProfile();
        spanX = Math.min(info.spanX, idv.numColumns);
        spanY = Math.min(info.spanY, idv.numRows);
    }

    public WidgetItem(ResolveInfo info, PackageManager pm) {
        super(new ComponentName(info.activityInfo.packageName, info.activityInfo.name),
                Process.myUserHandle());
        label = Utilities.trim(info.loadLabel(pm));
        widgetInfo = null;
        activityInfo = info.activityInfo;
        spanX = spanY = 1;
    }

    @Override
    public int compareTo(WidgetItem another) {
        if (sMyUserHandle == null) {
            // Delay these object creation until required.
            sMyUserHandle = Process.myUserHandle();
            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;
        }

        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);
    }
}