blob: 86bbc5588b4004c3f4e4c502a989fd72287a36bf [file] [log] [blame]
Dianne Hackborn8cc6a502009-08-05 21:29:42 -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 android.service.wallpaper;
18
19import com.android.internal.os.HandlerCaller;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070020import com.android.internal.view.BaseIWindow;
21import com.android.internal.view.BaseSurfaceHolder;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070022
Dianne Hackbornd6847842010-01-12 18:14:19 -080023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070025import android.app.Service;
Dianne Hackborn759a39e2009-08-09 17:20:27 -070026import android.app.WallpaperManager;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070029import android.content.Intent;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070030import android.content.IntentFilter;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080031import android.content.res.Configuration;
Mathias Agopian62bf4a02010-09-08 16:32:27 -070032import android.graphics.PixelFormat;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070033import android.graphics.Rect;
Dianne Hackborn75804932009-10-20 20:15:20 -070034import android.os.Bundle;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070035import android.os.IBinder;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070036import android.os.Looper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070037import android.os.Message;
Dianne Hackborn9fe6cb52011-09-09 13:02:43 -070038import android.os.PowerManager;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070039import android.os.RemoteException;
40import android.util.Log;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070041import android.util.LogPrinter;
Craig Mautner6881a102012-07-27 13:04:51 -070042import android.view.Display;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070043import android.view.Gravity;
44import android.view.IWindowSession;
Jeff Brown46b9ac02010-04-22 18:58:52 -070045import android.view.InputChannel;
Jeff Brownc5ed5912010-07-14 18:48:53 -070046import android.view.InputDevice;
Jeff Brown4952dfd2011-11-30 19:23:22 -080047import android.view.InputEvent;
Jeff Brown32cbc38552011-12-01 14:01:49 -080048import android.view.InputEventReceiver;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -070049import android.view.MotionEvent;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070050import android.view.SurfaceHolder;
51import android.view.View;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070052import android.view.ViewGroup;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070053import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070054import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070055
Dianne Hackborn527de8e2011-08-22 16:10:36 -070056import java.io.FileDescriptor;
57import java.io.PrintWriter;
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -080058import java.util.ArrayList;
59
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070060/**
61 * A wallpaper service is responsible for showing a live wallpaper behind
Dianne Hackborn23ef7b42009-11-18 18:20:39 -080062 * applications that would like to sit on top of it. This service object
63 * itself does very little -- its only purpose is to generate instances of
Dianne Hackborne4260f42009-11-18 21:15:59 -080064 * {@link Engine} as needed. Implementing a wallpaper thus
Dianne Hackborn23ef7b42009-11-18 18:20:39 -080065 * involves subclassing from this, subclassing an Engine implementation,
66 * and implementing {@link #onCreateEngine()} to return a new instance of
67 * your engine.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070068 */
69public abstract class WallpaperService extends Service {
70 /**
71 * The {@link Intent} that must be declared as handled by the service.
Dianne Hackbornd6847842010-01-12 18:14:19 -080072 * To be supported, the service must also require the
73 * {@link android.Manifest.permission#BIND_WALLPAPER} permission so
74 * that other applications can not abuse it.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070075 */
Dianne Hackbornd6847842010-01-12 18:14:19 -080076 @SdkConstant(SdkConstantType.SERVICE_ACTION)
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070077 public static final String SERVICE_INTERFACE =
Dianne Hackbornd6847842010-01-12 18:14:19 -080078 "android.service.wallpaper.WallpaperService";
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070079
Dianne Hackborneb034652009-09-07 00:49:58 -070080 /**
81 * Name under which a WallpaperService component publishes information
82 * about itself. This meta-data must reference an XML resource containing
83 * a <code>&lt;{@link android.R.styleable#Wallpaper wallpaper}&gt;</code>
84 * tag.
85 */
86 public static final String SERVICE_META_DATA = "android.service.wallpaper";
87
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070088 static final String TAG = "WallpaperService";
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070089 static final boolean DEBUG = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070090
91 private static final int DO_ATTACH = 10;
92 private static final int DO_DETACH = 20;
Dianne Hackborn284ac932009-08-28 10:34:25 -070093 private static final int DO_SET_DESIRED_SIZE = 30;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070094
95 private static final int MSG_UPDATE_SURFACE = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -070096 private static final int MSG_VISIBILITY_CHANGED = 10010;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070097 private static final int MSG_WALLPAPER_OFFSETS = 10020;
Dianne Hackborn75804932009-10-20 20:15:20 -070098 private static final int MSG_WALLPAPER_COMMAND = 10025;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070099 private static final int MSG_WINDOW_RESIZED = 10030;
Craig Mautner5702d4d2012-06-30 14:10:16 -0700100 private static final int MSG_WINDOW_MOVED = 10035;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700101 private static final int MSG_TOUCH_EVENT = 10040;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700102
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700103 private Looper mCallbackLooper;
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -0800104 private final ArrayList<Engine> mActiveEngines
105 = new ArrayList<Engine>();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700106
Dianne Hackborn75804932009-10-20 20:15:20 -0700107 static final class WallpaperCommand {
108 String action;
109 int x;
110 int y;
111 int z;
112 Bundle extras;
113 boolean sync;
114 }
115
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700116 /**
117 * The actual implementation of a wallpaper. A wallpaper service may
118 * have multiple instances running (for example as a real wallpaper
119 * and as a preview), each of which is represented by its own Engine
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700120 * instance. You must implement {@link WallpaperService#onCreateEngine()}
121 * to return your concrete Engine implementation.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700122 */
123 public class Engine {
124 IWallpaperEngineWrapper mIWallpaperEngine;
125
126 // Copies from mIWallpaperEngine.
127 HandlerCaller mCaller;
128 IWallpaperConnection mConnection;
129 IBinder mWindowToken;
130
131 boolean mInitializing = true;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700132 boolean mVisible;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700133 boolean mScreenOn = true;
134 boolean mReportedVisible;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700135 boolean mDestroyed;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700136
137 // Current window state.
138 boolean mCreated;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700139 boolean mSurfaceCreated;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700140 boolean mIsCreating;
141 boolean mDrawingAllowed;
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700142 boolean mOffsetsChanged;
Jeff Sharkey35be7562012-04-18 19:16:15 -0700143 boolean mFixedSizeAllowed;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700144 int mWidth;
145 int mHeight;
146 int mFormat;
147 int mType;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700148 int mCurWidth;
149 int mCurHeight;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700150 int mWindowFlags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700151 int mWindowPrivateFlags =
152 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700153 int mCurWindowFlags = mWindowFlags;
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700154 int mCurWindowPrivateFlags = mWindowPrivateFlags;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700155 final Rect mVisibleInsets = new Rect();
156 final Rect mWinFrame = new Rect();
157 final Rect mContentInsets = new Rect();
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700158 final Configuration mConfiguration = new Configuration();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700159
160 final WindowManager.LayoutParams mLayout
161 = new WindowManager.LayoutParams();
162 IWindowSession mSession;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700163 InputChannel mInputChannel;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700164
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700165 final Object mLock = new Object();
166 boolean mOffsetMessageEnqueued;
167 float mPendingXOffset;
168 float mPendingYOffset;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800169 float mPendingXOffsetStep;
170 float mPendingYOffsetStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700171 boolean mPendingSync;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700172 MotionEvent mPendingMove;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700173
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700174 final BroadcastReceiver mReceiver = new BroadcastReceiver() {
175 @Override
176 public void onReceive(Context context, Intent intent) {
177 if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
178 mScreenOn = true;
179 reportVisibility();
180 } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
181 mScreenOn = false;
182 reportVisibility();
183 }
184 }
185 };
186
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700187 final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() {
Mathias Agopian62bf4a02010-09-08 16:32:27 -0700188 {
Jeff Brown24572372011-06-09 19:05:15 -0700189 mRequestedFormat = PixelFormat.RGBX_8888;
Mathias Agopian62bf4a02010-09-08 16:32:27 -0700190 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700191
192 @Override
193 public boolean onAllowLockCanvas() {
194 return mDrawingAllowed;
195 }
196
197 @Override
198 public void onRelayoutContainer() {
199 Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE);
200 mCaller.sendMessage(msg);
201 }
202
203 @Override
204 public void onUpdateSurface() {
205 Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE);
206 mCaller.sendMessage(msg);
207 }
208
209 public boolean isCreating() {
210 return mIsCreating;
211 }
212
Dianne Hackborna48a37f2011-02-01 16:30:38 -0800213 @Override
214 public void setFixedSize(int width, int height) {
Jeff Sharkey35be7562012-04-18 19:16:15 -0700215 if (!mFixedSizeAllowed) {
Dianne Hackborna48a37f2011-02-01 16:30:38 -0800216 // Regular apps can't do this. It can only work for
217 // certain designs of window animations, so you can't
218 // rely on it.
219 throw new UnsupportedOperationException(
220 "Wallpapers currently only support sizing from layout");
221 }
Michael Jurkab8f939f2011-02-01 20:50:30 -0800222 super.setFixedSize(width, height);
Dianne Hackborna48a37f2011-02-01 16:30:38 -0800223 }
224
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700225 public void setKeepScreenOn(boolean screenOn) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700226 throw new UnsupportedOperationException(
227 "Wallpapers do not support keep screen on");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700228 }
229
230 };
Jeff Brown32cbc38552011-12-01 14:01:49 -0800231
232 final class WallpaperInputEventReceiver extends InputEventReceiver {
233 public WallpaperInputEventReceiver(InputChannel inputChannel, Looper looper) {
234 super(inputChannel, looper);
235 }
236
Jeff Brown46b9ac02010-04-22 18:58:52 -0700237 @Override
Jeff Brown32cbc38552011-12-01 14:01:49 -0800238 public void onInputEvent(InputEvent event) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700239 boolean handled = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 try {
Jeff Brown4952dfd2011-11-30 19:23:22 -0800241 if (event instanceof MotionEvent
242 && (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800243 MotionEvent dup = MotionEvent.obtainNoHistory((MotionEvent)event);
244 dispatchPointer(dup);
Jeff Brown3915bb82010-11-05 15:02:16 -0700245 handled = true;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700246 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700247 } finally {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800248 finishInputEvent(event, handled);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700249 }
250 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800251 }
252 WallpaperInputEventReceiver mInputEventReceiver;
253
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700254 final BaseIWindow mWindow = new BaseIWindow() {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700255 @Override
Svetoslav Ganov758143e2012-08-06 16:40:27 -0700256 public void resized(Rect frame, Rect contentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800257 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700258 Message msg = mCaller.obtainMessageI(MSG_WINDOW_RESIZED,
259 reportDraw ? 1 : 0);
260 mCaller.sendMessage(msg);
261 }
Craig Mautner5702d4d2012-06-30 14:10:16 -0700262
263 @Override
264 public void moved(int newX, int newY) {
265 Message msg = mCaller.obtainMessageII(MSG_WINDOW_MOVED, newX, newY);
266 mCaller.sendMessage(msg);
267 }
268
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700269 @Override
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700270 public void dispatchAppVisibility(boolean visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700271 // We don't do this in preview mode; we'll let the preview
272 // activity tell us when to run.
273 if (!mIWallpaperEngine.mIsPreview) {
274 Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED,
275 visible ? 1 : 0);
276 mCaller.sendMessage(msg);
277 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700278 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700279
280 @Override
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800281 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
282 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700283 synchronized (mLock) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700284 if (DEBUG) Log.v(TAG, "Dispatch wallpaper offsets: " + x + ", " + y);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700285 mPendingXOffset = x;
286 mPendingYOffset = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800287 mPendingXOffsetStep = xStep;
288 mPendingYOffsetStep = yStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700289 if (sync) {
290 mPendingSync = true;
291 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700292 if (!mOffsetMessageEnqueued) {
293 mOffsetMessageEnqueued = true;
294 Message msg = mCaller.obtainMessage(MSG_WALLPAPER_OFFSETS);
295 mCaller.sendMessage(msg);
296 }
297 }
298 }
Craig Mautner5702d4d2012-06-30 14:10:16 -0700299
300 @Override
Dianne Hackborn75804932009-10-20 20:15:20 -0700301 public void dispatchWallpaperCommand(String action, int x, int y,
302 int z, Bundle extras, boolean sync) {
303 synchronized (mLock) {
304 if (DEBUG) Log.v(TAG, "Dispatch wallpaper command: " + x + ", " + y);
305 WallpaperCommand cmd = new WallpaperCommand();
306 cmd.action = action;
307 cmd.x = x;
308 cmd.y = y;
309 cmd.z = z;
310 cmd.extras = extras;
311 cmd.sync = sync;
312 Message msg = mCaller.obtainMessage(MSG_WALLPAPER_COMMAND);
313 msg.obj = cmd;
314 mCaller.sendMessage(msg);
315 }
316 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700317 };
318
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700319 /**
320 * Provides access to the surface in which this wallpaper is drawn.
321 */
322 public SurfaceHolder getSurfaceHolder() {
323 return mSurfaceHolder;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700324 }
325
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700326 /**
327 * Convenience for {@link WallpaperManager#getDesiredMinimumWidth()
328 * WallpaperManager.getDesiredMinimumWidth()}, returning the width
329 * that the system would like this wallpaper to run in.
330 */
331 public int getDesiredMinimumWidth() {
332 return mIWallpaperEngine.mReqWidth;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700333 }
334
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700335 /**
336 * Convenience for {@link WallpaperManager#getDesiredMinimumHeight()
337 * WallpaperManager.getDesiredMinimumHeight()}, returning the height
338 * that the system would like this wallpaper to run in.
339 */
340 public int getDesiredMinimumHeight() {
341 return mIWallpaperEngine.mReqHeight;
342 }
343
344 /**
Dianne Hackborn284ac932009-08-28 10:34:25 -0700345 * Return whether the wallpaper is currently visible to the user,
346 * this is the last value supplied to
347 * {@link #onVisibilityChanged(boolean)}.
348 */
349 public boolean isVisible() {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700350 return mReportedVisible;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700351 }
352
353 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700354 * Returns true if this engine is running in preview mode -- that is,
355 * it is being shown to the user before they select it as the actual
356 * wallpaper.
357 */
358 public boolean isPreview() {
359 return mIWallpaperEngine.mIsPreview;
360 }
361
362 /**
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700363 * Control whether this wallpaper will receive raw touch events
364 * from the window manager as the user interacts with the window
365 * that is currently displaying the wallpaper. By default they
366 * are turned off. If enabled, the events will be received in
367 * {@link #onTouchEvent(MotionEvent)}.
368 */
369 public void setTouchEventsEnabled(boolean enabled) {
370 mWindowFlags = enabled
371 ? (mWindowFlags&~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
372 : (mWindowFlags|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
373 if (mCreated) {
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700374 updateSurface(false, false, false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700375 }
376 }
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700377
378 /**
379 * Control whether this wallpaper will receive notifications when the wallpaper
380 * has been scrolled. By default, wallpapers will receive notifications, although
381 * the default static image wallpapers do not. It is a performance optimization to
382 * set this to false.
383 *
384 * @param enabled whether the wallpaper wants to receive offset notifications
385 */
386 public void setOffsetNotificationsEnabled(boolean enabled) {
387 mWindowPrivateFlags = enabled
388 ? (mWindowPrivateFlags |
389 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS)
390 : (mWindowPrivateFlags &
391 ~WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS);
392 if (mCreated) {
393 updateSurface(false, false, false);
394 }
395 }
Jeff Sharkey35be7562012-04-18 19:16:15 -0700396
397 /** {@hide} */
398 public void setFixedSizeAllowed(boolean allowed) {
399 mFixedSizeAllowed = allowed;
400 }
401
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700402 /**
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700403 * Called once to initialize the engine. After returning, the
404 * engine's surface will be created by the framework.
405 */
406 public void onCreate(SurfaceHolder surfaceHolder) {
407 }
408
409 /**
410 * Called right before the engine is going away. After this the
411 * surface will be destroyed and this Engine object is no longer
412 * valid.
413 */
414 public void onDestroy() {
415 }
416
417 /**
418 * Called to inform you of the wallpaper becoming visible or
419 * hidden. <em>It is very important that a wallpaper only use
420 * CPU while it is visible.</em>.
421 */
422 public void onVisibilityChanged(boolean visible) {
423 }
424
425 /**
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700426 * Called as the user performs touch-screen interaction with the
427 * window that is currently showing this wallpaper. Note that the
428 * events you receive here are driven by the actual application the
Marco Nelissenae87bd02009-09-17 09:44:43 -0700429 * user is interacting with, so if it is slow you will get fewer
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700430 * move events.
431 */
432 public void onTouchEvent(MotionEvent event) {
433 }
434
435 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700436 * Called to inform you of the wallpaper's offsets changing
437 * within its contain, corresponding to the container's
438 * call to {@link WallpaperManager#setWallpaperOffsets(IBinder, float, float)
439 * WallpaperManager.setWallpaperOffsets()}.
440 */
441 public void onOffsetsChanged(float xOffset, float yOffset,
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800442 float xOffsetStep, float yOffsetStep,
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700443 int xPixelOffset, int yPixelOffset) {
444 }
445
446 /**
Dianne Hackborn75804932009-10-20 20:15:20 -0700447 * Process a command that was sent to the wallpaper with
Dianne Hackborn13bf82602009-11-05 21:45:51 -0800448 * {@link WallpaperManager#sendWallpaperCommand}.
Dianne Hackborn75804932009-10-20 20:15:20 -0700449 * The default implementation does nothing, and always returns null
450 * as the result.
451 *
452 * @param action The name of the command to perform. This tells you
453 * what to do and how to interpret the rest of the arguments.
454 * @param x Generic integer parameter.
455 * @param y Generic integer parameter.
456 * @param z Generic integer parameter.
457 * @param extras Any additional parameters.
458 * @param resultRequested If true, the caller is requesting that
459 * a result, appropriate for the command, be returned back.
460 * @return If returning a result, create a Bundle and place the
461 * result data in to it. Otherwise return null.
462 */
463 public Bundle onCommand(String action, int x, int y, int z,
464 Bundle extras, boolean resultRequested) {
465 return null;
466 }
467
468 /**
Dianne Hackborn284ac932009-08-28 10:34:25 -0700469 * Called when an application has changed the desired virtual size of
470 * the wallpaper.
471 */
472 public void onDesiredSizeChanged(int desiredWidth, int desiredHeight) {
473 }
474
475 /**
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700476 * Convenience for {@link SurfaceHolder.Callback#surfaceChanged
477 * SurfaceHolder.Callback.surfaceChanged()}.
478 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700479 public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
480 }
481
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700482 /**
Dianne Hackborn1d28f9c2010-07-13 20:38:06 -0700483 * Convenience for {@link SurfaceHolder.Callback2#surfaceRedrawNeeded
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700484 * SurfaceHolder.Callback.surfaceRedrawNeeded()}.
485 */
486 public void onSurfaceRedrawNeeded(SurfaceHolder holder) {
487 }
488
489 /**
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700490 * Convenience for {@link SurfaceHolder.Callback#surfaceCreated
491 * SurfaceHolder.Callback.surfaceCreated()}.
492 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700493 public void onSurfaceCreated(SurfaceHolder holder) {
494 }
495
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700496 /**
497 * Convenience for {@link SurfaceHolder.Callback#surfaceDestroyed
498 * SurfaceHolder.Callback.surfaceDestroyed()}.
499 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700500 public void onSurfaceDestroyed(SurfaceHolder holder) {
501 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700502
Dianne Hackborn527de8e2011-08-22 16:10:36 -0700503 protected void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) {
504 out.print(prefix); out.print("mInitializing="); out.print(mInitializing);
505 out.print(" mDestroyed="); out.println(mDestroyed);
506 out.print(prefix); out.print("mVisible="); out.print(mVisible);
507 out.print(" mScreenOn="); out.print(mScreenOn);
508 out.print(" mReportedVisible="); out.println(mReportedVisible);
509 out.print(prefix); out.print("mCreated="); out.print(mCreated);
510 out.print(" mSurfaceCreated="); out.print(mSurfaceCreated);
511 out.print(" mIsCreating="); out.print(mIsCreating);
512 out.print(" mDrawingAllowed="); out.println(mDrawingAllowed);
513 out.print(prefix); out.print("mWidth="); out.print(mWidth);
514 out.print(" mCurWidth="); out.print(mCurWidth);
515 out.print(" mHeight="); out.print(mHeight);
516 out.print(" mCurHeight="); out.println(mCurHeight);
517 out.print(prefix); out.print("mType="); out.print(mType);
518 out.print(" mWindowFlags="); out.print(mWindowFlags);
519 out.print(" mCurWindowFlags="); out.println(mCurWindowFlags);
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700520 out.print(" mWindowPrivateFlags="); out.print(mWindowPrivateFlags);
521 out.print(" mCurWindowPrivateFlags="); out.println(mCurWindowPrivateFlags);
Dianne Hackborn527de8e2011-08-22 16:10:36 -0700522 out.print(prefix); out.print("mVisibleInsets=");
523 out.print(mVisibleInsets.toShortString());
524 out.print(" mWinFrame="); out.print(mWinFrame.toShortString());
525 out.print(" mContentInsets="); out.println(mContentInsets.toShortString());
526 out.print(prefix); out.print("mConfiguration="); out.println(mConfiguration);
527 out.print(prefix); out.print("mLayout="); out.println(mLayout);
528 synchronized (mLock) {
529 out.print(prefix); out.print("mPendingXOffset="); out.print(mPendingXOffset);
530 out.print(" mPendingXOffset="); out.println(mPendingXOffset);
531 out.print(prefix); out.print("mPendingXOffsetStep=");
532 out.print(mPendingXOffsetStep);
533 out.print(" mPendingXOffsetStep="); out.println(mPendingXOffsetStep);
534 out.print(prefix); out.print("mOffsetMessageEnqueued=");
535 out.print(mOffsetMessageEnqueued);
536 out.print(" mPendingSync="); out.println(mPendingSync);
537 if (mPendingMove != null) {
538 out.print(prefix); out.print("mPendingMove="); out.println(mPendingMove);
539 }
540 }
541 }
542
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700543 private void dispatchPointer(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -0800544 if (event.isTouchEvent()) {
545 synchronized (mLock) {
546 if (event.getAction() == MotionEvent.ACTION_MOVE) {
547 mPendingMove = event;
548 } else {
549 mPendingMove = null;
550 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700551 }
Jeff Brown33bbfd22011-02-24 20:55:35 -0800552 Message msg = mCaller.obtainMessageO(MSG_TOUCH_EVENT, event);
553 mCaller.sendMessage(msg);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800554 } else {
555 event.recycle();
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700556 }
557 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700558
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700559 void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700560 if (mDestroyed) {
561 Log.w(TAG, "Ignoring updateSurface: destroyed");
562 }
563
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700564 int myWidth = mSurfaceHolder.getRequestedWidth();
Romain Guy980a9382010-01-08 15:06:28 -0800565 if (myWidth <= 0) myWidth = ViewGroup.LayoutParams.MATCH_PARENT;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700566 int myHeight = mSurfaceHolder.getRequestedHeight();
Romain Guy980a9382010-01-08 15:06:28 -0800567 if (myHeight <= 0) myHeight = ViewGroup.LayoutParams.MATCH_PARENT;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700568
569 final boolean creating = !mCreated;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700570 final boolean surfaceCreating = !mSurfaceCreated;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700571 final boolean formatChanged = mFormat != mSurfaceHolder.getRequestedFormat();
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700572 boolean sizeChanged = mWidth != myWidth || mHeight != myHeight;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700573 final boolean typeChanged = mType != mSurfaceHolder.getRequestedType();
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700574 final boolean flagsChanged = mCurWindowFlags != mWindowFlags ||
575 mCurWindowPrivateFlags != mWindowPrivateFlags;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700576 if (forceRelayout || creating || surfaceCreating || formatChanged || sizeChanged
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700577 || typeChanged || flagsChanged || redrawNeeded) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700578
Dianne Hackborn284ac932009-08-28 10:34:25 -0700579 if (DEBUG) Log.v(TAG, "Changes: creating=" + creating
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700580 + " format=" + formatChanged + " size=" + sizeChanged);
581
582 try {
583 mWidth = myWidth;
584 mHeight = myHeight;
585 mFormat = mSurfaceHolder.getRequestedFormat();
586 mType = mSurfaceHolder.getRequestedType();
587
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700588 mLayout.x = 0;
589 mLayout.y = 0;
590 mLayout.width = myWidth;
591 mLayout.height = myHeight;
592
593 mLayout.format = mFormat;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700594
595 mCurWindowFlags = mWindowFlags;
596 mLayout.flags = mWindowFlags
597 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
598 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
599 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
600 ;
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700601 mCurWindowPrivateFlags = mWindowPrivateFlags;
602 mLayout.privateFlags = mWindowPrivateFlags;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700603
604 mLayout.memoryType = mType;
605 mLayout.token = mWindowToken;
606
607 if (!mCreated) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700608 mLayout.type = mIWallpaperEngine.mWindowType;
Fabrice Di Meglioaac0d4e2012-07-19 19:21:26 -0700609 mLayout.gravity = Gravity.START|Gravity.TOP;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -0700610 mLayout.setTitle(WallpaperService.this.getClass().getName());
Dianne Hackborn284ac932009-08-28 10:34:25 -0700611 mLayout.windowAnimations =
612 com.android.internal.R.style.Animation_Wallpaper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700613 mInputChannel = new InputChannel();
Craig Mautner6881a102012-07-27 13:04:51 -0700614 if (mSession.addToDisplay(mWindow, mWindow.mSeq, mLayout, View.VISIBLE,
615 Display.DEFAULT_DISPLAY, mContentInsets, mInputChannel) < 0) {
Mattias Peterssond9463f52011-01-12 15:38:55 +0100616 Log.w(TAG, "Failed to add window while updating wallpaper surface.");
617 return;
618 }
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700619 mCreated = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700620
Jeff Brown32cbc38552011-12-01 14:01:49 -0800621 mInputEventReceiver = new WallpaperInputEventReceiver(
622 mInputChannel, Looper.myLooper());
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700623 }
624
625 mSurfaceHolder.mSurfaceLock.lock();
626 mDrawingAllowed = true;
627
628 final int relayoutResult = mSession.relayout(
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700629 mWindow, mWindow.mSeq, mLayout, mWidth, mHeight,
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700630 View.VISIBLE, 0, mWinFrame, mContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700631 mVisibleInsets, mConfiguration, mSurfaceHolder.mSurface);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700632
Dianne Hackborn284ac932009-08-28 10:34:25 -0700633 if (DEBUG) Log.v(TAG, "New surface: " + mSurfaceHolder.mSurface
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700634 + ", frame=" + mWinFrame);
635
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700636 int w = mWinFrame.width();
637 if (mCurWidth != w) {
638 sizeChanged = true;
639 mCurWidth = w;
640 }
641 int h = mWinFrame.height();
642 if (mCurHeight != h) {
643 sizeChanged = true;
644 mCurHeight = h;
645 }
Jeff Brown30bc34f2011-01-25 12:56:56 -0800646
647 mSurfaceHolder.setSurfaceFrameSize(w, h);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700648 mSurfaceHolder.mSurfaceLock.unlock();
649
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700650 if (!mSurfaceHolder.mSurface.isValid()) {
651 reportSurfaceDestroyed();
652 if (DEBUG) Log.v(TAG, "Layout: Surface destroyed");
653 return;
654 }
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700655
656 boolean didSurface = false;
657
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700658 try {
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700659 mSurfaceHolder.ungetCallbacks();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700660
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700661 if (surfaceCreating) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700662 mIsCreating = true;
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700663 didSurface = true;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700664 if (DEBUG) Log.v(TAG, "onSurfaceCreated("
665 + mSurfaceHolder + "): " + this);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700666 onSurfaceCreated(mSurfaceHolder);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700667 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700668 if (callbacks != null) {
669 for (SurfaceHolder.Callback c : callbacks) {
670 c.surfaceCreated(mSurfaceHolder);
671 }
672 }
673 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700674
Jeff Brown98365d72012-08-19 20:30:52 -0700675 redrawNeeded |= creating || (relayoutResult
676 & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700677
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700678 if (forceReport || creating || surfaceCreating
679 || formatChanged || sizeChanged) {
Dianne Hackborncbf15042009-08-18 18:29:09 -0700680 if (DEBUG) {
681 RuntimeException e = new RuntimeException();
682 e.fillInStackTrace();
683 Log.w(TAG, "forceReport=" + forceReport + " creating=" + creating
684 + " formatChanged=" + formatChanged
685 + " sizeChanged=" + sizeChanged, e);
686 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700687 if (DEBUG) Log.v(TAG, "onSurfaceChanged("
688 + mSurfaceHolder + ", " + mFormat
689 + ", " + mCurWidth + ", " + mCurHeight
690 + "): " + this);
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700691 didSurface = true;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700692 onSurfaceChanged(mSurfaceHolder, mFormat,
693 mCurWidth, mCurHeight);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700694 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700695 if (callbacks != null) {
696 for (SurfaceHolder.Callback c : callbacks) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700697 c.surfaceChanged(mSurfaceHolder, mFormat,
698 mCurWidth, mCurHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700699 }
700 }
701 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700702
703 if (redrawNeeded) {
704 onSurfaceRedrawNeeded(mSurfaceHolder);
705 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
706 if (callbacks != null) {
707 for (SurfaceHolder.Callback c : callbacks) {
708 if (c instanceof SurfaceHolder.Callback2) {
709 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
710 mSurfaceHolder);
711 }
712 }
713 }
714 }
715
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700716 if (didSurface && !mReportedVisible) {
717 // This wallpaper is currently invisible, but its
718 // surface has changed. At this point let's tell it
719 // again that it is invisible in case the report about
720 // the surface caused it to start running. We really
721 // don't want wallpapers running when not visible.
722 if (mIsCreating) {
723 // Some wallpapers will ignore this call if they
724 // had previously been told they were invisble,
725 // so if we are creating a new surface then toggle
726 // the state to get them to notice.
727 if (DEBUG) Log.v(TAG, "onVisibilityChanged(true) at surface: "
728 + this);
729 onVisibilityChanged(true);
730 }
731 if (DEBUG) Log.v(TAG, "onVisibilityChanged(false) at surface: "
732 + this);
733 onVisibilityChanged(false);
734 }
735
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700736 } finally {
737 mIsCreating = false;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700738 mSurfaceCreated = true;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700739 if (redrawNeeded) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700740 mSession.finishDrawing(mWindow);
741 }
742 }
743 } catch (RemoteException ex) {
744 }
745 if (DEBUG) Log.v(
746 TAG, "Layout: x=" + mLayout.x + " y=" + mLayout.y +
747 " w=" + mLayout.width + " h=" + mLayout.height);
748 }
749 }
750
751 void attach(IWallpaperEngineWrapper wrapper) {
Dianne Hackborncbf15042009-08-18 18:29:09 -0700752 if (DEBUG) Log.v(TAG, "attach: " + this + " wrapper=" + wrapper);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700753 if (mDestroyed) {
754 return;
755 }
756
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700757 mIWallpaperEngine = wrapper;
758 mCaller = wrapper.mCaller;
759 mConnection = wrapper.mConnection;
760 mWindowToken = wrapper.mWindowToken;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700761 mSurfaceHolder.setSizeFromLayout();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700762 mInitializing = true;
Jeff Brown98365d72012-08-19 20:30:52 -0700763 mSession = WindowManagerGlobal.getWindowSession(getMainLooper());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700764
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700765 mWindow.setSession(mSession);
Dianne Hackborn9fe6cb52011-09-09 13:02:43 -0700766
767 mScreenOn = ((PowerManager)getSystemService(Context.POWER_SERVICE)).isScreenOn();
768
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700769 IntentFilter filter = new IntentFilter();
770 filter.addAction(Intent.ACTION_SCREEN_ON);
771 filter.addAction(Intent.ACTION_SCREEN_OFF);
772 registerReceiver(mReceiver, filter);
773
Dianne Hackborn284ac932009-08-28 10:34:25 -0700774 if (DEBUG) Log.v(TAG, "onCreate(): " + this);
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700775 onCreate(mSurfaceHolder);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700776
777 mInitializing = false;
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700778 mReportedVisible = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700779 updateSurface(false, false, false);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700780 }
781
Dianne Hackborn284ac932009-08-28 10:34:25 -0700782 void doDesiredSizeChanged(int desiredWidth, int desiredHeight) {
783 if (!mDestroyed) {
784 if (DEBUG) Log.v(TAG, "onDesiredSizeChanged("
785 + desiredWidth + "," + desiredHeight + "): " + this);
Joe Onoratodcfae5c2010-10-28 18:03:23 -0700786 mIWallpaperEngine.mReqWidth = desiredWidth;
787 mIWallpaperEngine.mReqHeight = desiredHeight;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700788 onDesiredSizeChanged(desiredWidth, desiredHeight);
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700789 doOffsetsChanged(true);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700790 }
791 }
792
793 void doVisibilityChanged(boolean visible) {
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -0800794 if (!mDestroyed) {
795 mVisible = visible;
796 reportVisibility();
797 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700798 }
799
800 void reportVisibility() {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700801 if (!mDestroyed) {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700802 boolean visible = mVisible && mScreenOn;
803 if (mReportedVisible != visible) {
804 mReportedVisible = visible;
805 if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible
806 + "): " + this);
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700807 if (visible) {
808 // If becoming visible, in preview mode the surface
809 // may have been destroyed so now we need to make
810 // sure it is re-created.
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700811 doOffsetsChanged(false);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700812 updateSurface(false, false, false);
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700813 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700814 onVisibilityChanged(visible);
815 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700816 }
817 }
818
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700819 void doOffsetsChanged(boolean always) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700820 if (mDestroyed) {
821 return;
822 }
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700823
824 if (!always && !mOffsetsChanged) {
825 return;
826 }
827
Dianne Hackborn284ac932009-08-28 10:34:25 -0700828 float xOffset;
829 float yOffset;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800830 float xOffsetStep;
831 float yOffsetStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700832 boolean sync;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700833 synchronized (mLock) {
834 xOffset = mPendingXOffset;
835 yOffset = mPendingYOffset;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800836 xOffsetStep = mPendingXOffsetStep;
837 yOffsetStep = mPendingYOffsetStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700838 sync = mPendingSync;
839 mPendingSync = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700840 mOffsetMessageEnqueued = false;
841 }
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700842
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700843 if (mSurfaceCreated) {
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700844 if (mReportedVisible) {
845 if (DEBUG) Log.v(TAG, "Offsets change in " + this
846 + ": " + xOffset + "," + yOffset);
847 final int availw = mIWallpaperEngine.mReqWidth-mCurWidth;
848 final int xPixels = availw > 0 ? -(int)(availw*xOffset+.5f) : 0;
849 final int availh = mIWallpaperEngine.mReqHeight-mCurHeight;
850 final int yPixels = availh > 0 ? -(int)(availh*yOffset+.5f) : 0;
851 onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixels, yPixels);
852 } else {
853 mOffsetsChanged = true;
854 }
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700855 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700856
857 if (sync) {
858 try {
859 if (DEBUG) Log.v(TAG, "Reporting offsets change complete");
860 mSession.wallpaperOffsetsComplete(mWindow.asBinder());
861 } catch (RemoteException e) {
862 }
863 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700864 }
865
Dianne Hackborn75804932009-10-20 20:15:20 -0700866 void doCommand(WallpaperCommand cmd) {
867 Bundle result;
868 if (!mDestroyed) {
869 result = onCommand(cmd.action, cmd.x, cmd.y, cmd.z,
870 cmd.extras, cmd.sync);
871 } else {
872 result = null;
873 }
874 if (cmd.sync) {
875 try {
876 if (DEBUG) Log.v(TAG, "Reporting command complete");
877 mSession.wallpaperCommandComplete(mWindow.asBinder(), result);
878 } catch (RemoteException e) {
879 }
880 }
881 }
882
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700883 void reportSurfaceDestroyed() {
884 if (mSurfaceCreated) {
885 mSurfaceCreated = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700886 mSurfaceHolder.ungetCallbacks();
887 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
888 if (callbacks != null) {
889 for (SurfaceHolder.Callback c : callbacks) {
890 c.surfaceDestroyed(mSurfaceHolder);
891 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700892 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700893 if (DEBUG) Log.v(TAG, "onSurfaceDestroyed("
894 + mSurfaceHolder + "): " + this);
895 onSurfaceDestroyed(mSurfaceHolder);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700896 }
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700897 }
898
899 void detach() {
900 if (mDestroyed) {
901 return;
902 }
903
904 mDestroyed = true;
905
906 if (mVisible) {
907 mVisible = false;
908 if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this);
909 onVisibilityChanged(false);
910 }
911
912 reportSurfaceDestroyed();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700913
914 if (DEBUG) Log.v(TAG, "onDestroy(): " + this);
915 onDestroy();
916
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700917 unregisterReceiver(mReceiver);
918
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700919 if (mCreated) {
920 try {
Dianne Hackbornba3e31d2010-04-22 18:59:03 -0700921 if (DEBUG) Log.v(TAG, "Removing window and destroying surface "
922 + mSurfaceHolder.getSurface() + " of: " + this);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700923
Jeff Brown32cbc38552011-12-01 14:01:49 -0800924 if (mInputEventReceiver != null) {
925 mInputEventReceiver.dispose();
926 mInputEventReceiver = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700927 }
928
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700929 mSession.remove(mWindow);
930 } catch (RemoteException e) {
931 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -0700932 mSurfaceHolder.mSurface.release();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700933 mCreated = false;
Jeff Brown349703e2010-06-22 01:27:15 -0700934
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700935 // Dispose the input channel after removing the window so the Window Manager
936 // doesn't interpret the input channel being closed as an abnormal termination.
937 if (mInputChannel != null) {
938 mInputChannel.dispose();
939 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -0700940 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700941 }
942 }
943 }
944
945 class IWallpaperEngineWrapper extends IWallpaperEngine.Stub
946 implements HandlerCaller.Callback {
947 private final HandlerCaller mCaller;
948
949 final IWallpaperConnection mConnection;
950 final IBinder mWindowToken;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700951 final int mWindowType;
952 final boolean mIsPreview;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700953 int mReqWidth;
954 int mReqHeight;
955
956 Engine mEngine;
957
958 IWallpaperEngineWrapper(WallpaperService context,
959 IWallpaperConnection conn, IBinder windowToken,
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700960 int windowType, boolean isPreview, int reqWidth, int reqHeight) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700961 if (DEBUG && mCallbackLooper != null) {
962 mCallbackLooper.setMessageLogging(new LogPrinter(Log.VERBOSE, TAG));
963 }
964 mCaller = new HandlerCaller(context,
965 mCallbackLooper != null
966 ? mCallbackLooper : context.getMainLooper(),
967 this);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700968 mConnection = conn;
969 mWindowToken = windowToken;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700970 mWindowType = windowType;
971 mIsPreview = isPreview;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700972 mReqWidth = reqWidth;
973 mReqHeight = reqHeight;
974
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700975 Message msg = mCaller.obtainMessage(DO_ATTACH);
976 mCaller.sendMessage(msg);
977 }
978
Dianne Hackborn284ac932009-08-28 10:34:25 -0700979 public void setDesiredSize(int width, int height) {
980 Message msg = mCaller.obtainMessageII(DO_SET_DESIRED_SIZE, width, height);
981 mCaller.sendMessage(msg);
982 }
983
984 public void setVisibility(boolean visible) {
985 Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED,
986 visible ? 1 : 0);
987 mCaller.sendMessage(msg);
988 }
989
Dianne Hackborn6adba242009-11-10 11:10:09 -0800990 public void dispatchPointer(MotionEvent event) {
991 if (mEngine != null) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700992 mEngine.dispatchPointer(event);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800993 } else {
994 event.recycle();
Dianne Hackborn6adba242009-11-10 11:10:09 -0800995 }
996 }
Jeff Brown9f3bdfe2010-10-13 06:01:27 -0700997
998 public void dispatchWallpaperCommand(String action, int x, int y,
999 int z, Bundle extras) {
1000 if (mEngine != null) {
1001 mEngine.mWindow.dispatchWallpaperCommand(action, x, y, z, extras, false);
1002 }
1003 }
1004
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001005 public void destroy() {
1006 Message msg = mCaller.obtainMessage(DO_DETACH);
1007 mCaller.sendMessage(msg);
1008 }
1009
1010 public void executeMessage(Message message) {
1011 switch (message.what) {
1012 case DO_ATTACH: {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001013 try {
1014 mConnection.attachEngine(this);
1015 } catch (RemoteException e) {
1016 Log.w(TAG, "Wallpaper host disappeared", e);
1017 return;
1018 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001019 Engine engine = onCreateEngine();
1020 mEngine = engine;
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -08001021 mActiveEngines.add(engine);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001022 engine.attach(this);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001023 try {
1024 mConnection.engineShown(this);
1025 } catch (RemoteException e) {
1026 Log.w(TAG, "Wallpaper host disappeared", e);
1027 return;
1028 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001029 return;
1030 }
1031 case DO_DETACH: {
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -08001032 mActiveEngines.remove(mEngine);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001033 mEngine.detach();
1034 return;
1035 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001036 case DO_SET_DESIRED_SIZE: {
1037 mEngine.doDesiredSizeChanged(message.arg1, message.arg2);
1038 return;
1039 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001040 case MSG_UPDATE_SURFACE:
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001041 mEngine.updateSurface(true, false, false);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001042 break;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001043 case MSG_VISIBILITY_CHANGED:
1044 if (DEBUG) Log.v(TAG, "Visibility change in " + mEngine
1045 + ": " + message.arg1);
Dianne Hackborn284ac932009-08-28 10:34:25 -07001046 mEngine.doVisibilityChanged(message.arg1 != 0);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001047 break;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001048 case MSG_WALLPAPER_OFFSETS: {
Dianne Hackborn9e4e7272011-08-30 14:06:51 -07001049 mEngine.doOffsetsChanged(true);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001050 } break;
Dianne Hackborn75804932009-10-20 20:15:20 -07001051 case MSG_WALLPAPER_COMMAND: {
1052 WallpaperCommand cmd = (WallpaperCommand)message.obj;
1053 mEngine.doCommand(cmd);
1054 } break;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001055 case MSG_WINDOW_RESIZED: {
1056 final boolean reportDraw = message.arg1 != 0;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001057 mEngine.updateSurface(true, false, reportDraw);
Dianne Hackborn9e4e7272011-08-30 14:06:51 -07001058 mEngine.doOffsetsChanged(true);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001059 } break;
Craig Mautner5702d4d2012-06-30 14:10:16 -07001060 case MSG_WINDOW_MOVED: {
1061 // Do nothing. What does it mean for a Wallpaper to move?
1062 } break;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001063 case MSG_TOUCH_EVENT: {
Jeff Brown840db1f2010-10-21 17:36:54 -07001064 boolean skip = false;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001065 MotionEvent ev = (MotionEvent)message.obj;
Jeff Brown840db1f2010-10-21 17:36:54 -07001066 if (ev.getAction() == MotionEvent.ACTION_MOVE) {
1067 synchronized (mEngine.mLock) {
1068 if (mEngine.mPendingMove == ev) {
1069 mEngine.mPendingMove = null;
1070 } else {
1071 // this is not the motion event we are looking for....
1072 skip = true;
1073 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001074 }
1075 }
Jeff Brown840db1f2010-10-21 17:36:54 -07001076 if (!skip) {
1077 if (DEBUG) Log.v(TAG, "Delivering touch event: " + ev);
1078 mEngine.onTouchEvent(ev);
1079 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001080 ev.recycle();
1081 } break;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001082 default :
1083 Log.w(TAG, "Unknown message type " + message.what);
1084 }
1085 }
1086 }
1087
1088 /**
1089 * Implements the internal {@link IWallpaperService} interface to convert
1090 * incoming calls to it back to calls on an {@link WallpaperService}.
1091 */
1092 class IWallpaperServiceWrapper extends IWallpaperService.Stub {
1093 private final WallpaperService mTarget;
1094
1095 public IWallpaperServiceWrapper(WallpaperService context) {
1096 mTarget = context;
1097 }
1098
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001099 public void attach(IWallpaperConnection conn, IBinder windowToken,
1100 int windowType, boolean isPreview, int reqWidth, int reqHeight) {
1101 new IWallpaperEngineWrapper(mTarget, conn, windowToken,
1102 windowType, isPreview, reqWidth, reqHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001103 }
1104 }
1105
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -08001106 @Override
1107 public void onCreate() {
1108 super.onCreate();
1109 }
1110
1111 @Override
1112 public void onDestroy() {
1113 super.onDestroy();
1114 for (int i=0; i<mActiveEngines.size(); i++) {
1115 mActiveEngines.get(i).detach();
1116 }
1117 mActiveEngines.clear();
1118 }
1119
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001120 /**
1121 * Implement to return the implementation of the internal accessibility
1122 * service interface. Subclasses should not override.
1123 */
1124 @Override
1125 public final IBinder onBind(Intent intent) {
1126 return new IWallpaperServiceWrapper(this);
1127 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001128
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001129 /**
1130 * This allows subclasses to change the thread that most callbacks
1131 * occur on. Currently hidden because it is mostly needed for the
1132 * image wallpaper (which runs in the system process and doesn't want
1133 * to get stuck running on that seriously in use main thread). Not
1134 * exposed right now because the semantics of this are not totally
1135 * well defined and some callbacks can still happen on the main thread).
1136 * @hide
1137 */
1138 public void setCallbackLooper(Looper looper) {
1139 mCallbackLooper = looper;
1140 }
1141
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001142 /**
1143 * Must be implemented to return a new instance of the wallpaper's engine.
1144 * Note that multiple instances may be active at the same time, such as
1145 * when the wallpaper is currently set as the active wallpaper and the user
1146 * is in the wallpaper picker viewing a preview of it as well.
1147 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001148 public abstract Engine onCreateEngine();
Dianne Hackborn527de8e2011-08-22 16:10:36 -07001149
1150 @Override
1151 protected void dump(FileDescriptor fd, PrintWriter out, String[] args) {
1152 out.print("State of wallpaper "); out.print(this); out.println(":");
1153 for (int i=0; i<mActiveEngines.size(); i++) {
1154 Engine engine = mActiveEngines.get(i);
1155 out.print(" Engine "); out.print(engine); out.println(":");
1156 engine.dump(" ", fd, out, args);
1157 }
1158 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001159}