add: module http
This commit is contained in:
@@ -9,8 +9,8 @@ android {
|
|||||||
applicationId "com.stardust.scriptdroid"
|
applicationId "com.stardust.scriptdroid"
|
||||||
minSdkVersion 17
|
minSdkVersion 17
|
||||||
targetSdkVersion 23
|
targetSdkVersion 23
|
||||||
versionCode 206
|
versionCode 208
|
||||||
versionName "3.0.0 Alpha7"
|
versionName "3.0.0 Alpha9"
|
||||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
multiDexEnabled true
|
multiDexEnabled true
|
||||||
ndk {
|
ndk {
|
||||||
|
|||||||
@@ -7,6 +7,16 @@ import android.content.ContextWrapper;
|
|||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/8/4.
|
* Created by Stardust on 2017/8/4.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class FloatyWindowManger {
|
|||||||
|
|
||||||
public static void addWindow(Context context, FloatyWindow window) {
|
public static void addWindow(Context context, FloatyWindow window) {
|
||||||
context.startService(new Intent(context, FloatyService.class));
|
context.startService(new Intent(context, FloatyService.class));
|
||||||
if (SettingsCompat.canDrawOverlays(context)) {
|
if (!SettingsCompat.canDrawOverlays(context)) {
|
||||||
Toast.makeText(context, R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||||
manageDrawOverlays(context);
|
manageDrawOverlays(context);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -184,6 +184,9 @@ public class StorageFileProvider {
|
|||||||
@Override
|
@Override
|
||||||
public ObservableSource<ScriptFile> apply(@NonNull ScriptFile dir) throws Exception {
|
public ObservableSource<ScriptFile> apply(@NonNull ScriptFile dir) throws Exception {
|
||||||
ScriptFile[] scriptFiles = dir.listFiles();
|
ScriptFile[] scriptFiles = dir.listFiles();
|
||||||
|
if (scriptFiles == null) {
|
||||||
|
return Observable.empty();
|
||||||
|
}
|
||||||
mScriptFileCache.put(dir.getPath(), new ArrayList<>(Arrays.asList(scriptFiles)));
|
mScriptFileCache.put(dir.getPath(), new ArrayList<>(Arrays.asList(scriptFiles)));
|
||||||
return Observable.fromArray(scriptFiles);
|
return Observable.fromArray(scriptFiles);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package com.stardust.scriptdroid.sublime;
|
package com.stardust.scriptdroid.sublime;
|
||||||
|
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
import com.stardust.enhancedfloaty.FloatyWindow;
|
||||||
|
import com.stardust.enhancedfloaty.ResizableFloaty;
|
||||||
|
|
||||||
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.EventBus;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
@@ -62,9 +66,6 @@ public class SublimePluginClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void listenInternal(final Subscriber<? super Void> s) {
|
private void listenInternal(final Subscriber<? super Void> s) {
|
||||||
new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
try {
|
||||||
mSocket = new Socket(host, port);
|
mSocket = new Socket(host, port);
|
||||||
mConnectionState.onNext(true);
|
mConnectionState.onNext(true);
|
||||||
@@ -73,13 +74,11 @@ public class SublimePluginClient {
|
|||||||
s.onComplete();
|
s.onComplete();
|
||||||
readLoop(mSocket.getInputStream());
|
readLoop(mSocket.getInputStream());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
s.onError(e);
|
s.onError(e);
|
||||||
} finally {
|
} finally {
|
||||||
tryClose();
|
tryClose();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PublishSubject<Boolean> getConnectionState() {
|
public PublishSubject<Boolean> getConnectionState() {
|
||||||
|
|||||||
@@ -163,11 +163,9 @@ public class CodeMirrorEditor extends FrameLayout {
|
|||||||
|
|
||||||
private void setupWebSettings() {
|
private void setupWebSettings() {
|
||||||
WebSettings settings = mWebView.getSettings();
|
WebSettings settings = mWebView.getSettings();
|
||||||
settings.setUseWideViewPort(true);
|
|
||||||
settings.setBuiltInZoomControls(true);
|
settings.setBuiltInZoomControls(true);
|
||||||
//settings.setLoadWithOverviewMode(true);
|
//settings.setLoadWithOverviewMode(true);
|
||||||
settings.setJavaScriptEnabled(true);
|
settings.setJavaScriptEnabled(true);
|
||||||
//settings.setUseWideViewPort(true);
|
|
||||||
settings.setJavaScriptCanOpenWindowsAutomatically(true);
|
settings.setJavaScriptCanOpenWindowsAutomatically(true);
|
||||||
settings.setDomStorageEnabled(true);
|
settings.setDomStorageEnabled(true);
|
||||||
settings.setNeedInitialFocus(true);
|
settings.setNeedInitialFocus(true);
|
||||||
|
|||||||
@@ -102,11 +102,6 @@ public class EditorView extends FrameLayout {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void setMenuItemStatus(int id, boolean enabled) {
|
|
||||||
findViewById(id).setEnabled(enabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public EditorView(Context context) {
|
public EditorView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
@@ -159,10 +154,17 @@ public class EditorView extends FrameLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void setMenuItemStatus(int id, boolean enabled) {
|
||||||
|
findViewById(id).setEnabled(enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@AfterViews
|
@AfterViews
|
||||||
void init() {
|
void init() {
|
||||||
setUpEditor();
|
setUpEditor();
|
||||||
setUpInputMethodEnhancedBar();
|
setUpInputMethodEnhancedBar();
|
||||||
|
setMenuItemStatus(R.id.save, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUpInputMethodEnhancedBar() {
|
private void setUpInputMethodEnhancedBar() {
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import org.androidannotations.annotations.EFragment;
|
|||||||
import org.androidannotations.annotations.ViewById;
|
import org.androidannotations.annotations.ViewById;
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import io.reactivex.Observable;
|
import io.reactivex.Observable;
|
||||||
@@ -46,6 +47,11 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
|||||||
import io.reactivex.disposables.Disposable;
|
import io.reactivex.disposables.Disposable;
|
||||||
import io.reactivex.functions.Consumer;
|
import io.reactivex.functions.Consumer;
|
||||||
import io.reactivex.schedulers.Schedulers;
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import okhttp3.WebSocket;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -244,7 +250,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
|
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
|
||||||
if(isHidden()){
|
if (isHidden()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Toast.makeText(App.getApp(), e.getMessage(), Toast.LENGTH_SHORT).show();
|
Toast.makeText(App.getApp(), e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
|||||||
import io.reactivex.disposables.Disposable;
|
import io.reactivex.disposables.Disposable;
|
||||||
import io.reactivex.functions.Consumer;
|
import io.reactivex.functions.Consumer;
|
||||||
import io.reactivex.schedulers.Schedulers;
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/3/13.
|
* Created by Stardust on 2017/3/13.
|
||||||
@@ -57,8 +58,8 @@ public class MyScriptListFragment extends ViewPagerFragment implements BackPress
|
|||||||
mScriptFileList.setOnScriptFileClickListener(new ScriptListView.OnScriptFileClickListener() {
|
mScriptFileList.setOnScriptFileClickListener(new ScriptListView.OnScriptFileClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onScriptFileClick(View view, ScriptFile file) {
|
public void onScriptFileClick(View view, ScriptFile file) {
|
||||||
//Scripts.edit(file);
|
Scripts.edit(file);
|
||||||
EditorFloaty.floatingEdit(getContext(), file);
|
//EditorFloaty.floatingEdit(getContext(), file);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -114,7 +115,7 @@ public class MyScriptListFragment extends ViewPagerFragment implements BackPress
|
|||||||
mFloatingActionMenu.collapse();
|
mFloatingActionMenu.collapse();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(mScriptFileList.canGoBack()){
|
if (mScriptFileList.canGoBack()) {
|
||||||
mScriptFileList.goBack();
|
mScriptFileList.goBack();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
void onDirectoryChange(StorageFileProvider.DirectoryChangeEvent event) {
|
public void onDirectoryChange(StorageFileProvider.DirectoryChangeEvent event) {
|
||||||
if (!event.getDir().equals(mCurrentDirectory)) {
|
if (!event.getDir().equals(mCurrentDirectory)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@
|
|||||||
android:id="@+id/save"
|
android:id="@+id/save"
|
||||||
android:layout_width="40dp"
|
android:layout_width="40dp"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
android:enabled="false"
|
||||||
app:icon="@drawable/ic_save_white_48dp"
|
app:icon="@drawable/ic_save_white_48dp"
|
||||||
app:text="@string/text_save"/>
|
app:text="@string/text_save"/>
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ require("__general__")(__runtime__, this);
|
|||||||
|
|
||||||
(function(scope){
|
(function(scope){
|
||||||
var modules = ['app', 'automator', 'console', 'dialogs', 'io', 'selector', 'shell', 'web', 'ui',
|
var modules = ['app', 'automator', 'console', 'dialogs', 'io', 'selector', 'shell', 'web', 'ui',
|
||||||
"images", "timers", "events", "engines", "RootAutomator"];
|
"images", "timers", "events", "engines", "RootAutomator", "http"];
|
||||||
var len = modules.length;
|
var len = modules.length;
|
||||||
for(var i = 0; i < len; i++) {
|
for(var i = 0; i < len; i++) {
|
||||||
var m = modules[i];
|
var m = modules[i];
|
||||||
|
|||||||
122
autojs/src/main/assets/modules/__http__.js
Normal file
122
autojs/src/main/assets/modules/__http__.js
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
module.exports = function(runtime, scope){
|
||||||
|
importPackage(Packages["okhttp3"]);
|
||||||
|
var http = {};
|
||||||
|
http.get = function(url, options, callback){
|
||||||
|
options = options || {};
|
||||||
|
options.method = "GET";
|
||||||
|
return http.request(url, options, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
http.client = function(){
|
||||||
|
if(!http._client_){
|
||||||
|
http._client_ = new OkHttpClient();
|
||||||
|
}
|
||||||
|
return http._client_;
|
||||||
|
}
|
||||||
|
|
||||||
|
http.post = function(url, data, options, callback){
|
||||||
|
options = options || {};
|
||||||
|
options.method = "POST";
|
||||||
|
options.contentType = options.contentType || "application/x-www-form-urlencoded";
|
||||||
|
if(data){
|
||||||
|
fillPostData(options, data);
|
||||||
|
}
|
||||||
|
return http.request(url, options, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
http.postJson = function(url, data, options, callback){
|
||||||
|
options = options || {};
|
||||||
|
options.contentType = "application/json";
|
||||||
|
return http.post(url, data, options, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
http.request = function(url, options, callback){
|
||||||
|
var call = http.newCall(buildRequest(url, options));
|
||||||
|
if(!callback){
|
||||||
|
return wrapResponse(call.execute());
|
||||||
|
}
|
||||||
|
call.enqueue(new Callback({
|
||||||
|
onResponse: function(call, res){
|
||||||
|
callback(res);
|
||||||
|
},
|
||||||
|
onFailure: function(call, ex){
|
||||||
|
callback(null, ex);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
http.buildRequest = function(url, options){
|
||||||
|
var r = new Request.Builder();
|
||||||
|
if(!url.startsWith("http://") && !url.startsWith("https://")){
|
||||||
|
url = "http://" + url;
|
||||||
|
}
|
||||||
|
r.url(url);
|
||||||
|
if(options.headers){
|
||||||
|
setHeaders(r, options.headers);
|
||||||
|
}
|
||||||
|
if(options.body){
|
||||||
|
r.method(options.method, parseBody(options, options.body));
|
||||||
|
}else{
|
||||||
|
r.method(options.method, null);
|
||||||
|
}
|
||||||
|
return r.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
function fillPostData(options, data){
|
||||||
|
if(options.contentType == "application/x-www-form-urlencoded"){
|
||||||
|
var b = new FormBody.Builder();
|
||||||
|
for(var key in data){
|
||||||
|
if(data.hasOwnProperty(key)){
|
||||||
|
b.add(key, data[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
options.body = b.build();
|
||||||
|
}else if(options.contentType == "application/json"){
|
||||||
|
options.body = JSON.stringify(data);
|
||||||
|
}else{
|
||||||
|
//todo what?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setHeaders(r, headers){
|
||||||
|
for(var key in headers){
|
||||||
|
if(headers.hasOwnProperty(key)){
|
||||||
|
r.header(key, headers[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseBody(options, body){
|
||||||
|
if(typeof(body) == "string"){
|
||||||
|
body = RequestBody.create(MediaType.parse(options.contentType), body);
|
||||||
|
}else{
|
||||||
|
body = new RequestBody({
|
||||||
|
contentType: function(){
|
||||||
|
return MediaType.parse(options.contentType);
|
||||||
|
},
|
||||||
|
writeTo: body
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
function wrapResponse(res){
|
||||||
|
var r = {};
|
||||||
|
r.statusCode = res.code();
|
||||||
|
var headers = res.headers();
|
||||||
|
r.headers = {};
|
||||||
|
for(var i = 0; i < headers.size(); i++){
|
||||||
|
r.headers[headers.name(i)] = headers.value(i);
|
||||||
|
}
|
||||||
|
r.body = Object.create(res.body());
|
||||||
|
r.body.json = function(){
|
||||||
|
return JSON.parse(r.body.string());
|
||||||
|
}
|
||||||
|
r.request = res.request();
|
||||||
|
r.url = r.request.url();
|
||||||
|
r.method = r.request.method();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
return http;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user