You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.0 KiB
Java
103 lines
3.0 KiB
Java
package com.xypower.mpremote;
|
|
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
|
|
import java.io.File;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import dadb.AdbKeyPair;
|
|
import dadb.Dadb;
|
|
|
|
public class AdbManager {
|
|
private static class InternalAdb {
|
|
public Dadb adb;
|
|
public String ip;
|
|
public int port;
|
|
}
|
|
|
|
private static AdbKeyPair mAdbKeyPair;
|
|
private static Object mLocker = new Object();
|
|
private static Map<String, InternalAdb> mAdbs = new HashMap<>();
|
|
|
|
public static void initManager(Context context) {
|
|
File file = new File(context.getFilesDir(), ".keypair");
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
|
|
final File pubKeyFile = new File(file, "pub.key");
|
|
final File priKeyFile = new File(file, "pri.key");
|
|
if (!priKeyFile.exists() || !pubKeyFile.exists()) {
|
|
AdbKeyPair.generate(priKeyFile, pubKeyFile);
|
|
}
|
|
|
|
mAdbKeyPair = AdbKeyPair.read(priKeyFile, pubKeyFile);
|
|
}
|
|
|
|
public static Dadb getAdb(String ip, int port) {
|
|
synchronized (mLocker) {
|
|
if (mAdbs.containsKey(ip)) {
|
|
InternalAdb adb = mAdbs.get(ip);
|
|
if (adb != null && adb.port == port) {
|
|
if (adb.adb != null) {
|
|
return adb.adb;
|
|
}
|
|
}
|
|
mAdbs.remove(ip);
|
|
if (adb != null && adb.adb != null) {
|
|
try {
|
|
adb.adb.close();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
adb.adb = null;
|
|
}
|
|
adb = null;
|
|
}
|
|
InternalAdb adb = new InternalAdb();
|
|
adb.ip = ip;
|
|
adb.port = port;
|
|
adb.adb = Dadb.create(ip, port, mAdbKeyPair);
|
|
mAdbs.put(ip, adb);
|
|
return adb.adb;
|
|
}
|
|
}
|
|
|
|
public static void releaseAdb(String ip) {
|
|
synchronized (mLocker) {
|
|
if (mAdbs.containsKey(ip)) {
|
|
InternalAdb adb = mAdbs.get(ip);
|
|
mAdbs.remove(ip);
|
|
if (adb != null && adb.adb != null) {
|
|
try {
|
|
adb.adb.close();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
adb.adb = null;
|
|
}
|
|
adb = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void clear() {
|
|
synchronized (mLocker) {
|
|
for (InternalAdb adb : mAdbs.values()) {
|
|
if (adb != null && adb.adb != null) {
|
|
try {
|
|
adb.adb.close();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
adb.adb = null;
|
|
}
|
|
adb = null;
|
|
}
|
|
mAdbs.clear();
|
|
}
|
|
}
|
|
}
|