From 213c468c2032d1c1490c6a7951c999c36f452b8f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 13 Nov 2019 12:08:36 +0100 Subject: [PATCH] Move workarounds to a separate class Extract workarounds (currently only one) to a separate class to avoid polluting the main code. --- .../com/genymobile/scrcpy/ScreenEncoder.java | 11 +--------- .../com/genymobile/scrcpy/Workarounds.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 server/src/main/java/com/genymobile/scrcpy/Workarounds.java diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index e58310a1..504e9540 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -8,7 +8,6 @@ import android.media.MediaCodecInfo; import android.media.MediaFormat; import android.os.Build; import android.os.IBinder; -import android.os.Looper; import android.view.Surface; import java.io.FileDescriptor; @@ -53,15 +52,7 @@ public class ScreenEncoder implements Device.RotationListener { } public void streamScreen(Device device, FileDescriptor fd) throws IOException { - // Some devices internally create a Handler when creating an input Surface, causing an exception: - // "Can't create handler inside thread that has not called Looper.prepare()" - // - // - // Use Looper.prepareMainLooper() instead of Looper.prepare() to avoid a NullPointerException: - // "Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' - // on a null object reference" - // - Looper.prepareMainLooper(); + Workarounds.prepareMainLooper(); MediaFormat format = createFormat(bitRate, maxFps, iFrameInterval); device.setRotationListener(this); diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java new file mode 100644 index 00000000..4dbf152e --- /dev/null +++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java @@ -0,0 +1,21 @@ +package com.genymobile.scrcpy; + +import android.os.Looper; + +public final class Workarounds { + private Workarounds() { + // not instantiable + } + + public static void prepareMainLooper() { + // Some devices internally create a Handler when creating an input Surface, causing an exception: + // "Can't create handler inside thread that has not called Looper.prepare()" + // + // + // Use Looper.prepareMainLooper() instead of Looper.prepare() to avoid a NullPointerException: + // "Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' + // on a null object reference" + // + Looper.prepareMainLooper(); + } +}