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 | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 19 | import android.app.LauncherActivity; |
| 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; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.content.pm.ServiceInfo; |
| 30 | import android.graphics.drawable.Drawable; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 31 | import android.os.Binder; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 33 | import android.os.IBinder; |
| 34 | import android.os.ParcelFileDescriptor; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 35 | import android.os.RemoteException; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 36 | import android.os.SystemClock; |
| 37 | import android.service.wallpaper.IWallpaperConnection; |
| 38 | import android.service.wallpaper.IWallpaperEngine; |
| 39 | import android.service.wallpaper.IWallpaperService; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 40 | import android.service.wallpaper.WallpaperService; |
| 41 | import android.util.Log; |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 42 | import android.view.View; |
| 43 | import android.view.WindowManager; |
| 44 | import android.widget.ListView; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 45 | |
| 46 | import java.text.Collator; |
| 47 | import java.util.List; |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.Collections; |
| 50 | import java.util.Comparator; |
| 51 | |
| 52 | /** |
| 53 | * Displays a list of live wallpapers, allowing the user to select one |
| 54 | * and make it the system global wallpaper. |
| 55 | */ |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 56 | public class LiveWallpaperPickActivity extends LauncherActivity |
| 57 | implements View.OnClickListener { |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 58 | private static final String TAG = "LiveWallpaperPickActivity"; |
| 59 | |
| 60 | private PackageManager mPackageManager; |
| 61 | private WallpaperManager mWallpaperManager; |
| 62 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 63 | Intent mSelectedIntent; |
| 64 | WallpaperConnection mWallpaperConnection; |
| 65 | |
| 66 | class WallpaperConnection extends IWallpaperConnection.Stub |
| 67 | implements ServiceConnection { |
| 68 | final Intent mIntent; |
| 69 | IWallpaperService mService; |
| 70 | IWallpaperEngine mEngine; |
| 71 | boolean mConnected; |
| 72 | |
| 73 | public WallpaperConnection(Intent intent) { |
| 74 | mIntent = intent; |
| 75 | } |
| 76 | |
| 77 | public boolean connect() { |
| 78 | synchronized (this) { |
| 79 | if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | mConnected = true; |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public void disconnect() { |
| 89 | synchronized (this) { |
| 90 | mConnected = false; |
| 91 | if (mEngine != null) { |
| 92 | try { |
| 93 | mEngine.destroy(); |
| 94 | } catch (RemoteException e) { |
| 95 | } |
| 96 | mEngine = null; |
| 97 | } |
| 98 | unbindService(this); |
| 99 | mService = null; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 104 | if (mWallpaperConnection == this) { |
| 105 | mService = IWallpaperService.Stub.asInterface(service); |
| 106 | try { |
| 107 | View button = findViewById(R.id.set); |
| 108 | mService.attach(this, button.getWindowToken(), |
| 109 | WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA, |
| 110 | true, |
| 111 | button.getRootView().getWidth(), |
| 112 | button.getRootView().getHeight()); |
| 113 | } catch (RemoteException e) { |
| 114 | Log.w(TAG, "Failed attaching wallpaper; clearing", e); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public void onServiceDisconnected(ComponentName name) { |
| 120 | mService = null; |
| 121 | mEngine = null; |
| 122 | if (mWallpaperConnection == this) { |
| 123 | Log.w(TAG, "Wallpaper service gone: " + name); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public void attachEngine(IWallpaperEngine engine) { |
| 128 | synchronized (this) { |
| 129 | if (mConnected) { |
| 130 | mEngine = engine; |
| 131 | } else { |
| 132 | try { |
| 133 | engine.destroy(); |
| 134 | } catch (RemoteException e) { |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public ParcelFileDescriptor setWallpaper(String name) { |
| 141 | return null; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void onCreate(Bundle icicle) { |
| 148 | mPackageManager = getPackageManager(); |
| 149 | mWallpaperManager = WallpaperManager.getInstance(this); |
| 150 | |
| 151 | super.onCreate(icicle); |
| 152 | |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 153 | View button = findViewById(R.id.set); |
| 154 | button.setEnabled(false); |
| 155 | button.setOnClickListener(this); |
| 156 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 157 | // Set default return data |
| 158 | setResult(RESULT_CANCELED); |
| 159 | } |
| 160 | |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 161 | @Override |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 162 | public void onDetachedFromWindow() { |
| 163 | super.onDetachedFromWindow(); |
| 164 | if (mWallpaperConnection != null) { |
| 165 | mWallpaperConnection.disconnect(); |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 166 | } |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 167 | mWallpaperConnection = null; |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 171 | protected void onSetContentView() { |
| 172 | setContentView(R.layout.live_wallpaper_content); |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 173 | } |
Dianne Hackborn | 02e638e | 2009-08-20 19:33:42 -0700 | [diff] [blame^] | 174 | |
| 175 | @Override |
| 176 | protected Intent getTargetIntent() { |
| 177 | return new Intent(WallpaperService.SERVICE_INTERFACE); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) { |
| 182 | return mPackageManager.queryIntentServices(queryIntent, /* no flags */ 0); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 187 | mSelectedIntent = intentForPosition(position); |
| 188 | findViewById(R.id.set).setEnabled(true); |
| 189 | |
| 190 | WallpaperConnection conn = new WallpaperConnection(mSelectedIntent); |
| 191 | if (conn.connect()) { |
| 192 | if (mWallpaperConnection != null) { |
| 193 | mWallpaperConnection.disconnect(); |
| 194 | } |
| 195 | mWallpaperConnection = conn; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public void onClick(View v) { |
| 200 | if (mSelectedIntent != null) { |
| 201 | try { |
| 202 | mWallpaperManager.getIWallpaperManager().setWallpaperComponent( |
| 203 | mSelectedIntent.getComponent()); |
| 204 | this.setResult(RESULT_OK); |
| 205 | } catch (RemoteException e) { |
| 206 | // do nothing |
| 207 | } catch (RuntimeException e) { |
| 208 | Log.w(TAG, "Failure setting wallpaper", e); |
| 209 | } |
| 210 | finish(); |
| 211 | } |
Romain Guy | 41669fc | 2009-08-13 12:53:24 -0700 | [diff] [blame] | 212 | } |
| 213 | } |