mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
add: module http
This commit is contained in:
parent
1c4aa61a6d
commit
e6c0500358
@ -9,8 +9,8 @@ android {
|
||||
applicationId "com.stardust.scriptdroid"
|
||||
minSdkVersion 17
|
||||
targetSdkVersion 23
|
||||
versionCode 206
|
||||
versionName "3.0.0 Alpha7"
|
||||
versionCode 208
|
||||
versionName "3.0.0 Alpha9"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
multiDexEnabled true
|
||||
ndk {
|
||||
|
||||
@ -7,6 +7,16 @@ import android.content.ContextWrapper;
|
||||
import android.view.Window;
|
||||
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.
|
||||
*/
|
||||
|
||||
@ -20,7 +20,7 @@ public class FloatyWindowManger {
|
||||
|
||||
public static void addWindow(Context context, FloatyWindow window) {
|
||||
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();
|
||||
manageDrawOverlays(context);
|
||||
return;
|
||||
|
||||
@ -184,6 +184,9 @@ public class StorageFileProvider {
|
||||
@Override
|
||||
public ObservableSource<ScriptFile> apply(@NonNull ScriptFile dir) throws Exception {
|
||||
ScriptFile[] scriptFiles = dir.listFiles();
|
||||
if (scriptFiles == null) {
|
||||
return Observable.empty();
|
||||
}
|
||||
mScriptFileCache.put(dir.getPath(), new ArrayList<>(Arrays.asList(scriptFiles)));
|
||||
return Observable.fromArray(scriptFiles);
|
||||
}
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
package com.stardust.scriptdroid.sublime;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
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;
|
||||
@ -62,24 +66,19 @@ public class SublimePluginClient {
|
||||
}
|
||||
|
||||
private void listenInternal(final Subscriber<? super Void> s) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
mSocket = new Socket(host, port);
|
||||
mConnectionState.onNext(true);
|
||||
mSocket.setTcpNoDelay(true);
|
||||
mOutputStream = mSocket.getOutputStream();
|
||||
s.onComplete();
|
||||
readLoop(mSocket.getInputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
s.onError(e);
|
||||
} finally {
|
||||
tryClose();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
try {
|
||||
mSocket = new Socket(host, port);
|
||||
mConnectionState.onNext(true);
|
||||
mSocket.setTcpNoDelay(true);
|
||||
mOutputStream = mSocket.getOutputStream();
|
||||
s.onComplete();
|
||||
readLoop(mSocket.getInputStream());
|
||||
} catch (IOException e) {
|
||||
s.onError(e);
|
||||
} finally {
|
||||
tryClose();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public PublishSubject<Boolean> getConnectionState() {
|
||||
|
||||
@ -163,11 +163,9 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
|
||||
private void setupWebSettings() {
|
||||
WebSettings settings = mWebView.getSettings();
|
||||
settings.setUseWideViewPort(true);
|
||||
settings.setBuiltInZoomControls(true);
|
||||
//settings.setLoadWithOverviewMode(true);
|
||||
settings.setJavaScriptEnabled(true);
|
||||
//settings.setUseWideViewPort(true);
|
||||
settings.setJavaScriptCanOpenWindowsAutomatically(true);
|
||||
settings.setDomStorageEnabled(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) {
|
||||
super(context);
|
||||
}
|
||||
@ -159,10 +154,17 @@ public class EditorView extends FrameLayout {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setMenuItemStatus(int id, boolean enabled) {
|
||||
findViewById(id).setEnabled(enabled);
|
||||
}
|
||||
|
||||
|
||||
@AfterViews
|
||||
void init() {
|
||||
setUpEditor();
|
||||
setUpInputMethodEnhancedBar();
|
||||
setMenuItemStatus(R.id.save, false);
|
||||
}
|
||||
|
||||
private void setUpInputMethodEnhancedBar() {
|
||||
|
||||
@ -39,6 +39,7 @@ import org.androidannotations.annotations.EFragment;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
@ -46,6 +47,11 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.functions.Consumer;
|
||||
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
|
||||
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
|
||||
if(isHidden()){
|
||||
if (isHidden()) {
|
||||
return;
|
||||
}
|
||||
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.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/13.
|
||||
@ -57,8 +58,8 @@ public class MyScriptListFragment extends ViewPagerFragment implements BackPress
|
||||
mScriptFileList.setOnScriptFileClickListener(new ScriptListView.OnScriptFileClickListener() {
|
||||
@Override
|
||||
public void onScriptFileClick(View view, ScriptFile file) {
|
||||
//Scripts.edit(file);
|
||||
EditorFloaty.floatingEdit(getContext(), file);
|
||||
Scripts.edit(file);
|
||||
//EditorFloaty.floatingEdit(getContext(), file);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -114,7 +115,7 @@ public class MyScriptListFragment extends ViewPagerFragment implements BackPress
|
||||
mFloatingActionMenu.collapse();
|
||||
return true;
|
||||
}
|
||||
if(mScriptFileList.canGoBack()){
|
||||
if (mScriptFileList.canGoBack()) {
|
||||
mScriptFileList.goBack();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -159,7 +159,7 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
void onDirectoryChange(StorageFileProvider.DirectoryChangeEvent event) {
|
||||
public void onDirectoryChange(StorageFileProvider.DirectoryChangeEvent event) {
|
||||
if (!event.getDir().equals(mCurrentDirectory)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -57,6 +57,7 @@
|
||||
android:id="@+id/save"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="match_parent"
|
||||
android:enabled="false"
|
||||
app:icon="@drawable/ic_save_white_48dp"
|
||||
app:text="@string/text_save"/>
|
||||
|
||||
|
||||
@ -38,7 +38,7 @@ require("__general__")(__runtime__, this);
|
||||
|
||||
(function(scope){
|
||||
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;
|
||||
for(var i = 0; i < len; 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user