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 | |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 19 | import org.xmlpull.v1.XmlPullParserException; |
| 20 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 21 | import android.app.Activity; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 22 | import android.app.ListActivity; |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 23 | import android.app.WallpaperInfo; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 24 | import android.app.WallpaperManager; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 27 | import android.content.DialogInterface; |
| 28 | import android.content.Intent; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 29 | import android.content.ServiceConnection; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 30 | import android.content.pm.ComponentInfo; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
| 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.content.pm.ServiceInfo; |
| 34 | import android.graphics.drawable.Drawable; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 35 | import android.graphics.Bitmap; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 36 | import android.os.Binder; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 37 | import android.os.Bundle; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 38 | import android.os.IBinder; |
| 39 | import android.os.ParcelFileDescriptor; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 40 | import android.os.RemoteException; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 41 | import android.os.SystemClock; |
| 42 | import android.service.wallpaper.IWallpaperConnection; |
| 43 | import android.service.wallpaper.IWallpaperEngine; |
| 44 | import android.service.wallpaper.IWallpaperService; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 45 | import android.service.wallpaper.WallpaperService; |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 46 | import android.service.wallpaper.WallpaperSettingsActivity; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 47 | import android.util.Log; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 49 | import android.view.View; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 50 | import android.view.ViewGroup; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 51 | import android.view.WindowManager; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 52 | import android.widget.AdapterView; |
| 53 | import android.widget.BaseAdapter; |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 54 | import android.widget.Button; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 55 | import android.widget.Gallery; |
| 56 | import android.widget.ImageView; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 57 | import android.widget.ListView; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 58 | |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 59 | import java.io.IOException; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 60 | import java.text.Collator; |
| 61 | import java.util.List; |
| 62 | import java.util.ArrayList; |
| 63 | import java.util.Collections; |
| 64 | import 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 70 | public class LiveWallpaperPickActivity |
| 71 | extends Activity |
| 72 | implements AdapterView.OnItemSelectedListener, |
| 73 | View.OnClickListener |
| 74 | { |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 75 | private static final String TAG = "LiveWallpaperPickActivity"; |
| 76 | |
| 77 | private PackageManager mPackageManager; |
| 78 | private WallpaperManager mWallpaperManager; |
| 79 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 80 | Intent mSelectedIntent; |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 81 | WallpaperInfo mSelectedInfo; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 82 | WallpaperConnection mWallpaperConnection; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 83 | |
| 84 | private Gallery mGallery; |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 85 | private Button mConfigureButton; |
| 86 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 87 | private ArrayList<Intent> mWallpaperIntents; |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 88 | private ArrayList<WallpaperInfo> mWallpaperInfos; |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 89 | |
| 90 | private ArrayList<Bitmap> mThumbBitmaps; |
| 91 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 92 | 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 Hackborn | 5237ccb | 2009-08-29 19:17:48 -0700 | [diff] [blame] | 157 | try { |
| 158 | engine.setVisibility(true); |
| 159 | } catch (RemoteException e) { |
| 160 | } |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 161 | } 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 174 | |
| 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 Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 213 | mPackageManager.queryIntentServices(getTargetIntent(), |
| 214 | PackageManager.GET_META_DATA); |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 215 | |
| 216 | mWallpaperIntents = new ArrayList<Intent>(list.size()); |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 217 | mWallpaperInfos = new ArrayList<WallpaperInfo>(list.size()); |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 218 | |
| 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 Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 224 | 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 234 | 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 Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 241 | mWallpaperInfos.add(winfo); |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 242 | |
| 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 259 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 260 | @Override |
| 261 | public void onCreate(Bundle icicle) { |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 262 | super.onCreate(icicle); |
| 263 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 264 | mPackageManager = getPackageManager(); |
| 265 | mWallpaperManager = WallpaperManager.getInstance(this); |
| 266 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 267 | findLiveWallpapers(); |
| 268 | |
| 269 | setContentView(R.layout.live_wallpaper_content); |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 270 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 271 | mGallery = (Gallery) findViewById(R.id.gallery); |
| 272 | mGallery.setAdapter(new ImageAdapter(this)); |
| 273 | mGallery.setOnItemSelectedListener(this); |
| 274 | mGallery.setCallbackDuringFling(false); |
| 275 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 276 | View button = findViewById(R.id.set); |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 277 | button.setOnClickListener(this); |
| 278 | |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 279 | mConfigureButton = (Button)findViewById(R.id.configure); |
| 280 | mConfigureButton.setEnabled(false); |
| 281 | mConfigureButton.setOnClickListener(this); |
| 282 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 283 | // Set default return data |
| 284 | setResult(RESULT_CANCELED); |
| 285 | } |
| 286 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 287 | @Override |
Dianne Hackborn | 5237ccb | 2009-08-29 19:17:48 -0700 | [diff] [blame] | 288 | 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 310 | public void onDetachedFromWindow() { |
| 311 | super.onDetachedFromWindow(); |
| 312 | if (mWallpaperConnection != null) { |
| 313 | mWallpaperConnection.disconnect(); |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 314 | } |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 315 | mWallpaperConnection = null; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 318 | protected Intent getTargetIntent() { |
| 319 | return new Intent(WallpaperService.SERVICE_INTERFACE); |
| 320 | } |
| 321 | |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 322 | 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 Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 326 | mSelectedInfo = mWallpaperInfos.get(position); |
| 327 | mConfigureButton.setEnabled(mSelectedInfo != null |
| 328 | && mSelectedInfo.getSettingsActivity() != null); |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 329 | 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 Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 340 | public void onClick(View v) { // "Set" button |
| 341 | Log.d(TAG, "Set clicked"); |
| 342 | |
| 343 | // mSelectedIntent = mWallpaperIntents.get(mGallery.getSelectedItemPosition()); |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 344 | 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 356 | } |
Dianne Hackborn | 83a5d38 | 2009-09-15 18:50:10 -0700 | [diff] [blame^] | 357 | } 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 Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame] | 365 | } |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 366 | } |
Daniel Sandler | 86aea35 | 2009-09-11 15:59:22 -0400 | [diff] [blame] | 367 | |
| 368 | public void onNothingSelected(AdapterView parent) { |
| 369 | } |
| 370 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 371 | } |