fix: connection and log of SublimePluginClient
add: VSCode Extension support
This commit is contained in:
3
.idea/modules.xml
generated
3
.idea/modules.xml
generated
@@ -2,7 +2,8 @@
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/NoRootScriptDroid.iml" filepath="$PROJECT_DIR$/NoRootScriptDroid.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Auto.js.iml" filepath="$PROJECT_DIR$/Auto.js.iml" />
|
||||
<module fileurl="file://C:\Users\Stardust\Documents\AndroidProjects\Auto.js\NoRootScriptDroid.iml" filepath="C:\Users\Stardust\Documents\AndroidProjects\Auto.js\NoRootScriptDroid.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/autojs/autojs.iml" filepath="$PROJECT_DIR$/autojs/autojs.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/automator/automator.iml" filepath="$PROJECT_DIR$/automator/automator.iml" />
|
||||
|
||||
@@ -9,8 +9,8 @@ android {
|
||||
applicationId "com.stardust.scriptdroid"
|
||||
minSdkVersion 17
|
||||
targetSdkVersion 23
|
||||
versionCode 215
|
||||
versionName "3.0.0 Alpha16"
|
||||
versionCode 217
|
||||
versionName "3.0.0 Alpha17"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
multiDexEnabled true
|
||||
ndk {
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.stardust.scriptdroid.R;
|
||||
import com.stardust.autojs.core.image.ScreenCaptureRequestActivity;
|
||||
import com.stardust.autojs.core.util.Shell;
|
||||
import com.stardust.autojs.core.console.StardustConsole;
|
||||
import com.stardust.scriptdroid.sublime.SublimePluginService;
|
||||
import com.stardust.util.ScreenMetrics;
|
||||
import com.stardust.util.Supplier;
|
||||
import com.stardust.util.UiHandler;
|
||||
@@ -76,7 +77,14 @@ public class AutoJs {
|
||||
mContext = context;
|
||||
mUiHandler = new UiHandler(context);
|
||||
mAppUtils = new AppUtils(context);
|
||||
mGlobalConsole = new GlobalStardustConsole(mUiHandler);
|
||||
mGlobalConsole = new GlobalStardustConsole(mUiHandler){
|
||||
@Override
|
||||
public String println(int level, CharSequence charSequence) {
|
||||
String log = super.println(level, charSequence);
|
||||
SublimePluginService.log(log);
|
||||
return log;
|
||||
}
|
||||
};
|
||||
mNotificationObserver = new NotificationListener.Observer(context);
|
||||
mAccessibilityInfoProvider = new AccessibilityInfoProvider(context.getPackageManager());
|
||||
mScriptEngineService = buildScriptEngineService();
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package com.stardust.scriptdroid.sublime;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Build;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.stardust.enhancedfloaty.FloatyWindow;
|
||||
import com.stardust.enhancedfloaty.ResizableFloaty;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.reactivestreams.Subscriber;
|
||||
|
||||
@@ -72,15 +69,29 @@ public class SublimePluginClient {
|
||||
mSocket.setTcpNoDelay(true);
|
||||
mOutputStream = mSocket.getOutputStream();
|
||||
s.onComplete();
|
||||
readLoop(mSocket.getInputStream());
|
||||
sendDeviceName();
|
||||
} catch (IOException e) {
|
||||
s.onError(e);
|
||||
} finally {
|
||||
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;
|
||||
}
|
||||
@@ -97,18 +108,15 @@ public class SublimePluginClient {
|
||||
}
|
||||
}
|
||||
|
||||
public Observable<Void> send(final JsonObject object) {
|
||||
public Observable<JsonObject> send(final JsonObject object) {
|
||||
if (mSocket == null) {
|
||||
throw new IllegalStateException("Socket is not listening ");
|
||||
}
|
||||
return Observable.fromCallable(new Callable<Void>() {
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
mOutputStream.write(object.toString().getBytes());
|
||||
mOutputStream.write("\n".getBytes());
|
||||
mOutputStream.flush();
|
||||
return null;
|
||||
}
|
||||
return Observable.fromCallable(() -> {
|
||||
mOutputStream.write(object.toString().getBytes());
|
||||
mOutputStream.write("\n".getBytes());
|
||||
mOutputStream.flush();
|
||||
return object;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -50,11 +52,9 @@ public class SublimePluginService {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("type", "log");
|
||||
object.addProperty("log", log);
|
||||
try {
|
||||
client.send(object);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
client.send(object)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(ignored -> {
|
||||
}, Throwable::printStackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.io.StorageFileProvider;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/11.
|
||||
*/
|
||||
@@ -29,7 +31,7 @@ public class SublimeResponseHandler implements Handler {
|
||||
public boolean handle(JsonObject data) {
|
||||
String script = data.get("script").getAsString();
|
||||
String name = getName(data);
|
||||
int viewId = data.get("view_id").getAsInt();
|
||||
String viewId = data.get("view_id").getAsString();
|
||||
runScript(viewId, name, script);
|
||||
return false;
|
||||
}
|
||||
@@ -37,7 +39,7 @@ public class SublimeResponseHandler implements Handler {
|
||||
.handler("stop", new Handler() {
|
||||
@Override
|
||||
public boolean handle(JsonObject data) {
|
||||
int viewId = data.get("view_id").getAsInt();
|
||||
String viewId = data.get("view_id").getAsString();
|
||||
stopScript(viewId);
|
||||
return true;
|
||||
}
|
||||
@@ -55,7 +57,7 @@ public class SublimeResponseHandler implements Handler {
|
||||
|
||||
@Override
|
||||
public boolean handle(JsonObject data) {
|
||||
int viewId = data.get("view_id").getAsInt();
|
||||
String viewId = data.get("view_id").getAsString();
|
||||
String script = data.get("script").getAsString();
|
||||
String name = getName(data);
|
||||
stopScript(viewId);
|
||||
@@ -72,14 +74,14 @@ public class SublimeResponseHandler implements Handler {
|
||||
}));
|
||||
|
||||
|
||||
private SparseArray<ScriptExecution> mScriptExecutions = new SparseArray<>();
|
||||
private HashMap<String, ScriptExecution> mScriptExecutions = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean handle(JsonObject data) {
|
||||
return mRouter.handle(data);
|
||||
}
|
||||
|
||||
private void runScript(int viewId, String name, String script) {
|
||||
private void runScript(String viewId, String name, String script) {
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
name = "[" + viewId + "]";
|
||||
} else {
|
||||
@@ -88,11 +90,11 @@ public class SublimeResponseHandler implements Handler {
|
||||
mScriptExecutions.put(viewId, Scripts.run(new StringScriptSource("<remote>:" + name, script)));
|
||||
}
|
||||
|
||||
private void stopScript(int viewId) {
|
||||
private void stopScript(String viewId) {
|
||||
ScriptExecution execution = mScriptExecutions.get(viewId);
|
||||
if (execution != null) {
|
||||
execution.getEngine().forceStop();
|
||||
mScriptExecutions.delete(viewId);
|
||||
mScriptExecutions.remove(viewId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.stardust.scriptdroid.tool;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.DhcpInfo;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.text.format.Formatter;
|
||||
@@ -17,8 +18,20 @@ public class WifiTool {
|
||||
|
||||
public static String getWifiAddress(Context context) {
|
||||
WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);
|
||||
if(wifiMgr == null){
|
||||
return null;
|
||||
}
|
||||
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
|
||||
int ip = wifiInfo.getIpAddress();
|
||||
return Formatter.formatIpAddress(ip);
|
||||
}
|
||||
|
||||
public static String getRouterIp(Context context){
|
||||
WifiManager wifiService = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
||||
if(wifiService == null){
|
||||
return null;
|
||||
}
|
||||
DhcpInfo dhcpInfo = wifiService.getDhcpInfo();
|
||||
return Formatter.formatIpAddress(dhcpInfo.gateway);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ import org.androidannotations.annotations.EActivity;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
@EActivity(R.layout.activity_main)
|
||||
|
||||
@@ -247,7 +247,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
||||
}
|
||||
|
||||
private void inputRemoteHost() {
|
||||
String host = Pref.getServerAddressOrDefault(WifiTool.getWifiAddress(getActivity()));
|
||||
String host = Pref.getServerAddressOrDefault(WifiTool.getRouterIp(getActivity()));
|
||||
new MaterialDialog.Builder(getActivity())
|
||||
.title(R.string.text_server_address)
|
||||
.input("", host, (dialog, input) -> {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
android:clickable="true"
|
||||
android:foreground="?selectableItemBackground"
|
||||
app:cardBackgroundColor="#ffffff"
|
||||
app:cardElevation="0.5dp"
|
||||
app:cardElevation="0.618dp"
|
||||
app:cardUseCompatPadding="true">
|
||||
|
||||
<LinearLayout
|
||||
|
||||
@@ -30,10 +30,11 @@ public class GlobalStardustConsole extends StardustConsole {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int level, CharSequence charSequence) {
|
||||
charSequence = String.format(Locale.getDefault(), "%s/%s: %s",
|
||||
public String println(int level, CharSequence charSequence) {
|
||||
String log = String.format(Locale.getDefault(), "%s/%s: %s",
|
||||
DATE_FORMAT.format(new Date()), getLevelChar(level), charSequence.toString());
|
||||
super.println(level, charSequence);
|
||||
super.println(level, log);
|
||||
return log;
|
||||
}
|
||||
|
||||
private String getLevelChar(int level) {
|
||||
|
||||
@@ -60,8 +60,9 @@ public class NoOpConsole implements Console {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int level, CharSequence charSequence) {
|
||||
public String println(int level, CharSequence charSequence) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -106,7 +106,7 @@ public class StardustConsole extends AbstractConsole {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int level, CharSequence charSequence) {
|
||||
public String println(int level, CharSequence charSequence) {
|
||||
Log log = new Log(mIdCounter.getAndIncrement(), level, charSequence, true);
|
||||
mLogs.add(log);
|
||||
if (mGlobalConsole != null) {
|
||||
@@ -115,6 +115,7 @@ public class StardustConsole extends AbstractConsole {
|
||||
if (mLogListener != null && mLogListener.get() != null) {
|
||||
mLogListener.get().onNewLog(log);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public interface Console {
|
||||
@ScriptInterface
|
||||
void hide();
|
||||
|
||||
void println(int level, CharSequence charSequence);
|
||||
String println(int level, CharSequence charSequence);
|
||||
|
||||
void setTitle(CharSequence title);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.stardust;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -12,6 +14,6 @@ import static org.junit.Assert.*;
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() throws Exception {
|
||||
assertEquals(4, 2 + 2);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user