mirror of
https://github.com/revanced/jadb.git
synced 2024-11-19 10:39:23 +01:00
Merge pull request #91 from janosvitok/fix-sonar-findings
Fix sonar findings
This commit is contained in:
commit
5750321eaa
@ -32,11 +32,11 @@ public class AdbServerLauncher {
|
||||
}
|
||||
|
||||
private static String findAdbExecutable(Map<String, String> environment) {
|
||||
String android_home = environment.get("ANDROID_HOME");
|
||||
if (android_home == null || android_home.equals("")) {
|
||||
String androidHome = environment.get("ANDROID_HOME");
|
||||
if (androidHome == null || androidHome.equals("")) {
|
||||
return "adb";
|
||||
}
|
||||
return android_home + "/platform-tools/adb";
|
||||
return androidHome + "/platform-tools/adb";
|
||||
}
|
||||
|
||||
public void launch() throws IOException, InterruptedException {
|
||||
|
@ -18,6 +18,7 @@ public class DeviceWatcher implements Runnable {
|
||||
watch();
|
||||
}
|
||||
|
||||
@SuppressWarnings("squid:S2189") // watcher is stopped by closing transport
|
||||
public void watch() {
|
||||
try {
|
||||
while (true) {
|
||||
|
@ -41,9 +41,9 @@ class HostConnectToRemoteTcpDevice {
|
||||
void validate(String response) throws ConnectionToRemoteDeviceException;
|
||||
}
|
||||
|
||||
final static class ResponseValidatorImp implements ResponseValidator {
|
||||
private final static String SUCCESSFULLY_CONNECTED = "connected to";
|
||||
private final static String ALREADY_CONNECTED = "already connected to";
|
||||
static final class ResponseValidatorImp implements ResponseValidator {
|
||||
private static final String SUCCESSFULLY_CONNECTED = "connected to";
|
||||
private static final String ALREADY_CONNECTED = "already connected to";
|
||||
|
||||
|
||||
ResponseValidatorImp() {
|
||||
@ -64,7 +64,7 @@ class HostConnectToRemoteTcpDevice {
|
||||
}
|
||||
|
||||
private String extractError(String response) {
|
||||
int lastColon = response.lastIndexOf(":");
|
||||
int lastColon = response.lastIndexOf(':');
|
||||
if (lastColon != -1) {
|
||||
return response.substring(lastColon, response.length());
|
||||
} else {
|
||||
|
@ -41,10 +41,9 @@ public class HostDisconnectFromRemoteTcpDevice {
|
||||
void validate(String response) throws ConnectionToRemoteDeviceException;
|
||||
}
|
||||
|
||||
final static class ResponseValidatorImp implements ResponseValidator {
|
||||
private final static String SUCCESSFULLY_DISCONNECTED = "disconnected";
|
||||
private final static String ALREADY_DISCONNECTED = "error: no such device";
|
||||
|
||||
static final class ResponseValidatorImp implements ResponseValidator {
|
||||
private static final String SUCCESSFULLY_DISCONNECTED = "disconnected";
|
||||
private static final String ALREADY_DISCONNECTED = "error: no such device";
|
||||
|
||||
ResponseValidatorImp() {
|
||||
}
|
||||
@ -64,7 +63,7 @@ public class HostDisconnectFromRemoteTcpDevice {
|
||||
}
|
||||
|
||||
private String extractError(String response) {
|
||||
int lastColon = response.lastIndexOf(":");
|
||||
int lastColon = response.lastIndexOf(':');
|
||||
if (lastColon != -1) {
|
||||
return response.substring(lastColon, response.length());
|
||||
} else {
|
||||
|
@ -74,7 +74,7 @@ public class JadbConnection implements ITransportFactory {
|
||||
|
||||
public List<JadbDevice> parseDevices(String body) {
|
||||
String[] lines = body.split("\n");
|
||||
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
|
||||
ArrayList<JadbDevice> devices = new ArrayList<>(lines.length);
|
||||
for (String line : lines) {
|
||||
String[] parts = line.split("\t");
|
||||
if (parts.length > 1) {
|
||||
|
@ -7,13 +7,14 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JadbDevice {
|
||||
@SuppressWarnings("squid:S00115")
|
||||
public enum State {
|
||||
Unknown,
|
||||
Offline,
|
||||
Device,
|
||||
Recovery,
|
||||
BootLoader
|
||||
};
|
||||
}
|
||||
|
||||
private final String serial;
|
||||
private final ITransportFactory transportFactory;
|
||||
@ -150,7 +151,7 @@ public class JadbDevice {
|
||||
SyncTransport sync = transport.startSync();
|
||||
sync.send("LIST", remotePath);
|
||||
|
||||
List<RemoteFile> result = new ArrayList<RemoteFile>();
|
||||
List<RemoteFile> result = new ArrayList<>();
|
||||
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) {
|
||||
result.add(dent);
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class LookBackFilteringOutputStream extends FilterOutputStream {
|
||||
{
|
||||
super(inner);
|
||||
this.lookBackBufferSize = lookBackBufferSize;
|
||||
this.buffer = new ArrayDeque<Byte>(lookBackBufferSize);
|
||||
this.buffer = new ArrayDeque<>(lookBackBufferSize);
|
||||
}
|
||||
|
||||
protected void unwrite() {
|
||||
|
@ -21,10 +21,7 @@ public class RemoteFile {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
RemoteFile that = (RemoteFile) o;
|
||||
|
||||
if (!path.equals(that.path)) return false;
|
||||
|
||||
return true;
|
||||
return path.equals(that.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -7,6 +7,10 @@ import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class Stream {
|
||||
private Stream() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static void copy(InputStream in, OutputStream out) throws IOException {
|
||||
byte[] buffer = new byte[1024 * 10];
|
||||
int len;
|
||||
|
@ -1,6 +1,10 @@
|
||||
package se.vidstige.jadb.managers;
|
||||
|
||||
public class Bash {
|
||||
private Bash() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static String quote(String s) {
|
||||
// Check that s contains no whitespace
|
||||
if (s.matches("\\S+")) {
|
||||
|
@ -6,7 +6,7 @@ import se.vidstige.jadb.RemoteFile;
|
||||
import se.vidstige.jadb.Stream;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -22,10 +22,8 @@ public class PackageManager {
|
||||
}
|
||||
|
||||
public List<Package> getPackages() throws IOException, JadbException {
|
||||
ArrayList<Package> result = new ArrayList<Package>();
|
||||
BufferedReader input = null;
|
||||
try {
|
||||
input = new BufferedReader(new InputStreamReader(device.executeShell("pm", "list", "packages"), Charset.forName("UTF-8")));
|
||||
try (BufferedReader input = new BufferedReader(new InputStreamReader(device.executeShell("pm", "list", "packages"), StandardCharsets.UTF_8))) {
|
||||
ArrayList<Package> result = new ArrayList<>();
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
final String prefix = "package:";
|
||||
@ -33,10 +31,8 @@ public class PackageManager {
|
||||
result.add(new Package(line.substring(prefix.length())));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (input != null) input.close();
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getErrorMessage(String operation, String target, String errorMessage) {
|
||||
@ -49,7 +45,7 @@ public class PackageManager {
|
||||
|
||||
public void remove(RemoteFile file) throws IOException, JadbException {
|
||||
InputStream s = device.executeShell("rm", "-f", Bash.quote(file.getPath()));
|
||||
Stream.readAll(s, Charset.forName("UTF-8"));
|
||||
Stream.readAll(s, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void install(File apkFile, List<String> extraArguments) throws IOException, JadbException {
|
||||
@ -60,7 +56,7 @@ public class PackageManager {
|
||||
arguments.addAll(extraArguments);
|
||||
arguments.add(remote.getPath());
|
||||
InputStream s = device.executeShell("pm", arguments.toArray(new String[arguments.size()]));
|
||||
String result = Stream.readAll(s, Charset.forName("UTF-8"));
|
||||
String result = Stream.readAll(s, StandardCharsets.UTF_8);
|
||||
remove(remote);
|
||||
verifyOperation("install", apkFile.getName(), result);
|
||||
}
|
||||
@ -84,7 +80,7 @@ public class PackageManager {
|
||||
|
||||
public void uninstall(Package name) throws IOException, JadbException {
|
||||
InputStream s = device.executeShell("pm", "uninstall", name.toString());
|
||||
String result = Stream.readAll(s, Charset.forName("UTF-8"));
|
||||
String result = Stream.readAll(s, StandardCharsets.UTF_8);
|
||||
verifyOperation("uninstall", name.toString(), result);
|
||||
}
|
||||
|
||||
@ -116,15 +112,18 @@ public class PackageManager {
|
||||
public static final InstallOption ALLOW_TEST_APK =
|
||||
new InstallOption("-t");
|
||||
|
||||
@SuppressWarnings("squid:S00100")
|
||||
public static InstallOption WITH_INSTALLER_PACKAGE_NAME(String name)
|
||||
{
|
||||
return new InstallOption("-t", name);
|
||||
}
|
||||
|
||||
@SuppressWarnings("squid:S00100")
|
||||
public static InstallOption ON_SHARED_MASS_STORAGE(String name) {
|
||||
return new InstallOption("-s", name);
|
||||
}
|
||||
|
||||
@SuppressWarnings("squid:S00100")
|
||||
public static InstallOption ON_INTERNAL_SYSTEM_MEMORY(String name) {
|
||||
return new InstallOption("-f", name);
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import se.vidstige.jadb.SyncTransport;
|
||||
import java.io.*;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
class AdbProtocolHandler implements Runnable {
|
||||
private final Socket socket;
|
||||
@ -43,12 +43,12 @@ class AdbProtocolHandler implements Runnable {
|
||||
while (true) {
|
||||
byte[] buffer = new byte[4];
|
||||
input.readFully(buffer);
|
||||
String encodedLength = new String(buffer, Charset.forName("utf-8"));
|
||||
String encodedLength = new String(buffer, StandardCharsets.UTF_8);
|
||||
int length = Integer.parseInt(encodedLength, 16);
|
||||
|
||||
buffer = new byte[length];
|
||||
input.readFully(buffer);
|
||||
String command = new String(buffer, Charset.forName("utf-8"));
|
||||
String command = new String(buffer, StandardCharsets.UTF_8);
|
||||
|
||||
responder.onCommand(command);
|
||||
|
||||
@ -67,7 +67,7 @@ class AdbProtocolHandler implements Runnable {
|
||||
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
|
||||
}
|
||||
output.writeBytes("OKAY");
|
||||
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8")));
|
||||
send(output, new String(tmp.toByteArray(), StandardCharsets.UTF_8));
|
||||
} else if (command.startsWith("host:transport:")) {
|
||||
String serial = command.substring("host:transport:".length());
|
||||
selected = findDevice(serial);
|
||||
@ -133,7 +133,7 @@ class AdbProtocolHandler implements Runnable {
|
||||
private String readString(DataInput input, int length) throws IOException {
|
||||
byte[] responseBuffer = new byte[length];
|
||||
input.readFully(responseBuffer);
|
||||
return new String(responseBuffer, Charset.forName("utf-8"));
|
||||
return new String(responseBuffer, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void sync(DataOutput output, DataInput input) throws IOException, JadbException {
|
||||
|
@ -29,6 +29,7 @@ public abstract class SocketServer implements Runnable {
|
||||
return port;
|
||||
}
|
||||
|
||||
@SuppressWarnings("squid:S2189") // server is stopped by closing SocketServer
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@ -44,19 +45,20 @@ public abstract class SocketServer implements Runnable {
|
||||
clientThread.start();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Empty on purpose
|
||||
}
|
||||
}
|
||||
|
||||
private void serverReady() {
|
||||
synchronized (lockObject) {
|
||||
isStarted = true;
|
||||
lockObject.notify();
|
||||
lockObject.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void waitForServer() throws InterruptedException {
|
||||
synchronized (lockObject) {
|
||||
if (!isStarted) {
|
||||
while (!isStarted) {
|
||||
lockObject.wait();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user