Workaround a robelectric related NPE in SurfaceSyncGroup.addTimer

Robolectric mocks out `HandlerThread` in such a way that in certain instances `HandlerThread.getLooper` returns null. Since adding the null check is small enough, we can opt to make this change to allow robolectric tests to run.

Test: atest ClockworkSettingsRobotests
Bug: 237804605
Change-Id: Ib28f80413265a770cfac44cf451714f1b66dfbc8
This commit is contained in:
Eric Rahm
2023-05-24 18:32:21 -07:00
parent 32f42bf93b
commit 37f58660a1

View File

@@ -26,6 +26,7 @@ import android.os.Debug;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.Trace;
import android.util.ArraySet;
@@ -800,22 +801,25 @@ public final class SurfaceSyncGroup {
}
private void addTimeout() {
Looper looper = null;
synchronized (sHandlerThreadLock) {
if (sHandlerThread == null) {
sHandlerThread = new HandlerThread("SurfaceSyncGroupTimer");
sHandlerThread.start();
}
looper = sHandlerThread.getLooper();
}
synchronized (mLock) {
if (mTimeoutAdded || mTimeoutDisabled) {
if (mTimeoutAdded || mTimeoutDisabled || looper == null) {
// We only need one timeout for the entire SurfaceSyncGroup since we just want to
// ensure it doesn't stay stuck forever.
return;
}
if (mHandler == null) {
mHandler = new Handler(sHandlerThread.getLooper());
mHandler = new Handler(looper);
}
mTimeoutAdded = true;