blob: cfafda004db113dc099b38425423fef70f1a15da [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;
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 Guy41669fc2009-08-13 12:53:24 -0700146 @Override
147 public void onCreate(Bundle icicle) {
148 mPackageManager = getPackageManager();
149 mWallpaperManager = WallpaperManager.getInstance(this);
150
151 super.onCreate(icicle);
152
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700153 View button = findViewById(R.id.set);
154 button.setEnabled(false);
155 button.setOnClickListener(this);
156
Romain Guy41669fc2009-08-13 12:53:24 -0700157 // Set default return data
158 setResult(RESULT_CANCELED);
159 }
160
Romain Guy41669fc2009-08-13 12:53:24 -0700161 @Override
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700162 public void onDetachedFromWindow() {
163 super.onDetachedFromWindow();
164 if (mWallpaperConnection != null) {
165 mWallpaperConnection.disconnect();
Romain Guy41669fc2009-08-13 12:53:24 -0700166 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700167 mWallpaperConnection = null;
Romain Guy41669fc2009-08-13 12:53:24 -0700168 }
169
170 @Override
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700171 protected void onSetContentView() {
172 setContentView(R.layout.live_wallpaper_content);
Romain Guy41669fc2009-08-13 12:53:24 -0700173 }
Dianne Hackborn02e638e2009-08-20 19:33:42 -0700174
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 Guy41669fc2009-08-13 12:53:24 -0700212 }
213}