Implement previews in the wallpaper picker.

Note that the UI is ugly ugly ugly, but previews are actually working.
diff --git a/src/com/android/launcher2/LiveWallpaperPickActivity.java b/src/com/android/launcher2/LiveWallpaperPickActivity.java
index 6119a8a..cfafda0 100644
--- a/src/com/android/launcher2/LiveWallpaperPickActivity.java
+++ b/src/com/android/launcher2/LiveWallpaperPickActivity.java
@@ -16,17 +16,32 @@
 
 package com.android.launcher2;
 
+import android.app.LauncherActivity;
+import android.app.ListActivity;
 import android.app.WallpaperManager;
+import android.content.ComponentName;
+import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.ServiceConnection;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ServiceInfo;
 import android.graphics.drawable.Drawable;
+import android.os.Binder;
 import android.os.Bundle;
+import android.os.IBinder;
+import android.os.ParcelFileDescriptor;
 import android.os.RemoteException;
+import android.os.SystemClock;
+import android.service.wallpaper.IWallpaperConnection;
+import android.service.wallpaper.IWallpaperEngine;
+import android.service.wallpaper.IWallpaperService;
 import android.service.wallpaper.WallpaperService;
 import android.util.Log;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.ListView;
 
 import java.text.Collator;
 import java.util.List;
@@ -38,12 +53,96 @@
  * Displays a list of live wallpapers, allowing the user to select one
  * and make it the system global wallpaper.
  */
-public class LiveWallpaperPickActivity extends ActivityPicker {
+public class LiveWallpaperPickActivity extends LauncherActivity
+        implements View.OnClickListener {
     private static final String TAG = "LiveWallpaperPickActivity";
 
     private PackageManager mPackageManager;
     private WallpaperManager mWallpaperManager;
     
+    Intent mSelectedIntent;
+    WallpaperConnection mWallpaperConnection;
+    
+    class WallpaperConnection extends IWallpaperConnection.Stub
+            implements ServiceConnection {
+        final Intent mIntent;
+        IWallpaperService mService;
+        IWallpaperEngine mEngine;
+        boolean mConnected;
+
+        public WallpaperConnection(Intent intent) {
+            mIntent = intent;
+        }
+        
+        public boolean connect() {
+            synchronized (this) {
+                if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) {
+                    return false;
+                }
+                
+                mConnected = true;
+                return true;
+            }
+        }
+        
+        public void disconnect() {
+            synchronized (this) {
+                mConnected = false;
+                if (mEngine != null) {
+                    try {
+                        mEngine.destroy();
+                    } catch (RemoteException e) {
+                    }
+                    mEngine = null;
+                }
+                unbindService(this);
+                mService = null;
+            }
+        }
+        
+        public void onServiceConnected(ComponentName name, IBinder service) {
+            if (mWallpaperConnection == this) {
+                mService = IWallpaperService.Stub.asInterface(service);
+                try {
+                    View button = findViewById(R.id.set);
+                    mService.attach(this, button.getWindowToken(),
+                            WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA,
+                            true,
+                            button.getRootView().getWidth(),
+                            button.getRootView().getHeight());
+                } catch (RemoteException e) {
+                    Log.w(TAG, "Failed attaching wallpaper; clearing", e);
+                }
+            }
+        }
+
+        public void onServiceDisconnected(ComponentName name) {
+            mService = null;
+            mEngine = null;
+            if (mWallpaperConnection == this) {
+                Log.w(TAG, "Wallpaper service gone: " + name);
+            }
+        }
+        
+        public void attachEngine(IWallpaperEngine engine) {
+            synchronized (this) {
+                if (mConnected) {
+                    mEngine = engine;
+                } else {
+                    try {
+                        engine.destroy();
+                    } catch (RemoteException e) {
+                    }
+                }
+            }
+        }
+        
+        public ParcelFileDescriptor setWallpaper(String name) {
+            return null;
+        }
+    }
+    
+    
     @Override
     public void onCreate(Bundle icicle) {
         mPackageManager = getPackageManager();
@@ -51,66 +150,64 @@
         
         super.onCreate(icicle);
         
+        View button = findViewById(R.id.set);
+        button.setEnabled(false);
+        button.setOnClickListener(this);
+        
         // Set default return data
         setResult(RESULT_CANCELED);
     }
     
-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void onClick(DialogInterface dialog, int which) {
-        Intent intent = getIntentForPosition(which);
-        try {
-            mWallpaperManager.getIWallpaperManager().setWallpaperComponent(
-                    intent.getComponent());
-            this.setResult(RESULT_OK);
-        } catch (RemoteException e) {
-            // do nothing
-        } catch (RuntimeException e) {
-            Log.w(TAG, "Failure setting wallpaper", e);
+    public void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        if (mWallpaperConnection != null) {
+            mWallpaperConnection.disconnect();
         }
-        finish();
-    }
-
-    void putLiveWallpaperItems(List<ResolveInfo> ris,
-            List<PickAdapter.Item> items) {
-        final int size = ris.size();
-        for (int i = 0; i < size; i++) {
-            ServiceInfo si = ris.get(i).serviceInfo;
-            
-            CharSequence label = si.loadLabel(mPackageManager);
-            Drawable icon = si.loadIcon(mPackageManager);
-            
-            PickAdapter.Item item = new PickAdapter.Item(this, label, icon);
-            
-            item.packageName = si.packageName;
-            item.className = si.name;
-            
-            items.add(item);
-        }
+        mWallpaperConnection = null;
     }
     
     @Override
-    protected List<PickAdapter.Item> getItems() {
-        List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
-        
-        putInstalledLiveWallpapers(items);
-        
-        // Sort all items together by label
-        Collections.sort(items, new Comparator<PickAdapter.Item>() {
-                Collator mCollator = Collator.getInstance();
-                public int compare(PickAdapter.Item lhs, PickAdapter.Item rhs) {
-                    return mCollator.compare(lhs.label, rhs.label);
-                }
-            });
-
-        return items;
+    protected void onSetContentView() {
+        setContentView(R.layout.live_wallpaper_content);
     }
-
-    void putInstalledLiveWallpapers(List<PickAdapter.Item> items) {
-        List<ResolveInfo> ris = mPackageManager.queryIntentServices(
-                new Intent(WallpaperService.SERVICE_INTERFACE), 0);
-        putLiveWallpaperItems(ris, items);
+    
+    @Override
+    protected Intent getTargetIntent() {
+        return new Intent(WallpaperService.SERVICE_INTERFACE);
+    }
+    
+    @Override
+    protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) {
+        return mPackageManager.queryIntentServices(queryIntent, /* no flags */ 0);
+    }
+    
+    @Override
+    protected void onListItemClick(ListView l, View v, int position, long id) {
+        mSelectedIntent = intentForPosition(position);
+        findViewById(R.id.set).setEnabled(true);
+        
+        WallpaperConnection conn = new WallpaperConnection(mSelectedIntent);
+        if (conn.connect()) {
+            if (mWallpaperConnection != null) {
+                mWallpaperConnection.disconnect();
+            }
+            mWallpaperConnection = conn;
+        }
+    }
+    
+    public void onClick(View v) {
+        if (mSelectedIntent != null) {
+            try {
+                mWallpaperManager.getIWallpaperManager().setWallpaperComponent(
+                        mSelectedIntent.getComponent());
+                this.setResult(RESULT_OK);
+            } catch (RemoteException e) {
+                // do nothing
+            } catch (RuntimeException e) {
+                Log.w(TAG, "Failure setting wallpaper", e);
+            }
+            finish();
+        }
     }
 }