Report wallpaper offset to the wallpaper, use this in the image wallpaper.

Wallpapers can now be just the size of the screen, and get told when their
scroll position should change to do the updating on their own.
This commit is contained in:
Dianne Hackborn
2009-08-11 21:13:54 -07:00
parent a5f743f1fc
commit 72c82ab992
11 changed files with 189 additions and 41 deletions

View File

@@ -334,6 +334,25 @@ public class WallpaperManager {
}
}
/**
* Clear the offsets previously associated with this window through
* {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
* the window to its default state, where it does not cause the wallpaper
* to scroll from whatever its last offsets were.
*
* @param windowToken The window who these offsets should be associated
* with, as returned by {@link android.view.View#getWindowVisibility()
* View.getWindowToken()}.
*/
public void clearWallpaperOffsets(IBinder windowToken) {
try {
ViewRoot.getWindowSession(mContext.getMainLooper()).setWallpaperPosition(
windowToken, -1, -1);
} catch (RemoteException e) {
// Ignore.
}
}
/**
* Remove any currently set wallpaper, reverting to the system's default
* wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}

View File

@@ -24,7 +24,6 @@ import android.app.Service;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
@@ -33,6 +32,7 @@ import android.view.Gravity;
import android.view.IWindowSession;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewRoot;
import android.view.WindowManager;
import android.view.WindowManagerImpl;
@@ -50,13 +50,14 @@ public abstract class WallpaperService extends Service {
"android.service.wallpaper.WallpaperService";
static final String TAG = "WallpaperService";
static final boolean DEBUG = true;
static final boolean DEBUG = false;
private static final int DO_ATTACH = 10;
private static final int DO_DETACH = 20;
private static final int MSG_UPDATE_SURFACE = 10000;
private static final int MSG_VISIBILITY_CHANGED = 10010;
private static final int MSG_WALLPAPER_OFFSETS = 10020;
/**
* The actual implementation of a wallpaper. A wallpaper service may
@@ -83,6 +84,8 @@ public abstract class WallpaperService extends Service {
int mHeight;
int mFormat;
int mType;
int mCurWidth;
int mCurHeight;
boolean mDestroyReportNeeded;
final Rect mVisibleInsets = new Rect();
final Rect mWinFrame = new Rect();
@@ -92,6 +95,11 @@ public abstract class WallpaperService extends Service {
= new WindowManager.LayoutParams();
IWindowSession mSession;
final Object mLock = new Object();
boolean mOffsetMessageEnqueued;
float mPendingXOffset;
float mPendingYOffset;
final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() {
@Override
@@ -127,6 +135,20 @@ public abstract class WallpaperService extends Service {
visible ? 1 : 0);
mCaller.sendMessage(msg);
}
@Override
public void dispatchWallpaperOffsets(float x, float y) {
synchronized (mLock) {
mPendingXOffset = x;
mPendingYOffset = y;
if (!mOffsetMessageEnqueued) {
mOffsetMessageEnqueued = true;
Message msg = mCaller.obtainMessage(MSG_WALLPAPER_OFFSETS);
mCaller.sendMessage(msg);
}
}
}
};
/**
@@ -177,6 +199,16 @@ public abstract class WallpaperService extends Service {
public void onVisibilityChanged(boolean visible) {
}
/**
* Called to inform you of the wallpaper's offsets changing
* within its contain, corresponding to the container's
* call to {@link WallpaperManager#setWallpaperOffsets(IBinder, float, float)
* WallpaperManager.setWallpaperOffsets()}.
*/
public void onOffsetsChanged(float xOffset, float yOffset,
int xPixelOffset, int yPixelOffset) {
}
/**
* Convenience for {@link SurfaceHolder.Callback#surfaceChanged
* SurfaceHolder.Callback.surfaceChanged()}.
@@ -200,9 +232,9 @@ public abstract class WallpaperService extends Service {
void updateSurface(boolean force) {
int myWidth = mSurfaceHolder.getRequestedWidth();
if (myWidth <= 0) myWidth = mIWallpaperEngine.mReqWidth;
if (myWidth <= 0) myWidth = ViewGroup.LayoutParams.FILL_PARENT;
int myHeight = mSurfaceHolder.getRequestedHeight();
if (myHeight <= 0) myHeight = mIWallpaperEngine.mReqHeight;
if (myHeight <= 0) myHeight = ViewGroup.LayoutParams.FILL_PARENT;
final boolean creating = !mCreated;
final boolean formatChanged = mFormat != mSurfaceHolder.getRequestedFormat();
@@ -254,6 +286,9 @@ public abstract class WallpaperService extends Service {
if (DEBUG) Log.i(TAG, "New surface: " + mSurfaceHolder.mSurface
+ ", frame=" + mWinFrame);
mCurWidth = mWinFrame.width();
mCurHeight = mWinFrame.height();
mSurfaceHolder.mSurfaceLock.unlock();
try {
@@ -278,10 +313,12 @@ public abstract class WallpaperService extends Service {
}
}
if (creating || formatChanged || sizeChanged) {
onSurfaceChanged(mSurfaceHolder, mFormat, mWidth, mHeight);
onSurfaceChanged(mSurfaceHolder, mFormat,
mCurWidth, mCurHeight);
if (callbacks != null) {
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceChanged(mSurfaceHolder, mFormat, mWidth, mHeight);
c.surfaceChanged(mSurfaceHolder, mFormat,
mCurWidth, mCurHeight);
}
}
}
@@ -305,7 +342,10 @@ public abstract class WallpaperService extends Service {
mCaller = wrapper.mCaller;
mConnection = wrapper.mConnection;
mWindowToken = wrapper.mWindowToken;
mSurfaceHolder.setSizeFromLayout();
// XXX temp -- should run in size from layout (screen) mode.
mSurfaceHolder.setFixedSize(mIWallpaperEngine.mReqWidth,
mIWallpaperEngine.mReqHeight);
//mSurfaceHolder.setSizeFromLayout();
mInitializing = true;
mSession = ViewRoot.getWindowSession(getMainLooper());
mWindow.setSession(mSession);
@@ -396,6 +436,22 @@ public abstract class WallpaperService extends Service {
+ ": " + message.arg1);
mEngine.onVisibilityChanged(message.arg1 != 0);
break;
case MSG_WALLPAPER_OFFSETS: {
float xOffset;
float yOffset;
synchronized (mEngine.mLock) {
xOffset = mEngine.mPendingXOffset;
yOffset = mEngine.mPendingYOffset;
mEngine.mOffsetMessageEnqueued = false;
}
if (DEBUG) Log.v(TAG, "Offsets change in " + mEngine
+ ": " + xOffset + "," + yOffset);
final int availw = mReqWidth-mEngine.mCurWidth;
final int xPixels = availw > 0 ? -(int)(availw*xOffset+.5f) : 0;
final int availh = mReqHeight-mEngine.mCurHeight;
final int yPixels = availh > 0 ? -(int)(availh*yOffset+.5f) : 0;
mEngine.onOffsetsChanged(xOffset, yOffset, xPixels, yPixels);
} break;
default :
Log.w(TAG, "Unknown message type " + message.what);
}

View File

@@ -56,4 +56,9 @@ oneway interface IWindow {
* to date on the current state showing navigational focus (touch mode) too.
*/
void windowFocusChanged(boolean hasFocus, boolean inTouchMode);
/**
* Called for wallpaper windows when their offsets change.
*/
void dispatchWallpaperOffsets(float x, float y);
}

View File

@@ -16,6 +16,8 @@
package android.view;
import com.android.internal.view.BaseIWindow;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.CompatibilityInfo.Translator;
@@ -435,7 +437,7 @@ public class SurfaceView extends View {
updateWindow(false);
}
private static class MyWindow extends IWindow.Stub {
private static class MyWindow extends BaseIWindow {
private final WeakReference<SurfaceView> mSurfaceView;
public MyWindow(SurfaceView surfaceView) {

View File

@@ -2858,6 +2858,9 @@ public final class ViewRoot extends Handler implements ViewParent,
}
}
}
public void dispatchWallpaperOffsets(float x, float y) {
}
}
/**

View File

@@ -93,6 +93,10 @@ public class HandlerCaller {
mH.sendMessage(msg);
}
public boolean hasMessages(int what) {
return mH.hasMessages(what);
}
public void sendMessage(Message msg) {
mH.sendMessage(msg);
}

View File

@@ -21,6 +21,7 @@ import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
import android.content.Context;
import android.content.IntentFilter;
@@ -63,12 +64,23 @@ public class ImageWallpaper extends WallpaperService {
class DrawableEngine extends Engine {
private final Object mLock = new Object();
private final Rect mBounds = new Rect();
Drawable mBackground;
int mXOffset;
int mYOffset;
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
mBackground = mWallpaperManager.getDrawable();
mBounds.left = mBounds.top = 0;
mBounds.right = mBackground.getIntrinsicWidth();
mBounds.bottom = mBackground.getIntrinsicHeight();
int offx = (getDesiredMinimumWidth() - mBounds.right) / 2;
int offy = (getDesiredMinimumHeight() - mBounds.bottom) / 2;
mBounds.offset(offx, offy);
mBackground.setBounds(mBounds);
surfaceHolder.setSizeFromLayout();
}
@Override
@@ -76,6 +88,14 @@ public class ImageWallpaper extends WallpaperService {
drawFrame();
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
int xPixels, int yPixels) {
mXOffset = xPixels;
mYOffset = yPixels;
drawFrame();
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
@@ -94,19 +114,16 @@ public class ImageWallpaper extends WallpaperService {
void drawFrame() {
SurfaceHolder sh = getSurfaceHolder();
Canvas c = null;
try {
c = sh.lockCanvas();
if (c != null) {
final Rect frame = sh.getSurfaceFrame();
synchronized (mLock) {
final Drawable background = mBackground;
background.setBounds(frame);
background.draw(c);
}
Canvas c = sh.lockCanvas();
if (c != null) {
//final Rect frame = sh.getSurfaceFrame();
synchronized (mLock) {
final Drawable background = mBackground;
//background.setBounds(frame);
c.translate(mXOffset, mYOffset);
background.draw(c);
}
} finally {
if (c != null) sh.unlockCanvasAndPost(c);
sh.unlockCanvasAndPost(c);
}
}

View File

@@ -65,4 +65,7 @@ public class BaseIWindow extends IWindow.Stub {
public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
}
public void dispatchWallpaperOffsets(float x, float y) {
}
}