Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.service.wallpaper; |
| 18 | |
| 19 | import com.android.internal.os.HandlerCaller; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 20 | import com.android.internal.view.BaseIWindow; |
| 21 | import com.android.internal.view.BaseSurfaceHolder; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 22 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
| 24 | import android.annotation.SdkConstant.SdkConstantType; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 25 | import android.app.Service; |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 26 | import android.app.WallpaperManager; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 31 | import android.content.res.Configuration; |
Mathias Agopian | 62bf4a0 | 2010-09-08 16:32:27 -0700 | [diff] [blame] | 32 | import android.graphics.PixelFormat; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 33 | import android.graphics.Rect; |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 36 | import android.os.Looper; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 37 | import android.os.Message; |
Dianne Hackborn | 9fe6cb5 | 2011-09-09 13:02:43 -0700 | [diff] [blame] | 38 | import android.os.PowerManager; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 39 | import android.os.RemoteException; |
| 40 | import android.util.Log; |
Craig Mautner | 6881a10 | 2012-07-27 13:04:51 -0700 | [diff] [blame] | 41 | import android.view.Display; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 42 | import android.view.Gravity; |
| 43 | import android.view.IWindowSession; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | import android.view.InputChannel; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 45 | import android.view.InputDevice; |
Jeff Brown | 4952dfd | 2011-11-30 19:23:22 -0800 | [diff] [blame] | 46 | import android.view.InputEvent; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 47 | import android.view.InputEventReceiver; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 48 | import android.view.MotionEvent; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 49 | import android.view.SurfaceHolder; |
| 50 | import android.view.View; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 51 | import android.view.ViewGroup; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 52 | import android.view.WindowManager; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 53 | import android.view.WindowManagerGlobal; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 54 | |
Dianne Hackborn | 527de8e | 2011-08-22 16:10:36 -0700 | [diff] [blame] | 55 | import java.io.FileDescriptor; |
| 56 | import java.io.PrintWriter; |
Dianne Hackborn | af1f42b | 2009-11-20 16:27:27 -0800 | [diff] [blame] | 57 | import java.util.ArrayList; |
| 58 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 59 | /** |
| 60 | * A wallpaper service is responsible for showing a live wallpaper behind |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 61 | * applications that would like to sit on top of it. This service object |
| 62 | * itself does very little -- its only purpose is to generate instances of |
Dianne Hackborn | e4260f4 | 2009-11-18 21:15:59 -0800 | [diff] [blame] | 63 | * {@link Engine} as needed. Implementing a wallpaper thus |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 64 | * involves subclassing from this, subclassing an Engine implementation, |
| 65 | * and implementing {@link #onCreateEngine()} to return a new instance of |
| 66 | * your engine. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 67 | */ |
| 68 | public abstract class WallpaperService extends Service { |
| 69 | /** |
| 70 | * The {@link Intent} that must be declared as handled by the service. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 71 | * To be supported, the service must also require the |
| 72 | * {@link android.Manifest.permission#BIND_WALLPAPER} permission so |
| 73 | * that other applications can not abuse it. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 74 | */ |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 75 | @SdkConstant(SdkConstantType.SERVICE_ACTION) |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 76 | public static final String SERVICE_INTERFACE = |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 77 | "android.service.wallpaper.WallpaperService"; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 78 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Name under which a WallpaperService component publishes information |
| 81 | * about itself. This meta-data must reference an XML resource containing |
| 82 | * a <code><{@link android.R.styleable#Wallpaper wallpaper}></code> |
| 83 | * tag. |
| 84 | */ |
| 85 | public static final String SERVICE_META_DATA = "android.service.wallpaper"; |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 86 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 87 | static final String TAG = "WallpaperService"; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 88 | static final boolean DEBUG = false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 89 | |
| 90 | private static final int DO_ATTACH = 10; |
| 91 | private static final int DO_DETACH = 20; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 92 | private static final int DO_SET_DESIRED_SIZE = 30; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 93 | |
| 94 | private static final int MSG_UPDATE_SURFACE = 10000; |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 95 | private static final int MSG_VISIBILITY_CHANGED = 10010; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 96 | private static final int MSG_WALLPAPER_OFFSETS = 10020; |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 97 | private static final int MSG_WALLPAPER_COMMAND = 10025; |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 98 | private static final int MSG_WINDOW_RESIZED = 10030; |
Craig Mautner | 5702d4d | 2012-06-30 14:10:16 -0700 | [diff] [blame] | 99 | private static final int MSG_WINDOW_MOVED = 10035; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 100 | private static final int MSG_TOUCH_EVENT = 10040; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 101 | |
Dianne Hackborn | af1f42b | 2009-11-20 16:27:27 -0800 | [diff] [blame] | 102 | private final ArrayList<Engine> mActiveEngines |
| 103 | = new ArrayList<Engine>(); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 104 | |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 105 | static final class WallpaperCommand { |
| 106 | String action; |
| 107 | int x; |
| 108 | int y; |
| 109 | int z; |
| 110 | Bundle extras; |
| 111 | boolean sync; |
| 112 | } |
| 113 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 114 | /** |
| 115 | * The actual implementation of a wallpaper. A wallpaper service may |
| 116 | * have multiple instances running (for example as a real wallpaper |
| 117 | * and as a preview), each of which is represented by its own Engine |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 118 | * instance. You must implement {@link WallpaperService#onCreateEngine()} |
| 119 | * to return your concrete Engine implementation. |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 120 | */ |
| 121 | public class Engine { |
| 122 | IWallpaperEngineWrapper mIWallpaperEngine; |
| 123 | |
| 124 | // Copies from mIWallpaperEngine. |
| 125 | HandlerCaller mCaller; |
| 126 | IWallpaperConnection mConnection; |
| 127 | IBinder mWindowToken; |
| 128 | |
| 129 | boolean mInitializing = true; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 130 | boolean mVisible; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 131 | boolean mScreenOn = true; |
| 132 | boolean mReportedVisible; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 133 | boolean mDestroyed; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 134 | |
| 135 | // Current window state. |
| 136 | boolean mCreated; |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 137 | boolean mSurfaceCreated; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 138 | boolean mIsCreating; |
| 139 | boolean mDrawingAllowed; |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 140 | boolean mOffsetsChanged; |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 141 | boolean mFixedSizeAllowed; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 142 | int mWidth; |
| 143 | int mHeight; |
| 144 | int mFormat; |
| 145 | int mType; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 146 | int mCurWidth; |
| 147 | int mCurHeight; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 148 | int mWindowFlags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Chet Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 149 | int mWindowPrivateFlags = |
| 150 | WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 151 | int mCurWindowFlags = mWindowFlags; |
Chet Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 152 | int mCurWindowPrivateFlags = mWindowPrivateFlags; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 153 | final Rect mVisibleInsets = new Rect(); |
| 154 | final Rect mWinFrame = new Rect(); |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 155 | final Rect mOverscanInsets = new Rect(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 156 | final Rect mContentInsets = new Rect(); |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 157 | final Configuration mConfiguration = new Configuration(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 158 | |
| 159 | final WindowManager.LayoutParams mLayout |
| 160 | = new WindowManager.LayoutParams(); |
| 161 | IWindowSession mSession; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 162 | InputChannel mInputChannel; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 163 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 164 | final Object mLock = new Object(); |
| 165 | boolean mOffsetMessageEnqueued; |
| 166 | float mPendingXOffset; |
| 167 | float mPendingYOffset; |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 168 | float mPendingXOffsetStep; |
| 169 | float mPendingYOffsetStep; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 170 | boolean mPendingSync; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 171 | MotionEvent mPendingMove; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 172 | |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 173 | final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 174 | @Override |
| 175 | public void onReceive(Context context, Intent intent) { |
| 176 | if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { |
| 177 | mScreenOn = true; |
| 178 | reportVisibility(); |
| 179 | } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { |
| 180 | mScreenOn = false; |
| 181 | reportVisibility(); |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 186 | final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() { |
Mathias Agopian | 62bf4a0 | 2010-09-08 16:32:27 -0700 | [diff] [blame] | 187 | { |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 188 | mRequestedFormat = PixelFormat.RGBX_8888; |
Mathias Agopian | 62bf4a0 | 2010-09-08 16:32:27 -0700 | [diff] [blame] | 189 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 190 | |
| 191 | @Override |
| 192 | public boolean onAllowLockCanvas() { |
| 193 | return mDrawingAllowed; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onRelayoutContainer() { |
| 198 | Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE); |
| 199 | mCaller.sendMessage(msg); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void onUpdateSurface() { |
| 204 | Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE); |
| 205 | mCaller.sendMessage(msg); |
| 206 | } |
| 207 | |
| 208 | public boolean isCreating() { |
| 209 | return mIsCreating; |
| 210 | } |
| 211 | |
Dianne Hackborn | a48a37f | 2011-02-01 16:30:38 -0800 | [diff] [blame] | 212 | @Override |
| 213 | public void setFixedSize(int width, int height) { |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 214 | if (!mFixedSizeAllowed) { |
Dianne Hackborn | a48a37f | 2011-02-01 16:30:38 -0800 | [diff] [blame] | 215 | // Regular apps can't do this. It can only work for |
| 216 | // certain designs of window animations, so you can't |
| 217 | // rely on it. |
| 218 | throw new UnsupportedOperationException( |
| 219 | "Wallpapers currently only support sizing from layout"); |
| 220 | } |
Michael Jurka | b8f939f | 2011-02-01 20:50:30 -0800 | [diff] [blame] | 221 | super.setFixedSize(width, height); |
Dianne Hackborn | a48a37f | 2011-02-01 16:30:38 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 224 | public void setKeepScreenOn(boolean screenOn) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 225 | throw new UnsupportedOperationException( |
| 226 | "Wallpapers do not support keep screen on"); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | }; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 230 | |
| 231 | final class WallpaperInputEventReceiver extends InputEventReceiver { |
| 232 | public WallpaperInputEventReceiver(InputChannel inputChannel, Looper looper) { |
| 233 | super(inputChannel, looper); |
| 234 | } |
| 235 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 236 | @Override |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 237 | public void onInputEvent(InputEvent event) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 238 | boolean handled = false; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 239 | try { |
Jeff Brown | 4952dfd | 2011-11-30 19:23:22 -0800 | [diff] [blame] | 240 | if (event instanceof MotionEvent |
| 241 | && (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 242 | MotionEvent dup = MotionEvent.obtainNoHistory((MotionEvent)event); |
| 243 | dispatchPointer(dup); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 244 | handled = true; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 245 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 246 | } finally { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 247 | finishInputEvent(event, handled); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 250 | } |
| 251 | WallpaperInputEventReceiver mInputEventReceiver; |
| 252 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 253 | final BaseIWindow mWindow = new BaseIWindow() { |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 254 | @Override |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 255 | public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 256 | Rect visibleInsets, boolean reportDraw, Configuration newConfig) { |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 257 | Message msg = mCaller.obtainMessageI(MSG_WINDOW_RESIZED, |
| 258 | reportDraw ? 1 : 0); |
| 259 | mCaller.sendMessage(msg); |
| 260 | } |
Craig Mautner | 5702d4d | 2012-06-30 14:10:16 -0700 | [diff] [blame] | 261 | |
| 262 | @Override |
| 263 | public void moved(int newX, int newY) { |
| 264 | Message msg = mCaller.obtainMessageII(MSG_WINDOW_MOVED, newX, newY); |
| 265 | mCaller.sendMessage(msg); |
| 266 | } |
| 267 | |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 268 | @Override |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 269 | public void dispatchAppVisibility(boolean visible) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 270 | // We don't do this in preview mode; we'll let the preview |
| 271 | // activity tell us when to run. |
| 272 | if (!mIWallpaperEngine.mIsPreview) { |
| 273 | Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED, |
| 274 | visible ? 1 : 0); |
| 275 | mCaller.sendMessage(msg); |
| 276 | } |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 277 | } |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 278 | |
| 279 | @Override |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 280 | public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep, |
| 281 | boolean sync) { |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 282 | synchronized (mLock) { |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 283 | if (DEBUG) Log.v(TAG, "Dispatch wallpaper offsets: " + x + ", " + y); |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 284 | mPendingXOffset = x; |
| 285 | mPendingYOffset = y; |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 286 | mPendingXOffsetStep = xStep; |
| 287 | mPendingYOffsetStep = yStep; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 288 | if (sync) { |
| 289 | mPendingSync = true; |
| 290 | } |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 291 | if (!mOffsetMessageEnqueued) { |
| 292 | mOffsetMessageEnqueued = true; |
| 293 | Message msg = mCaller.obtainMessage(MSG_WALLPAPER_OFFSETS); |
| 294 | mCaller.sendMessage(msg); |
| 295 | } |
| 296 | } |
| 297 | } |
Craig Mautner | 5702d4d | 2012-06-30 14:10:16 -0700 | [diff] [blame] | 298 | |
| 299 | @Override |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 300 | public void dispatchWallpaperCommand(String action, int x, int y, |
| 301 | int z, Bundle extras, boolean sync) { |
| 302 | synchronized (mLock) { |
| 303 | if (DEBUG) Log.v(TAG, "Dispatch wallpaper command: " + x + ", " + y); |
| 304 | WallpaperCommand cmd = new WallpaperCommand(); |
| 305 | cmd.action = action; |
| 306 | cmd.x = x; |
| 307 | cmd.y = y; |
| 308 | cmd.z = z; |
| 309 | cmd.extras = extras; |
| 310 | cmd.sync = sync; |
| 311 | Message msg = mCaller.obtainMessage(MSG_WALLPAPER_COMMAND); |
| 312 | msg.obj = cmd; |
| 313 | mCaller.sendMessage(msg); |
| 314 | } |
| 315 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Provides access to the surface in which this wallpaper is drawn. |
| 320 | */ |
| 321 | public SurfaceHolder getSurfaceHolder() { |
| 322 | return mSurfaceHolder; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Convenience for {@link WallpaperManager#getDesiredMinimumWidth() |
| 327 | * WallpaperManager.getDesiredMinimumWidth()}, returning the width |
| 328 | * that the system would like this wallpaper to run in. |
| 329 | */ |
| 330 | public int getDesiredMinimumWidth() { |
| 331 | return mIWallpaperEngine.mReqWidth; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Convenience for {@link WallpaperManager#getDesiredMinimumHeight() |
| 336 | * WallpaperManager.getDesiredMinimumHeight()}, returning the height |
| 337 | * that the system would like this wallpaper to run in. |
| 338 | */ |
| 339 | public int getDesiredMinimumHeight() { |
| 340 | return mIWallpaperEngine.mReqHeight; |
| 341 | } |
| 342 | |
| 343 | /** |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 344 | * Return whether the wallpaper is currently visible to the user, |
| 345 | * this is the last value supplied to |
| 346 | * {@link #onVisibilityChanged(boolean)}. |
| 347 | */ |
| 348 | public boolean isVisible() { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 349 | return mReportedVisible; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 353 | * Returns true if this engine is running in preview mode -- that is, |
| 354 | * it is being shown to the user before they select it as the actual |
| 355 | * wallpaper. |
| 356 | */ |
| 357 | public boolean isPreview() { |
| 358 | return mIWallpaperEngine.mIsPreview; |
| 359 | } |
| 360 | |
| 361 | /** |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 362 | * Control whether this wallpaper will receive raw touch events |
| 363 | * from the window manager as the user interacts with the window |
| 364 | * that is currently displaying the wallpaper. By default they |
| 365 | * are turned off. If enabled, the events will be received in |
| 366 | * {@link #onTouchEvent(MotionEvent)}. |
| 367 | */ |
| 368 | public void setTouchEventsEnabled(boolean enabled) { |
| 369 | mWindowFlags = enabled |
| 370 | ? (mWindowFlags&~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) |
| 371 | : (mWindowFlags|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); |
| 372 | if (mCreated) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 373 | updateSurface(false, false, false); |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
Chet Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 376 | |
| 377 | /** |
| 378 | * Control whether this wallpaper will receive notifications when the wallpaper |
| 379 | * has been scrolled. By default, wallpapers will receive notifications, although |
| 380 | * the default static image wallpapers do not. It is a performance optimization to |
| 381 | * set this to false. |
| 382 | * |
| 383 | * @param enabled whether the wallpaper wants to receive offset notifications |
| 384 | */ |
| 385 | public void setOffsetNotificationsEnabled(boolean enabled) { |
| 386 | mWindowPrivateFlags = enabled |
| 387 | ? (mWindowPrivateFlags | |
| 388 | WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS) |
| 389 | : (mWindowPrivateFlags & |
| 390 | ~WindowManager.LayoutParams.PRIVATE_FLAG_WANTS_OFFSET_NOTIFICATIONS); |
| 391 | if (mCreated) { |
| 392 | updateSurface(false, false, false); |
| 393 | } |
| 394 | } |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 395 | |
| 396 | /** {@hide} */ |
| 397 | public void setFixedSizeAllowed(boolean allowed) { |
| 398 | mFixedSizeAllowed = allowed; |
| 399 | } |
| 400 | |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 401 | /** |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 402 | * Called once to initialize the engine. After returning, the |
| 403 | * engine's surface will be created by the framework. |
| 404 | */ |
| 405 | public void onCreate(SurfaceHolder surfaceHolder) { |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Called right before the engine is going away. After this the |
| 410 | * surface will be destroyed and this Engine object is no longer |
| 411 | * valid. |
| 412 | */ |
| 413 | public void onDestroy() { |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Called to inform you of the wallpaper becoming visible or |
| 418 | * hidden. <em>It is very important that a wallpaper only use |
| 419 | * CPU while it is visible.</em>. |
| 420 | */ |
| 421 | public void onVisibilityChanged(boolean visible) { |
| 422 | } |
| 423 | |
| 424 | /** |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 425 | * Called as the user performs touch-screen interaction with the |
| 426 | * window that is currently showing this wallpaper. Note that the |
| 427 | * events you receive here are driven by the actual application the |
Marco Nelissen | ae87bd0 | 2009-09-17 09:44:43 -0700 | [diff] [blame] | 428 | * user is interacting with, so if it is slow you will get fewer |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 429 | * move events. |
| 430 | */ |
| 431 | public void onTouchEvent(MotionEvent event) { |
| 432 | } |
| 433 | |
| 434 | /** |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 435 | * Called to inform you of the wallpaper's offsets changing |
| 436 | * within its contain, corresponding to the container's |
| 437 | * call to {@link WallpaperManager#setWallpaperOffsets(IBinder, float, float) |
| 438 | * WallpaperManager.setWallpaperOffsets()}. |
| 439 | */ |
| 440 | public void onOffsetsChanged(float xOffset, float yOffset, |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 441 | float xOffsetStep, float yOffsetStep, |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 442 | int xPixelOffset, int yPixelOffset) { |
| 443 | } |
| 444 | |
| 445 | /** |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 446 | * Process a command that was sent to the wallpaper with |
Dianne Hackborn | 13bf8260 | 2009-11-05 21:45:51 -0800 | [diff] [blame] | 447 | * {@link WallpaperManager#sendWallpaperCommand}. |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 448 | * The default implementation does nothing, and always returns null |
| 449 | * as the result. |
| 450 | * |
| 451 | * @param action The name of the command to perform. This tells you |
| 452 | * what to do and how to interpret the rest of the arguments. |
| 453 | * @param x Generic integer parameter. |
| 454 | * @param y Generic integer parameter. |
| 455 | * @param z Generic integer parameter. |
| 456 | * @param extras Any additional parameters. |
| 457 | * @param resultRequested If true, the caller is requesting that |
| 458 | * a result, appropriate for the command, be returned back. |
| 459 | * @return If returning a result, create a Bundle and place the |
| 460 | * result data in to it. Otherwise return null. |
| 461 | */ |
| 462 | public Bundle onCommand(String action, int x, int y, int z, |
| 463 | Bundle extras, boolean resultRequested) { |
| 464 | return null; |
| 465 | } |
| 466 | |
| 467 | /** |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 468 | * Called when an application has changed the desired virtual size of |
| 469 | * the wallpaper. |
| 470 | */ |
| 471 | public void onDesiredSizeChanged(int desiredWidth, int desiredHeight) { |
| 472 | } |
| 473 | |
| 474 | /** |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 475 | * Convenience for {@link SurfaceHolder.Callback#surfaceChanged |
| 476 | * SurfaceHolder.Callback.surfaceChanged()}. |
| 477 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 478 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
| 479 | } |
| 480 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 481 | /** |
Dianne Hackborn | 1d28f9c | 2010-07-13 20:38:06 -0700 | [diff] [blame] | 482 | * Convenience for {@link SurfaceHolder.Callback2#surfaceRedrawNeeded |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 483 | * SurfaceHolder.Callback.surfaceRedrawNeeded()}. |
| 484 | */ |
| 485 | public void onSurfaceRedrawNeeded(SurfaceHolder holder) { |
| 486 | } |
| 487 | |
| 488 | /** |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 489 | * Convenience for {@link SurfaceHolder.Callback#surfaceCreated |
| 490 | * SurfaceHolder.Callback.surfaceCreated()}. |
| 491 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 492 | public void onSurfaceCreated(SurfaceHolder holder) { |
| 493 | } |
| 494 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 495 | /** |
| 496 | * Convenience for {@link SurfaceHolder.Callback#surfaceDestroyed |
| 497 | * SurfaceHolder.Callback.surfaceDestroyed()}. |
| 498 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 499 | public void onSurfaceDestroyed(SurfaceHolder holder) { |
| 500 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 501 | |
Dianne Hackborn | 527de8e | 2011-08-22 16:10:36 -0700 | [diff] [blame] | 502 | protected void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) { |
| 503 | out.print(prefix); out.print("mInitializing="); out.print(mInitializing); |
| 504 | out.print(" mDestroyed="); out.println(mDestroyed); |
| 505 | out.print(prefix); out.print("mVisible="); out.print(mVisible); |
| 506 | out.print(" mScreenOn="); out.print(mScreenOn); |
| 507 | out.print(" mReportedVisible="); out.println(mReportedVisible); |
| 508 | out.print(prefix); out.print("mCreated="); out.print(mCreated); |
| 509 | out.print(" mSurfaceCreated="); out.print(mSurfaceCreated); |
| 510 | out.print(" mIsCreating="); out.print(mIsCreating); |
| 511 | out.print(" mDrawingAllowed="); out.println(mDrawingAllowed); |
| 512 | out.print(prefix); out.print("mWidth="); out.print(mWidth); |
| 513 | out.print(" mCurWidth="); out.print(mCurWidth); |
| 514 | out.print(" mHeight="); out.print(mHeight); |
| 515 | out.print(" mCurHeight="); out.println(mCurHeight); |
| 516 | out.print(prefix); out.print("mType="); out.print(mType); |
| 517 | out.print(" mWindowFlags="); out.print(mWindowFlags); |
| 518 | out.print(" mCurWindowFlags="); out.println(mCurWindowFlags); |
Chet Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 519 | out.print(" mWindowPrivateFlags="); out.print(mWindowPrivateFlags); |
| 520 | out.print(" mCurWindowPrivateFlags="); out.println(mCurWindowPrivateFlags); |
Dianne Hackborn | 527de8e | 2011-08-22 16:10:36 -0700 | [diff] [blame] | 521 | out.print(prefix); out.print("mVisibleInsets="); |
| 522 | out.print(mVisibleInsets.toShortString()); |
| 523 | out.print(" mWinFrame="); out.print(mWinFrame.toShortString()); |
| 524 | out.print(" mContentInsets="); out.println(mContentInsets.toShortString()); |
| 525 | out.print(prefix); out.print("mConfiguration="); out.println(mConfiguration); |
| 526 | out.print(prefix); out.print("mLayout="); out.println(mLayout); |
| 527 | synchronized (mLock) { |
| 528 | out.print(prefix); out.print("mPendingXOffset="); out.print(mPendingXOffset); |
| 529 | out.print(" mPendingXOffset="); out.println(mPendingXOffset); |
| 530 | out.print(prefix); out.print("mPendingXOffsetStep="); |
| 531 | out.print(mPendingXOffsetStep); |
| 532 | out.print(" mPendingXOffsetStep="); out.println(mPendingXOffsetStep); |
| 533 | out.print(prefix); out.print("mOffsetMessageEnqueued="); |
| 534 | out.print(mOffsetMessageEnqueued); |
| 535 | out.print(" mPendingSync="); out.println(mPendingSync); |
| 536 | if (mPendingMove != null) { |
| 537 | out.print(prefix); out.print("mPendingMove="); out.println(mPendingMove); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 542 | private void dispatchPointer(MotionEvent event) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 543 | if (event.isTouchEvent()) { |
| 544 | synchronized (mLock) { |
| 545 | if (event.getAction() == MotionEvent.ACTION_MOVE) { |
| 546 | mPendingMove = event; |
| 547 | } else { |
| 548 | mPendingMove = null; |
| 549 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 550 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 551 | Message msg = mCaller.obtainMessageO(MSG_TOUCH_EVENT, event); |
| 552 | mCaller.sendMessage(msg); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 553 | } else { |
| 554 | event.recycle(); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 557 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 558 | void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 559 | if (mDestroyed) { |
| 560 | Log.w(TAG, "Ignoring updateSurface: destroyed"); |
| 561 | } |
| 562 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 563 | int myWidth = mSurfaceHolder.getRequestedWidth(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 564 | if (myWidth <= 0) myWidth = ViewGroup.LayoutParams.MATCH_PARENT; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 565 | int myHeight = mSurfaceHolder.getRequestedHeight(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 566 | if (myHeight <= 0) myHeight = ViewGroup.LayoutParams.MATCH_PARENT; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 567 | |
| 568 | final boolean creating = !mCreated; |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 569 | final boolean surfaceCreating = !mSurfaceCreated; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 570 | final boolean formatChanged = mFormat != mSurfaceHolder.getRequestedFormat(); |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 571 | boolean sizeChanged = mWidth != myWidth || mHeight != myHeight; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 572 | final boolean typeChanged = mType != mSurfaceHolder.getRequestedType(); |
Chet Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 573 | final boolean flagsChanged = mCurWindowFlags != mWindowFlags || |
| 574 | mCurWindowPrivateFlags != mWindowPrivateFlags; |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 575 | if (forceRelayout || creating || surfaceCreating || formatChanged || sizeChanged |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 576 | || typeChanged || flagsChanged || redrawNeeded |
| 577 | || !mIWallpaperEngine.mShownReported) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 578 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 579 | if (DEBUG) Log.v(TAG, "Changes: creating=" + creating |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 580 | + " format=" + formatChanged + " size=" + sizeChanged); |
| 581 | |
| 582 | try { |
| 583 | mWidth = myWidth; |
| 584 | mHeight = myHeight; |
| 585 | mFormat = mSurfaceHolder.getRequestedFormat(); |
| 586 | mType = mSurfaceHolder.getRequestedType(); |
| 587 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 588 | mLayout.x = 0; |
| 589 | mLayout.y = 0; |
| 590 | mLayout.width = myWidth; |
| 591 | mLayout.height = myHeight; |
| 592 | |
| 593 | mLayout.format = mFormat; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 594 | |
| 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 Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 601 | mCurWindowPrivateFlags = mWindowPrivateFlags; |
| 602 | mLayout.privateFlags = mWindowPrivateFlags; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 603 | |
| 604 | mLayout.memoryType = mType; |
| 605 | mLayout.token = mWindowToken; |
| 606 | |
| 607 | if (!mCreated) { |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 608 | mLayout.type = mIWallpaperEngine.mWindowType; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 609 | mLayout.gravity = Gravity.START|Gravity.TOP; |
Dianne Hackborn | 0586a1b | 2009-09-06 21:08:27 -0700 | [diff] [blame] | 610 | mLayout.setTitle(WallpaperService.this.getClass().getName()); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 611 | mLayout.windowAnimations = |
| 612 | com.android.internal.R.style.Animation_Wallpaper; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 613 | mInputChannel = new InputChannel(); |
Craig Mautner | 6881a10 | 2012-07-27 13:04:51 -0700 | [diff] [blame] | 614 | if (mSession.addToDisplay(mWindow, mWindow.mSeq, mLayout, View.VISIBLE, |
| 615 | Display.DEFAULT_DISPLAY, mContentInsets, mInputChannel) < 0) { |
Mattias Petersson | d9463f5 | 2011-01-12 15:38:55 +0100 | [diff] [blame] | 616 | Log.w(TAG, "Failed to add window while updating wallpaper surface."); |
| 617 | return; |
| 618 | } |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 619 | mCreated = true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 620 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 621 | mInputEventReceiver = new WallpaperInputEventReceiver( |
| 622 | mInputChannel, Looper.myLooper()); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | mSurfaceHolder.mSurfaceLock.lock(); |
| 626 | mDrawingAllowed = true; |
| 627 | |
| 628 | final int relayoutResult = mSession.relayout( |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 629 | mWindow, mWindow.mSeq, mLayout, mWidth, mHeight, |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 630 | View.VISIBLE, 0, mWinFrame, mOverscanInsets, mContentInsets, |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 631 | mVisibleInsets, mConfiguration, mSurfaceHolder.mSurface); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 632 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 633 | if (DEBUG) Log.v(TAG, "New surface: " + mSurfaceHolder.mSurface |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 634 | + ", frame=" + mWinFrame); |
| 635 | |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 636 | 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 Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 646 | |
| 647 | mSurfaceHolder.setSurfaceFrameSize(w, h); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 648 | mSurfaceHolder.mSurfaceLock.unlock(); |
| 649 | |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 650 | if (!mSurfaceHolder.mSurface.isValid()) { |
| 651 | reportSurfaceDestroyed(); |
| 652 | if (DEBUG) Log.v(TAG, "Layout: Surface destroyed"); |
| 653 | return; |
| 654 | } |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 655 | |
| 656 | boolean didSurface = false; |
| 657 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 658 | try { |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 659 | mSurfaceHolder.ungetCallbacks(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 660 | |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 661 | if (surfaceCreating) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 662 | mIsCreating = true; |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 663 | didSurface = true; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 664 | if (DEBUG) Log.v(TAG, "onSurfaceCreated(" |
| 665 | + mSurfaceHolder + "): " + this); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 666 | onSurfaceCreated(mSurfaceHolder); |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 667 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 668 | if (callbacks != null) { |
| 669 | for (SurfaceHolder.Callback c : callbacks) { |
| 670 | c.surfaceCreated(mSurfaceHolder); |
| 671 | } |
| 672 | } |
| 673 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 674 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 675 | redrawNeeded |= creating || (relayoutResult |
| 676 | & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 677 | |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 678 | if (forceReport || creating || surfaceCreating |
| 679 | || formatChanged || sizeChanged) { |
Dianne Hackborn | cbf1504 | 2009-08-18 18:29:09 -0700 | [diff] [blame] | 680 | 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 Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 687 | if (DEBUG) Log.v(TAG, "onSurfaceChanged(" |
| 688 | + mSurfaceHolder + ", " + mFormat |
| 689 | + ", " + mCurWidth + ", " + mCurHeight |
| 690 | + "): " + this); |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 691 | didSurface = true; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 692 | onSurfaceChanged(mSurfaceHolder, mFormat, |
| 693 | mCurWidth, mCurHeight); |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 694 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 695 | if (callbacks != null) { |
| 696 | for (SurfaceHolder.Callback c : callbacks) { |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 697 | c.surfaceChanged(mSurfaceHolder, mFormat, |
| 698 | mCurWidth, mCurHeight); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 702 | |
| 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 Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 716 | 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 Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 736 | } finally { |
| 737 | mIsCreating = false; |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 738 | mSurfaceCreated = true; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 739 | if (redrawNeeded) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 740 | mSession.finishDrawing(mWindow); |
| 741 | } |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 742 | mIWallpaperEngine.reportShown(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 743 | } |
| 744 | } catch (RemoteException ex) { |
| 745 | } |
| 746 | if (DEBUG) Log.v( |
| 747 | TAG, "Layout: x=" + mLayout.x + " y=" + mLayout.y + |
| 748 | " w=" + mLayout.width + " h=" + mLayout.height); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | void attach(IWallpaperEngineWrapper wrapper) { |
Dianne Hackborn | cbf1504 | 2009-08-18 18:29:09 -0700 | [diff] [blame] | 753 | if (DEBUG) Log.v(TAG, "attach: " + this + " wrapper=" + wrapper); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 754 | if (mDestroyed) { |
| 755 | return; |
| 756 | } |
| 757 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 758 | mIWallpaperEngine = wrapper; |
| 759 | mCaller = wrapper.mCaller; |
| 760 | mConnection = wrapper.mConnection; |
| 761 | mWindowToken = wrapper.mWindowToken; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 762 | mSurfaceHolder.setSizeFromLayout(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 763 | mInitializing = true; |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 764 | mSession = WindowManagerGlobal.getWindowSession(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 765 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 766 | mWindow.setSession(mSession); |
Dianne Hackborn | 9fe6cb5 | 2011-09-09 13:02:43 -0700 | [diff] [blame] | 767 | |
| 768 | mScreenOn = ((PowerManager)getSystemService(Context.POWER_SERVICE)).isScreenOn(); |
| 769 | |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 770 | IntentFilter filter = new IntentFilter(); |
| 771 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 772 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 773 | registerReceiver(mReceiver, filter); |
| 774 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 775 | if (DEBUG) Log.v(TAG, "onCreate(): " + this); |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 776 | onCreate(mSurfaceHolder); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 777 | |
| 778 | mInitializing = false; |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 779 | mReportedVisible = false; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 780 | updateSurface(false, false, false); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 783 | void doDesiredSizeChanged(int desiredWidth, int desiredHeight) { |
| 784 | if (!mDestroyed) { |
| 785 | if (DEBUG) Log.v(TAG, "onDesiredSizeChanged(" |
| 786 | + desiredWidth + "," + desiredHeight + "): " + this); |
Joe Onorato | dcfae5c | 2010-10-28 18:03:23 -0700 | [diff] [blame] | 787 | mIWallpaperEngine.mReqWidth = desiredWidth; |
| 788 | mIWallpaperEngine.mReqHeight = desiredHeight; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 789 | onDesiredSizeChanged(desiredWidth, desiredHeight); |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 790 | doOffsetsChanged(true); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | void doVisibilityChanged(boolean visible) { |
Dianne Hackborn | af1f42b | 2009-11-20 16:27:27 -0800 | [diff] [blame] | 795 | if (!mDestroyed) { |
| 796 | mVisible = visible; |
| 797 | reportVisibility(); |
| 798 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void reportVisibility() { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 802 | if (!mDestroyed) { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 803 | boolean visible = mVisible && mScreenOn; |
| 804 | if (mReportedVisible != visible) { |
| 805 | mReportedVisible = visible; |
| 806 | if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible |
| 807 | + "): " + this); |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 808 | if (visible) { |
| 809 | // If becoming visible, in preview mode the surface |
| 810 | // may have been destroyed so now we need to make |
| 811 | // sure it is re-created. |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 812 | doOffsetsChanged(false); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 813 | updateSurface(false, false, false); |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 814 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 815 | onVisibilityChanged(visible); |
| 816 | } |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 820 | void doOffsetsChanged(boolean always) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 821 | if (mDestroyed) { |
| 822 | return; |
| 823 | } |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 824 | |
| 825 | if (!always && !mOffsetsChanged) { |
| 826 | return; |
| 827 | } |
| 828 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 829 | float xOffset; |
| 830 | float yOffset; |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 831 | float xOffsetStep; |
| 832 | float yOffsetStep; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 833 | boolean sync; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 834 | synchronized (mLock) { |
| 835 | xOffset = mPendingXOffset; |
| 836 | yOffset = mPendingYOffset; |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 837 | xOffsetStep = mPendingXOffsetStep; |
| 838 | yOffsetStep = mPendingYOffsetStep; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 839 | sync = mPendingSync; |
| 840 | mPendingSync = false; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 841 | mOffsetMessageEnqueued = false; |
| 842 | } |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 843 | |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 844 | if (mSurfaceCreated) { |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 845 | if (mReportedVisible) { |
| 846 | if (DEBUG) Log.v(TAG, "Offsets change in " + this |
| 847 | + ": " + xOffset + "," + yOffset); |
| 848 | final int availw = mIWallpaperEngine.mReqWidth-mCurWidth; |
| 849 | final int xPixels = availw > 0 ? -(int)(availw*xOffset+.5f) : 0; |
| 850 | final int availh = mIWallpaperEngine.mReqHeight-mCurHeight; |
| 851 | final int yPixels = availh > 0 ? -(int)(availh*yOffset+.5f) : 0; |
| 852 | onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixels, yPixels); |
| 853 | } else { |
| 854 | mOffsetsChanged = true; |
| 855 | } |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 856 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 857 | |
| 858 | if (sync) { |
| 859 | try { |
| 860 | if (DEBUG) Log.v(TAG, "Reporting offsets change complete"); |
| 861 | mSession.wallpaperOffsetsComplete(mWindow.asBinder()); |
| 862 | } catch (RemoteException e) { |
| 863 | } |
| 864 | } |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 867 | void doCommand(WallpaperCommand cmd) { |
| 868 | Bundle result; |
| 869 | if (!mDestroyed) { |
| 870 | result = onCommand(cmd.action, cmd.x, cmd.y, cmd.z, |
| 871 | cmd.extras, cmd.sync); |
| 872 | } else { |
| 873 | result = null; |
| 874 | } |
| 875 | if (cmd.sync) { |
| 876 | try { |
| 877 | if (DEBUG) Log.v(TAG, "Reporting command complete"); |
| 878 | mSession.wallpaperCommandComplete(mWindow.asBinder(), result); |
| 879 | } catch (RemoteException e) { |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 884 | void reportSurfaceDestroyed() { |
| 885 | if (mSurfaceCreated) { |
| 886 | mSurfaceCreated = false; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 887 | mSurfaceHolder.ungetCallbacks(); |
| 888 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
| 889 | if (callbacks != null) { |
| 890 | for (SurfaceHolder.Callback c : callbacks) { |
| 891 | c.surfaceDestroyed(mSurfaceHolder); |
| 892 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 893 | } |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 894 | if (DEBUG) Log.v(TAG, "onSurfaceDestroyed(" |
| 895 | + mSurfaceHolder + "): " + this); |
| 896 | onSurfaceDestroyed(mSurfaceHolder); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 897 | } |
Dianne Hackborn | 18ee31e | 2010-04-27 15:54:02 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void detach() { |
| 901 | if (mDestroyed) { |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | mDestroyed = true; |
| 906 | |
| 907 | if (mVisible) { |
| 908 | mVisible = false; |
| 909 | if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this); |
| 910 | onVisibilityChanged(false); |
| 911 | } |
| 912 | |
| 913 | reportSurfaceDestroyed(); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 914 | |
| 915 | if (DEBUG) Log.v(TAG, "onDestroy(): " + this); |
| 916 | onDestroy(); |
| 917 | |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 918 | unregisterReceiver(mReceiver); |
| 919 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 920 | if (mCreated) { |
| 921 | try { |
Dianne Hackborn | ba3e31d | 2010-04-22 18:59:03 -0700 | [diff] [blame] | 922 | if (DEBUG) Log.v(TAG, "Removing window and destroying surface " |
| 923 | + mSurfaceHolder.getSurface() + " of: " + this); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 924 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 925 | if (mInputEventReceiver != null) { |
| 926 | mInputEventReceiver.dispose(); |
| 927 | mInputEventReceiver = null; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 930 | mSession.remove(mWindow); |
| 931 | } catch (RemoteException e) { |
| 932 | } |
Dianne Hackborn | 0586a1b | 2009-09-06 21:08:27 -0700 | [diff] [blame] | 933 | mSurfaceHolder.mSurface.release(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 934 | mCreated = false; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 935 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 936 | // Dispose the input channel after removing the window so the Window Manager |
| 937 | // doesn't interpret the input channel being closed as an abnormal termination. |
| 938 | if (mInputChannel != null) { |
| 939 | mInputChannel.dispose(); |
| 940 | mInputChannel = null; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 941 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | class IWallpaperEngineWrapper extends IWallpaperEngine.Stub |
| 947 | implements HandlerCaller.Callback { |
| 948 | private final HandlerCaller mCaller; |
| 949 | |
| 950 | final IWallpaperConnection mConnection; |
| 951 | final IBinder mWindowToken; |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 952 | final int mWindowType; |
| 953 | final boolean mIsPreview; |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 954 | boolean mShownReported; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 955 | int mReqWidth; |
| 956 | int mReqHeight; |
| 957 | |
| 958 | Engine mEngine; |
| 959 | |
| 960 | IWallpaperEngineWrapper(WallpaperService context, |
| 961 | IWallpaperConnection conn, IBinder windowToken, |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 962 | int windowType, boolean isPreview, int reqWidth, int reqHeight) { |
Mita Yun | aa8dc2e | 2012-12-10 18:32:03 -0800 | [diff] [blame] | 963 | mCaller = new HandlerCaller(context, context.getMainLooper(), this, true); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 964 | mConnection = conn; |
| 965 | mWindowToken = windowToken; |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 966 | mWindowType = windowType; |
| 967 | mIsPreview = isPreview; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 968 | mReqWidth = reqWidth; |
| 969 | mReqHeight = reqHeight; |
| 970 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 971 | Message msg = mCaller.obtainMessage(DO_ATTACH); |
| 972 | mCaller.sendMessage(msg); |
| 973 | } |
| 974 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 975 | public void setDesiredSize(int width, int height) { |
| 976 | Message msg = mCaller.obtainMessageII(DO_SET_DESIRED_SIZE, width, height); |
| 977 | mCaller.sendMessage(msg); |
| 978 | } |
| 979 | |
| 980 | public void setVisibility(boolean visible) { |
| 981 | Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED, |
| 982 | visible ? 1 : 0); |
| 983 | mCaller.sendMessage(msg); |
| 984 | } |
| 985 | |
Dianne Hackborn | 6adba24 | 2009-11-10 11:10:09 -0800 | [diff] [blame] | 986 | public void dispatchPointer(MotionEvent event) { |
| 987 | if (mEngine != null) { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 988 | mEngine.dispatchPointer(event); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 989 | } else { |
| 990 | event.recycle(); |
Dianne Hackborn | 6adba24 | 2009-11-10 11:10:09 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
Jeff Brown | 9f3bdfe | 2010-10-13 06:01:27 -0700 | [diff] [blame] | 993 | |
| 994 | public void dispatchWallpaperCommand(String action, int x, int y, |
| 995 | int z, Bundle extras) { |
| 996 | if (mEngine != null) { |
| 997 | mEngine.mWindow.dispatchWallpaperCommand(action, x, y, z, extras, false); |
| 998 | } |
| 999 | } |
| 1000 | |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 1001 | public void reportShown() { |
| 1002 | if (!mShownReported) { |
| 1003 | mShownReported = true; |
| 1004 | try { |
| 1005 | mConnection.engineShown(this); |
| 1006 | } catch (RemoteException e) { |
| 1007 | Log.w(TAG, "Wallpaper host disappeared", e); |
| 1008 | return; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1013 | public void destroy() { |
| 1014 | Message msg = mCaller.obtainMessage(DO_DETACH); |
| 1015 | mCaller.sendMessage(msg); |
| 1016 | } |
| 1017 | |
| 1018 | public void executeMessage(Message message) { |
| 1019 | switch (message.what) { |
| 1020 | case DO_ATTACH: { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1021 | try { |
| 1022 | mConnection.attachEngine(this); |
| 1023 | } catch (RemoteException e) { |
| 1024 | Log.w(TAG, "Wallpaper host disappeared", e); |
| 1025 | return; |
| 1026 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1027 | Engine engine = onCreateEngine(); |
| 1028 | mEngine = engine; |
Dianne Hackborn | af1f42b | 2009-11-20 16:27:27 -0800 | [diff] [blame] | 1029 | mActiveEngines.add(engine); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1030 | engine.attach(this); |
| 1031 | return; |
| 1032 | } |
| 1033 | case DO_DETACH: { |
Dianne Hackborn | af1f42b | 2009-11-20 16:27:27 -0800 | [diff] [blame] | 1034 | mActiveEngines.remove(mEngine); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1035 | mEngine.detach(); |
| 1036 | return; |
| 1037 | } |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1038 | case DO_SET_DESIRED_SIZE: { |
| 1039 | mEngine.doDesiredSizeChanged(message.arg1, message.arg2); |
| 1040 | return; |
| 1041 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1042 | case MSG_UPDATE_SURFACE: |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1043 | mEngine.updateSurface(true, false, false); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1044 | break; |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 1045 | case MSG_VISIBILITY_CHANGED: |
| 1046 | if (DEBUG) Log.v(TAG, "Visibility change in " + mEngine |
| 1047 | + ": " + message.arg1); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1048 | mEngine.doVisibilityChanged(message.arg1 != 0); |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 1049 | break; |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1050 | case MSG_WALLPAPER_OFFSETS: { |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 1051 | mEngine.doOffsetsChanged(true); |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1052 | } break; |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1053 | case MSG_WALLPAPER_COMMAND: { |
| 1054 | WallpaperCommand cmd = (WallpaperCommand)message.obj; |
| 1055 | mEngine.doCommand(cmd); |
| 1056 | } break; |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 1057 | case MSG_WINDOW_RESIZED: { |
| 1058 | final boolean reportDraw = message.arg1 != 0; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1059 | mEngine.updateSurface(true, false, reportDraw); |
Dianne Hackborn | 9e4e727 | 2011-08-30 14:06:51 -0700 | [diff] [blame] | 1060 | mEngine.doOffsetsChanged(true); |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 1061 | } break; |
Craig Mautner | 5702d4d | 2012-06-30 14:10:16 -0700 | [diff] [blame] | 1062 | case MSG_WINDOW_MOVED: { |
| 1063 | // Do nothing. What does it mean for a Wallpaper to move? |
| 1064 | } break; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 1065 | case MSG_TOUCH_EVENT: { |
Jeff Brown | 840db1f | 2010-10-21 17:36:54 -0700 | [diff] [blame] | 1066 | boolean skip = false; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 1067 | MotionEvent ev = (MotionEvent)message.obj; |
Jeff Brown | 840db1f | 2010-10-21 17:36:54 -0700 | [diff] [blame] | 1068 | if (ev.getAction() == MotionEvent.ACTION_MOVE) { |
| 1069 | synchronized (mEngine.mLock) { |
| 1070 | if (mEngine.mPendingMove == ev) { |
| 1071 | mEngine.mPendingMove = null; |
| 1072 | } else { |
| 1073 | // this is not the motion event we are looking for.... |
| 1074 | skip = true; |
| 1075 | } |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
Jeff Brown | 840db1f | 2010-10-21 17:36:54 -0700 | [diff] [blame] | 1078 | if (!skip) { |
| 1079 | if (DEBUG) Log.v(TAG, "Delivering touch event: " + ev); |
| 1080 | mEngine.onTouchEvent(ev); |
| 1081 | } |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 1082 | ev.recycle(); |
| 1083 | } break; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1084 | default : |
| 1085 | Log.w(TAG, "Unknown message type " + message.what); |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * Implements the internal {@link IWallpaperService} interface to convert |
| 1092 | * incoming calls to it back to calls on an {@link WallpaperService}. |
| 1093 | */ |
| 1094 | class IWallpaperServiceWrapper extends IWallpaperService.Stub { |
| 1095 | private final WallpaperService mTarget; |
| 1096 | |
| 1097 | public IWallpaperServiceWrapper(WallpaperService context) { |
| 1098 | mTarget = context; |
| 1099 | } |
| 1100 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 1101 | @Override |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 1102 | public void attach(IWallpaperConnection conn, IBinder windowToken, |
| 1103 | int windowType, boolean isPreview, int reqWidth, int reqHeight) { |
| 1104 | new IWallpaperEngineWrapper(mTarget, conn, windowToken, |
| 1105 | windowType, isPreview, reqWidth, reqHeight); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 1108 | |
Dianne Hackborn | af1f42b | 2009-11-20 16:27:27 -0800 | [diff] [blame] | 1109 | @Override |
| 1110 | public void onCreate() { |
| 1111 | super.onCreate(); |
| 1112 | } |
| 1113 | |
| 1114 | @Override |
| 1115 | public void onDestroy() { |
| 1116 | super.onDestroy(); |
| 1117 | for (int i=0; i<mActiveEngines.size(); i++) { |
| 1118 | mActiveEngines.get(i).detach(); |
| 1119 | } |
| 1120 | mActiveEngines.clear(); |
| 1121 | } |
| 1122 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1123 | /** |
| 1124 | * Implement to return the implementation of the internal accessibility |
| 1125 | * service interface. Subclasses should not override. |
| 1126 | */ |
| 1127 | @Override |
| 1128 | public final IBinder onBind(Intent intent) { |
| 1129 | return new IWallpaperServiceWrapper(this); |
| 1130 | } |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 1131 | |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 1132 | /** |
| 1133 | * Must be implemented to return a new instance of the wallpaper's engine. |
| 1134 | * Note that multiple instances may be active at the same time, such as |
| 1135 | * when the wallpaper is currently set as the active wallpaper and the user |
| 1136 | * is in the wallpaper picker viewing a preview of it as well. |
| 1137 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1138 | public abstract Engine onCreateEngine(); |
Dianne Hackborn | 527de8e | 2011-08-22 16:10:36 -0700 | [diff] [blame] | 1139 | |
| 1140 | @Override |
| 1141 | protected void dump(FileDescriptor fd, PrintWriter out, String[] args) { |
| 1142 | out.print("State of wallpaper "); out.print(this); out.println(":"); |
| 1143 | for (int i=0; i<mActiveEngines.size(); i++) { |
| 1144 | Engine engine = mActiveEngines.get(i); |
| 1145 | out.print(" Engine "); out.print(engine); out.println(":"); |
| 1146 | engine.dump(" ", fd, out, args); |
| 1147 | } |
| 1148 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1149 | } |