Skip to content

Commit 6018aee

Browse files
author
Craig Mautner
committed
Add throwing InvalidDisplayException from addView.
Throw an InvalidDisplayException to addView if the display being added to has been removed. Handle this exception in Dialog.show() by removing the view after it has been added and rethrow the exception from there. Add javadoc to ViewManager.addView and Presentation.show explaining the new exception and how best to handle it. Bug: 7368565 partially fixed. It remains for the Videos app to handle Presentation.show throwing the InvalidDisplayException. Change-Id: Ib4303c9b3f7bf7a0cfa95d19bd60a0c128658c48
1 parent 04c8d40 commit 6018aee

File tree

7 files changed

+78
-26
lines changed

7 files changed

+78
-26
lines changed

api/17.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7094,7 +7094,7 @@ package android.content.res {
70947094
method public int getIndexCount();
70957095
method public int getInt(int, int);
70967096
method public int getInteger(int, int);
7097-
method public deprecated int getLayoutDimension(int, java.lang.String);
7097+
method public int getLayoutDimension(int, java.lang.String);
70987098
method public int getLayoutDimension(int, int);
70997099
method public java.lang.String getNonResourceString(int);
71007100
method public java.lang.String getPositionDescription();
@@ -8263,6 +8263,7 @@ package android.graphics {
82638263
method public int getScaledWidth(int);
82648264
method public final int getWidth();
82658265
method public final boolean hasAlpha();
8266+
method public final boolean hasMipMap();
82668267
method public final boolean isMutable();
82678268
method public final boolean isPremultiplied();
82688269
method public final boolean isRecycled();
@@ -8271,6 +8272,7 @@ package android.graphics {
82718272
method public boolean sameAs(android.graphics.Bitmap);
82728273
method public void setDensity(int);
82738274
method public void setHasAlpha(boolean);
8275+
method public final void setHasMipMap(boolean);
82748276
method public void setPixel(int, int, int);
82758277
method public void setPixels(int[], int, int, int, int, int, int);
82768278
method public void writeToParcel(android.os.Parcel, int);
@@ -25975,6 +25977,11 @@ package android.view {
2597525977
ctor public WindowManager.BadTokenException(java.lang.String);
2597625978
}
2597725979

25980+
public static class WindowManager.InvalidDisplayException extends java.lang.RuntimeException {
25981+
ctor public WindowManager.InvalidDisplayException();
25982+
ctor public WindowManager.InvalidDisplayException(java.lang.String);
25983+
}
25984+
2597825985
public static class WindowManager.LayoutParams extends android.view.ViewGroup.LayoutParams implements android.os.Parcelable {
2597925986
ctor public WindowManager.LayoutParams();
2598025987
ctor public WindowManager.LayoutParams(int);

api/current.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16151,7 +16151,7 @@ package android.os {
1615116151

1615216152
public class Looper {
1615316153
method public void dump(android.util.Printer, java.lang.String);
16154-
method public static synchronized android.os.Looper getMainLooper();
16154+
method public static android.os.Looper getMainLooper();
1615516155
method public java.lang.Thread getThread();
1615616156
method public static void loop();
1615716157
method public static android.os.Looper myLooper();
@@ -25977,6 +25977,11 @@ package android.view {
2597725977
ctor public WindowManager.BadTokenException(java.lang.String);
2597825978
}
2597925979

25980+
public static class WindowManager.InvalidDisplayException extends java.lang.RuntimeException {
25981+
ctor public WindowManager.InvalidDisplayException();
25982+
ctor public WindowManager.InvalidDisplayException(java.lang.String);
25983+
}
25984+
2598025985
public static class WindowManager.LayoutParams extends android.view.ViewGroup.LayoutParams implements android.os.Parcelable {
2598125986
ctor public WindowManager.LayoutParams();
2598225987
ctor public WindowManager.LayoutParams(int);

core/java/android/app/Presentation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ protected void onStop() {
140140
super.onStop();
141141
}
142142

143+
/**
144+
* Inherited from {@link Dialog#show}. Will throw
145+
* {@link android.view.WindowManager.InvalidDisplayException} if the specified secondary
146+
* {@link Display} can't be found.
147+
*/
148+
@Override
149+
public void show() {
150+
super.show();
151+
}
152+
143153
/**
144154
* Called by the system when the {@link Display} to which the presentation
145155
* is attached has been removed.

core/java/android/view/ViewManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
*/
2222
public interface ViewManager
2323
{
24+
/**
25+
* Assign the passed LayoutParams to the passed View and add the view to the window.
26+
* <p>Throws {@link android.view.WindowManager.BadTokenException} for certain programming
27+
* errors, such as adding a second view to a window without removing the first view.
28+
* <p>Throws {@link android.view.WindowManager.InvalidDisplayException} if the window is on a
29+
* secondary {@link Display} and the specified display can't be found
30+
* (see {@link android.app.Presentation}).
31+
* @param view The view to be added to this window.
32+
* @param params The LayoutParams to assign to view.
33+
*/
2434
public void addView(View view, ViewGroup.LayoutParams params);
2535
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
2636
public void removeView(View view);

core/java/android/view/ViewRootImpl.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,6 @@ public void setView(View view, WindowManager.LayoutParams attrs, View panelParen
558558
mPendingVisibleInsets.set(0, 0, 0, 0);
559559
if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow);
560560
if (res < WindowManagerGlobal.ADD_OKAY) {
561-
mView = null;
562561
mAttachInfo.mRootView = null;
563562
mAdded = false;
564563
mFallbackEventHandler.setView(null);
@@ -594,6 +593,10 @@ public void setView(View view, WindowManager.LayoutParams attrs, View panelParen
594593
throw new WindowManager.BadTokenException(
595594
"Unable to add window " + mWindow +
596595
" -- permission denied for this window type");
596+
case WindowManagerGlobal.ADD_INVALID_DISPLAY:
597+
throw new WindowManager.InvalidDisplayException(
598+
"Unable to add window " + mWindow +
599+
" -- the specified display can not be found");
597600
}
598601
throw new RuntimeException(
599602
"Unable to add window -- unknown error code " + res);
@@ -810,27 +813,21 @@ void handleScreenStateChange(boolean on) {
810813
}
811814
}
812815

813-
/**
814-
* {@inheritDoc}
815-
*/
816+
@Override
816817
public void requestFitSystemWindows() {
817818
checkThread();
818819
mFitSystemWindowsRequested = true;
819820
scheduleTraversals();
820821
}
821822

822-
/**
823-
* {@inheritDoc}
824-
*/
823+
@Override
825824
public void requestLayout() {
826825
checkThread();
827826
mLayoutRequested = true;
828827
scheduleTraversals();
829828
}
830829

831-
/**
832-
* {@inheritDoc}
833-
*/
830+
@Override
834831
public boolean isLayoutRequested() {
835832
return mLayoutRequested;
836833
}
@@ -850,6 +847,7 @@ void invalidateWorld(View view) {
850847
}
851848
}
852849

850+
@Override
853851
public void invalidateChild(View child, Rect dirty) {
854852
invalidateChildInParent(null, dirty);
855853
}

core/java/android/view/WindowManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ public BadTokenException(String name) {
6161
}
6262
}
6363

64+
/**
65+
* Exception that is thrown when calling {@link #addView} to a secondary display that cannot
66+
* be found. See {@link android.app.Presentation} for more information on secondary displays.
67+
*/
68+
public static class InvalidDisplayException extends RuntimeException {
69+
public InvalidDisplayException() {
70+
}
71+
72+
public InvalidDisplayException(String name) {
73+
super(name);
74+
}
75+
}
76+
6477
/**
6578
* Returns the {@link Display} upon which this {@link WindowManager} instance
6679
* will create new windows.

core/java/android/view/WindowManagerGlobal.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public void addView(View view, ViewGroup.LayoutParams params,
186186
mSystemPropertyUpdater = new Runnable() {
187187
@Override public void run() {
188188
synchronized (mLock) {
189-
for (ViewRootImpl root : mRoots) {
190-
root.loadSystemProperties();
189+
for (ViewRootImpl viewRoot : mRoots) {
190+
viewRoot.loadSystemProperties();
191191
}
192192
}
193193
}
@@ -242,7 +242,18 @@ public void addView(View view, ViewGroup.LayoutParams params,
242242
}
243243

244244
// do this last because it fires off messages to start doing things
245-
root.setView(view, wparams, panelParentView);
245+
try {
246+
root.setView(view, wparams, panelParentView);
247+
} catch (RuntimeException e) {
248+
// BadTokenException or InvalidDisplayException, clean up.
249+
synchronized (mLock) {
250+
final int index = findViewLocked(view, false);
251+
if (index >= 0) {
252+
removeViewLocked(index, true);
253+
}
254+
}
255+
throw e;
256+
}
246257
}
247258

248259
public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
@@ -360,20 +371,18 @@ private static void removeItem(Object[] dst, Object[] src, int index) {
360371
}
361372

362373
private int findViewLocked(View view, boolean required) {
363-
synchronized (mLock) {
364-
if (mViews != null) {
365-
final int count = mViews.length;
366-
for (int i = 0; i < count; i++) {
367-
if (mViews[i] == view) {
368-
return i;
369-
}
374+
if (mViews != null) {
375+
final int count = mViews.length;
376+
for (int i = 0; i < count; i++) {
377+
if (mViews[i] == view) {
378+
return i;
370379
}
371380
}
372-
if (required) {
373-
throw new IllegalArgumentException("View not attached to window manager");
374-
}
375-
return -1;
376381
}
382+
if (required) {
383+
throw new IllegalArgumentException("View not attached to window manager");
384+
}
385+
return -1;
377386
}
378387

379388
public void startTrimMemory(int level) {

0 commit comments

Comments
 (0)