refactor: SublimePlugin
This commit is contained in:
@@ -32,7 +32,7 @@ import com.stardust.scriptdroid.R;
|
|||||||
import com.stardust.autojs.core.image.ScreenCaptureRequestActivity;
|
import com.stardust.autojs.core.image.ScreenCaptureRequestActivity;
|
||||||
import com.stardust.autojs.core.util.Shell;
|
import com.stardust.autojs.core.util.Shell;
|
||||||
import com.stardust.autojs.core.console.StardustConsole;
|
import com.stardust.autojs.core.console.StardustConsole;
|
||||||
import com.stardust.scriptdroid.sublime.SublimePluginService;
|
import com.stardust.scriptdroid.pluginclient.DevPluginService;
|
||||||
import com.stardust.util.ScreenMetrics;
|
import com.stardust.util.ScreenMetrics;
|
||||||
import com.stardust.util.Supplier;
|
import com.stardust.util.Supplier;
|
||||||
import com.stardust.util.UiHandler;
|
import com.stardust.util.UiHandler;
|
||||||
@@ -81,7 +81,7 @@ public class AutoJs {
|
|||||||
@Override
|
@Override
|
||||||
public String println(int level, CharSequence charSequence) {
|
public String println(int level, CharSequence charSequence) {
|
||||||
String log = super.println(level, charSequence);
|
String log = super.println(level, charSequence);
|
||||||
SublimePluginService.log(log);
|
DevPluginService.getInstance().log(log);
|
||||||
return log;
|
return log;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package com.stardust.scriptdroid.pluginclient;
|
||||||
|
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import com.stardust.scriptdroid.tool.SimpleObserver;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.subjects.PublishSubject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/5/10.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DevPluginClient {
|
||||||
|
|
||||||
|
public static class State {
|
||||||
|
|
||||||
|
public static final int DISCONNECTED = 0;
|
||||||
|
public static final int CONNECTING = 1;
|
||||||
|
public static final int CONNECTED = 2;
|
||||||
|
|
||||||
|
private final int mState;
|
||||||
|
private final Throwable mException;
|
||||||
|
|
||||||
|
public State(int state, Throwable exception) {
|
||||||
|
mState = state;
|
||||||
|
mException = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State(int state) {
|
||||||
|
this(state, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getState() {
|
||||||
|
return mState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Throwable getException() {
|
||||||
|
return mException;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final State STATE_CONNECTED = new State(State.CONNECTED);
|
||||||
|
private static final State STATE_CONNECTING = new State(State.CONNECTING);
|
||||||
|
|
||||||
|
private volatile Socket mSocket;
|
||||||
|
private Handler mResponseHandler;
|
||||||
|
private String host;
|
||||||
|
private int port;
|
||||||
|
private OutputStream mOutputStream;
|
||||||
|
private final PublishSubject<State> mConnection;
|
||||||
|
private int mState;
|
||||||
|
|
||||||
|
public DevPluginClient(String host, int port, PublishSubject<State> connection) {
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
mConnection = connection;
|
||||||
|
mConnection.subscribe(state -> mState = state.getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getState() {
|
||||||
|
return mState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponseHandler(Handler handler) {
|
||||||
|
mResponseHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectToServer() {
|
||||||
|
if (mState != State.DISCONNECTED) {
|
||||||
|
throw new IllegalStateException("Connecting or Connected!");
|
||||||
|
}
|
||||||
|
new Thread(() -> {
|
||||||
|
if (mState != State.DISCONNECTED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mConnection.onNext(STATE_CONNECTING);
|
||||||
|
try {
|
||||||
|
connect();
|
||||||
|
if (mSocket != null)
|
||||||
|
readDataFromSocket();
|
||||||
|
close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
close();
|
||||||
|
mConnection.onNext(new State(State.DISCONNECTED, e));
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void readDataFromSocket() throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
|
||||||
|
JsonParser parser = new JsonParser();
|
||||||
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
|
String line = reader.readLine();
|
||||||
|
if (line == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mResponseHandler != null) {
|
||||||
|
handleData(parser, line);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleData(JsonParser parser, String line) {
|
||||||
|
try {
|
||||||
|
JsonElement jsonElement = parser.parse(line);
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
mResponseHandler.handle(jsonObject);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
//ignore exception thrown by data handler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connect() throws IOException {
|
||||||
|
mSocket = new Socket(host, port);
|
||||||
|
mConnection.onNext(STATE_CONNECTED);
|
||||||
|
mOutputStream = mSocket.getOutputStream();
|
||||||
|
sendDeviceName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendDeviceName() {
|
||||||
|
JsonObject object = new JsonObject();
|
||||||
|
object.addProperty("type", "device_name");
|
||||||
|
object.addProperty("device_name", Build.BRAND + " " + Build.MODEL);
|
||||||
|
send(object).subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Observable<JsonObject> send(final JsonObject object) {
|
||||||
|
if (mState != State.CONNECTED) {
|
||||||
|
throw new IllegalStateException("Not connected!");
|
||||||
|
}
|
||||||
|
return Observable.fromCallable(() -> {
|
||||||
|
mOutputStream.write(object.toString().getBytes());
|
||||||
|
mOutputStream.write("\n".getBytes());
|
||||||
|
mOutputStream.flush();
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Observable<JsonObject> send(Map<String, Object> data) {
|
||||||
|
JsonObject object = new JsonObject();
|
||||||
|
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||||
|
Object value = entry.getValue();
|
||||||
|
if (value instanceof Number) {
|
||||||
|
object.addProperty(entry.getKey(), (Number) value);
|
||||||
|
} else if (value instanceof Boolean) {
|
||||||
|
object.addProperty(entry.getKey(), (Boolean) value);
|
||||||
|
} else if (value instanceof Character) {
|
||||||
|
object.addProperty(entry.getKey(), (Character) value);
|
||||||
|
} else {
|
||||||
|
object.addProperty(entry.getKey(), value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return send(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean close() {
|
||||||
|
if (mSocket != null) {
|
||||||
|
try {
|
||||||
|
mSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
mSocket = null;
|
||||||
|
mOutputStream = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.stardust.scriptdroid.sublime;
|
package com.stardust.scriptdroid.pluginclient;
|
||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.SparseArray;
|
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonNull;
|
import com.google.gson.JsonNull;
|
||||||
@@ -21,56 +20,40 @@ import java.util.HashMap;
|
|||||||
* Created by Stardust on 2017/5/11.
|
* Created by Stardust on 2017/5/11.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class SublimeResponseHandler implements Handler {
|
public class DevPluginResponseHandler implements Handler {
|
||||||
|
|
||||||
|
|
||||||
private Router mRouter = new Router("type")
|
private Router mRouter = new Router("type")
|
||||||
.handler("command", new Router("command")
|
.handler("command", new Router("command")
|
||||||
.handler("run", new Handler() {
|
.handler("run", data -> {
|
||||||
@Override
|
String script = data.get("script").getAsString();
|
||||||
public boolean handle(JsonObject data) {
|
String name = getName(data);
|
||||||
String script = data.get("script").getAsString();
|
String viewId = data.get("view_id").getAsString();
|
||||||
String name = getName(data);
|
runScript(viewId, name, script);
|
||||||
String viewId = data.get("view_id").getAsString();
|
return false;
|
||||||
runScript(viewId, name, script);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.handler("stop", new Handler() {
|
.handler("stop", data -> {
|
||||||
@Override
|
String viewId = data.get("view_id").getAsString();
|
||||||
public boolean handle(JsonObject data) {
|
stopScript(viewId);
|
||||||
String viewId = data.get("view_id").getAsString();
|
return true;
|
||||||
stopScript(viewId);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.handler("save", new Handler() {
|
.handler("save", data -> {
|
||||||
@Override
|
String script = data.get("script").getAsString();
|
||||||
public boolean handle(JsonObject data) {
|
String name = getName(data);
|
||||||
String script = data.get("script").getAsString();
|
saveScript(name, script);
|
||||||
String name = getName(data);
|
return false;
|
||||||
saveScript(name, script);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.handler("rerun", new Handler() {
|
.handler("rerun", data -> {
|
||||||
|
String viewId = data.get("view_id").getAsString();
|
||||||
@Override
|
String script = data.get("script").getAsString();
|
||||||
public boolean handle(JsonObject data) {
|
String name = getName(data);
|
||||||
String viewId = data.get("view_id").getAsString();
|
stopScript(viewId);
|
||||||
String script = data.get("script").getAsString();
|
runScript(viewId, name, script);
|
||||||
String name = getName(data);
|
return false;
|
||||||
stopScript(viewId);
|
|
||||||
runScript(viewId, name, script);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.handler("stopAll", new Handler() {
|
.handler("stopAll", data -> {
|
||||||
@Override
|
AutoJs.getInstance().getScriptEngineService().stopAllAndToast();
|
||||||
public boolean handle(JsonObject data) {
|
return false;
|
||||||
AutoJs.getInstance().getScriptEngineService().stopAllAndToast();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package com.stardust.scriptdroid.pluginclient;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
import io.reactivex.subjects.PublishSubject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/5/11.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DevPluginService {
|
||||||
|
|
||||||
|
private static DevPluginService sInstance = new DevPluginService();
|
||||||
|
private DevPluginClient mClient;
|
||||||
|
private final PublishSubject<DevPluginClient.State> mConnection = PublishSubject.create();
|
||||||
|
|
||||||
|
public static DevPluginService getInstance() {
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return mClient != null && mClient.getState() == DevPluginClient.State.CONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDisconnected(){
|
||||||
|
return mClient == null || mClient.getState() == DevPluginClient.State.DISCONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnectIfNeeded() {
|
||||||
|
if (isDisconnected())
|
||||||
|
return;
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect() {
|
||||||
|
mClient.close();
|
||||||
|
mClient = null;
|
||||||
|
mConnection.onNext(new DevPluginClient.State(DevPluginClient.State.DISCONNECTED));
|
||||||
|
}
|
||||||
|
|
||||||
|
public PublishSubject<DevPluginClient.State> getConnection() {
|
||||||
|
return mConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectToServer(String host) {
|
||||||
|
mClient = new DevPluginClient(host, 1209, mConnection);
|
||||||
|
mClient.setResponseHandler(new DevPluginResponseHandler());
|
||||||
|
mClient.connectToServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void log(String log) {
|
||||||
|
if (!isConnected())
|
||||||
|
return;
|
||||||
|
JsonObject object = new JsonObject();
|
||||||
|
object.addProperty("type", "log");
|
||||||
|
object.addProperty("log", log);
|
||||||
|
mClient.send(object)
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.subscribe(ignored -> {
|
||||||
|
}, Throwable::printStackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.stardust.scriptdroid.sublime;
|
package com.stardust.scriptdroid.pluginclient;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.stardust.scriptdroid.sublime;
|
package com.stardust.scriptdroid.pluginclient;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
package com.stardust.scriptdroid.sublime;
|
|
||||||
|
|
||||||
import android.os.Build;
|
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
|
||||||
import org.reactivestreams.Subscriber;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
import io.reactivex.Observable;
|
|
||||||
import io.reactivex.subjects.PublishSubject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Stardust on 2017/5/10.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class SublimePluginClient {
|
|
||||||
|
|
||||||
|
|
||||||
private volatile Socket mSocket;
|
|
||||||
private Handler mResponseHandler;
|
|
||||||
private String host;
|
|
||||||
private int port;
|
|
||||||
private OutputStream mOutputStream;
|
|
||||||
private final PublishSubject<Boolean> mConnectionState;
|
|
||||||
|
|
||||||
public SublimePluginClient(String host, int port, PublishSubject<Boolean> connectionState) {
|
|
||||||
this.host = host;
|
|
||||||
this.port = port;
|
|
||||||
mConnectionState = connectionState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setResponseHandler(Handler handler) {
|
|
||||||
mResponseHandler = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Observable<Void> listen() {
|
|
||||||
if (mSocket != null) {
|
|
||||||
throw new IllegalStateException("Socket listening ");
|
|
||||||
}
|
|
||||||
return Observable.fromPublisher(new Publisher<Void>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void subscribe(Subscriber<? super Void> s) {
|
|
||||||
if (mSocket != null) {
|
|
||||||
s.onError(new IllegalStateException("Socket listening "));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
listenInternal(s);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void listenInternal(final Subscriber<? super Void> s) {
|
|
||||||
try {
|
|
||||||
mSocket = new Socket(host, port);
|
|
||||||
mConnectionState.onNext(true);
|
|
||||||
mSocket.setTcpNoDelay(true);
|
|
||||||
mOutputStream = mSocket.getOutputStream();
|
|
||||||
s.onComplete();
|
|
||||||
sendDeviceName();
|
|
||||||
} catch (IOException e) {
|
|
||||||
s.onError(e);
|
|
||||||
tryClose();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
readLoop(mSocket.getInputStream());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}finally {
|
|
||||||
tryClose();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendDeviceName() {
|
|
||||||
JsonObject object = new JsonObject();
|
|
||||||
object.addProperty("type", "device_name");
|
|
||||||
object.addProperty("device_name", Build.MODEL);
|
|
||||||
send(object).subscribe();
|
|
||||||
}
|
|
||||||
|
|
||||||
public PublishSubject<Boolean> getConnectionState() {
|
|
||||||
return mConnectionState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isListening() {
|
|
||||||
return mSocket != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void tryClose() {
|
|
||||||
try {
|
|
||||||
close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Observable<JsonObject> send(final JsonObject object) {
|
|
||||||
if (mSocket == null) {
|
|
||||||
throw new IllegalStateException("Socket is not listening ");
|
|
||||||
}
|
|
||||||
return Observable.fromCallable(() -> {
|
|
||||||
mOutputStream.write(object.toString().getBytes());
|
|
||||||
mOutputStream.write("\n".getBytes());
|
|
||||||
mOutputStream.flush();
|
|
||||||
return object;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close() throws IOException {
|
|
||||||
if (mSocket != null) {
|
|
||||||
mSocket.close();
|
|
||||||
mSocket = null;
|
|
||||||
mOutputStream = null;
|
|
||||||
mConnectionState.onNext(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void readLoop(InputStream stream) throws IOException {
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
|
||||||
JsonParser parser = new JsonParser();
|
|
||||||
while (!Thread.currentThread().isInterrupted()) {
|
|
||||||
String line = reader.readLine();
|
|
||||||
if (line == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (mResponseHandler != null) {
|
|
||||||
JsonElement jsonElement = parser.parse(line);
|
|
||||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
||||||
mResponseHandler.handle(jsonObject);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
package com.stardust.scriptdroid.sublime;
|
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
|
|
||||||
import io.reactivex.Observable;
|
|
||||||
import io.reactivex.Scheduler;
|
|
||||||
import io.reactivex.schedulers.Schedulers;
|
|
||||||
import io.reactivex.subjects.PublishSubject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Stardust on 2017/5/11.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class SublimePluginService {
|
|
||||||
|
|
||||||
private static SublimePluginClient client;
|
|
||||||
private static final PublishSubject<Boolean> CONNECTION_STATE = PublishSubject.create();
|
|
||||||
|
|
||||||
public static boolean isConnected() {
|
|
||||||
return client != null && client.isListening();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void disconnectIfNeeded() {
|
|
||||||
if (!isConnected())
|
|
||||||
return;
|
|
||||||
disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void disconnect() {
|
|
||||||
try {
|
|
||||||
client.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
client = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static PublishSubject<Boolean> getConnectionState() {
|
|
||||||
return CONNECTION_STATE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Observable<Void> connect(String host) {
|
|
||||||
client = new SublimePluginClient(host, 1209, CONNECTION_STATE);
|
|
||||||
client.setResponseHandler(new SublimeResponseHandler());
|
|
||||||
return client.listen();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void log(String log) {
|
|
||||||
if (!isConnected())
|
|
||||||
return;
|
|
||||||
JsonObject object = new JsonObject();
|
|
||||||
object.addProperty("type", "log");
|
|
||||||
object.addProperty("log", log);
|
|
||||||
client.send(object)
|
|
||||||
.subscribeOn(Schedulers.io())
|
|
||||||
.subscribe(ignored -> {
|
|
||||||
}, Throwable::printStackTrace);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,6 @@ import android.widget.TextView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.afollestad.materialdialogs.MaterialDialog;
|
import com.afollestad.materialdialogs.MaterialDialog;
|
||||||
import com.bumptech.glide.Glide;
|
|
||||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||||
import com.bumptech.glide.request.target.SimpleTarget;
|
import com.bumptech.glide.request.target.SimpleTarget;
|
||||||
import com.bumptech.glide.request.transition.Transition;
|
import com.bumptech.glide.request.transition.Transition;
|
||||||
@@ -18,7 +17,7 @@ import com.stardust.scriptdroid.App;
|
|||||||
import com.stardust.scriptdroid.Pref;
|
import com.stardust.scriptdroid.Pref;
|
||||||
import com.stardust.scriptdroid.R;
|
import com.stardust.scriptdroid.R;
|
||||||
import com.stardust.scriptdroid.network.GlideApp;
|
import com.stardust.scriptdroid.network.GlideApp;
|
||||||
import com.stardust.scriptdroid.network.UserService;
|
import com.stardust.scriptdroid.pluginclient.DevPluginClient;
|
||||||
import com.stardust.scriptdroid.ui.floating.CircularMenu;
|
import com.stardust.scriptdroid.ui.floating.CircularMenu;
|
||||||
import com.stardust.scriptdroid.ui.floating.FloatyWindowManger;
|
import com.stardust.scriptdroid.ui.floating.FloatyWindowManger;
|
||||||
import com.stardust.scriptdroid.network.NodeBB;
|
import com.stardust.scriptdroid.network.NodeBB;
|
||||||
@@ -36,7 +35,7 @@ import com.stardust.scriptdroid.ui.widget.AvatarView;
|
|||||||
import com.stardust.theme.ThemeColorManager;
|
import com.stardust.theme.ThemeColorManager;
|
||||||
import com.stardust.theme.ThemeColorManagerCompat;
|
import com.stardust.theme.ThemeColorManagerCompat;
|
||||||
import com.stardust.view.accessibility.AccessibilityService;
|
import com.stardust.view.accessibility.AccessibilityService;
|
||||||
import com.stardust.scriptdroid.sublime.SublimePluginService;
|
import com.stardust.scriptdroid.pluginclient.DevPluginService;
|
||||||
import com.stardust.scriptdroid.tool.AccessibilityServiceTool;
|
import com.stardust.scriptdroid.tool.AccessibilityServiceTool;
|
||||||
import com.stardust.scriptdroid.tool.WifiTool;
|
import com.stardust.scriptdroid.tool.WifiTool;
|
||||||
import com.stardust.util.IntentUtil;
|
import com.stardust.util.IntentUtil;
|
||||||
@@ -95,23 +94,25 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
mConnectionStateDisposable = SublimePluginService.getConnectionState()
|
mConnectionStateDisposable = DevPluginService.getInstance().getConnection()
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.doOnNext(connected -> mConnectionItem.getSwitchCompat().setChecked(connected))
|
.subscribe(state -> {
|
||||||
.subscribe();
|
if (mConnectionItem != null) {
|
||||||
|
mConnectionItem.getSwitchCompat().setChecked(state.getState() == DevPluginClient.State.CONNECTED, false);
|
||||||
|
mConnectionItem.setProgress(state.getState() == DevPluginClient.State.CONNECTING);
|
||||||
|
}
|
||||||
|
if (state.getException() != null) {
|
||||||
|
showMessage(state.getException().getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
EventBus.getDefault().register(this);
|
EventBus.getDefault().register(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
syncSwitchState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
syncSwitchState();
|
||||||
syncUserInfo();
|
syncUserInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +122,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
|||||||
if (Pref.isFloatingMenuShown()) {
|
if (Pref.isFloatingMenuShown()) {
|
||||||
mFloatingWindowItem.getSwitchCompat().setChecked(true);
|
mFloatingWindowItem.getSwitchCompat().setChecked(true);
|
||||||
}
|
}
|
||||||
|
mConnectionItem.getSwitchCompat().setChecked(DevPluginService.getInstance().isConnected(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void syncUserInfo() {
|
private void syncUserInfo() {
|
||||||
@@ -238,11 +240,11 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
|||||||
@Click(R.id.debug)
|
@Click(R.id.debug)
|
||||||
void connectOrDisconnectToRemote() {
|
void connectOrDisconnectToRemote() {
|
||||||
boolean checked = mConnectionItem.getSwitchCompat().isChecked();
|
boolean checked = mConnectionItem.getSwitchCompat().isChecked();
|
||||||
boolean connected = SublimePluginService.isConnected();
|
boolean connected = DevPluginService.getInstance().isConnected();
|
||||||
if (checked && !connected) {
|
if (checked && !connected) {
|
||||||
inputRemoteHost();
|
inputRemoteHost();
|
||||||
} else if (!checked && connected) {
|
} else if (!checked && connected) {
|
||||||
SublimePluginService.disconnectIfNeeded();
|
DevPluginService.getInstance().disconnectIfNeeded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,28 +266,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectToRemote(String host) {
|
private void connectToRemote(String host) {
|
||||||
mConnectionItem.setProgress(true);
|
DevPluginService.getInstance().connectToServer(host);
|
||||||
Toast.makeText(App.getApp(), R.string.text_connecting, Toast.LENGTH_SHORT).show();
|
|
||||||
SublimePluginService.connect(host)
|
|
||||||
.subscribeOn(Schedulers.io())
|
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
|
||||||
.subscribe(new SimpleObserver<Void>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
|
|
||||||
if (isHidden()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Toast.makeText(App.getApp(), e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
||||||
mConnectionItem.getSwitchCompat().setChecked(false, false);
|
|
||||||
mConnectionItem.setProgress(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onComplete() {
|
|
||||||
mConnectionItem.setProgress(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Click(R.id.check_for_updates)
|
@Click(R.id.check_for_updates)
|
||||||
@@ -350,6 +331,13 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void showMessage(CharSequence text) {
|
||||||
|
if (getContext() == null)
|
||||||
|
return;
|
||||||
|
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|||||||
@@ -73,12 +73,12 @@ public class PrefSwitch extends SwitchCompat implements SharedPreferences.OnShar
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setChecked(boolean checked) {
|
public void setChecked(boolean checked) {
|
||||||
setChecked(checked, true);
|
super.setChecked(checked);
|
||||||
|
notifyPrefChanged(checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setChecked(boolean checked, boolean notifyChange) {
|
public void setChecked(boolean checked, boolean notifyChange) {
|
||||||
super.setChecked(checked);
|
super.setChecked(checked, notifyChange);
|
||||||
if (notifyChange) {
|
if (notifyChange) {
|
||||||
notifyPrefChanged(checked);
|
notifyPrefChanged(checked);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -281,6 +281,7 @@
|
|||||||
<string name="text_reset_fail">重置失败</string>
|
<string name="text_reset_fail">重置失败</string>
|
||||||
<string name="text_reset_succeed">重置成功</string>
|
<string name="text_reset_succeed">重置成功</string>
|
||||||
<string name="text_cannot_read_file" formatted="true">无法读取文件: %s</string>
|
<string name="text_cannot_read_file" formatted="true">无法读取文件: %s</string>
|
||||||
|
<string name="text_connected">已连接</string>
|
||||||
|
|
||||||
|
|
||||||
<string-array name="ad_showing_mode_keys">
|
<string-array name="ad_showing_mode_keys">
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:textIsSelectable="true"
|
||||||
android:textSize="15sp">
|
android:textSize="15sp">
|
||||||
|
|
||||||
</TextView>
|
</TextView>
|
||||||
Reference in New Issue
Block a user