blob: d311a2193d545317524e9480590fd969b958bdf8 [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
Dianne Hackborn83a5d382009-09-15 18:50:10 -070019import org.xmlpull.v1.XmlPullParserException;
20
Daniel Sandler86aea352009-09-11 15:59:22 -040021import android.app.Activity;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070022import android.app.ListActivity;
Dianne Hackborn83a5d382009-09-15 18:50:10 -070023import android.app.WallpaperInfo;
Romain Guy41669fc2009-08-13 12:53:24 -070024import android.app.WallpaperManager;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070025import android.content.ComponentName;
26import android.content.Context;
Romain Guy41669fc2009-08-13 12:53:24 -070027import android.content.DialogInterface;
28import android.content.Intent;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070029import android.content.ServiceConnection;
Daniel Sandler86aea352009-09-11 15:59:22 -040030import android.content.pm.ComponentInfo;
Romain Guy41669fc2009-08-13 12:53:24 -070031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.pm.ServiceInfo;
34import android.graphics.drawable.Drawable;
Daniel Sandler86aea352009-09-11 15:59:22 -040035import android.graphics.Bitmap;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070036import android.os.Binder;
Romain Guy41669fc2009-08-13 12:53:24 -070037import android.os.Bundle;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070038import android.os.IBinder;
39import android.os.ParcelFileDescriptor;
Romain Guy41669fc2009-08-13 12:53:24 -070040import android.os.RemoteException;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070041import android.os.SystemClock;
42import android.service.wallpaper.IWallpaperConnection;
43import android.service.wallpaper.IWallpaperEngine;
44import android.service.wallpaper.IWallpaperService;
Romain Guy41669fc2009-08-13 12:53:24 -070045import android.service.wallpaper.WallpaperService;
Dianne Hackborn83a5d382009-09-15 18:50:10 -070046import android.service.wallpaper.WallpaperSettingsActivity;
Romain Guy41669fc2009-08-13 12:53:24 -070047import android.util.Log;
Daniel Sandler86aea352009-09-11 15:59:22 -040048import android.view.LayoutInflater;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070049import android.view.View;
Daniel Sandler86aea352009-09-11 15:59:22 -040050import android.view.ViewGroup;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070051import android.view.WindowManager;
Daniel Sandler86aea352009-09-11 15:59:22 -040052import android.widget.AdapterView;
53import android.widget.BaseAdapter;
Dianne Hackborn83a5d382009-09-15 18:50:10 -070054import android.widget.Button;
Daniel Sandler86aea352009-09-11 15:59:22 -040055import android.widget.Gallery;
56import android.widget.ImageView;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070057import android.widget.ListView;
Romain Guy41669fc2009-08-13 12:53:24 -070058
Dianne Hackborn83a5d382009-09-15 18:50:10 -070059import java.io.IOException;
Romain Guy41669fc2009-08-13 12:53:24 -070060import java.text.Collator;
61import java.util.List;
62import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Comparator;
65
66/**
67 * Displays a list of live wallpapers, allowing the user to select one
68 * and make it the system global wallpaper.
69 */
Daniel Sandler86aea352009-09-11 15:59:22 -040070public class LiveWallpaperPickActivity
71 extends Activity
72 implements AdapterView.OnItemSelectedListener,
73 View.OnClickListener
74{
Romain Guy41669fc2009-08-13 12:53:24 -070075 private static final String TAG = "LiveWallpaperPickActivity";
76
77 private PackageManager mPackageManager;
78 private WallpaperManager mWallpaperManager;
79
Dianne Hackborn02e638e2009-08-20 19:33:42 -070080 Intent mSelectedIntent;
Dianne Hackborn83a5d382009-09-15 18:50:10 -070081 WallpaperInfo mSelectedInfo;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070082 WallpaperConnection mWallpaperConnection;
Daniel Sandler86aea352009-09-11 15:59:22 -040083
84 private Gallery mGallery;
Dianne Hackborn83a5d382009-09-15 18:50:10 -070085 private Button mConfigureButton;
86
Daniel Sandler86aea352009-09-11 15:59:22 -040087 private ArrayList<Intent> mWallpaperIntents;
Dianne Hackborn83a5d382009-09-15 18:50:10 -070088 private ArrayList<WallpaperInfo> mWallpaperInfos;
Daniel Sandler86aea352009-09-11 15:59:22 -040089
90 private ArrayList<Bitmap> mThumbBitmaps;
91
Dianne Hackborn02e638e2009-08-20 19:33:42 -070092 class WallpaperConnection extends IWallpaperConnection.Stub
93 implements ServiceConnection {
94 final Intent mIntent;
95 IWallpaperService mService;
96 IWallpaperEngine mEngine;
97 boolean mConnected;
98
99 public WallpaperConnection(Intent intent) {
100 mIntent = intent;
101 }
102
103 public boolean connect() {
104 synchronized (this) {
105 if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) {
106 return false;
107 }
108
109 mConnected = true;
110 return true;
111 }
112 }
113
114 public void disconnect() {
115 synchronized (this) {
116 mConnected = false;
117 if (mEngine != null) {
118 try {
119 mEngine.destroy();
120 } catch (RemoteException e) {
121 }
122 mEngine = null;
123 }
124 unbindService(this);
125 mService = null;
126 }
127 }
128
129 public void onServiceConnected(ComponentName name, IBinder service) {
130 if (mWallpaperConnection == this) {
131 mService = IWallpaperService.Stub.asInterface(service);
132 try {
133 View button = findViewById(R.id.set);
134 mService.attach(this, button.getWindowToken(),
135 WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA,
136 true,
137 button.getRootView().getWidth(),
138 button.getRootView().getHeight());
139 } catch (RemoteException e) {
140 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
141 }
142 }
143 }
144
145 public void onServiceDisconnected(ComponentName name) {
146 mService = null;
147 mEngine = null;
148 if (mWallpaperConnection == this) {
149 Log.w(TAG, "Wallpaper service gone: " + name);
150 }
151 }
152
153 public void attachEngine(IWallpaperEngine engine) {
154 synchronized (this) {
155 if (mConnected) {
156 mEngine = engine;
Dianne Hackborn5237ccb2009-08-29 19:17:48 -0700157 try {
158 engine.setVisibility(true);
159 } catch (RemoteException e) {
160 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700161 } else {
162 try {
163 engine.destroy();
164 } catch (RemoteException e) {
165 }
166 }
167 }
168 }
169
170 public ParcelFileDescriptor setWallpaper(String name) {
171 return null;
172 }
173 }
Daniel Sandler86aea352009-09-11 15:59:22 -0400174
175 private class ImageAdapter extends BaseAdapter {
176 private LayoutInflater mLayoutInflater;
177
178 ImageAdapter(LiveWallpaperPickActivity context) {
179 mLayoutInflater = context.getLayoutInflater();
180 }
181
182 public int getCount() {
183 return mThumbBitmaps.size();
184 }
185
186 public Object getItem(int position) {
187 return position;
188 }
189
190 public long getItemId(int position) {
191 return position;
192 }
193
194 public View getView(int position, View convertView, ViewGroup parent) {
195 ImageView image;
196
197 if (convertView == null) {
198 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
199 } else {
200 image = (ImageView) convertView;
201 }
202
203 image.setImageBitmap(mThumbBitmaps.get(position));
204 image.getDrawable().setDither(true);
205 return image;
206 }
207 }
208
209
210 private void findLiveWallpapers() {
211 mThumbBitmaps = new ArrayList<Bitmap>(24);
212 List<ResolveInfo> list =
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700213 mPackageManager.queryIntentServices(getTargetIntent(),
214 PackageManager.GET_META_DATA);
Daniel Sandler86aea352009-09-11 15:59:22 -0400215
216 mWallpaperIntents = new ArrayList<Intent>(list.size());
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700217 mWallpaperInfos = new ArrayList<WallpaperInfo>(list.size());
Daniel Sandler86aea352009-09-11 15:59:22 -0400218
219 int listSize = list.size();
220 Log.d(TAG, String.format("findLiveWallpapers: found %d wallpaper services", listSize));
221 for (int i = 0; i < listSize; i++) {
222 ResolveInfo resolveInfo = list.get(i);
223 ComponentInfo ci = resolveInfo.serviceInfo;
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700224 WallpaperInfo winfo;
225 try {
226 winfo = new WallpaperInfo(this, resolveInfo);
227 } catch (XmlPullParserException e) {
228 Log.w(TAG, "Skipping wallpaper " + ci, e);
229 continue;
230 } catch (IOException e) {
231 Log.w(TAG, "Skipping wallpaper " + ci, e);
232 continue;
233 }
Daniel Sandler86aea352009-09-11 15:59:22 -0400234 String packageName = ci.applicationInfo.packageName;
235 String className = ci.name;
236 Log.d(TAG, String.format("findLiveWallpapers: [%d] pkg=%s cls=%s",
237 i, packageName, className));
238 Intent intent = new Intent(getTargetIntent());
239 intent.setClassName(packageName, className);
240 mWallpaperIntents.add(intent);
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700241 mWallpaperInfos.add(winfo);
Daniel Sandler86aea352009-09-11 15:59:22 -0400242
243 Bitmap thumb = Bitmap.createBitmap(240,160,Bitmap.Config.ARGB_8888);
244 android.graphics.Canvas can = new android.graphics.Canvas(thumb);
245 android.graphics.Paint pt = new android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG|android.graphics.Paint.DITHER_FLAG);
246 pt.setARGB(255, 0, 0, 255);
247 can.drawPaint(pt);
248 pt.setARGB(255, 255, 255, 255);
249 pt.setTextSize(12);
250 can.drawText(className, 16, 150, pt);
251 pt.setTextSize(80);
252 can.drawText(String.format("%d", i), 100,100, pt);
253 mThumbBitmaps.add(thumb);
254 }
255
256
257 }
258
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700259
Romain Guy41669fc2009-08-13 12:53:24 -0700260 @Override
261 public void onCreate(Bundle icicle) {
Daniel Sandler86aea352009-09-11 15:59:22 -0400262 super.onCreate(icicle);
263
Romain Guy41669fc2009-08-13 12:53:24 -0700264 mPackageManager = getPackageManager();
265 mWallpaperManager = WallpaperManager.getInstance(this);
266
Daniel Sandler86aea352009-09-11 15:59:22 -0400267 findLiveWallpapers();
268
269 setContentView(R.layout.live_wallpaper_content);
Romain Guy41669fc2009-08-13 12:53:24 -0700270
Daniel Sandler86aea352009-09-11 15:59:22 -0400271 mGallery = (Gallery) findViewById(R.id.gallery);
272 mGallery.setAdapter(new ImageAdapter(this));
273 mGallery.setOnItemSelectedListener(this);
274 mGallery.setCallbackDuringFling(false);
275
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700276 View button = findViewById(R.id.set);
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700277 button.setOnClickListener(this);
278
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700279 mConfigureButton = (Button)findViewById(R.id.configure);
280 mConfigureButton.setEnabled(false);
281 mConfigureButton.setOnClickListener(this);
282
Romain Guy41669fc2009-08-13 12:53:24 -0700283 // Set default return data
284 setResult(RESULT_CANCELED);
285 }
286
Romain Guy41669fc2009-08-13 12:53:24 -0700287 @Override
Dianne Hackborn5237ccb2009-08-29 19:17:48 -0700288 public void onResume() {
289 super.onResume();
290 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
291 try {
292 mWallpaperConnection.mEngine.setVisibility(true);
293 } catch (RemoteException e) {
294 }
295 }
296 }
297
298 @Override
299 public void onPause() {
300 super.onPause();
301 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
302 try {
303 mWallpaperConnection.mEngine.setVisibility(false);
304 } catch (RemoteException e) {
305 }
306 }
307 }
308
309 @Override
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700310 public void onDetachedFromWindow() {
311 super.onDetachedFromWindow();
312 if (mWallpaperConnection != null) {
313 mWallpaperConnection.disconnect();
Romain Guy41669fc2009-08-13 12:53:24 -0700314 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700315 mWallpaperConnection = null;
Romain Guy41669fc2009-08-13 12:53:24 -0700316 }
317
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700318 protected Intent getTargetIntent() {
319 return new Intent(WallpaperService.SERVICE_INTERFACE);
320 }
321
Daniel Sandler86aea352009-09-11 15:59:22 -0400322 public void onItemSelected(AdapterView parent, View v, int position, long id) {
323 Log.d(TAG, String.format("onItemSelected: position=%d", position));
324
325 mSelectedIntent = mWallpaperIntents.get(position);
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700326 mSelectedInfo = mWallpaperInfos.get(position);
327 mConfigureButton.setEnabled(mSelectedInfo != null
328 && mSelectedInfo.getSettingsActivity() != null);
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700329 findViewById(R.id.set).setEnabled(true);
330
331 WallpaperConnection conn = new WallpaperConnection(mSelectedIntent);
332 if (conn.connect()) {
333 if (mWallpaperConnection != null) {
334 mWallpaperConnection.disconnect();
335 }
336 mWallpaperConnection = conn;
337 }
338 }
339
Daniel Sandler86aea352009-09-11 15:59:22 -0400340 public void onClick(View v) { // "Set" button
341 Log.d(TAG, "Set clicked");
342
343// mSelectedIntent = mWallpaperIntents.get(mGallery.getSelectedItemPosition());
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700344 if (v.getId() == R.id.set) {
345 if (mSelectedIntent != null) {
346 try {
347 mWallpaperManager.getIWallpaperManager().setWallpaperComponent(
348 mSelectedIntent.getComponent());
349 this.setResult(RESULT_OK);
350 } catch (RemoteException e) {
351 // do nothing
352 } catch (RuntimeException e) {
353 Log.w(TAG, "Failure setting wallpaper", e);
354 }
355 finish();
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700356 }
Dianne Hackborn83a5d382009-09-15 18:50:10 -0700357 } else if (v.getId() == R.id.configure) {
358 if (mSelectedInfo != null && mSelectedInfo.getSettingsActivity() != null) {
359 Intent intent = new Intent();
360 intent.setComponent(new ComponentName(mSelectedInfo.getPackageName(),
361 mSelectedInfo.getSettingsActivity()));
362 intent.putExtra(WallpaperSettingsActivity.EXTRA_PREVIEW_MODE, true);
363 startActivity(intent);
364 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700365 }
Romain Guy41669fc2009-08-13 12:53:24 -0700366 }
Daniel Sandler86aea352009-09-11 15:59:22 -0400367
368 public void onNothingSelected(AdapterView parent) {
369 }
370
Romain Guy41669fc2009-08-13 12:53:24 -0700371}