blob: d4237435cf32dabe7021a92a063a8c224249d6b4 [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
Daniel Sandler86aea352009-09-11 15:59:22 -040019import android.app.Activity;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070020import android.app.ListActivity;
Romain Guy41669fc2009-08-13 12:53:24 -070021import android.app.WallpaperManager;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070022import android.content.ComponentName;
23import android.content.Context;
Romain Guy41669fc2009-08-13 12:53:24 -070024import android.content.DialogInterface;
25import android.content.Intent;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070026import android.content.ServiceConnection;
Daniel Sandler86aea352009-09-11 15:59:22 -040027import android.content.pm.ComponentInfo;
Romain Guy41669fc2009-08-13 12:53:24 -070028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
30import android.content.pm.ServiceInfo;
31import android.graphics.drawable.Drawable;
Daniel Sandler86aea352009-09-11 15:59:22 -040032import android.graphics.Bitmap;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070033import android.os.Binder;
Romain Guy41669fc2009-08-13 12:53:24 -070034import android.os.Bundle;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070035import android.os.IBinder;
36import android.os.ParcelFileDescriptor;
Romain Guy41669fc2009-08-13 12:53:24 -070037import android.os.RemoteException;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070038import android.os.SystemClock;
39import android.service.wallpaper.IWallpaperConnection;
40import android.service.wallpaper.IWallpaperEngine;
41import android.service.wallpaper.IWallpaperService;
Romain Guy41669fc2009-08-13 12:53:24 -070042import android.service.wallpaper.WallpaperService;
43import android.util.Log;
Daniel Sandler86aea352009-09-11 15:59:22 -040044import android.view.LayoutInflater;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070045import android.view.View;
Daniel Sandler86aea352009-09-11 15:59:22 -040046import android.view.ViewGroup;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070047import android.view.WindowManager;
Daniel Sandler86aea352009-09-11 15:59:22 -040048import android.widget.AdapterView;
49import android.widget.BaseAdapter;
50import android.widget.Gallery;
51import android.widget.ImageView;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070052import android.widget.ListView;
Romain Guy41669fc2009-08-13 12:53:24 -070053
54import java.text.Collator;
55import java.util.List;
56import java.util.ArrayList;
57import java.util.Collections;
58import java.util.Comparator;
59
60/**
61 * Displays a list of live wallpapers, allowing the user to select one
62 * and make it the system global wallpaper.
63 */
Daniel Sandler86aea352009-09-11 15:59:22 -040064public class LiveWallpaperPickActivity
65 extends Activity
66 implements AdapterView.OnItemSelectedListener,
67 View.OnClickListener
68{
Romain Guy41669fc2009-08-13 12:53:24 -070069 private static final String TAG = "LiveWallpaperPickActivity";
70
71 private PackageManager mPackageManager;
72 private WallpaperManager mWallpaperManager;
73
Dianne Hackborn02e638e2009-08-20 19:33:42 -070074 Intent mSelectedIntent;
75 WallpaperConnection mWallpaperConnection;
Daniel Sandler86aea352009-09-11 15:59:22 -040076
77 private Gallery mGallery;
78
79 private ArrayList<Intent> mWallpaperIntents;
80
81 private ArrayList<Bitmap> mThumbBitmaps;
82
Dianne Hackborn02e638e2009-08-20 19:33:42 -070083 class WallpaperConnection extends IWallpaperConnection.Stub
84 implements ServiceConnection {
85 final Intent mIntent;
86 IWallpaperService mService;
87 IWallpaperEngine mEngine;
88 boolean mConnected;
89
90 public WallpaperConnection(Intent intent) {
91 mIntent = intent;
92 }
93
94 public boolean connect() {
95 synchronized (this) {
96 if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) {
97 return false;
98 }
99
100 mConnected = true;
101 return true;
102 }
103 }
104
105 public void disconnect() {
106 synchronized (this) {
107 mConnected = false;
108 if (mEngine != null) {
109 try {
110 mEngine.destroy();
111 } catch (RemoteException e) {
112 }
113 mEngine = null;
114 }
115 unbindService(this);
116 mService = null;
117 }
118 }
119
120 public void onServiceConnected(ComponentName name, IBinder service) {
121 if (mWallpaperConnection == this) {
122 mService = IWallpaperService.Stub.asInterface(service);
123 try {
124 View button = findViewById(R.id.set);
125 mService.attach(this, button.getWindowToken(),
126 WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA,
127 true,
128 button.getRootView().getWidth(),
129 button.getRootView().getHeight());
130 } catch (RemoteException e) {
131 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
132 }
133 }
134 }
135
136 public void onServiceDisconnected(ComponentName name) {
137 mService = null;
138 mEngine = null;
139 if (mWallpaperConnection == this) {
140 Log.w(TAG, "Wallpaper service gone: " + name);
141 }
142 }
143
144 public void attachEngine(IWallpaperEngine engine) {
145 synchronized (this) {
146 if (mConnected) {
147 mEngine = engine;
Dianne Hackborn5237ccb2009-08-29 19:17:48 -0700148 try {
149 engine.setVisibility(true);
150 } catch (RemoteException e) {
151 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700152 } else {
153 try {
154 engine.destroy();
155 } catch (RemoteException e) {
156 }
157 }
158 }
159 }
160
161 public ParcelFileDescriptor setWallpaper(String name) {
162 return null;
163 }
164 }
Daniel Sandler86aea352009-09-11 15:59:22 -0400165
166 private class ImageAdapter extends BaseAdapter {
167 private LayoutInflater mLayoutInflater;
168
169 ImageAdapter(LiveWallpaperPickActivity context) {
170 mLayoutInflater = context.getLayoutInflater();
171 }
172
173 public int getCount() {
174 return mThumbBitmaps.size();
175 }
176
177 public Object getItem(int position) {
178 return position;
179 }
180
181 public long getItemId(int position) {
182 return position;
183 }
184
185 public View getView(int position, View convertView, ViewGroup parent) {
186 ImageView image;
187
188 if (convertView == null) {
189 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
190 } else {
191 image = (ImageView) convertView;
192 }
193
194 image.setImageBitmap(mThumbBitmaps.get(position));
195 image.getDrawable().setDither(true);
196 return image;
197 }
198 }
199
200
201 private void findLiveWallpapers() {
202 mThumbBitmaps = new ArrayList<Bitmap>(24);
203 List<ResolveInfo> list =
204 mPackageManager.queryIntentServices(getTargetIntent(),
205 /*noflags*/ 0);
206
207 mWallpaperIntents = new ArrayList<Intent>(list.size());
208
209 int listSize = list.size();
210 Log.d(TAG, String.format("findLiveWallpapers: found %d wallpaper services", listSize));
211 for (int i = 0; i < listSize; i++) {
212 ResolveInfo resolveInfo = list.get(i);
213 ComponentInfo ci = resolveInfo.serviceInfo;
214 String packageName = ci.applicationInfo.packageName;
215 String className = ci.name;
216 Log.d(TAG, String.format("findLiveWallpapers: [%d] pkg=%s cls=%s",
217 i, packageName, className));
218 Intent intent = new Intent(getTargetIntent());
219 intent.setClassName(packageName, className);
220 mWallpaperIntents.add(intent);
221
222 Bitmap thumb = Bitmap.createBitmap(240,160,Bitmap.Config.ARGB_8888);
223 android.graphics.Canvas can = new android.graphics.Canvas(thumb);
224 android.graphics.Paint pt = new android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG|android.graphics.Paint.DITHER_FLAG);
225 pt.setARGB(255, 0, 0, 255);
226 can.drawPaint(pt);
227 pt.setARGB(255, 255, 255, 255);
228 pt.setTextSize(12);
229 can.drawText(className, 16, 150, pt);
230 pt.setTextSize(80);
231 can.drawText(String.format("%d", i), 100,100, pt);
232 mThumbBitmaps.add(thumb);
233 }
234
235
236 }
237
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700238
Romain Guy41669fc2009-08-13 12:53:24 -0700239 @Override
240 public void onCreate(Bundle icicle) {
Daniel Sandler86aea352009-09-11 15:59:22 -0400241 super.onCreate(icicle);
242
Romain Guy41669fc2009-08-13 12:53:24 -0700243 mPackageManager = getPackageManager();
244 mWallpaperManager = WallpaperManager.getInstance(this);
245
Daniel Sandler86aea352009-09-11 15:59:22 -0400246 findLiveWallpapers();
247
248 setContentView(R.layout.live_wallpaper_content);
Romain Guy41669fc2009-08-13 12:53:24 -0700249
Daniel Sandler86aea352009-09-11 15:59:22 -0400250 mGallery = (Gallery) findViewById(R.id.gallery);
251 mGallery.setAdapter(new ImageAdapter(this));
252 mGallery.setOnItemSelectedListener(this);
253 mGallery.setCallbackDuringFling(false);
254
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700255 View button = findViewById(R.id.set);
Daniel Sandler86aea352009-09-11 15:59:22 -0400256// button.setEnabled(false);
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700257 button.setOnClickListener(this);
258
Romain Guy41669fc2009-08-13 12:53:24 -0700259 // Set default return data
260 setResult(RESULT_CANCELED);
261 }
262
Romain Guy41669fc2009-08-13 12:53:24 -0700263 @Override
Dianne Hackborn5237ccb2009-08-29 19:17:48 -0700264 public void onResume() {
265 super.onResume();
266 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
267 try {
268 mWallpaperConnection.mEngine.setVisibility(true);
269 } catch (RemoteException e) {
270 }
271 }
272 }
273
274 @Override
275 public void onPause() {
276 super.onPause();
277 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
278 try {
279 mWallpaperConnection.mEngine.setVisibility(false);
280 } catch (RemoteException e) {
281 }
282 }
283 }
284
285 @Override
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700286 public void onDetachedFromWindow() {
287 super.onDetachedFromWindow();
288 if (mWallpaperConnection != null) {
289 mWallpaperConnection.disconnect();
Romain Guy41669fc2009-08-13 12:53:24 -0700290 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700291 mWallpaperConnection = null;
Romain Guy41669fc2009-08-13 12:53:24 -0700292 }
293
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700294 protected Intent getTargetIntent() {
295 return new Intent(WallpaperService.SERVICE_INTERFACE);
296 }
297
Daniel Sandler86aea352009-09-11 15:59:22 -0400298 public void onItemSelected(AdapterView parent, View v, int position, long id) {
299 Log.d(TAG, String.format("onItemSelected: position=%d", position));
300
301 mSelectedIntent = mWallpaperIntents.get(position);
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700302 findViewById(R.id.set).setEnabled(true);
303
304 WallpaperConnection conn = new WallpaperConnection(mSelectedIntent);
305 if (conn.connect()) {
306 if (mWallpaperConnection != null) {
307 mWallpaperConnection.disconnect();
308 }
309 mWallpaperConnection = conn;
310 }
311 }
312
Daniel Sandler86aea352009-09-11 15:59:22 -0400313 public void onClick(View v) { // "Set" button
314 Log.d(TAG, "Set clicked");
315
316// mSelectedIntent = mWallpaperIntents.get(mGallery.getSelectedItemPosition());
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700317 if (mSelectedIntent != null) {
318 try {
319 mWallpaperManager.getIWallpaperManager().setWallpaperComponent(
320 mSelectedIntent.getComponent());
321 this.setResult(RESULT_OK);
322 } catch (RemoteException e) {
323 // do nothing
324 } catch (RuntimeException e) {
325 Log.w(TAG, "Failure setting wallpaper", e);
326 }
327 finish();
328 }
Romain Guy41669fc2009-08-13 12:53:24 -0700329 }
Daniel Sandler86aea352009-09-11 15:59:22 -0400330
331 public void onNothingSelected(AdapterView parent) {
332 }
333
Romain Guy41669fc2009-08-13 12:53:24 -0700334}