[Magnifier-60] Fix race condition after #dismiss

The CL fixes a race condition introduced by
I2799229bee7acfbd6236089487a5766f46605366, which caused apps to crash
when, for example, the following happened:
 1. #show(xc, yc, xm, ym) is called on the main thread, and the
    arguments are stored in mPrevShow[Source/Window]Coords
 2. #show(xc, yc, xm', ym') is called on the main thread (note that only
    the window position is different from the previous call), and we
    post a job to the pixel copy handler thread to only update the
    expected position of mWindow (without doing pixel copy)
 3. the job starts and checks that mWindow != currentWindowInstance is
    false
 4. #dismiss() is called on the main thread, mWindow is set to null
 5. the position update job at step 3 continues, grabs mLock but now mWindow is null
    and the crash happens

Bug: 113272299
Test: manual testing
Test: atest CtsWidgetTestCases:android.widget.cts.MagnifierTest
Change-Id: Ibc815888f4eeaf077fa1a9ddfb1d4d401382623a
This commit is contained in:
Mihai Popa
2018-09-03 17:25:54 +01:00
parent beeaf5589d
commit ddcd54813b

View File

@@ -215,11 +215,11 @@ public final class Magnifier {
final Point windowCoords = getCurrentClampedWindowCoordinates();
final InternalPopupWindow currentWindowInstance = mWindow;
sPixelCopyHandlerThread.getThreadHandler().post(() -> {
if (mWindow != currentWindowInstance) {
// The magnifier was dismissed (and maybe shown again) in the meantime.
return;
}
synchronized (mLock) {
if (mWindow != currentWindowInstance) {
// The magnifier was dismissed (and maybe shown again) in the meantime.
return;
}
mWindow.setContentPositionForNextDraw(windowCoords.x, windowCoords.y);
}
});