blob: 40d183a52eee97b8d7a206f6eabd19933b7baeb2 [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 Hackborn02e638e2009-08-20 19:33:42 -070019import android.app.LauncherActivity;
20import android.app.ListActivity;
Romain Guy41669fc2009-08-13 12:53:24 -070021import android.app.WallpaperManager;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070022import android.content.ComponentName;
23import android.content.Context;
Romain Guy41669fc2009-08-13 12:53:24 -070024import android.content.DialogInterface;
25import android.content.Intent;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070026import android.content.ServiceConnection;
Romain Guy41669fc2009-08-13 12:53:24 -070027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.content.pm.ServiceInfo;
30import android.graphics.drawable.Drawable;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070031import android.os.Binder;
Romain Guy41669fc2009-08-13 12:53:24 -070032import android.os.Bundle;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070033import android.os.IBinder;
34import android.os.ParcelFileDescriptor;
Romain Guy41669fc2009-08-13 12:53:24 -070035import android.os.RemoteException;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070036import android.os.SystemClock;
37import android.service.wallpaper.IWallpaperConnection;
38import android.service.wallpaper.IWallpaperEngine;
39import android.service.wallpaper.IWallpaperService;
Romain Guy41669fc2009-08-13 12:53:24 -070040import android.service.wallpaper.WallpaperService;
41import android.util.Log;
Dianne Hackborn02e638e2009-08-20 19:33:42 -070042import android.view.View;
43import android.view.WindowManager;
44import android.widget.ListView;
Romain Guy41669fc2009-08-13 12:53:24 -070045
46import java.text.Collator;
47import java.util.List;
48import java.util.ArrayList;
49import java.util.Collections;
50import 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 Hackborn02e638e2009-08-20 19:33:42 -070056public class LiveWallpaperPickActivity extends LauncherActivity
57 implements View.OnClickListener {
Romain Guy41669fc2009-08-13 12:53:24 -070058 private static final String TAG = "LiveWallpaperPickActivity";
59
60 private PackageManager mPackageManager;
61 private WallpaperManager mWallpaperManager;
62
Dianne Hackborn02e638e2009-08-20 19:33:42 -070063 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;
Dianne Hackborn5237ccb2009-08-29 19:17:48 -0700131 try {
132 engine.setVisibility(true);
133 } catch (RemoteException e) {
134 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700135 } else {
136 try {
137 engine.destroy();
138 } catch (RemoteException e) {
139 }
140 }
141 }
142 }
143
144 public ParcelFileDescriptor setWallpaper(String name) {
145 return null;
146 }
147 }
148
149
Romain Guy41669fc2009-08-13 12:53:24 -0700150 @Override
151 public void onCreate(Bundle icicle) {
152 mPackageManager = getPackageManager();
153 mWallpaperManager = WallpaperManager.getInstance(this);
154
155 super.onCreate(icicle);
156
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700157 View button = findViewById(R.id.set);
158 button.setEnabled(false);
159 button.setOnClickListener(this);
160
Romain Guy41669fc2009-08-13 12:53:24 -0700161 // Set default return data
162 setResult(RESULT_CANCELED);
163 }
164
Romain Guy41669fc2009-08-13 12:53:24 -0700165 @Override
Dianne Hackborn5237ccb2009-08-29 19:17:48 -0700166 public void onResume() {
167 super.onResume();
168 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
169 try {
170 mWallpaperConnection.mEngine.setVisibility(true);
171 } catch (RemoteException e) {
172 }
173 }
174 }
175
176 @Override
177 public void onPause() {
178 super.onPause();
179 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
180 try {
181 mWallpaperConnection.mEngine.setVisibility(false);
182 } catch (RemoteException e) {
183 }
184 }
185 }
186
187 @Override
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700188 public void onDetachedFromWindow() {
189 super.onDetachedFromWindow();
190 if (mWallpaperConnection != null) {
191 mWallpaperConnection.disconnect();
Romain Guy41669fc2009-08-13 12:53:24 -0700192 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700193 mWallpaperConnection = null;
Romain Guy41669fc2009-08-13 12:53:24 -0700194 }
195
196 @Override
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700197 protected void onSetContentView() {
198 setContentView(R.layout.live_wallpaper_content);
Romain Guy41669fc2009-08-13 12:53:24 -0700199 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700200
201 @Override
202 protected Intent getTargetIntent() {
203 return new Intent(WallpaperService.SERVICE_INTERFACE);
204 }
205
206 @Override
207 protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) {
208 return mPackageManager.queryIntentServices(queryIntent, /* no flags */ 0);
209 }
210
211 @Override
212 protected void onListItemClick(ListView l, View v, int position, long id) {
213 mSelectedIntent = intentForPosition(position);
214 findViewById(R.id.set).setEnabled(true);
215
216 WallpaperConnection conn = new WallpaperConnection(mSelectedIntent);
217 if (conn.connect()) {
218 if (mWallpaperConnection != null) {
219 mWallpaperConnection.disconnect();
220 }
221 mWallpaperConnection = conn;
222 }
223 }
224
225 public void onClick(View v) {
226 if (mSelectedIntent != null) {
227 try {
228 mWallpaperManager.getIWallpaperManager().setWallpaperComponent(
229 mSelectedIntent.getComponent());
230 this.setResult(RESULT_OK);
231 } catch (RemoteException e) {
232 // do nothing
233 } catch (RuntimeException e) {
234 Log.w(TAG, "Failure setting wallpaper", e);
235 }
236 finish();
237 }
Romain Guy41669fc2009-08-13 12:53:24 -0700238 }
239}