From a066c28a1f814e62ac917d9f0d5b229cf74ef197 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Laurini Date: Sat, 20 Feb 2021 14:32:16 -0300 Subject: [PATCH] Avoid calls unavailable on API level 21 --- .../com/genymobile/scrcpy/Controller.java | 30 ++++++++++++------- .../java/com/genymobile/scrcpy/Server.java | 24 ++++++++++----- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Controller.java b/server/src/main/java/com/genymobile/scrcpy/Controller.java index 8683449b..f1cdc0b3 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Controller.java +++ b/server/src/main/java/com/genymobile/scrcpy/Controller.java @@ -152,11 +152,15 @@ public class Controller { int id = msg.getGameControllerId(); int axis = msg.getGameControllerAxis(); int value = msg.getGameControllerAxisValue(); - if (!gameControllers.contains(id)) { + + GameController controller = gameControllers.get(id); + + if (controller != null) { + controller.setAxis(axis, value); + } else { Ln.w("Received data for non-existant controller."); - break; } - gameControllers.get(id).setAxis(axis, value); + break; } break; case ControlMessage.TYPE_INJECT_GAME_CONTROLLER_BUTTON: @@ -164,11 +168,14 @@ public class Controller { int id = msg.getGameControllerId(); int button = msg.getGameControllerButton(); int state = msg.getGameControllerButtonState(); - if (!gameControllers.contains(id)) { + + GameController controller = gameControllers.get(id); + + if (controller != null) { + controller.setButton(button, state); + } else { Ln.w("Received data for non-existant controller."); - break; } - gameControllers.get(id).setButton(button, state); } break; case ControlMessage.TYPE_INJECT_GAME_CONTROLLER_DEVICE: @@ -182,12 +189,15 @@ public class Controller { break; case GameController.DEVICE_REMOVED: - if (!gameControllers.contains(id)) { + GameController controller = gameControllers.get(id); + + if (controller != null) { + controller.close(); + gameControllers.delete(id); + } else { Ln.w("Non-existant game controller removed."); - break; } - gameControllers.get(id).close(); - gameControllers.delete(id); + break; default: diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index b6940c41..19909492 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -7,13 +7,12 @@ import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.os.BatteryManager; import android.os.Build; +import android.text.TextUtils; import java.io.InputStream; +import java.io.FileOutputStream; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; +import java.nio.channels.FileChannel; import java.util.List; import java.util.Locale; @@ -30,16 +29,25 @@ public final class Server { private static void scrcpy(Options options) throws IOException { Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")"); - Ln.i("Supported ABIs: " + String.join(", ", Build.SUPPORTED_ABIS)); + Ln.i("Supported ABIs: " + TextUtils.join(", ", Build.SUPPORTED_ABIS)); final Device device = new Device(options); List codecOptions = CodecOption.parse(options.getCodecOptions()); for (String lib : NATIVE_LIBRARIES) { for (String abi : Build.SUPPORTED_ABIS) { try { - InputStream stream = Server.class.getResourceAsStream("/lib/" + abi + "/" + lib); - Path destPath = Paths.get(SERVER_DIR + "/" + lib); - Files.copy(stream, destPath, StandardCopyOption.REPLACE_EXISTING); + InputStream resStream = Server.class.getResourceAsStream("/lib/" + abi + "/" + lib); + FileOutputStream fileStream = new FileOutputStream(SERVER_DIR + "/" + lib); + + byte[] buffer = new byte[1024]; + int length; + while ((length = resStream.read(buffer)) > 0) { + fileStream.write(buffer, 0, length); + } + + resStream.close(); + fileStream.close(); + break; } catch (Exception e) { Ln.e("Could not extract native library for " + abi, e);