Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 19 | import android.app.Activity; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 20 | import android.app.ListActivity; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 21 | import android.app.WallpaperManager; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 26 | import android.content.ServiceConnection; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 27 | import android.content.pm.ComponentInfo; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
| 30 | import android.content.pm.ServiceInfo; |
| 31 | import android.graphics.drawable.Drawable; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 32 | import android.graphics.Bitmap; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 33 | import android.os.Binder; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
| 36 | import android.os.ParcelFileDescriptor; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 38 | import android.os.SystemClock; |
| 39 | import android.service.wallpaper.IWallpaperConnection; |
| 40 | import android.service.wallpaper.IWallpaperEngine; |
| 41 | import android.service.wallpaper.IWallpaperService; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 42 | import android.service.wallpaper.WallpaperService; |
| 43 | import android.util.Log; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 44 | import android.view.LayoutInflater; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 45 | import android.view.View; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 46 | import android.view.ViewGroup; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 47 | import android.view.WindowManager; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 48 | import android.widget.AdapterView; |
| 49 | import android.widget.BaseAdapter; |
| 50 | import android.widget.Gallery; |
| 51 | import android.widget.ImageView; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 52 | import android.widget.ListView; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 53 | |
| 54 | import java.text.Collator; |
| 55 | import java.util.List; |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.Collections; |
| 58 | import 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 64 | public class LiveWallpaperPickActivity |
| 65 | extends Activity |
| 66 | implements AdapterView.OnItemSelectedListener, |
| 67 | View.OnClickListener |
| 68 | { |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 69 | private static final String TAG = "LiveWallpaperPickActivity"; |
| 70 | |
| 71 | private PackageManager mPackageManager; |
| 72 | private WallpaperManager mWallpaperManager; |
| 73 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 74 | Intent mSelectedIntent; |
| 75 | WallpaperConnection mWallpaperConnection; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 76 | |
| 77 | private Gallery mGallery; |
| 78 | |
| 79 | private ArrayList<Intent> mWallpaperIntents; |
| 80 | |
| 81 | private ArrayList<Bitmap> mThumbBitmaps; |
| 82 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 83 | 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 Hackborn | 5237ccb | 2009-08-29 19:17:48 -0700 | [diff] [blame] | 148 | try { |
| 149 | engine.setVisibility(true); |
| 150 | } catch (RemoteException e) { |
| 151 | } |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 152 | } 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 165 | |
| 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 238 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void onCreate(Bundle icicle) { |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 241 | super.onCreate(icicle); |
| 242 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 243 | mPackageManager = getPackageManager(); |
| 244 | mWallpaperManager = WallpaperManager.getInstance(this); |
| 245 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 246 | findLiveWallpapers(); |
| 247 | |
| 248 | setContentView(R.layout.live_wallpaper_content); |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 249 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 250 | mGallery = (Gallery) findViewById(R.id.gallery); |
| 251 | mGallery.setAdapter(new ImageAdapter(this)); |
| 252 | mGallery.setOnItemSelectedListener(this); |
| 253 | mGallery.setCallbackDuringFling(false); |
| 254 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 255 | View button = findViewById(R.id.set); |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 256 | // button.setEnabled(false); |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 257 | button.setOnClickListener(this); |
| 258 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 259 | // Set default return data |
| 260 | setResult(RESULT_CANCELED); |
| 261 | } |
| 262 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 263 | @Override |
Dianne Hackborn | 5237ccb | 2009-08-29 19:17:48 -0700 | [diff] [blame] | 264 | 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 286 | public void onDetachedFromWindow() { |
| 287 | super.onDetachedFromWindow(); |
| 288 | if (mWallpaperConnection != null) { |
| 289 | mWallpaperConnection.disconnect(); |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 290 | } |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 291 | mWallpaperConnection = null; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 294 | protected Intent getTargetIntent() { |
| 295 | return new Intent(WallpaperService.SERVICE_INTERFACE); |
| 296 | } |
| 297 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 298 | 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 302 | 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 313 | public void onClick(View v) { // "Set" button |
| 314 | Log.d(TAG, "Set clicked"); |
| 315 | |
| 316 | // mSelectedIntent = mWallpaperIntents.get(mGallery.getSelectedItemPosition()); |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 317 | 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 Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 329 | } |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame^] | 330 | |
| 331 | public void onNothingSelected(AdapterView parent) { |
| 332 | } |
| 333 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 334 | } |