Merge "Add new WindowId for cross-process monitoring of focus." into jb-mr2-dev

This commit is contained in:
Dianne Hackborn
2013-03-05 17:37:43 +00:00
committed by Android (Google) Code Review
10 changed files with 406 additions and 19 deletions

View File

@ -146,7 +146,7 @@ LOCAL_SRC_FILES += \
core/java/android/os/IRemoteCallback.aidl \
core/java/android/os/ISchedulingPolicyService.aidl \
core/java/android/os/IUpdateLock.aidl \
core/java/android/os/IUserManager.aidl \
core/java/android/os/IUserManager.aidl \
core/java/android/os/IVibratorService.aidl \
core/java/android/service/dreams/IDreamManager.aidl \
core/java/android/service/dreams/IDreamService.aidl \
@ -164,6 +164,8 @@ LOCAL_SRC_FILES += \
core/java/android/view/IOnKeyguardExitResult.aidl \
core/java/android/view/IRotationWatcher.aidl \
core/java/android/view/IWindow.aidl \
core/java/android/view/IWindowFocusObserver.aidl \
core/java/android/view/IWindowId.aidl \
core/java/android/view/IWindowManager.aidl \
core/java/android/view/IWindowSession.aidl \
core/java/android/speech/IRecognitionListener.aidl \

View File

@ -25279,6 +25279,7 @@ package android.view {
method public int getVisibility();
method public final int getWidth();
method protected int getWindowAttachCount();
method public android.view.WindowId getWindowId();
method public int getWindowSystemUiVisibility();
method public android.os.IBinder getWindowToken();
method public int getWindowVisibility();
@ -26224,6 +26225,21 @@ package android.view {
method public abstract android.view.ActionMode onWindowStartingActionMode(android.view.ActionMode.Callback);
}
public class WindowId implements android.os.Parcelable {
method public int describeContents();
method public boolean isFocused();
method public void registerFocusObserver(android.view.WindowId.FocusObserver);
method public void unregisterFocusObserver(android.view.WindowId.FocusObserver);
method public void writeToParcel(android.os.Parcel, int);
field public static final android.os.Parcelable.Creator CREATOR;
}
public static abstract class WindowId.FocusObserver {
ctor public WindowId.FocusObserver();
method public abstract void onFocusGained(android.view.WindowId);
method public abstract void onFocusLost(android.view.WindowId);
}
public abstract interface WindowManager implements android.view.ViewManager {
method public abstract android.view.Display getDefaultDisplay();
method public abstract void removeViewImmediate(android.view.View);

View File

@ -0,0 +1,23 @@
/* Copyright 2013, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package android.view;
/** {@hide} */
interface IWindowFocusObserver
{
void focusGained(IBinder inputToken);
void focusLost(IBinder inputToken);
}

View File

@ -0,0 +1,26 @@
/* Copyright 2013, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package android.view;
import android.view.IWindowFocusObserver;
/** {@hide} */
interface IWindowId
{
void registerFocusObserver(IWindowFocusObserver observer);
void unregisterFocusObserver(IWindowFocusObserver observer);
boolean isFocused();
}

View File

@ -24,6 +24,7 @@ import android.graphics.Region;
import android.os.Bundle;
import android.view.InputChannel;
import android.view.IWindow;
import android.view.IWindowId;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.Surface;
@ -188,4 +189,6 @@ interface IWindowSession {
* Notifies that a rectangle on the screen has been requested.
*/
void onRectangleOnScreenRequested(IBinder token, in Rect rectangle, boolean immediate);
IWindowId getWindowId(IBinder window);
}

View File

@ -12020,6 +12020,26 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
}
/**
* Retrieve the {@link WindowId} for the window this view is
* currently attached to.
*/
public WindowId getWindowId() {
if (mAttachInfo == null) {
return null;
}
if (mAttachInfo.mWindowId == null) {
try {
mAttachInfo.mIWindowId = mAttachInfo.mSession.getWindowId(
mAttachInfo.mWindowToken);
mAttachInfo.mWindowId = new WindowId(
mAttachInfo.mIWindowId);
} catch (RemoteException e) {
}
}
return mAttachInfo.mWindowId;
}
/**
* Retrieve a unique token identifying the top-level "real" window of
* the window that this view is attached to. That is, this is like
@ -17898,6 +17918,9 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
HardwareCanvas mHardwareCanvas;
IWindowId mIWindowId;
WindowId mWindowId;
/**
* The top view of the hierarchy.
*/

View File

@ -0,0 +1,224 @@
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import java.util.HashMap;
/**
* Safe identifier for a window. This currently allows you to retrieve and observe
* the input focus state of the window. Most applications will
* not use this, instead relying on the simpler (and more efficient) methods available
* on {@link View}. This classes is useful when window input interactions need to be
* done across processes: the class itself is a Parcelable that can be passed to other
* processes for them to interact with your window, and it provides a limited safe API
* that doesn't allow the other process to negatively harm your window.
*/
public class WindowId implements Parcelable {
private final IWindowId mToken;
/**
* Subclass for observing changes to the focus state of an {@link WindowId}.
* You should use the same instance of this class for observing multiple
* {@link WindowId} objects, since this class is fairly heavy-weight -- the
* base class includes all of the mechanisms for connecting to and receiving updates
* from the window.
*/
public static abstract class FocusObserver {
final IWindowFocusObserver.Stub mIObserver = new IWindowFocusObserver.Stub() {
@Override
public void focusGained(IBinder inputToken) {
WindowId token;
synchronized (mRegistrations) {
token = mRegistrations.get(inputToken);
}
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(1, token));
} else {
onFocusGained(token);
}
}
@Override
public void focusLost(IBinder inputToken) {
WindowId token;
synchronized (mRegistrations) {
token = mRegistrations.get(inputToken);
}
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(2, token));
} else {
onFocusLost(token);
}
}
};
final HashMap<IBinder, WindowId> mRegistrations
= new HashMap<IBinder, WindowId>();
class H extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
onFocusGained((WindowId)msg.obj);
break;
case 2:
onFocusLost((WindowId)msg.obj);
break;
default:
super.handleMessage(msg);
}
}
}
final Handler mHandler;
/**
* Construct a new observer. This observer will be configured so that all
* of its callbacks are dispatched on the current calling thread.
*/
public FocusObserver() {
mHandler = new H();
}
/**
* Called when one of the monitored windows gains input focus.
*/
public abstract void onFocusGained(WindowId token);
/**
* Called when one of the monitored windows loses input focus.
*/
public abstract void onFocusLost(WindowId token);
}
/**
* Retrieve the current focus state of the associated window.
*/
public boolean isFocused() {
try {
return mToken.isFocused();
} catch (RemoteException e) {
return false;
}
}
/**
* Start monitoring for changes in the focus state of the window.
*/
public void registerFocusObserver(FocusObserver observer) {
synchronized (observer.mRegistrations) {
if (observer.mRegistrations.containsKey(mToken.asBinder())) {
throw new IllegalStateException(
"Focus observer already registered with input token");
}
observer.mRegistrations.put(mToken.asBinder(), this);
try {
mToken.registerFocusObserver(observer.mIObserver);
} catch (RemoteException e) {
}
}
}
/**
* Stop monitoring changes in the focus state of the window.
*/
public void unregisterFocusObserver(FocusObserver observer) {
synchronized (observer.mRegistrations) {
if (observer.mRegistrations.remove(mToken.asBinder()) == null) {
throw new IllegalStateException("Focus observer not registered with input token");
}
try {
mToken.unregisterFocusObserver(observer.mIObserver);
} catch (RemoteException e) {
}
}
}
/**
* Comparison operator on two IntentSender objects, such that true
* is returned then they both represent the same operation from the
* same package.
*/
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof WindowId) {
return mToken.asBinder().equals(((WindowId) otherObj)
.mToken.asBinder());
}
return false;
}
@Override
public int hashCode() {
return mToken.asBinder().hashCode();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(128);
sb.append("IntentSender{");
sb.append(Integer.toHexString(System.identityHashCode(this)));
sb.append(": ");
sb.append(mToken != null ? mToken.asBinder() : null);
sb.append('}');
return sb.toString();
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeStrongBinder(mToken.asBinder());
}
public static final Parcelable.Creator<WindowId> CREATOR
= new Parcelable.Creator<WindowId>() {
public WindowId createFromParcel(Parcel in) {
IBinder target = in.readStrongBinder();
return target != null ? new WindowId(target) : null;
}
public WindowId[] newArray(int size) {
return new WindowId[size];
}
};
/** @hide */
public IWindowId getTarget() {
return mToken;
}
/** @hide */
public WindowId(IWindowId target) {
mToken = target;
}
/** @hide */
public WindowId(IBinder target) {
mToken = IWindowId.Stub.asInterface(target);
}
}

View File

@ -16,6 +16,7 @@
package com.android.server.wm;
import android.view.IWindowId;
import com.android.internal.view.IInputContext;
import com.android.internal.view.IInputMethodClient;
import com.android.internal.view.IInputMethodManager;
@ -447,6 +448,10 @@ final class Session extends IWindowSession.Stub
}
}
public IWindowId getWindowId(IBinder window) {
return mService.getWindowId(window);
}
void windowAddedLocked() {
if (mSurfaceSession == null) {
if (WindowManagerService.localLOGV) Slog.v(

View File

@ -43,6 +43,7 @@ import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
import android.app.AppOpsManager;
import android.view.IWindowId;
import com.android.internal.app.IBatteryStats;
import com.android.internal.policy.PolicyManager;
import com.android.internal.policy.impl.PhoneWindowManager;
@ -2662,6 +2663,13 @@ public class WindowManagerService extends IWindowManager.Stub
}
}
public IWindowId getWindowId(IBinder token) {
synchronized (mWindowMap) {
WindowState window = mWindowMap.get(token);
return window != null ? window.mWindowId : null;
}
}
public int relayoutWindow(Session session, IWindow client, int seq,
WindowManager.LayoutParams attrs, int requestedWidth,
int requestedHeight, int viewVisibility, int flags,
@ -6739,22 +6747,14 @@ public class WindowManagerService extends IWindowManager.Stub
//System.out.println("Changing focus from " + lastFocus
// + " to " + newFocus);
if (newFocus != null) {
try {
//Slog.i(TAG, "Gaining focus: " + newFocus);
newFocus.mClient.windowFocusChanged(true, mInTouchMode);
} catch (RemoteException e) {
// Ignore if process has died.
}
//Slog.i(TAG, "Gaining focus: " + newFocus);
newFocus.reportFocusChangedSerialized(true, mInTouchMode);
notifyFocusChanged();
}
if (lastFocus != null) {
try {
//Slog.i(TAG, "Losing focus: " + lastFocus);
lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
} catch (RemoteException e) {
// Ignore if process has died.
}
//Slog.i(TAG, "Losing focus: " + lastFocus);
lastFocus.reportFocusChangedSerialized(false, mInTouchMode);
}
}
} break;
@ -6769,12 +6769,8 @@ public class WindowManagerService extends IWindowManager.Stub
final int N = losers.size();
for (int i=0; i<N; i++) {
try {
//Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
} catch (RemoteException e) {
// Ignore if process has died.
}
//Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
losers.get(i).reportFocusChangedSerialized(false, mInTouchMode);
}
} break;

View File

@ -25,6 +25,9 @@ import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD;
import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
import android.app.AppOpsManager;
import android.os.RemoteCallbackList;
import android.view.IWindowFocusObserver;
import android.view.IWindowId;
import com.android.server.input.InputWindowHandle;
import android.content.Context;
@ -73,6 +76,7 @@ final class WindowState implements WindowManagerPolicy.WindowState {
final int mAppOp;
// UserId and appId of the owner. Don't display windows of non-current user.
final int mOwnerUid;
final IWindowId mWindowId;
WindowToken mToken;
WindowToken mRootToken;
AppWindowToken mAppToken;
@ -101,6 +105,8 @@ final class WindowState implements WindowManagerPolicy.WindowState {
boolean mAttachedHidden; // is our parent window hidden?
boolean mWallpaperVisible; // for wallpaper, what was last vis report?
RemoteCallbackList<IWindowFocusObserver> mFocusCallbacks;
/**
* The window size that was requested by the application. These are in
* the application's coordinate space (without compatibility scale applied).
@ -296,6 +302,20 @@ final class WindowState implements WindowManagerPolicy.WindowState {
mAppOp = appOp;
mToken = token;
mOwnerUid = s.mUid;
mWindowId = new IWindowId.Stub() {
@Override
public void registerFocusObserver(IWindowFocusObserver observer) {
WindowState.this.registerFocusObserver(observer);
}
@Override
public void unregisterFocusObserver(IWindowFocusObserver observer) {
WindowState.this.unregisterFocusObserver(observer);
}
@Override
public boolean isFocused() {
return WindowState.this.isFocused();
}
};
mAttrs.copyFrom(a);
mViewVisibility = viewVisibility;
mDisplayContent = displayContent;
@ -1178,6 +1198,55 @@ final class WindowState implements WindowManagerPolicy.WindowState {
return mDisplayContent.getWindowList();
}
/**
* Report a focus change. Must be called with no locks held, and consistently
* from the same serialized thread (such as dispatched from a handler).
*/
public void reportFocusChangedSerialized(boolean focused, boolean inTouchMode) {
try {
mClient.windowFocusChanged(focused, inTouchMode);
} catch (RemoteException e) {
}
if (mFocusCallbacks != null) {
final int N = mFocusCallbacks.beginBroadcast();
for (int i=0; i<N; i++) {
IWindowFocusObserver obs = mFocusCallbacks.getBroadcastItem(i);
try {
if (focused) {
obs.focusGained(mWindowId.asBinder());
} else {
obs.focusLost(mWindowId.asBinder());
}
} catch (RemoteException e) {
}
}
mFocusCallbacks.finishBroadcast();
}
}
public void registerFocusObserver(IWindowFocusObserver observer) {
synchronized(mService.mWindowMap) {
if (mFocusCallbacks == null) {
mFocusCallbacks = new RemoteCallbackList<IWindowFocusObserver>();
}
mFocusCallbacks.register(observer);
}
}
public void unregisterFocusObserver(IWindowFocusObserver observer) {
synchronized(mService.mWindowMap) {
if (mFocusCallbacks != null) {
mFocusCallbacks.unregister(observer);
}
}
}
public boolean isFocused() {
synchronized(mService.mWindowMap) {
return mService.mCurrentFocus == this;
}
}
void dump(PrintWriter pw, String prefix, boolean dumpAll) {
pw.print(prefix); pw.print("mDisplayId="); pw.print(mDisplayContent.getDisplayId());
pw.print(" mSession="); pw.print(mSession);