Avoid calls unavailable on API level 21

This commit is contained in:
Luiz Henrique Laurini 2021-02-20 14:32:16 -03:00
commit a066c28a1f
2 changed files with 36 additions and 18 deletions

View file

@ -152,11 +152,15 @@ public class Controller {
int id = msg.getGameControllerId(); int id = msg.getGameControllerId();
int axis = msg.getGameControllerAxis(); int axis = msg.getGameControllerAxis();
int value = msg.getGameControllerAxisValue(); 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."); Ln.w("Received data for non-existant controller.");
break;
} }
gameControllers.get(id).setAxis(axis, value); break;
} }
break; break;
case ControlMessage.TYPE_INJECT_GAME_CONTROLLER_BUTTON: case ControlMessage.TYPE_INJECT_GAME_CONTROLLER_BUTTON:
@ -164,11 +168,14 @@ public class Controller {
int id = msg.getGameControllerId(); int id = msg.getGameControllerId();
int button = msg.getGameControllerButton(); int button = msg.getGameControllerButton();
int state = msg.getGameControllerButtonState(); 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."); Ln.w("Received data for non-existant controller.");
break;
} }
gameControllers.get(id).setButton(button, state);
} }
break; break;
case ControlMessage.TYPE_INJECT_GAME_CONTROLLER_DEVICE: case ControlMessage.TYPE_INJECT_GAME_CONTROLLER_DEVICE:
@ -182,12 +189,15 @@ public class Controller {
break; break;
case GameController.DEVICE_REMOVED: case GameController.DEVICE_REMOVED:
if (!gameControllers.contains(id)) { GameController controller = gameControllers.get(id);
Ln.w("Non-existant game controller removed.");
break; if (controller != null) {
} controller.close();
gameControllers.get(id).close();
gameControllers.delete(id); gameControllers.delete(id);
} else {
Ln.w("Non-existant game controller removed.");
}
break; break;
default: default:

View file

@ -7,13 +7,12 @@ import android.media.MediaCodec;
import android.media.MediaCodecInfo; import android.media.MediaCodecInfo;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import java.io.InputStream; import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -30,16 +29,25 @@ public final class Server {
private static void scrcpy(Options options) throws IOException { private static void scrcpy(Options options) throws IOException {
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")"); 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); final Device device = new Device(options);
List<CodecOption> codecOptions = CodecOption.parse(options.getCodecOptions()); List<CodecOption> codecOptions = CodecOption.parse(options.getCodecOptions());
for (String lib : NATIVE_LIBRARIES) { for (String lib : NATIVE_LIBRARIES) {
for (String abi : Build.SUPPORTED_ABIS) { for (String abi : Build.SUPPORTED_ABIS) {
try { try {
InputStream stream = Server.class.getResourceAsStream("/lib/" + abi + "/" + lib); InputStream resStream = Server.class.getResourceAsStream("/lib/" + abi + "/" + lib);
Path destPath = Paths.get(SERVER_DIR + "/" + lib); FileOutputStream fileStream = new FileOutputStream(SERVER_DIR + "/" + lib);
Files.copy(stream, destPath, StandardCopyOption.REPLACE_EXISTING);
byte[] buffer = new byte[1024];
int length;
while ((length = resStream.read(buffer)) > 0) {
fileStream.write(buffer, 0, length);
}
resStream.close();
fileStream.close();
break; break;
} catch (Exception e) { } catch (Exception e) {
Ln.e("Could not extract native library for " + abi, e); Ln.e("Could not extract native library for " + abi, e);