blob: 6119a8af047cb20215f47bb32bce42b4ff7d7c66 [file] [log] [blame]
Romain Guy41669fc2009-08-13 12:53:24 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
19import android.app.WallpaperManager;
20import android.content.DialogInterface;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.content.pm.ServiceInfo;
25import android.graphics.drawable.Drawable;
26import android.os.Bundle;
27import android.os.RemoteException;
28import android.service.wallpaper.WallpaperService;
29import android.util.Log;
30
31import java.text.Collator;
32import java.util.List;
33import java.util.ArrayList;
34import java.util.Collections;
35import java.util.Comparator;
36
37/**
38 * Displays a list of live wallpapers, allowing the user to select one
39 * and make it the system global wallpaper.
40 */
41public class LiveWallpaperPickActivity extends ActivityPicker {
42 private static final String TAG = "LiveWallpaperPickActivity";
43
44 private PackageManager mPackageManager;
45 private WallpaperManager mWallpaperManager;
46
47 @Override
48 public void onCreate(Bundle icicle) {
49 mPackageManager = getPackageManager();
50 mWallpaperManager = WallpaperManager.getInstance(this);
51
52 super.onCreate(icicle);
53
54 // Set default return data
55 setResult(RESULT_CANCELED);
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 @Override
62 public void onClick(DialogInterface dialog, int which) {
63 Intent intent = getIntentForPosition(which);
64 try {
65 mWallpaperManager.getIWallpaperManager().setWallpaperComponent(
66 intent.getComponent());
67 this.setResult(RESULT_OK);
68 } catch (RemoteException e) {
69 // do nothing
70 } catch (RuntimeException e) {
71 Log.w(TAG, "Failure setting wallpaper", e);
72 }
73 finish();
74 }
75
76 void putLiveWallpaperItems(List<ResolveInfo> ris,
77 List<PickAdapter.Item> items) {
78 final int size = ris.size();
79 for (int i = 0; i < size; i++) {
80 ServiceInfo si = ris.get(i).serviceInfo;
81
82 CharSequence label = si.loadLabel(mPackageManager);
83 Drawable icon = si.loadIcon(mPackageManager);
84
85 PickAdapter.Item item = new PickAdapter.Item(this, label, icon);
86
87 item.packageName = si.packageName;
88 item.className = si.name;
89
90 items.add(item);
91 }
92 }
93
94 @Override
95 protected List<PickAdapter.Item> getItems() {
96 List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
97
98 putInstalledLiveWallpapers(items);
99
100 // Sort all items together by label
101 Collections.sort(items, new Comparator<PickAdapter.Item>() {
102 Collator mCollator = Collator.getInstance();
103 public int compare(PickAdapter.Item lhs, PickAdapter.Item rhs) {
104 return mCollator.compare(lhs.label, rhs.label);
105 }
106 });
107
108 return items;
109 }
110
111 void putInstalledLiveWallpapers(List<PickAdapter.Item> items) {
112 List<ResolveInfo> ris = mPackageManager.queryIntentServices(
113 new Intent(WallpaperService.SERVICE_INTERFACE), 0);
114 putLiveWallpaperItems(ris, items);
115 }
116}