summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/widget/WidgetsModel.java
blob: c400d63669a3ad8c314f71453b9d6216560ab512 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

package com.android.launcher3.widget;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.launcher3.IconCache;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.LauncherModel.WidgetAndShortcutNameComparator;
import com.android.launcher3.compat.UserHandleCompat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Widgets data model that is used by the adapters of the widget views and controllers.
 *
 * <p> The widgets and shortcuts are organized using package name as its index.
 */
public class WidgetsModel {

    private static final String TAG = "WidgetsModel";
    private static final boolean DEBUG = false;

    /* List of packages that is tracked by this model. */
    private List<String> mPackageNames = new ArrayList<>();

    private Map<String, PackageItemInfo> mPackageItemInfoList = new HashMap<>();

    /* Map of widgets and shortcuts that are tracked per package. */
    private Map<String, ArrayList<Object>> mWidgetsList = new HashMap<>();

    /* Notifies the adapter when data changes. */
    private RecyclerView.Adapter mAdapter;

    private Comparator mWidgetAndShortcutNameComparator;

    private IconCache mIconCache;

    public WidgetsModel(Context context, RecyclerView.Adapter adapter) {
        mAdapter = adapter;
        mWidgetAndShortcutNameComparator = new WidgetAndShortcutNameComparator(context);
        mIconCache = LauncherAppState.getInstance().getIconCache();
    }

    // Access methods that may be deleted if the private fields are made package-private.
    public int getPackageSize() {
        return mPackageNames.size();
    }

    // Access methods that may be deleted if the private fields are made package-private.
    public String getPackageName(int pos) {
        return mPackageNames.get(pos);
    }

    public PackageItemInfo getPackageItemInfo(String packageName) {
        return mPackageItemInfoList.get(packageName);
    }

    public List<Object> getSortedWidgets(String packageName) {
        return mWidgetsList.get(packageName);
    }

    public void addWidgetsAndShortcuts(ArrayList<Object> widgetsShortcuts, PackageManager pm) {
        if (DEBUG) {
            Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + widgetsShortcuts.size());
        }

        // clear the lists.
        mPackageNames.clear();
        mWidgetsList.clear();

        // add and update.
        for (Object o: widgetsShortcuts) {
            String packageName = "";
            if (o instanceof LauncherAppWidgetProviderInfo) {
                LauncherAppWidgetProviderInfo widgetInfo = (LauncherAppWidgetProviderInfo) o;
                packageName = widgetInfo.provider.getPackageName();
            } else if (o instanceof ResolveInfo) {
                ResolveInfo resolveInfo = (ResolveInfo) o;
                packageName = resolveInfo.activityInfo.packageName;
            } else {
                Log.e(TAG, String.format("addWidgetsAndShortcuts, nothing added for class=%s",
                        o.getClass().toString()));
                
            }

            ArrayList<Object> widgetsShortcutsList = mWidgetsList.get(packageName);
            if (widgetsShortcutsList != null) {
                widgetsShortcutsList.add(o);
            } else {
                widgetsShortcutsList = new ArrayList<Object>();
                widgetsShortcutsList.add(o);
                mWidgetsList.put(packageName, widgetsShortcutsList);
                mPackageNames.add(packageName);
            }
        }
        for (String packageName: mPackageNames) {
            PackageItemInfo pInfo = mPackageItemInfoList.get(packageName);
            if (pInfo == null) {
                pInfo = new PackageItemInfo();
                mIconCache.getTitleAndIconForApp(packageName, UserHandleCompat.myUserHandle(),
                        true /* useLowResIcon */, pInfo);
                mPackageItemInfoList.put(packageName, pInfo);
            }
        }

        // sort.
        sortPackageList();
        for (String packageName: mPackageNames) {
            Collections.sort(mWidgetsList.get(packageName), mWidgetAndShortcutNameComparator);
        }

        // notify.
        mAdapter.notifyDataSetChanged();
    }

    private void sortPackageList() {
        Collections.sort(mPackageNames, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                String lhsTitle = mPackageItemInfoList.get(lhs).title.toString();
                String rhsTitle = mPackageItemInfoList.get(rhs).title.toString();
                return lhsTitle.compareTo(rhsTitle);
            }
        });
    }
}