WallpaperService.java revision 9f3bdfe3eb04723efa07a2d0e6a7709da1191fa5
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 17package android.service.wallpaper; 18 19import com.android.internal.os.HandlerCaller; 20import com.android.internal.view.BaseIWindow; 21import com.android.internal.view.BaseInputHandler; 22import com.android.internal.view.BaseSurfaceHolder; 23 24import android.annotation.SdkConstant; 25import android.annotation.SdkConstant.SdkConstantType; 26import android.app.Service; 27import android.app.WallpaperManager; 28import android.content.BroadcastReceiver; 29import android.content.Context; 30import android.content.Intent; 31import android.content.IntentFilter; 32import android.content.res.Configuration; 33import android.graphics.PixelFormat; 34import android.graphics.Rect; 35import android.os.Bundle; 36import android.os.IBinder; 37import android.os.Looper; 38import android.os.Message; 39import android.os.RemoteException; 40import android.util.Log; 41import android.util.LogPrinter; 42import android.view.Gravity; 43import android.view.IWindowSession; 44import android.view.InputChannel; 45import android.view.InputDevice; 46import android.view.InputHandler; 47import android.view.InputQueue; 48import android.view.KeyEvent; 49import android.view.MotionEvent; 50import android.view.SurfaceHolder; 51import android.view.View; 52import android.view.ViewGroup; 53import android.view.ViewRoot; 54import android.view.WindowManager; 55import android.view.WindowManagerImpl; 56import android.view.WindowManagerPolicy; 57 58import java.util.ArrayList; 59 60/** 61 * A wallpaper service is responsible for showing a live wallpaper behind 62 * applications that would like to sit on top of it. This service object 63 * itself does very little -- its only purpose is to generate instances of 64 * {@link Engine} as needed. Implementing a wallpaper thus 65 * involves subclassing from this, subclassing an Engine implementation, 66 * and implementing {@link #onCreateEngine()} to return a new instance of 67 * your engine. 68 */ 69public abstract class WallpaperService extends Service { 70 /** 71 * The {@link Intent} that must be declared as handled by the service. 72 * To be supported, the service must also require the 73 * {@link android.Manifest.permission#BIND_WALLPAPER} permission so 74 * that other applications can not abuse it. 75 */ 76 @SdkConstant(SdkConstantType.SERVICE_ACTION) 77 public static final String SERVICE_INTERFACE = 78 "android.service.wallpaper.WallpaperService"; 79 80 /** 81 * Name under which a WallpaperService component publishes information 82 * about itself. This meta-data must reference an XML resource containing 83 * a <code><{@link android.R.styleable#Wallpaper wallpaper}></code> 84 * tag. 85 */ 86 public static final String SERVICE_META_DATA = "android.service.wallpaper"; 87 88 static final String TAG = "WallpaperService"; 89 static final boolean DEBUG = false; 90 91 private static final int DO_ATTACH = 10; 92 private static final int DO_DETACH = 20; 93 private static final int DO_SET_DESIRED_SIZE = 30; 94 95 private static final int MSG_UPDATE_SURFACE = 10000; 96 private static final int MSG_VISIBILITY_CHANGED = 10010; 97 private static final int MSG_WALLPAPER_OFFSETS = 10020; 98 private static final int MSG_WALLPAPER_COMMAND = 10025; 99 private static final int MSG_WINDOW_RESIZED = 10030; 100 private static final int MSG_TOUCH_EVENT = 10040; 101 102 private Looper mCallbackLooper; 103 private final ArrayList<Engine> mActiveEngines 104 = new ArrayList<Engine>(); 105 106 static final class WallpaperCommand { 107 String action; 108 int x; 109 int y; 110 int z; 111 Bundle extras; 112 boolean sync; 113 } 114 115 /** 116 * The actual implementation of a wallpaper. A wallpaper service may 117 * have multiple instances running (for example as a real wallpaper 118 * and as a preview), each of which is represented by its own Engine 119 * instance. You must implement {@link WallpaperService#onCreateEngine()} 120 * to return your concrete Engine implementation. 121 */ 122 public class Engine { 123 IWallpaperEngineWrapper mIWallpaperEngine; 124 125 // Copies from mIWallpaperEngine. 126 HandlerCaller mCaller; 127 IWallpaperConnection mConnection; 128 IBinder mWindowToken; 129 130 boolean mInitializing = true; 131 boolean mVisible; 132 boolean mScreenOn = true; 133 boolean mReportedVisible; 134 boolean mDestroyed; 135 136 // Current window state. 137 boolean mCreated; 138 boolean mSurfaceCreated; 139 boolean mIsCreating; 140 boolean mDrawingAllowed; 141 int mWidth; 142 int mHeight; 143 int mFormat; 144 int mType; 145 int mCurWidth; 146 int mCurHeight; 147 int mWindowFlags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; 148 int mCurWindowFlags = mWindowFlags; 149 final Rect mVisibleInsets = new Rect(); 150 final Rect mWinFrame = new Rect(); 151 final Rect mContentInsets = new Rect(); 152 final Configuration mConfiguration = new Configuration(); 153 154 final WindowManager.LayoutParams mLayout 155 = new WindowManager.LayoutParams(); 156 IWindowSession mSession; 157 InputChannel mInputChannel; 158 159 final Object mLock = new Object(); 160 boolean mOffsetMessageEnqueued; 161 float mPendingXOffset; 162 float mPendingYOffset; 163 float mPendingXOffsetStep; 164 float mPendingYOffsetStep; 165 boolean mPendingSync; 166 MotionEvent mPendingMove; 167 168 final BroadcastReceiver mReceiver = new BroadcastReceiver() { 169 @Override 170 public void onReceive(Context context, Intent intent) { 171 if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { 172 mScreenOn = true; 173 reportVisibility(); 174 } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { 175 mScreenOn = false; 176 reportVisibility(); 177 } 178 } 179 }; 180 181 final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() { 182 { 183 mRequestedFormat = PixelFormat.RGB_565; 184 } 185 186 @Override 187 public boolean onAllowLockCanvas() { 188 return mDrawingAllowed; 189 } 190 191 @Override 192 public void onRelayoutContainer() { 193 Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE); 194 mCaller.sendMessage(msg); 195 } 196 197 @Override 198 public void onUpdateSurface() { 199 Message msg = mCaller.obtainMessage(MSG_UPDATE_SURFACE); 200 mCaller.sendMessage(msg); 201 } 202 203 public boolean isCreating() { 204 return mIsCreating; 205 } 206 207 @Override 208 public void setFixedSize(int width, int height) { 209 throw new UnsupportedOperationException( 210 "Wallpapers currently only support sizing from layout"); 211 } 212 213 public void setKeepScreenOn(boolean screenOn) { 214 throw new UnsupportedOperationException( 215 "Wallpapers do not support keep screen on"); 216 } 217 218 }; 219 220 final InputHandler mInputHandler = new BaseInputHandler() { 221 @Override 222 public void handleMotion(MotionEvent event, 223 InputQueue.FinishedCallback finishedCallback) { 224 boolean handled = false; 225 try { 226 int source = event.getSource(); 227 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) { 228 dispatchPointer(event); 229 handled = true; 230 } 231 } finally { 232 finishedCallback.finished(handled); 233 } 234 } 235 }; 236 237 final BaseIWindow mWindow = new BaseIWindow() { 238 @Override 239 public void resized(int w, int h, Rect coveredInsets, 240 Rect visibleInsets, boolean reportDraw, Configuration newConfig) { 241 Message msg = mCaller.obtainMessageI(MSG_WINDOW_RESIZED, 242 reportDraw ? 1 : 0); 243 mCaller.sendMessage(msg); 244 } 245 246 @Override 247 public void dispatchAppVisibility(boolean visible) { 248 // We don't do this in preview mode; we'll let the preview 249 // activity tell us when to run. 250 if (!mIWallpaperEngine.mIsPreview) { 251 Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED, 252 visible ? 1 : 0); 253 mCaller.sendMessage(msg); 254 } 255 } 256 257 @Override 258 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep, 259 boolean sync) { 260 synchronized (mLock) { 261 if (DEBUG) Log.v(TAG, "Dispatch wallpaper offsets: " + x + ", " + y); 262 mPendingXOffset = x; 263 mPendingYOffset = y; 264 mPendingXOffsetStep = xStep; 265 mPendingYOffsetStep = yStep; 266 if (sync) { 267 mPendingSync = true; 268 } 269 if (!mOffsetMessageEnqueued) { 270 mOffsetMessageEnqueued = true; 271 Message msg = mCaller.obtainMessage(MSG_WALLPAPER_OFFSETS); 272 mCaller.sendMessage(msg); 273 } 274 } 275 } 276 277 public void dispatchWallpaperCommand(String action, int x, int y, 278 int z, Bundle extras, boolean sync) { 279 synchronized (mLock) { 280 if (DEBUG) Log.v(TAG, "Dispatch wallpaper command: " + x + ", " + y); 281 WallpaperCommand cmd = new WallpaperCommand(); 282 cmd.action = action; 283 cmd.x = x; 284 cmd.y = y; 285 cmd.z = z; 286 cmd.extras = extras; 287 cmd.sync = sync; 288 Message msg = mCaller.obtainMessage(MSG_WALLPAPER_COMMAND); 289 msg.obj = cmd; 290 mCaller.sendMessage(msg); 291 } 292 } 293 }; 294 295 /** 296 * Provides access to the surface in which this wallpaper is drawn. 297 */ 298 public SurfaceHolder getSurfaceHolder() { 299 return mSurfaceHolder; 300 } 301 302 /** 303 * Convenience for {@link WallpaperManager#getDesiredMinimumWidth() 304 * WallpaperManager.getDesiredMinimumWidth()}, returning the width 305 * that the system would like this wallpaper to run in. 306 */ 307 public int getDesiredMinimumWidth() { 308 return mIWallpaperEngine.mReqWidth; 309 } 310 311 /** 312 * Convenience for {@link WallpaperManager#getDesiredMinimumHeight() 313 * WallpaperManager.getDesiredMinimumHeight()}, returning the height 314 * that the system would like this wallpaper to run in. 315 */ 316 public int getDesiredMinimumHeight() { 317 return mIWallpaperEngine.mReqHeight; 318 } 319 320 /** 321 * Return whether the wallpaper is currently visible to the user, 322 * this is the last value supplied to 323 * {@link #onVisibilityChanged(boolean)}. 324 */ 325 public boolean isVisible() { 326 return mReportedVisible; 327 } 328 329 /** 330 * Returns true if this engine is running in preview mode -- that is, 331 * it is being shown to the user before they select it as the actual 332 * wallpaper. 333 */ 334 public boolean isPreview() { 335 return mIWallpaperEngine.mIsPreview; 336 } 337 338 /** 339 * Control whether this wallpaper will receive raw touch events 340 * from the window manager as the user interacts with the window 341 * that is currently displaying the wallpaper. By default they 342 * are turned off. If enabled, the events will be received in 343 * {@link #onTouchEvent(MotionEvent)}. 344 */ 345 public void setTouchEventsEnabled(boolean enabled) { 346 mWindowFlags = enabled 347 ? (mWindowFlags&~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) 348 : (mWindowFlags|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 349 if (mCreated) { 350 updateSurface(false, false, false); 351 } 352 } 353 354 /** 355 * Called once to initialize the engine. After returning, the 356 * engine's surface will be created by the framework. 357 */ 358 public void onCreate(SurfaceHolder surfaceHolder) { 359 } 360 361 /** 362 * Called right before the engine is going away. After this the 363 * surface will be destroyed and this Engine object is no longer 364 * valid. 365 */ 366 public void onDestroy() { 367 } 368 369 /** 370 * Called to inform you of the wallpaper becoming visible or 371 * hidden. <em>It is very important that a wallpaper only use 372 * CPU while it is visible.</em>. 373 */ 374 public void onVisibilityChanged(boolean visible) { 375 } 376 377 /** 378 * Called as the user performs touch-screen interaction with the 379 * window that is currently showing this wallpaper. Note that the 380 * events you receive here are driven by the actual application the 381 * user is interacting with, so if it is slow you will get fewer 382 * move events. 383 */ 384 public void onTouchEvent(MotionEvent event) { 385 } 386 387 /** 388 * Called to inform you of the wallpaper's offsets changing 389 * within its contain, corresponding to the container's 390 * call to {@link WallpaperManager#setWallpaperOffsets(IBinder, float, float) 391 * WallpaperManager.setWallpaperOffsets()}. 392 */ 393 public void onOffsetsChanged(float xOffset, float yOffset, 394 float xOffsetStep, float yOffsetStep, 395 int xPixelOffset, int yPixelOffset) { 396 } 397 398 /** 399 * Process a command that was sent to the wallpaper with 400 * {@link WallpaperManager#sendWallpaperCommand}. 401 * The default implementation does nothing, and always returns null 402 * as the result. 403 * 404 * @param action The name of the command to perform. This tells you 405 * what to do and how to interpret the rest of the arguments. 406 * @param x Generic integer parameter. 407 * @param y Generic integer parameter. 408 * @param z Generic integer parameter. 409 * @param extras Any additional parameters. 410 * @param resultRequested If true, the caller is requesting that 411 * a result, appropriate for the command, be returned back. 412 * @return If returning a result, create a Bundle and place the 413 * result data in to it. Otherwise return null. 414 */ 415 public Bundle onCommand(String action, int x, int y, int z, 416 Bundle extras, boolean resultRequested) { 417 return null; 418 } 419 420 /** 421 * Called when an application has changed the desired virtual size of 422 * the wallpaper. 423 */ 424 public void onDesiredSizeChanged(int desiredWidth, int desiredHeight) { 425 } 426 427 /** 428 * Convenience for {@link SurfaceHolder.Callback#surfaceChanged 429 * SurfaceHolder.Callback.surfaceChanged()}. 430 */ 431 public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { 432 } 433 434 /** 435 * Convenience for {@link SurfaceHolder.Callback2#surfaceRedrawNeeded 436 * SurfaceHolder.Callback.surfaceRedrawNeeded()}. 437 */ 438 public void onSurfaceRedrawNeeded(SurfaceHolder holder) { 439 } 440 441 /** 442 * Convenience for {@link SurfaceHolder.Callback#surfaceCreated 443 * SurfaceHolder.Callback.surfaceCreated()}. 444 */ 445 public void onSurfaceCreated(SurfaceHolder holder) { 446 } 447 448 /** 449 * Convenience for {@link SurfaceHolder.Callback#surfaceDestroyed 450 * SurfaceHolder.Callback.surfaceDestroyed()}. 451 */ 452 public void onSurfaceDestroyed(SurfaceHolder holder) { 453 } 454 455 private void dispatchPointer(MotionEvent event) { 456 synchronized (mLock) { 457 if (event.getAction() == MotionEvent.ACTION_MOVE) { 458 mPendingMove = event; 459 } else { 460 mPendingMove = null; 461 } 462 } 463 464 Message msg = mCaller.obtainMessageO(MSG_TOUCH_EVENT, event); 465 mCaller.sendMessage(msg); 466 } 467 468 void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) { 469 if (mDestroyed) { 470 Log.w(TAG, "Ignoring updateSurface: destroyed"); 471 } 472 473 int myWidth = mSurfaceHolder.getRequestedWidth(); 474 if (myWidth <= 0) myWidth = ViewGroup.LayoutParams.MATCH_PARENT; 475 int myHeight = mSurfaceHolder.getRequestedHeight(); 476 if (myHeight <= 0) myHeight = ViewGroup.LayoutParams.MATCH_PARENT; 477 478 final boolean creating = !mCreated; 479 final boolean surfaceCreating = !mSurfaceCreated; 480 final boolean formatChanged = mFormat != mSurfaceHolder.getRequestedFormat(); 481 boolean sizeChanged = mWidth != myWidth || mHeight != myHeight; 482 final boolean typeChanged = mType != mSurfaceHolder.getRequestedType(); 483 final boolean flagsChanged = mCurWindowFlags != mWindowFlags; 484 if (forceRelayout || creating || surfaceCreating || formatChanged || sizeChanged 485 || typeChanged || flagsChanged || redrawNeeded) { 486 487 if (DEBUG) Log.v(TAG, "Changes: creating=" + creating 488 + " format=" + formatChanged + " size=" + sizeChanged); 489 490 try { 491 mWidth = myWidth; 492 mHeight = myHeight; 493 mFormat = mSurfaceHolder.getRequestedFormat(); 494 mType = mSurfaceHolder.getRequestedType(); 495 496 mLayout.x = 0; 497 mLayout.y = 0; 498 mLayout.width = myWidth; 499 mLayout.height = myHeight; 500 501 mLayout.format = mFormat; 502 503 mCurWindowFlags = mWindowFlags; 504 mLayout.flags = mWindowFlags 505 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS 506 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 507 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 508 ; 509 510 mLayout.memoryType = mType; 511 mLayout.token = mWindowToken; 512 513 if (!mCreated) { 514 mLayout.type = mIWallpaperEngine.mWindowType; 515 mLayout.gravity = Gravity.LEFT|Gravity.TOP; 516 mLayout.setTitle(WallpaperService.this.getClass().getName()); 517 mLayout.windowAnimations = 518 com.android.internal.R.style.Animation_Wallpaper; 519 mInputChannel = new InputChannel(); 520 mSession.add(mWindow, mLayout, View.VISIBLE, mContentInsets, 521 mInputChannel); 522 mCreated = true; 523 524 InputQueue.registerInputChannel(mInputChannel, mInputHandler, 525 Looper.myQueue()); 526 } 527 528 mSurfaceHolder.mSurfaceLock.lock(); 529 mDrawingAllowed = true; 530 531 final int relayoutResult = mSession.relayout( 532 mWindow, mLayout, mWidth, mHeight, 533 View.VISIBLE, false, mWinFrame, mContentInsets, 534 mVisibleInsets, mConfiguration, mSurfaceHolder.mSurface); 535 536 if (DEBUG) Log.v(TAG, "New surface: " + mSurfaceHolder.mSurface 537 + ", frame=" + mWinFrame); 538 539 int w = mWinFrame.width(); 540 if (mCurWidth != w) { 541 sizeChanged = true; 542 mCurWidth = w; 543 } 544 int h = mWinFrame.height(); 545 if (mCurHeight != h) { 546 sizeChanged = true; 547 mCurHeight = h; 548 } 549 550 mSurfaceHolder.mSurfaceLock.unlock(); 551 552 if (!mSurfaceHolder.mSurface.isValid()) { 553 reportSurfaceDestroyed(); 554 if (DEBUG) Log.v(TAG, "Layout: Surface destroyed"); 555 return; 556 } 557 558 try { 559 mSurfaceHolder.ungetCallbacks(); 560 561 if (surfaceCreating) { 562 mIsCreating = true; 563 if (DEBUG) Log.v(TAG, "onSurfaceCreated(" 564 + mSurfaceHolder + "): " + this); 565 onSurfaceCreated(mSurfaceHolder); 566 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 567 if (callbacks != null) { 568 for (SurfaceHolder.Callback c : callbacks) { 569 c.surfaceCreated(mSurfaceHolder); 570 } 571 } 572 } 573 574 redrawNeeded |= creating 575 || (relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0; 576 577 if (forceReport || creating || surfaceCreating 578 || formatChanged || sizeChanged) { 579 if (DEBUG) { 580 RuntimeException e = new RuntimeException(); 581 e.fillInStackTrace(); 582 Log.w(TAG, "forceReport=" + forceReport + " creating=" + creating 583 + " formatChanged=" + formatChanged 584 + " sizeChanged=" + sizeChanged, e); 585 } 586 if (DEBUG) Log.v(TAG, "onSurfaceChanged(" 587 + mSurfaceHolder + ", " + mFormat 588 + ", " + mCurWidth + ", " + mCurHeight 589 + "): " + this); 590 onSurfaceChanged(mSurfaceHolder, mFormat, 591 mCurWidth, mCurHeight); 592 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 593 if (callbacks != null) { 594 for (SurfaceHolder.Callback c : callbacks) { 595 c.surfaceChanged(mSurfaceHolder, mFormat, 596 mCurWidth, mCurHeight); 597 } 598 } 599 } 600 601 if (redrawNeeded) { 602 onSurfaceRedrawNeeded(mSurfaceHolder); 603 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 604 if (callbacks != null) { 605 for (SurfaceHolder.Callback c : callbacks) { 606 if (c instanceof SurfaceHolder.Callback2) { 607 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded( 608 mSurfaceHolder); 609 } 610 } 611 } 612 } 613 614 } finally { 615 mIsCreating = false; 616 mSurfaceCreated = true; 617 if (redrawNeeded) { 618 mSession.finishDrawing(mWindow); 619 } 620 } 621 } catch (RemoteException ex) { 622 } 623 if (DEBUG) Log.v( 624 TAG, "Layout: x=" + mLayout.x + " y=" + mLayout.y + 625 " w=" + mLayout.width + " h=" + mLayout.height); 626 } 627 } 628 629 void attach(IWallpaperEngineWrapper wrapper) { 630 if (DEBUG) Log.v(TAG, "attach: " + this + " wrapper=" + wrapper); 631 if (mDestroyed) { 632 return; 633 } 634 635 mIWallpaperEngine = wrapper; 636 mCaller = wrapper.mCaller; 637 mConnection = wrapper.mConnection; 638 mWindowToken = wrapper.mWindowToken; 639 mSurfaceHolder.setSizeFromLayout(); 640 mInitializing = true; 641 mSession = ViewRoot.getWindowSession(getMainLooper()); 642 643 mWindow.setSession(mSession); 644 645 IntentFilter filter = new IntentFilter(); 646 filter.addAction(Intent.ACTION_SCREEN_ON); 647 filter.addAction(Intent.ACTION_SCREEN_OFF); 648 registerReceiver(mReceiver, filter); 649 650 if (DEBUG) Log.v(TAG, "onCreate(): " + this); 651 onCreate(mSurfaceHolder); 652 653 mInitializing = false; 654 updateSurface(false, false, false); 655 } 656 657 void doDesiredSizeChanged(int desiredWidth, int desiredHeight) { 658 if (!mDestroyed) { 659 if (DEBUG) Log.v(TAG, "onDesiredSizeChanged(" 660 + desiredWidth + "," + desiredHeight + "): " + this); 661 mIWallpaperEngine.mReqWidth = desiredWidth; 662 mIWallpaperEngine.mReqHeight = desiredHeight; 663 onDesiredSizeChanged(desiredWidth, desiredHeight); 664 doOffsetsChanged(); 665 } 666 } 667 668 void doVisibilityChanged(boolean visible) { 669 if (!mDestroyed) { 670 mVisible = visible; 671 reportVisibility(); 672 } 673 } 674 675 void reportVisibility() { 676 if (!mDestroyed) { 677 boolean visible = mVisible && mScreenOn; 678 if (mReportedVisible != visible) { 679 mReportedVisible = visible; 680 if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible 681 + "): " + this); 682 if (visible) { 683 // If becoming visible, in preview mode the surface 684 // may have been destroyed so now we need to make 685 // sure it is re-created. 686 updateSurface(false, false, false); 687 } 688 onVisibilityChanged(visible); 689 } 690 } 691 } 692 693 void doOffsetsChanged() { 694 if (mDestroyed) { 695 return; 696 } 697 698 float xOffset; 699 float yOffset; 700 float xOffsetStep; 701 float yOffsetStep; 702 boolean sync; 703 synchronized (mLock) { 704 xOffset = mPendingXOffset; 705 yOffset = mPendingYOffset; 706 xOffsetStep = mPendingXOffsetStep; 707 yOffsetStep = mPendingYOffsetStep; 708 sync = mPendingSync; 709 mPendingSync = false; 710 mOffsetMessageEnqueued = false; 711 } 712 713 if (mSurfaceCreated) { 714 if (DEBUG) Log.v(TAG, "Offsets change in " + this 715 + ": " + xOffset + "," + yOffset); 716 final int availw = mIWallpaperEngine.mReqWidth-mCurWidth; 717 final int xPixels = availw > 0 ? -(int)(availw*xOffset+.5f) : 0; 718 final int availh = mIWallpaperEngine.mReqHeight-mCurHeight; 719 final int yPixels = availh > 0 ? -(int)(availh*yOffset+.5f) : 0; 720 onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixels, yPixels); 721 } 722 723 if (sync) { 724 try { 725 if (DEBUG) Log.v(TAG, "Reporting offsets change complete"); 726 mSession.wallpaperOffsetsComplete(mWindow.asBinder()); 727 } catch (RemoteException e) { 728 } 729 } 730 } 731 732 void doCommand(WallpaperCommand cmd) { 733 Bundle result; 734 if (!mDestroyed) { 735 result = onCommand(cmd.action, cmd.x, cmd.y, cmd.z, 736 cmd.extras, cmd.sync); 737 } else { 738 result = null; 739 } 740 if (cmd.sync) { 741 try { 742 if (DEBUG) Log.v(TAG, "Reporting command complete"); 743 mSession.wallpaperCommandComplete(mWindow.asBinder(), result); 744 } catch (RemoteException e) { 745 } 746 } 747 } 748 749 void reportSurfaceDestroyed() { 750 if (mSurfaceCreated) { 751 mSurfaceCreated = false; 752 mSurfaceHolder.ungetCallbacks(); 753 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 754 if (callbacks != null) { 755 for (SurfaceHolder.Callback c : callbacks) { 756 c.surfaceDestroyed(mSurfaceHolder); 757 } 758 } 759 if (DEBUG) Log.v(TAG, "onSurfaceDestroyed(" 760 + mSurfaceHolder + "): " + this); 761 onSurfaceDestroyed(mSurfaceHolder); 762 } 763 } 764 765 void detach() { 766 if (mDestroyed) { 767 return; 768 } 769 770 mDestroyed = true; 771 772 if (mVisible) { 773 mVisible = false; 774 if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this); 775 onVisibilityChanged(false); 776 } 777 778 reportSurfaceDestroyed(); 779 780 if (DEBUG) Log.v(TAG, "onDestroy(): " + this); 781 onDestroy(); 782 783 unregisterReceiver(mReceiver); 784 785 if (mCreated) { 786 try { 787 if (DEBUG) Log.v(TAG, "Removing window and destroying surface " 788 + mSurfaceHolder.getSurface() + " of: " + this); 789 790 if (mInputChannel != null) { 791 InputQueue.unregisterInputChannel(mInputChannel); 792 } 793 794 mSession.remove(mWindow); 795 } catch (RemoteException e) { 796 } 797 mSurfaceHolder.mSurface.release(); 798 mCreated = false; 799 800 // Dispose the input channel after removing the window so the Window Manager 801 // doesn't interpret the input channel being closed as an abnormal termination. 802 if (mInputChannel != null) { 803 mInputChannel.dispose(); 804 mInputChannel = null; 805 } 806 } 807 } 808 } 809 810 class IWallpaperEngineWrapper extends IWallpaperEngine.Stub 811 implements HandlerCaller.Callback { 812 private final HandlerCaller mCaller; 813 814 final IWallpaperConnection mConnection; 815 final IBinder mWindowToken; 816 final int mWindowType; 817 final boolean mIsPreview; 818 int mReqWidth; 819 int mReqHeight; 820 821 Engine mEngine; 822 823 IWallpaperEngineWrapper(WallpaperService context, 824 IWallpaperConnection conn, IBinder windowToken, 825 int windowType, boolean isPreview, int reqWidth, int reqHeight) { 826 if (DEBUG && mCallbackLooper != null) { 827 mCallbackLooper.setMessageLogging(new LogPrinter(Log.VERBOSE, TAG)); 828 } 829 mCaller = new HandlerCaller(context, 830 mCallbackLooper != null 831 ? mCallbackLooper : context.getMainLooper(), 832 this); 833 mConnection = conn; 834 mWindowToken = windowToken; 835 mWindowType = windowType; 836 mIsPreview = isPreview; 837 mReqWidth = reqWidth; 838 mReqHeight = reqHeight; 839 840 Message msg = mCaller.obtainMessage(DO_ATTACH); 841 mCaller.sendMessage(msg); 842 } 843 844 public void setDesiredSize(int width, int height) { 845 Message msg = mCaller.obtainMessageII(DO_SET_DESIRED_SIZE, width, height); 846 mCaller.sendMessage(msg); 847 } 848 849 public void setVisibility(boolean visible) { 850 Message msg = mCaller.obtainMessageI(MSG_VISIBILITY_CHANGED, 851 visible ? 1 : 0); 852 mCaller.sendMessage(msg); 853 } 854 855 public void dispatchPointer(MotionEvent event) { 856 if (mEngine != null) { 857 mEngine.dispatchPointer(event); 858 } 859 } 860 861 public void dispatchWallpaperCommand(String action, int x, int y, 862 int z, Bundle extras) { 863 if (mEngine != null) { 864 mEngine.mWindow.dispatchWallpaperCommand(action, x, y, z, extras, false); 865 } 866 } 867 868 public void destroy() { 869 Message msg = mCaller.obtainMessage(DO_DETACH); 870 mCaller.sendMessage(msg); 871 } 872 873 public void executeMessage(Message message) { 874 switch (message.what) { 875 case DO_ATTACH: { 876 try { 877 mConnection.attachEngine(this); 878 } catch (RemoteException e) { 879 Log.w(TAG, "Wallpaper host disappeared", e); 880 return; 881 } 882 Engine engine = onCreateEngine(); 883 mEngine = engine; 884 mActiveEngines.add(engine); 885 engine.attach(this); 886 return; 887 } 888 case DO_DETACH: { 889 mActiveEngines.remove(mEngine); 890 mEngine.detach(); 891 return; 892 } 893 case DO_SET_DESIRED_SIZE: { 894 mEngine.doDesiredSizeChanged(message.arg1, message.arg2); 895 return; 896 } 897 case MSG_UPDATE_SURFACE: 898 mEngine.updateSurface(true, false, false); 899 break; 900 case MSG_VISIBILITY_CHANGED: 901 if (DEBUG) Log.v(TAG, "Visibility change in " + mEngine 902 + ": " + message.arg1); 903 mEngine.doVisibilityChanged(message.arg1 != 0); 904 break; 905 case MSG_WALLPAPER_OFFSETS: { 906 mEngine.doOffsetsChanged(); 907 } break; 908 case MSG_WALLPAPER_COMMAND: { 909 WallpaperCommand cmd = (WallpaperCommand)message.obj; 910 mEngine.doCommand(cmd); 911 } break; 912 case MSG_WINDOW_RESIZED: { 913 final boolean reportDraw = message.arg1 != 0; 914 mEngine.updateSurface(true, false, reportDraw); 915 mEngine.doOffsetsChanged(); 916 } break; 917 case MSG_TOUCH_EVENT: { 918 boolean skip = false; 919 MotionEvent ev = (MotionEvent)message.obj; 920 if (ev.getAction() == MotionEvent.ACTION_MOVE) { 921 synchronized (mEngine.mLock) { 922 if (mEngine.mPendingMove == ev) { 923 mEngine.mPendingMove = null; 924 } else { 925 // this is not the motion event we are looking for.... 926 skip = true; 927 } 928 } 929 } 930 if (!skip) { 931 if (DEBUG) Log.v(TAG, "Delivering touch event: " + ev); 932 mEngine.onTouchEvent(ev); 933 } 934 ev.recycle(); 935 } break; 936 default : 937 Log.w(TAG, "Unknown message type " + message.what); 938 } 939 } 940 } 941 942 /** 943 * Implements the internal {@link IWallpaperService} interface to convert 944 * incoming calls to it back to calls on an {@link WallpaperService}. 945 */ 946 class IWallpaperServiceWrapper extends IWallpaperService.Stub { 947 private final WallpaperService mTarget; 948 949 public IWallpaperServiceWrapper(WallpaperService context) { 950 mTarget = context; 951 } 952 953 public void attach(IWallpaperConnection conn, IBinder windowToken, 954 int windowType, boolean isPreview, int reqWidth, int reqHeight) { 955 new IWallpaperEngineWrapper(mTarget, conn, windowToken, 956 windowType, isPreview, reqWidth, reqHeight); 957 } 958 } 959 960 @Override 961 public void onCreate() { 962 super.onCreate(); 963 } 964 965 @Override 966 public void onDestroy() { 967 super.onDestroy(); 968 for (int i=0; i<mActiveEngines.size(); i++) { 969 mActiveEngines.get(i).detach(); 970 } 971 mActiveEngines.clear(); 972 } 973 974 /** 975 * Implement to return the implementation of the internal accessibility 976 * service interface. Subclasses should not override. 977 */ 978 @Override 979 public final IBinder onBind(Intent intent) { 980 return new IWallpaperServiceWrapper(this); 981 } 982 983 /** 984 * This allows subclasses to change the thread that most callbacks 985 * occur on. Currently hidden because it is mostly needed for the 986 * image wallpaper (which runs in the system process and doesn't want 987 * to get stuck running on that seriously in use main thread). Not 988 * exposed right now because the semantics of this are not totally 989 * well defined and some callbacks can still happen on the main thread). 990 * @hide 991 */ 992 public void setCallbackLooper(Looper looper) { 993 mCallbackLooper = looper; 994 } 995 996 /** 997 * Must be implemented to return a new instance of the wallpaper's engine. 998 * Note that multiple instances may be active at the same time, such as 999 * when the wallpaper is currently set as the active wallpaper and the user 1000 * is in the wallpaper picker viewing a preview of it as well. 1001 */ 1002 public abstract Engine onCreateEngine(); 1003} 1004