summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher/AddAdapter.java
blob: 49422c67172950709449fe608b57d1e26365189b (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.BaseExpandableListAdapter;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Shows a list of all the items that can be added to the workspace.
 */
public final class AddAdapter extends BaseExpandableListAdapter {
    private static final int GROUP_APPLICATIONS = 0;
    private static final int GROUP_SHORTCUTS = 1;
    private static final int GROUP_WIDGETS = 2;
    private static final int GROUP_WALLPAPERS = 3;

    private final Intent mCreateShortcutIntent;
    private Intent mSetWallpaperIntent;
    private final LayoutInflater mInflater;
    private Launcher mLauncher;
    private Group[] mGroups;

    /**
     * Abstract class representing one thing that can be added
     */
    public abstract class AddAction implements Runnable {
        private final Context mContext;

        AddAction(Context context) {
            mContext = context;
        }

        Drawable getIcon(int resource) {
            return mContext.getResources().getDrawable(resource);
        }

        public abstract void bindView(View v);
    }

    /**
     * Class representing an action that will create set the wallpaper.
     */
    public class SetWallpaperAction extends CreateShortcutAction {
        SetWallpaperAction(Context context, ResolveInfo info) {
            super(context, info);
        }

        public void run() {
            Intent intent = new Intent(mSetWallpaperIntent);
            ActivityInfo activityInfo = mInfo.activityInfo;
            intent.setComponent(new ComponentName(activityInfo.applicationInfo.packageName,
                    activityInfo.name));
            mLauncher.startActivity(intent);
        }
    }
    
    /**
     * Class representing an action that will create a specific type
     * of shortcut
     */
    public class CreateShortcutAction extends AddAction {
        
        ResolveInfo mInfo;
        private CharSequence mLabel;
        private Drawable mIcon;

        CreateShortcutAction(Context context, ResolveInfo info) {
            super(context);
            mInfo = info;
        }

        @Override
        public void bindView(View view) {
            ResolveInfo info = mInfo;
            TextView text = (TextView) view;

            PackageManager pm = mLauncher.getPackageManager();

            if (mLabel == null) {
                mLabel = info.loadLabel(pm);
                if (mLabel == null) {
                    mLabel = info.activityInfo.name;
                }
            }
            if (mIcon == null) {
                mIcon = info.loadIcon(pm);
            }

            text.setText(mLabel);
            text.setCompoundDrawablesWithIntrinsicBounds(mIcon, null, null, null);
        }

        public void run() {
            Intent intent = new Intent(mCreateShortcutIntent);
            ActivityInfo activityInfo = mInfo.activityInfo;
            intent.setComponent(new ComponentName(activityInfo.applicationInfo.packageName,
                    activityInfo.name));
            mLauncher.addShortcut(intent);
        }
    }
    
    /**
     * Class representing an action that will add a folder
     */
    public class CreateFolderAction extends AddAction {
        
        CreateFolderAction(Context context) {
            super(context);
        }

        @Override
        public void bindView(View view) {
            TextView text = (TextView) view;
            text.setText(R.string.add_folder);
            text.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.ic_launcher_folder),
                    null, null, null);
        }

        public void run() {
            mLauncher.addFolder();
        }
    }

    /**
     * Class representing an action that will add a folder
     */
    public class CreateClockAction extends AddAction {

        CreateClockAction(Context context) {
            super(context);
        }

        @Override
        public void bindView(View view) {
            TextView text = (TextView) view;
            text.setText(R.string.add_clock);
            Drawable icon = getIcon(R.drawable.ic_launcher_alarmclock);
            text.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        }

        public void run() {
            mLauncher.addClock();
        }
    }

    /**
     * Class representing an action that will add a PhotoFrame
     */
    public class CreatePhotoFrameAction extends AddAction {
        CreatePhotoFrameAction(Context context) {
            super(context);
        }

        @Override
        public void bindView(View view) {
            TextView text = (TextView) view;
            text.setText(R.string.add_photo_frame);
            Drawable icon = getIcon(R.drawable.ic_launcher_gallery);
            text.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        }

        public void run() {
            mLauncher.getPhotoForPhotoFrame();
        }
    }


    /**
     * Class representing an action that will add a Search widget
     */
    public class CreateSearchAction extends AddAction {
        CreateSearchAction(Context context) {
            super(context);
        }

        @Override
        public void bindView(View view) {
            TextView text = (TextView) view;
            text.setText(R.string.add_search);
            Drawable icon = getIcon(R.drawable.ic_search_gadget);
            text.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        }

        public void run() {
            mLauncher.addSearch();
        }
    }
    
    private class Group {
        private String mName;
        private ArrayList<AddAction> mList;

        Group(String name) {
            mName = name;
            mList = new ArrayList<AddAction>();
        }

        void add(AddAction action) {
            mList.add(action);
        }

        int size() {
            return mList.size();
        }

        String getName() {
            return mName;
        }

        void run(int position) {
            mList.get(position).run();
        }

        void bindView(int childPosition, View view) {
            mList.get(childPosition).bindView(view);
        }

        public Object get(int childPosition) {
            return mList.get(childPosition);
        }
    }

    private class ApplicationsGroup extends Group {
        private final Launcher mLauncher;
        private final ArrayList<ApplicationInfo> mApplications;

        ApplicationsGroup(Launcher launcher, String name) {
            super(name);
            mLauncher = launcher;
            mApplications = Launcher.getModel().getApplications();
        }

        @Override
        int size() {
            return mApplications == null ? 0 : mApplications.size();
        }

        @Override
        void add(AddAction action) {
        }

        @Override
        void run(int position) {
            final ApplicationInfo info = mApplications.get(position);
            mLauncher.addApplicationShortcut(info);
        }

        @Override
        void bindView(int childPosition, View view) {
            TextView text = (TextView) view.findViewById(R.id.title);

            final ApplicationInfo info = mApplications.get(childPosition);
            text.setText(info.title);
            if (!info.filtered) {
                info.icon = Utilities.createIconThumbnail(info.icon, mLauncher);
                info.filtered = true;
            }
            text.setCompoundDrawablesWithIntrinsicBounds(info.icon, null, null, null);
        }

        @Override
        public Object get(int childPosition) {
            return mApplications.get(childPosition);
        }
    }

    public AddAdapter(Launcher launcher, boolean forFolder) {
        mCreateShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
        mCreateShortcutIntent.setComponent(null);

        mSetWallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
        mSetWallpaperIntent.setComponent(null);

        mLauncher = launcher;
        mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mGroups = new Group[forFolder ? 2 : 4];
        final Group[] groups = mGroups;
        groups[GROUP_APPLICATIONS] = new ApplicationsGroup(mLauncher,
                mLauncher.getString(R.string.group_applications));
        groups[GROUP_SHORTCUTS] = new Group(mLauncher.getString(R.string.group_shortcuts));

        if (!forFolder) {
            groups[GROUP_WALLPAPERS] = new Group(mLauncher.getString(R.string.group_wallpapers));
            groups[GROUP_SHORTCUTS].add(new CreateFolderAction(launcher));
            groups[GROUP_WIDGETS] = new Group(mLauncher.getString(R.string.group_widgets));
            final Group widgets = groups[GROUP_WIDGETS];
            widgets.add(new CreateClockAction(launcher));
            widgets.add(new CreatePhotoFrameAction(launcher));
            widgets.add(new CreateSearchAction(launcher));
        }
        
        PackageManager packageManager = launcher.getPackageManager();

        List<ResolveInfo> list = findTargetsForIntent(mCreateShortcutIntent, packageManager);
        if (list != null && list.size() > 0) {
            int count = list.size();
            final Group shortcuts = groups[GROUP_SHORTCUTS];
            for (int i = 0; i < count; i++) {
                ResolveInfo resolveInfo = list.get(i);
                shortcuts.add(new CreateShortcutAction(launcher, resolveInfo));
            }
        }

        list = findTargetsForIntent(mSetWallpaperIntent, packageManager);
        if (list != null && list.size() > 0) {
            int count = list.size();
            final Group shortcuts = groups[GROUP_WALLPAPERS];
            for (int i = 0; i < count; i++) {
                ResolveInfo resolveInfo = list.get(i);
                shortcuts.add(new SetWallpaperAction(launcher, resolveInfo));
            }
        }
    }

    private List<ResolveInfo> findTargetsForIntent(Intent intent, PackageManager packageManager) {
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (list != null) {
            int count = list.size();
            if (count > 1) {
                // Only display the first matches that are either of equal
                // priority or have asked to be default options.
                ResolveInfo firstInfo = list.get(0);
                for (int i=1; i<count; i++) {
                    ResolveInfo resolveInfo = list.get(i);
                    if (firstInfo.priority != resolveInfo.priority ||
                        firstInfo.isDefault != resolveInfo.isDefault) {
                        while (i < count) {
                            list.remove(i);
                            count--;
                        }
                    }
                }
                Collections.sort(list, new ResolveInfo.DisplayNameComparator(packageManager));
            }
        }
        return list;
    }

    public int getGroupCount() {
        return mGroups.length;
    }

    public int getChildrenCount(int groupPosition) {
        return mGroups[groupPosition].size();
    }

    public Object getGroup(int groupPosition) {
        return mGroups[groupPosition].getName();
    }

    public Object getChild(int groupPosition, int childPosition) {
        return mGroups[groupPosition].get(childPosition);
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (groupPosition << 16) | childPosition;
    }

    public boolean hasStableIds() {
        return true;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = mInflater.inflate(R.layout.create_shortcut_group_item, parent, false);
        } else {
            view = convertView;
        }
        ((TextView) view).setText(mGroups[groupPosition].getName());
        return view;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = mInflater.inflate(R.layout.create_shortcut_list_item, parent, false);
        } else {
            view = convertView;
        }
        mGroups[groupPosition].bindView(childPosition, view);
        return view;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    void performAction(int groupPosition, int childPosition) {
        mGroups[groupPosition].run(childPosition);
    }
}