blob: d146e5e8ce27bce92297a7f65304ce7c611e968b [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
Dianne Hackborn067e5f62014-09-07 23:14:30 -070019import android.content.res.TypedArray;
Filip Gruszczynski4544b922015-04-16 13:18:58 -070020import android.graphics.Canvas;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070021import android.view.WindowInsets;
Jeff Brown3d110b22014-11-21 19:01:13 -080022
Dianne Hackborn067e5f62014-09-07 23:14:30 -070023import com.android.internal.R;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070024import com.android.internal.os.HandlerCaller;
Griff Hazena0938022015-03-13 10:01:41 -070025import com.android.internal.util.ScreenShapeHelper;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070026import com.android.internal.view.BaseIWindow;
27import com.android.internal.view.BaseSurfaceHolder;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070028
Dianne Hackbornd6847842010-01-12 18:14:19 -080029import android.annotation.SdkConstant;
30import android.annotation.SdkConstant.SdkConstantType;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070031import android.app.Service;
Dianne Hackborn759a39e2009-08-09 17:20:27 -070032import android.app.WallpaperManager;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070033import android.content.Context;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070034import android.content.Intent;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080035import android.content.res.Configuration;
Mathias Agopian62bf4a02010-09-08 16:32:27 -070036import android.graphics.PixelFormat;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070037import android.graphics.Rect;
Jeff Brown3d110b22014-11-21 19:01:13 -080038import android.hardware.display.DisplayManager;
39import android.hardware.display.DisplayManager.DisplayListener;
Dianne Hackborn75804932009-10-20 20:15:20 -070040import android.os.Bundle;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070041import android.os.IBinder;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070042import android.os.Looper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070043import android.os.Message;
44import android.os.RemoteException;
45import android.util.Log;
Craig Mautner6881a102012-07-27 13:04:51 -070046import android.view.Display;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070047import android.view.Gravity;
48import android.view.IWindowSession;
Jeff Brown46b9ac02010-04-22 18:58:52 -070049import android.view.InputChannel;
Jeff Brownc5ed5912010-07-14 18:48:53 -070050import android.view.InputDevice;
Jeff Brown4952dfd2011-11-30 19:23:22 -080051import android.view.InputEvent;
Jeff Brown32cbc38552011-12-01 14:01:49 -080052import android.view.InputEventReceiver;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -070053import android.view.MotionEvent;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070054import android.view.SurfaceHolder;
55import android.view.View;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070056import android.view.ViewGroup;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070057import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070058import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070059
Dianne Hackborn527de8e2011-08-22 16:10:36 -070060import java.io.FileDescriptor;
61import java.io.PrintWriter;
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -080062import java.util.ArrayList;
63
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070064/**
65 * A wallpaper service is responsible for showing a live wallpaper behind
Dianne Hackborn23ef7b42009-11-18 18:20:39 -080066 * applications that would like to sit on top of it. This service object
67 * itself does very little -- its only purpose is to generate instances of
Dianne Hackborne4260f42009-11-18 21:15:59 -080068 * {@link Engine} as needed. Implementing a wallpaper thus
Dianne Hackborn23ef7b42009-11-18 18:20:39 -080069 * involves subclassing from this, subclassing an Engine implementation,
70 * and implementing {@link #onCreateEngine()} to return a new instance of
71 * your engine.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070072 */
73public abstract class WallpaperService extends Service {
74 /**
75 * The {@link Intent} that must be declared as handled by the service.
Dianne Hackbornd6847842010-01-12 18:14:19 -080076 * To be supported, the service must also require the
77 * {@link android.Manifest.permission#BIND_WALLPAPER} permission so
78 * that other applications can not abuse it.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070079 */
Dianne Hackbornd6847842010-01-12 18:14:19 -080080 @SdkConstant(SdkConstantType.SERVICE_ACTION)
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070081 public static final String SERVICE_INTERFACE =
Dianne Hackbornd6847842010-01-12 18:14:19 -080082 "android.service.wallpaper.WallpaperService";
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070083
Dianne Hackborneb034652009-09-07 00:49:58 -070084 /**
85 * Name under which a WallpaperService component publishes information
86 * about itself. This meta-data must reference an XML resource containing
87 * a <code>&lt;{@link android.R.styleable#Wallpaper wallpaper}&gt;</code>
88 * tag.
89 */
90 public static final String SERVICE_META_DATA = "android.service.wallpaper";
Craig Mautnerb1ef3692012-11-16 17:31:04 -080091
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070092 static final String TAG = "WallpaperService";
Dianne Hackborn72c82ab2009-08-11 21:13:54 -070093 static final boolean DEBUG = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070094
95 private static final int DO_ATTACH = 10;
96 private static final int DO_DETACH = 20;
Dianne Hackborn284ac932009-08-28 10:34:25 -070097 private static final int DO_SET_DESIRED_SIZE = 30;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070098 private static final int DO_SET_DISPLAY_PADDING = 40;
99
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700100 private static final int MSG_UPDATE_SURFACE = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700101 private static final int MSG_VISIBILITY_CHANGED = 10010;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700102 private static final int MSG_WALLPAPER_OFFSETS = 10020;
Dianne Hackborn75804932009-10-20 20:15:20 -0700103 private static final int MSG_WALLPAPER_COMMAND = 10025;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700104 private static final int MSG_WINDOW_RESIZED = 10030;
Craig Mautner5702d4d2012-06-30 14:10:16 -0700105 private static final int MSG_WINDOW_MOVED = 10035;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700106 private static final int MSG_TOUCH_EVENT = 10040;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700107
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -0800108 private final ArrayList<Engine> mActiveEngines
109 = new ArrayList<Engine>();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700110
Dianne Hackborn75804932009-10-20 20:15:20 -0700111 static final class WallpaperCommand {
112 String action;
113 int x;
114 int y;
115 int z;
116 Bundle extras;
117 boolean sync;
118 }
119
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700120 /**
121 * The actual implementation of a wallpaper. A wallpaper service may
122 * have multiple instances running (for example as a real wallpaper
123 * and as a preview), each of which is represented by its own Engine
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700124 * instance. You must implement {@link WallpaperService#onCreateEngine()}
125 * to return your concrete Engine implementation.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700126 */
127 public class Engine {
128 IWallpaperEngineWrapper mIWallpaperEngine;
129
130 // Copies from mIWallpaperEngine.
131 HandlerCaller mCaller;
132 IWallpaperConnection mConnection;
133 IBinder mWindowToken;
134
135 boolean mInitializing = true;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700136 boolean mVisible;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700137 boolean mReportedVisible;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700138 boolean mDestroyed;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700139
140 // Current window state.
141 boolean mCreated;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700142 boolean mSurfaceCreated;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700143 boolean mIsCreating;
144 boolean mDrawingAllowed;
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700145 boolean mOffsetsChanged;
Jeff Sharkey35be7562012-04-18 19:16:15 -0700146 boolean mFixedSizeAllowed;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700147 int mWidth;
148 int mHeight;
149 int mFormat;
150 int mType;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700151 int mCurWidth;
152 int mCurHeight;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700153 int mWindowFlags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700154 int mWindowPrivateFlags =
155 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700156 int mCurWindowFlags = mWindowFlags;
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700157 int mCurWindowPrivateFlags = mWindowPrivateFlags;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700158 final Rect mVisibleInsets = new Rect();
159 final Rect mWinFrame = new Rect();
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800160 final Rect mOverscanInsets = new Rect();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700161 final Rect mContentInsets = new Rect();
Adrian Roosfa104232014-06-20 16:10:14 -0700162 final Rect mStableInsets = new Rect();
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700163 final Rect mOutsets = new Rect();
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700164 final Rect mDispatchedOverscanInsets = new Rect();
165 final Rect mDispatchedContentInsets = new Rect();
166 final Rect mDispatchedStableInsets = new Rect();
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700167 final Rect mDispatchedOutsets = new Rect();
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700168 final Rect mFinalSystemInsets = new Rect();
169 final Rect mFinalStableInsets = new Rect();
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700170 final Configuration mConfiguration = new Configuration();
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700171
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700172 final WindowManager.LayoutParams mLayout
173 = new WindowManager.LayoutParams();
174 IWindowSession mSession;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700175 InputChannel mInputChannel;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700176
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700177 final Object mLock = new Object();
178 boolean mOffsetMessageEnqueued;
179 float mPendingXOffset;
180 float mPendingYOffset;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800181 float mPendingXOffsetStep;
182 float mPendingYOffsetStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700183 boolean mPendingSync;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700184 MotionEvent mPendingMove;
Jeff Brown3d110b22014-11-21 19:01:13 -0800185
186 DisplayManager mDisplayManager;
187 Display mDisplay;
Filip Gruszczynski4544b922015-04-16 13:18:58 -0700188 private int mDisplayState;
Jeff Brown3d110b22014-11-21 19:01:13 -0800189
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700190 final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() {
Mathias Agopian62bf4a02010-09-08 16:32:27 -0700191 {
Jeff Brown24572372011-06-09 19:05:15 -0700192 mRequestedFormat = PixelFormat.RGBX_8888;
Mathias Agopian62bf4a02010-09-08 16:32:27 -0700193 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700194
195 @Override
196 public boolean onAllowLockCanvas() {
197 return mDrawingAllowed;
198 }
199
200 @Override
201 public void onRelayoutContainer() {
202 Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE);
203 mCaller.sendMessage(msg);
204 }
205
206 @Override
207 public void onUpdateSurface() {
208 Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE);
209 mCaller.sendMessage(msg);
210 }
211
212 public boolean isCreating() {
213 return mIsCreating;
214 }
215
Dianne Hackborna48a37f2011-02-01 16:30:38 -0800216 @Override
217 public void setFixedSize(int width, int height) {
Jeff Sharkey35be7562012-04-18 19:16:15 -0700218 if (!mFixedSizeAllowed) {
Dianne Hackborna48a37f2011-02-01 16:30:38 -0800219 // Regular apps can't do this. It can only work for
220 // certain designs of window animations, so you can't
221 // rely on it.
222 throw new UnsupportedOperationException(
223 "Wallpapers currently only support sizing from layout");
224 }
Michael Jurkab8f939f2011-02-01 20:50:30 -0800225 super.setFixedSize(width, height);
Dianne Hackborna48a37f2011-02-01 16:30:38 -0800226 }
227
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700228 public void setKeepScreenOn(boolean screenOn) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700229 throw new UnsupportedOperationException(
230 "Wallpapers do not support keep screen on");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700231 }
Filip Gruszczynski4544b922015-04-16 13:18:58 -0700232
233 @Override
234 public Canvas lockCanvas() {
235 if (mDisplayState == Display.STATE_DOZE
236 || mDisplayState == Display.STATE_DOZE_SUSPEND) {
237 try {
238 mSession.pokeDrawLock(mWindow);
239 } catch (RemoteException e) {
240 // System server died, can be ignored.
241 }
242 }
243 return super.lockCanvas();
244 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700245 };
Jeff Brown32cbc38552011-12-01 14:01:49 -0800246
247 final class WallpaperInputEventReceiver extends InputEventReceiver {
248 public WallpaperInputEventReceiver(InputChannel inputChannel, Looper looper) {
249 super(inputChannel, looper);
250 }
251
Jeff Brown46b9ac02010-04-22 18:58:52 -0700252 @Override
Jeff Brown32cbc38552011-12-01 14:01:49 -0800253 public void onInputEvent(InputEvent event) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700254 boolean handled = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700255 try {
Jeff Brown4952dfd2011-11-30 19:23:22 -0800256 if (event instanceof MotionEvent
257 && (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800258 MotionEvent dup = MotionEvent.obtainNoHistory((MotionEvent)event);
259 dispatchPointer(dup);
Jeff Brown3915bb82010-11-05 15:02:16 -0700260 handled = true;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700261 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700262 } finally {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800263 finishInputEvent(event, handled);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 }
265 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800266 }
267 WallpaperInputEventReceiver mInputEventReceiver;
268
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700269 final BaseIWindow mWindow = new BaseIWindow() {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700270 @Override
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800271 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets,
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700272 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
Jorim Jaggi253a20f2015-11-03 12:38:42 +0100273 Configuration newConfig, Rect backDropRect) {
Filip Gruszczynski03850592015-07-01 08:35:28 -0700274 Message msg = mCaller.obtainMessageIO(MSG_WINDOW_RESIZED,
275 reportDraw ? 1 : 0, outsets);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700276 mCaller.sendMessage(msg);
277 }
Craig Mautner5702d4d2012-06-30 14:10:16 -0700278
279 @Override
280 public void moved(int newX, int newY) {
281 Message msg = mCaller.obtainMessageII(MSG_WINDOW_MOVED, newX, newY);
282 mCaller.sendMessage(msg);
283 }
284
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700285 @Override
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700286 public void dispatchAppVisibility(boolean visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700287 // We don't do this in preview mode; we'll let the preview
288 // activity tell us when to run.
289 if (!mIWallpaperEngine.mIsPreview) {
290 Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED,
291 visible ? 1 : 0);
292 mCaller.sendMessage(msg);
293 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700294 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700295
296 @Override
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800297 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
298 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700299 synchronized (mLock) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700300 if (DEBUG) Log.v(TAG, "Dispatch wallpaper offsets: " + x + ", " + y);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700301 mPendingXOffset = x;
302 mPendingYOffset = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800303 mPendingXOffsetStep = xStep;
304 mPendingYOffsetStep = yStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700305 if (sync) {
306 mPendingSync = true;
307 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700308 if (!mOffsetMessageEnqueued) {
309 mOffsetMessageEnqueued = true;
310 Message msg = mCaller.obtainMessage(MSG_WALLPAPER_OFFSETS);
311 mCaller.sendMessage(msg);
312 }
313 }
314 }
Craig Mautner5702d4d2012-06-30 14:10:16 -0700315
316 @Override
Dianne Hackborn75804932009-10-20 20:15:20 -0700317 public void dispatchWallpaperCommand(String action, int x, int y,
318 int z, Bundle extras, boolean sync) {
319 synchronized (mLock) {
320 if (DEBUG) Log.v(TAG, "Dispatch wallpaper command: " + x + ", " + y);
321 WallpaperCommand cmd = new WallpaperCommand();
322 cmd.action = action;
323 cmd.x = x;
324 cmd.y = y;
325 cmd.z = z;
326 cmd.extras = extras;
327 cmd.sync = sync;
328 Message msg = mCaller.obtainMessage(MSG_WALLPAPER_COMMAND);
329 msg.obj = cmd;
330 mCaller.sendMessage(msg);
331 }
332 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700333 };
334
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700335 /**
336 * Provides access to the surface in which this wallpaper is drawn.
337 */
338 public SurfaceHolder getSurfaceHolder() {
339 return mSurfaceHolder;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700340 }
341
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700342 /**
343 * Convenience for {@link WallpaperManager#getDesiredMinimumWidth()
344 * WallpaperManager.getDesiredMinimumWidth()}, returning the width
345 * that the system would like this wallpaper to run in.
346 */
347 public int getDesiredMinimumWidth() {
348 return mIWallpaperEngine.mReqWidth;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700349 }
350
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700351 /**
352 * Convenience for {@link WallpaperManager#getDesiredMinimumHeight()
353 * WallpaperManager.getDesiredMinimumHeight()}, returning the height
354 * that the system would like this wallpaper to run in.
355 */
356 public int getDesiredMinimumHeight() {
357 return mIWallpaperEngine.mReqHeight;
358 }
359
360 /**
Dianne Hackborn284ac932009-08-28 10:34:25 -0700361 * Return whether the wallpaper is currently visible to the user,
362 * this is the last value supplied to
363 * {@link #onVisibilityChanged(boolean)}.
364 */
365 public boolean isVisible() {
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700366 return mReportedVisible;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700367 }
368
369 /**
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700370 * Returns true if this engine is running in preview mode -- that is,
371 * it is being shown to the user before they select it as the actual
372 * wallpaper.
373 */
374 public boolean isPreview() {
375 return mIWallpaperEngine.mIsPreview;
376 }
377
378 /**
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700379 * Control whether this wallpaper will receive raw touch events
380 * from the window manager as the user interacts with the window
381 * that is currently displaying the wallpaper. By default they
382 * are turned off. If enabled, the events will be received in
383 * {@link #onTouchEvent(MotionEvent)}.
384 */
385 public void setTouchEventsEnabled(boolean enabled) {
386 mWindowFlags = enabled
387 ? (mWindowFlags&~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
388 : (mWindowFlags|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
389 if (mCreated) {
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700390 updateSurface(false, false, false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700391 }
392 }
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700393
394 /**
395 * Control whether this wallpaper will receive notifications when the wallpaper
396 * has been scrolled. By default, wallpapers will receive notifications, although
397 * the default static image wallpapers do not. It is a performance optimization to
398 * set this to false.
399 *
400 * @param enabled whether the wallpaper wants to receive offset notifications
401 */
402 public void setOffsetNotificationsEnabled(boolean enabled) {
403 mWindowPrivateFlags = enabled
404 ? (mWindowPrivateFlags |
405 WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS)
406 : (mWindowPrivateFlags &
407 ~WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS);
408 if (mCreated) {
409 updateSurface(false, false, false);
410 }
411 }
Jeff Sharkey35be7562012-04-18 19:16:15 -0700412
413 /** {@hide} */
414 public void setFixedSizeAllowed(boolean allowed) {
415 mFixedSizeAllowed = allowed;
416 }
417
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700418 /**
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700419 * Called once to initialize the engine. After returning, the
420 * engine's surface will be created by the framework.
421 */
422 public void onCreate(SurfaceHolder surfaceHolder) {
423 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700424
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700425 /**
426 * Called right before the engine is going away. After this the
427 * surface will be destroyed and this Engine object is no longer
428 * valid.
429 */
430 public void onDestroy() {
431 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700432
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700433 /**
434 * Called to inform you of the wallpaper becoming visible or
435 * hidden. <em>It is very important that a wallpaper only use
436 * CPU while it is visible.</em>.
437 */
438 public void onVisibilityChanged(boolean visible) {
439 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700440
441 /**
442 * Called with the current insets that are in effect for the wallpaper.
443 * This gives you the part of the overall wallpaper surface that will
444 * generally be visible to the user (ignoring position offsets applied to it).
445 *
446 * @param insets Insets to apply.
447 */
448 public void onApplyWindowInsets(WindowInsets insets) {
449 }
450
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700451 /**
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700452 * Called as the user performs touch-screen interaction with the
453 * window that is currently showing this wallpaper. Note that the
454 * events you receive here are driven by the actual application the
Marco Nelissenae87bd02009-09-17 09:44:43 -0700455 * user is interacting with, so if it is slow you will get fewer
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700456 * move events.
457 */
458 public void onTouchEvent(MotionEvent event) {
459 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700460
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700461 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700462 * Called to inform you of the wallpaper's offsets changing
463 * within its contain, corresponding to the container's
464 * call to {@link WallpaperManager#setWallpaperOffsets(IBinder, float, float)
465 * WallpaperManager.setWallpaperOffsets()}.
466 */
467 public void onOffsetsChanged(float xOffset, float yOffset,
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800468 float xOffsetStep, float yOffsetStep,
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700469 int xPixelOffset, int yPixelOffset) {
470 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700471
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700472 /**
Dianne Hackborn75804932009-10-20 20:15:20 -0700473 * Process a command that was sent to the wallpaper with
Dianne Hackborn13bf82602009-11-05 21:45:51 -0800474 * {@link WallpaperManager#sendWallpaperCommand}.
Dianne Hackborn75804932009-10-20 20:15:20 -0700475 * The default implementation does nothing, and always returns null
476 * as the result.
477 *
478 * @param action The name of the command to perform. This tells you
479 * what to do and how to interpret the rest of the arguments.
480 * @param x Generic integer parameter.
481 * @param y Generic integer parameter.
482 * @param z Generic integer parameter.
483 * @param extras Any additional parameters.
484 * @param resultRequested If true, the caller is requesting that
485 * a result, appropriate for the command, be returned back.
486 * @return If returning a result, create a Bundle and place the
487 * result data in to it. Otherwise return null.
488 */
489 public Bundle onCommand(String action, int x, int y, int z,
490 Bundle extras, boolean resultRequested) {
491 return null;
492 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700493
Dianne Hackborn75804932009-10-20 20:15:20 -0700494 /**
Dianne Hackborn284ac932009-08-28 10:34:25 -0700495 * Called when an application has changed the desired virtual size of
496 * the wallpaper.
497 */
498 public void onDesiredSizeChanged(int desiredWidth, int desiredHeight) {
499 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700500
Dianne Hackborn284ac932009-08-28 10:34:25 -0700501 /**
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700502 * Convenience for {@link SurfaceHolder.Callback#surfaceChanged
503 * SurfaceHolder.Callback.surfaceChanged()}.
504 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700505 public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
506 }
507
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700508 /**
Dianne Hackborn1d28f9c2010-07-13 20:38:06 -0700509 * Convenience for {@link SurfaceHolder.Callback2#surfaceRedrawNeeded
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700510 * SurfaceHolder.Callback.surfaceRedrawNeeded()}.
511 */
512 public void onSurfaceRedrawNeeded(SurfaceHolder holder) {
513 }
514
515 /**
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700516 * Convenience for {@link SurfaceHolder.Callback#surfaceCreated
517 * SurfaceHolder.Callback.surfaceCreated()}.
518 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700519 public void onSurfaceCreated(SurfaceHolder holder) {
520 }
521
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700522 /**
523 * Convenience for {@link SurfaceHolder.Callback#surfaceDestroyed
524 * SurfaceHolder.Callback.surfaceDestroyed()}.
525 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700526 public void onSurfaceDestroyed(SurfaceHolder holder) {
527 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700528
Dianne Hackborn527de8e2011-08-22 16:10:36 -0700529 protected void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) {
530 out.print(prefix); out.print("mInitializing="); out.print(mInitializing);
531 out.print(" mDestroyed="); out.println(mDestroyed);
532 out.print(prefix); out.print("mVisible="); out.print(mVisible);
Dianne Hackborn527de8e2011-08-22 16:10:36 -0700533 out.print(" mReportedVisible="); out.println(mReportedVisible);
Jeff Brown3d110b22014-11-21 19:01:13 -0800534 out.print(prefix); out.print("mDisplay="); out.println(mDisplay);
Dianne Hackborn527de8e2011-08-22 16:10:36 -0700535 out.print(prefix); out.print("mCreated="); out.print(mCreated);
536 out.print(" mSurfaceCreated="); out.print(mSurfaceCreated);
537 out.print(" mIsCreating="); out.print(mIsCreating);
538 out.print(" mDrawingAllowed="); out.println(mDrawingAllowed);
539 out.print(prefix); out.print("mWidth="); out.print(mWidth);
540 out.print(" mCurWidth="); out.print(mCurWidth);
541 out.print(" mHeight="); out.print(mHeight);
542 out.print(" mCurHeight="); out.println(mCurHeight);
543 out.print(prefix); out.print("mType="); out.print(mType);
544 out.print(" mWindowFlags="); out.print(mWindowFlags);
545 out.print(" mCurWindowFlags="); out.println(mCurWindowFlags);
Dianne Hackbornd052a942014-11-21 15:23:13 -0800546 out.print(prefix); out.print("mWindowPrivateFlags="); out.print(mWindowPrivateFlags);
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700547 out.print(" mCurWindowPrivateFlags="); out.println(mCurWindowPrivateFlags);
Dianne Hackborn527de8e2011-08-22 16:10:36 -0700548 out.print(prefix); out.print("mVisibleInsets=");
549 out.print(mVisibleInsets.toShortString());
550 out.print(" mWinFrame="); out.print(mWinFrame.toShortString());
551 out.print(" mContentInsets="); out.println(mContentInsets.toShortString());
552 out.print(prefix); out.print("mConfiguration="); out.println(mConfiguration);
553 out.print(prefix); out.print("mLayout="); out.println(mLayout);
554 synchronized (mLock) {
555 out.print(prefix); out.print("mPendingXOffset="); out.print(mPendingXOffset);
556 out.print(" mPendingXOffset="); out.println(mPendingXOffset);
557 out.print(prefix); out.print("mPendingXOffsetStep=");
558 out.print(mPendingXOffsetStep);
559 out.print(" mPendingXOffsetStep="); out.println(mPendingXOffsetStep);
560 out.print(prefix); out.print("mOffsetMessageEnqueued=");
561 out.print(mOffsetMessageEnqueued);
562 out.print(" mPendingSync="); out.println(mPendingSync);
563 if (mPendingMove != null) {
564 out.print(prefix); out.print("mPendingMove="); out.println(mPendingMove);
565 }
566 }
567 }
568
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700569 private void dispatchPointer(MotionEvent event) {
Jeff Brown33bbfd22011-02-24 20:55:35 -0800570 if (event.isTouchEvent()) {
571 synchronized (mLock) {
572 if (event.getAction() == MotionEvent.ACTION_MOVE) {
573 mPendingMove = event;
574 } else {
575 mPendingMove = null;
576 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700577 }
Jeff Brown33bbfd22011-02-24 20:55:35 -0800578 Message msg = mCaller.obtainMessageO(MSG_TOUCH_EVENT, event);
579 mCaller.sendMessage(msg);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800580 } else {
581 event.recycle();
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700582 }
583 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700584
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700585 void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700586 if (mDestroyed) {
587 Log.w(TAG, "Ignoring updateSurface: destroyed");
588 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700589
590 boolean fixedSize = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700591 int myWidth = mSurfaceHolder.getRequestedWidth();
Romain Guy980a9382010-01-08 15:06:28 -0800592 if (myWidth <= 0) myWidth = ViewGroup.LayoutParams.MATCH_PARENT;
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700593 else fixedSize = true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700594 int myHeight = mSurfaceHolder.getRequestedHeight();
Romain Guy980a9382010-01-08 15:06:28 -0800595 if (myHeight <= 0) myHeight = ViewGroup.LayoutParams.MATCH_PARENT;
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700596 else fixedSize = true;
597
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700598 final boolean creating = !mCreated;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700599 final boolean surfaceCreating = !mSurfaceCreated;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700600 final boolean formatChanged = mFormat != mSurfaceHolder.getRequestedFormat();
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700601 boolean sizeChanged = mWidth != myWidth || mHeight != myHeight;
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700602 boolean insetsChanged = !mCreated;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700603 final boolean typeChanged = mType != mSurfaceHolder.getRequestedType();
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700604 final boolean flagsChanged = mCurWindowFlags != mWindowFlags ||
605 mCurWindowPrivateFlags != mWindowPrivateFlags;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700606 if (forceRelayout || creating || surfaceCreating || formatChanged || sizeChanged
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700607 || typeChanged || flagsChanged || redrawNeeded
608 || !mIWallpaperEngine.mShownReported) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700609
Dianne Hackborn284ac932009-08-28 10:34:25 -0700610 if (DEBUG) Log.v(TAG, "Changes: creating=" + creating
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700611 + " format=" + formatChanged + " size=" + sizeChanged);
612
613 try {
614 mWidth = myWidth;
615 mHeight = myHeight;
616 mFormat = mSurfaceHolder.getRequestedFormat();
617 mType = mSurfaceHolder.getRequestedType();
618
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700619 mLayout.x = 0;
620 mLayout.y = 0;
621 mLayout.width = myWidth;
622 mLayout.height = myHeight;
623
624 mLayout.format = mFormat;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700625
626 mCurWindowFlags = mWindowFlags;
627 mLayout.flags = mWindowFlags
628 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
629 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
630 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
631 ;
Chet Haasea8e5a2b2011-10-28 13:18:16 -0700632 mCurWindowPrivateFlags = mWindowPrivateFlags;
633 mLayout.privateFlags = mWindowPrivateFlags;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700634
635 mLayout.memoryType = mType;
636 mLayout.token = mWindowToken;
637
638 if (!mCreated) {
Filip Gruszczynskid66ba062015-03-23 12:59:02 -0700639 // Retrieve watch round info
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700640 TypedArray windowStyle = obtainStyledAttributes(
641 com.android.internal.R.styleable.Window);
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700642 windowStyle.recycle();
643
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700644 // Add window
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700645 mLayout.type = mIWallpaperEngine.mWindowType;
Fabrice Di Meglioaac0d4e2012-07-19 19:21:26 -0700646 mLayout.gravity = Gravity.START|Gravity.TOP;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -0700647 mLayout.setTitle(WallpaperService.this.getClass().getName());
Dianne Hackborn284ac932009-08-28 10:34:25 -0700648 mLayout.windowAnimations =
649 com.android.internal.R.style.Animation_Wallpaper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700650 mInputChannel = new InputChannel();
Craig Mautner6881a102012-07-27 13:04:51 -0700651 if (mSession.addToDisplay(mWindow, mWindow.mSeq, mLayout, View.VISIBLE,
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700652 Display.DEFAULT_DISPLAY, mContentInsets, mStableInsets, mOutsets,
Adrian Roos37d7a682014-11-06 18:15:16 +0100653 mInputChannel) < 0) {
Mattias Peterssond9463f52011-01-12 15:38:55 +0100654 Log.w(TAG, "Failed to add window while updating wallpaper surface.");
655 return;
656 }
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700657 mCreated = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700658
Jeff Brown32cbc38552011-12-01 14:01:49 -0800659 mInputEventReceiver = new WallpaperInputEventReceiver(
660 mInputChannel, Looper.myLooper());
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700661 }
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700662
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700663 mSurfaceHolder.mSurfaceLock.lock();
664 mDrawingAllowed = true;
665
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700666 if (!fixedSize) {
667 mLayout.surfaceInsets.set(mIWallpaperEngine.mDisplayPadding);
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700668 mLayout.surfaceInsets.left += mOutsets.left;
669 mLayout.surfaceInsets.top += mOutsets.top;
670 mLayout.surfaceInsets.right += mOutsets.right;
671 mLayout.surfaceInsets.bottom += mOutsets.bottom;
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700672 } else {
673 mLayout.surfaceInsets.set(0, 0, 0, 0);
674 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700675 final int relayoutResult = mSession.relayout(
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700676 mWindow, mWindow.mSeq, mLayout, mWidth, mHeight,
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800677 View.VISIBLE, 0, mWinFrame, mOverscanInsets, mContentInsets,
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700678 mVisibleInsets, mStableInsets, mOutsets, mConfiguration,
679 mSurfaceHolder.mSurface);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700680
Dianne Hackborn284ac932009-08-28 10:34:25 -0700681 if (DEBUG) Log.v(TAG, "New surface: " + mSurfaceHolder.mSurface
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700682 + ", frame=" + mWinFrame);
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700683
Adrian Roos2ae38052014-12-09 17:03:01 +0000684 int w = mWinFrame.width();
685 int h = mWinFrame.height();
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700686
687 if (!fixedSize) {
688 final Rect padding = mIWallpaperEngine.mDisplayPadding;
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700689 w += padding.left + padding.right + mOutsets.left + mOutsets.right;
690 h += padding.top + padding.bottom + mOutsets.top + mOutsets.bottom;
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700691 mOverscanInsets.left += padding.left;
692 mOverscanInsets.top += padding.top;
693 mOverscanInsets.right += padding.right;
694 mOverscanInsets.bottom += padding.bottom;
695 mContentInsets.left += padding.left;
696 mContentInsets.top += padding.top;
697 mContentInsets.right += padding.right;
698 mContentInsets.bottom += padding.bottom;
699 mStableInsets.left += padding.left;
700 mStableInsets.top += padding.top;
701 mStableInsets.right += padding.right;
702 mStableInsets.bottom += padding.bottom;
703 }
704
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700705 if (mCurWidth != w) {
706 sizeChanged = true;
707 mCurWidth = w;
708 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700709 if (mCurHeight != h) {
710 sizeChanged = true;
711 mCurHeight = h;
712 }
Jeff Brown30bc34f2011-01-25 12:56:56 -0800713
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700714 if (DEBUG) {
715 Log.v(TAG, "Wallpaper size has changed: (" + mCurWidth + ", " + mCurHeight);
716 }
717
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700718 insetsChanged |= !mDispatchedOverscanInsets.equals(mOverscanInsets);
719 insetsChanged |= !mDispatchedContentInsets.equals(mContentInsets);
720 insetsChanged |= !mDispatchedStableInsets.equals(mStableInsets);
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700721 insetsChanged |= !mDispatchedOutsets.equals(mOutsets);
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700722
Jeff Brown30bc34f2011-01-25 12:56:56 -0800723 mSurfaceHolder.setSurfaceFrameSize(w, h);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700724 mSurfaceHolder.mSurfaceLock.unlock();
725
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700726 if (!mSurfaceHolder.mSurface.isValid()) {
727 reportSurfaceDestroyed();
728 if (DEBUG) Log.v(TAG, "Layout: Surface destroyed");
729 return;
730 }
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700731
732 boolean didSurface = false;
733
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700734 try {
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700735 mSurfaceHolder.ungetCallbacks();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700736
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700737 if (surfaceCreating) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700738 mIsCreating = true;
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700739 didSurface = true;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700740 if (DEBUG) Log.v(TAG, "onSurfaceCreated("
741 + mSurfaceHolder + "): " + this);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700742 onSurfaceCreated(mSurfaceHolder);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700743 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700744 if (callbacks != null) {
745 for (SurfaceHolder.Callback c : callbacks) {
746 c.surfaceCreated(mSurfaceHolder);
747 }
748 }
749 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700750
Jeff Brown98365d72012-08-19 20:30:52 -0700751 redrawNeeded |= creating || (relayoutResult
752 & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700753
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700754 if (forceReport || creating || surfaceCreating
755 || formatChanged || sizeChanged) {
Dianne Hackborncbf15042009-08-18 18:29:09 -0700756 if (DEBUG) {
757 RuntimeException e = new RuntimeException();
758 e.fillInStackTrace();
759 Log.w(TAG, "forceReport=" + forceReport + " creating=" + creating
760 + " formatChanged=" + formatChanged
761 + " sizeChanged=" + sizeChanged, e);
762 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700763 if (DEBUG) Log.v(TAG, "onSurfaceChanged("
764 + mSurfaceHolder + ", " + mFormat
765 + ", " + mCurWidth + ", " + mCurHeight
766 + "): " + this);
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700767 didSurface = true;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700768 onSurfaceChanged(mSurfaceHolder, mFormat,
769 mCurWidth, mCurHeight);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700770 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700771 if (callbacks != null) {
772 for (SurfaceHolder.Callback c : callbacks) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -0700773 c.surfaceChanged(mSurfaceHolder, mFormat,
774 mCurWidth, mCurHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700775 }
776 }
777 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700778
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700779 if (insetsChanged) {
780 mDispatchedOverscanInsets.set(mOverscanInsets);
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700781 mDispatchedOverscanInsets.left += mOutsets.left;
782 mDispatchedOverscanInsets.top += mOutsets.top;
783 mDispatchedOverscanInsets.right += mOutsets.right;
784 mDispatchedOverscanInsets.bottom += mOutsets.bottom;
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700785 mDispatchedContentInsets.set(mContentInsets);
786 mDispatchedStableInsets.set(mStableInsets);
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700787 mDispatchedOutsets.set(mOutsets);
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700788 mFinalSystemInsets.set(mDispatchedOverscanInsets);
789 mFinalStableInsets.set(mDispatchedStableInsets);
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700790 WindowInsets insets = new WindowInsets(mFinalSystemInsets,
Adam Powell01f280d2015-05-18 16:07:42 -0700791 null, mFinalStableInsets,
792 getResources().getConfiguration().isScreenRound());
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700793 if (DEBUG) {
794 Log.v(TAG, "dispatching insets=" + insets);
795 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700796 onApplyWindowInsets(insets);
797 }
798
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700799 if (redrawNeeded) {
800 onSurfaceRedrawNeeded(mSurfaceHolder);
801 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
802 if (callbacks != null) {
803 for (SurfaceHolder.Callback c : callbacks) {
804 if (c instanceof SurfaceHolder.Callback2) {
805 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
806 mSurfaceHolder);
807 }
808 }
809 }
810 }
811
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700812 if (didSurface && !mReportedVisible) {
813 // This wallpaper is currently invisible, but its
814 // surface has changed. At this point let's tell it
815 // again that it is invisible in case the report about
816 // the surface caused it to start running. We really
817 // don't want wallpapers running when not visible.
818 if (mIsCreating) {
819 // Some wallpapers will ignore this call if they
820 // had previously been told they were invisble,
821 // so if we are creating a new surface then toggle
822 // the state to get them to notice.
823 if (DEBUG) Log.v(TAG, "onVisibilityChanged(true) at surface: "
824 + this);
825 onVisibilityChanged(true);
826 }
827 if (DEBUG) Log.v(TAG, "onVisibilityChanged(false) at surface: "
828 + this);
829 onVisibilityChanged(false);
830 }
831
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700832 } finally {
833 mIsCreating = false;
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700834 mSurfaceCreated = true;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700835 if (redrawNeeded) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700836 mSession.finishDrawing(mWindow);
837 }
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700838 mIWallpaperEngine.reportShown();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700839 }
840 } catch (RemoteException ex) {
841 }
842 if (DEBUG) Log.v(
843 TAG, "Layout: x=" + mLayout.x + " y=" + mLayout.y +
844 " w=" + mLayout.width + " h=" + mLayout.height);
845 }
846 }
847
848 void attach(IWallpaperEngineWrapper wrapper) {
Dianne Hackborncbf15042009-08-18 18:29:09 -0700849 if (DEBUG) Log.v(TAG, "attach: " + this + " wrapper=" + wrapper);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700850 if (mDestroyed) {
851 return;
852 }
853
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700854 mIWallpaperEngine = wrapper;
855 mCaller = wrapper.mCaller;
856 mConnection = wrapper.mConnection;
857 mWindowToken = wrapper.mWindowToken;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700858 mSurfaceHolder.setSizeFromLayout();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700859 mInitializing = true;
Jeff Brownf9e989d2013-04-04 23:04:03 -0700860 mSession = WindowManagerGlobal.getWindowSession();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700861
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700862 mWindow.setSession(mSession);
Dianne Hackborn9fe6cb52011-09-09 13:02:43 -0700863
Filip Gruszczynski4544b922015-04-16 13:18:58 -0700864 mLayout.packageName = getPackageName();
865
Jeff Brown3d110b22014-11-21 19:01:13 -0800866 mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
867 mDisplayManager.registerDisplayListener(mDisplayListener, mCaller.getHandler());
868 mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
Filip Gruszczynski4544b922015-04-16 13:18:58 -0700869 mDisplayState = mDisplay.getState();
Dianne Hackborn9fe6cb52011-09-09 13:02:43 -0700870
Dianne Hackborn284ac932009-08-28 10:34:25 -0700871 if (DEBUG) Log.v(TAG, "onCreate(): " + this);
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700872 onCreate(mSurfaceHolder);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700873
874 mInitializing = false;
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700875 mReportedVisible = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700876 updateSurface(false, false, false);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700877 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700878
Dianne Hackborn284ac932009-08-28 10:34:25 -0700879 void doDesiredSizeChanged(int desiredWidth, int desiredHeight) {
880 if (!mDestroyed) {
881 if (DEBUG) Log.v(TAG, "onDesiredSizeChanged("
882 + desiredWidth + "," + desiredHeight + "): " + this);
Joe Onoratodcfae5c2010-10-28 18:03:23 -0700883 mIWallpaperEngine.mReqWidth = desiredWidth;
884 mIWallpaperEngine.mReqHeight = desiredHeight;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700885 onDesiredSizeChanged(desiredWidth, desiredHeight);
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700886 doOffsetsChanged(true);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700887 }
888 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700889
890 void doDisplayPaddingChanged(Rect padding) {
891 if (!mDestroyed) {
892 if (DEBUG) Log.v(TAG, "onDisplayPaddingChanged(" + padding + "): " + this);
893 if (!mIWallpaperEngine.mDisplayPadding.equals(padding)) {
894 mIWallpaperEngine.mDisplayPadding.set(padding);
895 updateSurface(true, false, false);
896 }
897 }
898 }
899
Dianne Hackborn284ac932009-08-28 10:34:25 -0700900 void doVisibilityChanged(boolean visible) {
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -0800901 if (!mDestroyed) {
902 mVisible = visible;
903 reportVisibility();
904 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700905 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -0700906
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700907 void reportVisibility() {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700908 if (!mDestroyed) {
Filip Gruszczynski4544b922015-04-16 13:18:58 -0700909 mDisplayState = mDisplay == null ? Display.STATE_UNKNOWN : mDisplay.getState();
910 boolean visible = mVisible && mDisplayState != Display.STATE_OFF;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700911 if (mReportedVisible != visible) {
912 mReportedVisible = visible;
913 if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible
914 + "): " + this);
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700915 if (visible) {
916 // If becoming visible, in preview mode the surface
917 // may have been destroyed so now we need to make
918 // sure it is re-created.
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700919 doOffsetsChanged(false);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700920 updateSurface(false, false, false);
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700921 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700922 onVisibilityChanged(visible);
923 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700924 }
925 }
926
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700927 void doOffsetsChanged(boolean always) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700928 if (mDestroyed) {
929 return;
930 }
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700931
932 if (!always && !mOffsetsChanged) {
933 return;
934 }
935
Dianne Hackborn284ac932009-08-28 10:34:25 -0700936 float xOffset;
937 float yOffset;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800938 float xOffsetStep;
939 float yOffsetStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700940 boolean sync;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700941 synchronized (mLock) {
942 xOffset = mPendingXOffset;
943 yOffset = mPendingYOffset;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800944 xOffsetStep = mPendingXOffsetStep;
945 yOffsetStep = mPendingYOffsetStep;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700946 sync = mPendingSync;
947 mPendingSync = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700948 mOffsetMessageEnqueued = false;
949 }
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700950
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700951 if (mSurfaceCreated) {
Dianne Hackborn9e4e7272011-08-30 14:06:51 -0700952 if (mReportedVisible) {
953 if (DEBUG) Log.v(TAG, "Offsets change in " + this
954 + ": " + xOffset + "," + yOffset);
955 final int availw = mIWallpaperEngine.mReqWidth-mCurWidth;
956 final int xPixels = availw > 0 ? -(int)(availw*xOffset+.5f) : 0;
957 final int availh = mIWallpaperEngine.mReqHeight-mCurHeight;
958 final int yPixels = availh > 0 ? -(int)(availh*yOffset+.5f) : 0;
959 onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixels, yPixels);
960 } else {
961 mOffsetsChanged = true;
962 }
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700963 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700964
965 if (sync) {
966 try {
967 if (DEBUG) Log.v(TAG, "Reporting offsets change complete");
968 mSession.wallpaperOffsetsComplete(mWindow.asBinder());
969 } catch (RemoteException e) {
970 }
971 }
Dianne Hackborn284ac932009-08-28 10:34:25 -0700972 }
973
Dianne Hackborn75804932009-10-20 20:15:20 -0700974 void doCommand(WallpaperCommand cmd) {
975 Bundle result;
976 if (!mDestroyed) {
977 result = onCommand(cmd.action, cmd.x, cmd.y, cmd.z,
978 cmd.extras, cmd.sync);
979 } else {
980 result = null;
981 }
982 if (cmd.sync) {
983 try {
984 if (DEBUG) Log.v(TAG, "Reporting command complete");
985 mSession.wallpaperCommandComplete(mWindow.asBinder(), result);
986 } catch (RemoteException e) {
987 }
988 }
989 }
990
Dianne Hackborn18ee31e2010-04-27 15:54:02 -0700991 void reportSurfaceDestroyed() {
992 if (mSurfaceCreated) {
993 mSurfaceCreated = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700994 mSurfaceHolder.ungetCallbacks();
995 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
996 if (callbacks != null) {
997 for (SurfaceHolder.Callback c : callbacks) {
998 c.surfaceDestroyed(mSurfaceHolder);
999 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001000 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001001 if (DEBUG) Log.v(TAG, "onSurfaceDestroyed("
1002 + mSurfaceHolder + "): " + this);
1003 onSurfaceDestroyed(mSurfaceHolder);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001004 }
Dianne Hackborn18ee31e2010-04-27 15:54:02 -07001005 }
1006
1007 void detach() {
1008 if (mDestroyed) {
1009 return;
1010 }
1011
1012 mDestroyed = true;
Jeff Brown3d110b22014-11-21 19:01:13 -08001013
1014 if (mDisplayManager != null) {
1015 mDisplayManager.unregisterDisplayListener(mDisplayListener);
1016 }
1017
Dianne Hackborn18ee31e2010-04-27 15:54:02 -07001018 if (mVisible) {
1019 mVisible = false;
1020 if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this);
1021 onVisibilityChanged(false);
1022 }
1023
1024 reportSurfaceDestroyed();
Dianne Hackborn284ac932009-08-28 10:34:25 -07001025
1026 if (DEBUG) Log.v(TAG, "onDestroy(): " + this);
1027 onDestroy();
Jeff Brown3d110b22014-11-21 19:01:13 -08001028
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001029 if (mCreated) {
1030 try {
Dianne Hackbornba3e31d2010-04-22 18:59:03 -07001031 if (DEBUG) Log.v(TAG, "Removing window and destroying surface "
1032 + mSurfaceHolder.getSurface() + " of: " + this);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001033
Jeff Brown32cbc38552011-12-01 14:01:49 -08001034 if (mInputEventReceiver != null) {
1035 mInputEventReceiver.dispose();
1036 mInputEventReceiver = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001037 }
1038
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001039 mSession.remove(mWindow);
1040 } catch (RemoteException e) {
1041 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001042 mSurfaceHolder.mSurface.release();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001043 mCreated = false;
Jeff Brown349703e2010-06-22 01:27:15 -07001044
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001045 // Dispose the input channel after removing the window so the Window Manager
1046 // doesn't interpret the input channel being closed as an abnormal termination.
1047 if (mInputChannel != null) {
1048 mInputChannel.dispose();
1049 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001050 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001051 }
1052 }
Jeff Brown3d110b22014-11-21 19:01:13 -08001053
1054 private final DisplayListener mDisplayListener = new DisplayListener() {
1055 @Override
1056 public void onDisplayChanged(int displayId) {
1057 if (mDisplay.getDisplayId() == displayId) {
1058 reportVisibility();
1059 }
1060 }
1061
1062 @Override
1063 public void onDisplayRemoved(int displayId) {
1064 }
1065
1066 @Override
1067 public void onDisplayAdded(int displayId) {
1068 }
1069 };
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001070 }
Jeff Brown3d110b22014-11-21 19:01:13 -08001071
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001072 class IWallpaperEngineWrapper extends IWallpaperEngine.Stub
1073 implements HandlerCaller.Callback {
1074 private final HandlerCaller mCaller;
1075
1076 final IWallpaperConnection mConnection;
1077 final IBinder mWindowToken;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001078 final int mWindowType;
1079 final boolean mIsPreview;
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -07001080 boolean mShownReported;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001081 int mReqWidth;
1082 int mReqHeight;
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001083 final Rect mDisplayPadding = new Rect();
1084
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001085 Engine mEngine;
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001086
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001087 IWallpaperEngineWrapper(WallpaperService context,
1088 IWallpaperConnection conn, IBinder windowToken,
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001089 int windowType, boolean isPreview, int reqWidth, int reqHeight, Rect padding) {
Mita Yunaa8dc2e2012-12-10 18:32:03 -08001090 mCaller = new HandlerCaller(context, context.getMainLooper(), this, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001091 mConnection = conn;
1092 mWindowToken = windowToken;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001093 mWindowType = windowType;
1094 mIsPreview = isPreview;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001095 mReqWidth = reqWidth;
1096 mReqHeight = reqHeight;
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001097 mDisplayPadding.set(padding);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001098
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001099 Message msg = mCaller.obtainMessage(DO_ATTACH);
1100 mCaller.sendMessage(msg);
1101 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001102
Dianne Hackborn284ac932009-08-28 10:34:25 -07001103 public void setDesiredSize(int width, int height) {
1104 Message msg = mCaller.obtainMessageII(DO_SET_DESIRED_SIZE, width, height);
1105 mCaller.sendMessage(msg);
1106 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001107
1108 public void setDisplayPadding(Rect padding) {
1109 Message msg = mCaller.obtainMessageO(DO_SET_DISPLAY_PADDING, padding);
1110 mCaller.sendMessage(msg);
1111 }
1112
Dianne Hackborn284ac932009-08-28 10:34:25 -07001113 public void setVisibility(boolean visible) {
1114 Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED,
1115 visible ? 1 : 0);
1116 mCaller.sendMessage(msg);
1117 }
1118
Dianne Hackborn6adba242009-11-10 11:10:09 -08001119 public void dispatchPointer(MotionEvent event) {
1120 if (mEngine != null) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001121 mEngine.dispatchPointer(event);
Jeff Brown32cbc38552011-12-01 14:01:49 -08001122 } else {
1123 event.recycle();
Dianne Hackborn6adba242009-11-10 11:10:09 -08001124 }
1125 }
Jeff Brown9f3bdfe2010-10-13 06:01:27 -07001126
1127 public void dispatchWallpaperCommand(String action, int x, int y,
1128 int z, Bundle extras) {
1129 if (mEngine != null) {
1130 mEngine.mWindow.dispatchWallpaperCommand(action, x, y, z, extras, false);
1131 }
1132 }
1133
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -07001134 public void reportShown() {
1135 if (!mShownReported) {
1136 mShownReported = true;
1137 try {
1138 mConnection.engineShown(this);
1139 } catch (RemoteException e) {
1140 Log.w(TAG, "Wallpaper host disappeared", e);
1141 return;
1142 }
1143 }
1144 }
1145
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001146 public void destroy() {
1147 Message msg = mCaller.obtainMessage(DO_DETACH);
1148 mCaller.sendMessage(msg);
1149 }
1150
1151 public void executeMessage(Message message) {
1152 switch (message.what) {
1153 case DO_ATTACH: {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001154 try {
1155 mConnection.attachEngine(this);
1156 } catch (RemoteException e) {
1157 Log.w(TAG, "Wallpaper host disappeared", e);
1158 return;
1159 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001160 Engine engine = onCreateEngine();
1161 mEngine = engine;
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -08001162 mActiveEngines.add(engine);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001163 engine.attach(this);
1164 return;
1165 }
1166 case DO_DETACH: {
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -08001167 mActiveEngines.remove(mEngine);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001168 mEngine.detach();
1169 return;
1170 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001171 case DO_SET_DESIRED_SIZE: {
1172 mEngine.doDesiredSizeChanged(message.arg1, message.arg2);
1173 return;
1174 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001175 case DO_SET_DISPLAY_PADDING: {
1176 mEngine.doDisplayPaddingChanged((Rect) message.obj);
1177 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001178 case MSG_UPDATE_SURFACE:
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001179 mEngine.updateSurface(true, false, false);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001180 break;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001181 case MSG_VISIBILITY_CHANGED:
1182 if (DEBUG) Log.v(TAG, "Visibility change in " + mEngine
1183 + ": " + message.arg1);
Dianne Hackborn284ac932009-08-28 10:34:25 -07001184 mEngine.doVisibilityChanged(message.arg1 != 0);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001185 break;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001186 case MSG_WALLPAPER_OFFSETS: {
Dianne Hackborn9e4e7272011-08-30 14:06:51 -07001187 mEngine.doOffsetsChanged(true);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001188 } break;
Dianne Hackborn75804932009-10-20 20:15:20 -07001189 case MSG_WALLPAPER_COMMAND: {
1190 WallpaperCommand cmd = (WallpaperCommand)message.obj;
1191 mEngine.doCommand(cmd);
1192 } break;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001193 case MSG_WINDOW_RESIZED: {
1194 final boolean reportDraw = message.arg1 != 0;
Filip Gruszczynski03850592015-07-01 08:35:28 -07001195 mEngine.mOutsets.set((Rect) message.obj);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001196 mEngine.updateSurface(true, false, reportDraw);
Dianne Hackborn9e4e7272011-08-30 14:06:51 -07001197 mEngine.doOffsetsChanged(true);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001198 } break;
Craig Mautner5702d4d2012-06-30 14:10:16 -07001199 case MSG_WINDOW_MOVED: {
1200 // Do nothing. What does it mean for a Wallpaper to move?
1201 } break;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001202 case MSG_TOUCH_EVENT: {
Jeff Brown840db1f2010-10-21 17:36:54 -07001203 boolean skip = false;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001204 MotionEvent ev = (MotionEvent)message.obj;
Jeff Brown840db1f2010-10-21 17:36:54 -07001205 if (ev.getAction() == MotionEvent.ACTION_MOVE) {
1206 synchronized (mEngine.mLock) {
1207 if (mEngine.mPendingMove == ev) {
1208 mEngine.mPendingMove = null;
1209 } else {
1210 // this is not the motion event we are looking for....
1211 skip = true;
1212 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001213 }
1214 }
Jeff Brown840db1f2010-10-21 17:36:54 -07001215 if (!skip) {
1216 if (DEBUG) Log.v(TAG, "Delivering touch event: " + ev);
1217 mEngine.onTouchEvent(ev);
1218 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001219 ev.recycle();
1220 } break;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001221 default :
1222 Log.w(TAG, "Unknown message type " + message.what);
1223 }
1224 }
1225 }
1226
1227 /**
1228 * Implements the internal {@link IWallpaperService} interface to convert
1229 * incoming calls to it back to calls on an {@link WallpaperService}.
1230 */
1231 class IWallpaperServiceWrapper extends IWallpaperService.Stub {
1232 private final WallpaperService mTarget;
1233
1234 public IWallpaperServiceWrapper(WallpaperService context) {
1235 mTarget = context;
1236 }
1237
Craig Mautnerb1ef3692012-11-16 17:31:04 -08001238 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001239 public void attach(IWallpaperConnection conn, IBinder windowToken,
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001240 int windowType, boolean isPreview, int reqWidth, int reqHeight, Rect padding) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001241 new IWallpaperEngineWrapper(mTarget, conn, windowToken,
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001242 windowType, isPreview, reqWidth, reqHeight, padding);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001243 }
1244 }
Craig Mautnerb1ef3692012-11-16 17:31:04 -08001245
Dianne Hackbornaf1f42b2009-11-20 16:27:27 -08001246 @Override
1247 public void onCreate() {
1248 super.onCreate();
1249 }
1250
1251 @Override
1252 public void onDestroy() {
1253 super.onDestroy();
1254 for (int i=0; i<mActiveEngines.size(); i++) {
1255 mActiveEngines.get(i).detach();
1256 }
1257 mActiveEngines.clear();
1258 }
1259
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001260 /**
1261 * Implement to return the implementation of the internal accessibility
1262 * service interface. Subclasses should not override.
1263 */
1264 @Override
1265 public final IBinder onBind(Intent intent) {
1266 return new IWallpaperServiceWrapper(this);
1267 }
Craig Mautnerb1ef3692012-11-16 17:31:04 -08001268
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001269 /**
1270 * Must be implemented to return a new instance of the wallpaper's engine.
1271 * Note that multiple instances may be active at the same time, such as
1272 * when the wallpaper is currently set as the active wallpaper and the user
1273 * is in the wallpaper picker viewing a preview of it as well.
1274 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001275 public abstract Engine onCreateEngine();
Dianne Hackborn527de8e2011-08-22 16:10:36 -07001276
1277 @Override
1278 protected void dump(FileDescriptor fd, PrintWriter out, String[] args) {
1279 out.print("State of wallpaper "); out.print(this); out.println(":");
1280 for (int i=0; i<mActiveEngines.size(); i++) {
1281 Engine engine = mActiveEngines.get(i);
1282 out.print(" Engine "); out.print(engine); out.println(":");
1283 engine.dump(" ", fd, out, args);
1284 }
1285 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001286}