Skip to content

Commit

Permalink
Move workarounds execution
Browse files Browse the repository at this point in the history
Expose a single public static method to apply all necessary workarounds.
  • Loading branch information
rom1v committed Jun 19, 2023
1 parent 48a00fb commit 8bf2764
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
21 changes: 1 addition & 20 deletions server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,7 @@ private static void scrcpy(Options options) throws IOException, ConfigurationExc
boolean audio = options.getAudio();
boolean sendDummyByte = options.getSendDummyByte();

Workarounds.prepareMainLooper();

// Workarounds must be applied for Meizu phones:
// - <https://github.com/Genymobile/scrcpy/issues/240>
// - <https://github.com/Genymobile/scrcpy/issues/365>
// - <https://github.com/Genymobile/scrcpy/issues/2656>
//
// But only apply when strictly necessary, since workarounds can cause other issues:
// - <https://github.com/Genymobile/scrcpy/issues/940>
// - <https://github.com/Genymobile/scrcpy/issues/994>
if (Build.BRAND.equalsIgnoreCase("meizu")) {
Workarounds.fillAppInfo();
}

// Before Android 11, audio is not supported.
// Since Android 12, we can properly set a context on the AudioRecord.
// Only on Android 11 we must fill the application context for the AudioRecord to work.
if (audio && Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
Workarounds.fillAppContext();
}
Workarounds.apply(audio);

List<AsyncProcessor> asyncProcessors = new ArrayList<>();

Expand Down
29 changes: 26 additions & 3 deletions server/src/main/java/com/genymobile/scrcpy/Workarounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,31 @@ private Workarounds() {
// not instantiable
}

public static void apply(boolean audio) {
Workarounds.prepareMainLooper();

// Workarounds must be applied for Meizu phones:
// - <https://github.com/Genymobile/scrcpy/issues/240>
// - <https://github.com/Genymobile/scrcpy/issues/365>
// - <https://github.com/Genymobile/scrcpy/issues/2656>
//
// But only apply when strictly necessary, since workarounds can cause other issues:
// - <https://github.com/Genymobile/scrcpy/issues/940>
// - <https://github.com/Genymobile/scrcpy/issues/994>
if (Build.BRAND.equalsIgnoreCase("meizu")) {
Workarounds.fillAppInfo();
}

// Before Android 11, audio is not supported.
// Since Android 12, we can properly set a context on the AudioRecord.
// Only on Android 11 we must fill the application context for the AudioRecord to work.
if (audio && Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
Workarounds.fillAppContext();
}
}

@SuppressWarnings("deprecation")
public static void prepareMainLooper() {
private 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()"
// <https://github.com/Genymobile/scrcpy/issues/240>
Expand Down Expand Up @@ -57,7 +80,7 @@ private static void fillActivityThread() throws Exception {
}

@SuppressLint("PrivateApi,DiscouragedPrivateApi")
public static void fillAppInfo() {
private static void fillAppInfo() {
try {
fillActivityThread();

Expand Down Expand Up @@ -86,7 +109,7 @@ public static void fillAppInfo() {
}

@SuppressLint("PrivateApi,DiscouragedPrivateApi")
public static void fillAppContext() {
private static void fillAppContext() {
try {
fillActivityThread();

Expand Down

0 comments on commit 8bf2764

Please sign in to comment.