forked from apache/incubator-weex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core][eagle] support httpmodule in android
- Loading branch information
Showing
21 changed files
with
526 additions
and
95 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
101 changes: 101 additions & 0 deletions
101
android/sdk/src/main/java/com/taobao/weex/bridge/RequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.taobao.weex.bridge; | ||
|
||
import android.net.Uri; | ||
import android.text.TextUtils; | ||
|
||
import com.taobao.weex.WXEnvironment; | ||
import com.taobao.weex.WXHttpListener; | ||
import com.taobao.weex.WXSDKInstance; | ||
import com.taobao.weex.WXSDKManager; | ||
import com.taobao.weex.adapter.IWXHttpAdapter; | ||
import com.taobao.weex.adapter.URIAdapter; | ||
import com.taobao.weex.base.CalledByNative; | ||
import com.taobao.weex.common.WXRequest; | ||
import com.taobao.weex.common.WXResponse; | ||
import com.taobao.weex.http.WXHttpUtil; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.taobao.weex.http.WXHttpUtil.KEY_USER_AGENT; | ||
|
||
public class RequestHandler { | ||
|
||
native void nativeInvokeOnSuccess(long callback, String result); | ||
native void nativeInvokeOnFailed(long callback); | ||
|
||
@CalledByNative | ||
public static RequestHandler create() { | ||
return new RequestHandler(); | ||
} | ||
|
||
@CalledByNative | ||
public void send(String instanceId, String url, long nativeCallback) { | ||
if (TextUtils.isEmpty(instanceId) || TextUtils.isEmpty(url) || | ||
nativeCallback == 0 || | ||
!WXSDKManager.getInstance().getAllInstanceMap().containsKey( | ||
instanceId)) { | ||
return; | ||
} | ||
|
||
WXSDKManager manager = WXSDKManager.getInstance(); | ||
WXSDKInstance instance = | ||
WXSDKManager.getInstance().getSDKInstance(instanceId); | ||
if (instance == null) | ||
return; | ||
IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter(); | ||
|
||
WXRequest wxRequest = new WXRequest(); | ||
wxRequest.url = manager.getURIAdapter() | ||
.rewrite(instance, URIAdapter.BUNDLE, Uri.parse(url)) | ||
.toString(); | ||
|
||
if (wxRequest.paramMap == null) { | ||
wxRequest.paramMap = new HashMap<>(); | ||
} | ||
wxRequest.paramMap.put( | ||
KEY_USER_AGENT, WXHttpUtil.assembleUserAgent( | ||
instance.getContext(), WXEnvironment.getConfig())); | ||
wxRequest.paramMap.put("isBundleRequest", "true"); | ||
adapter.sendRequest(wxRequest, new OnHttpListenerInner(instance, nativeCallback, url)); | ||
} | ||
|
||
class OnHttpListenerInner extends WXHttpListener { | ||
private long sNativeCallback; | ||
|
||
OnHttpListenerInner(WXSDKInstance instance, long nativeCallback, String bundlUrl) { | ||
super(instance, bundlUrl); | ||
sNativeCallback = nativeCallback; | ||
} | ||
|
||
@Override | ||
public void onSuccess(WXResponse response) { | ||
String script = new String(response.originalData); | ||
nativeInvokeOnSuccess(sNativeCallback, script); | ||
} | ||
|
||
@Override | ||
public void onFail(WXResponse response) { | ||
nativeInvokeOnFailed(sNativeCallback); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
weex_core/Source/android/jniprebuild/jniheader/RequestHandler_jni.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2014 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// This file is autogenerated by | ||
// weex/weex_core/Source/android/jniprebuild/jni_generator.py | ||
// For | ||
// com/taobao/weex/bridge/RequestHandler | ||
|
||
#ifndef com_taobao_weex_bridge_RequestHandler_JNI | ||
#define com_taobao_weex_bridge_RequestHandler_JNI | ||
|
||
#include <jni.h> | ||
|
||
//#include "base/android/jni_int_wrapper.h" | ||
|
||
// Step 1: forward declarations. | ||
namespace { | ||
const char kRequestHandlerClassPath[] = "com/taobao/weex/bridge/RequestHandler"; | ||
// Leaking this jclass as we cannot use LazyInstance from some threads. | ||
jclass g_RequestHandler_clazz = NULL; | ||
#define RequestHandler_clazz(env) g_RequestHandler_clazz | ||
|
||
} // namespace | ||
|
||
static void InvokeOnSuccess(JNIEnv* env, jobject jcaller, | ||
jlong callback, | ||
jstring result); | ||
|
||
static void InvokeOnFailed(JNIEnv* env, jobject jcaller, | ||
jlong callback); | ||
|
||
// Step 2: method stubs. | ||
|
||
static intptr_t g_RequestHandler_create = 0; | ||
static base::android::ScopedLocalJavaRef<jobject> | ||
Java_RequestHandler_create(JNIEnv* env) { | ||
/* Must call RegisterNativesImpl() */ | ||
//CHECK_CLAZZ(env, RequestHandler_clazz(env), | ||
// RequestHandler_clazz(env), NULL); | ||
jmethodID method_id = | ||
base::android::GetMethod( | ||
env, RequestHandler_clazz(env), | ||
base::android::STATIC_METHOD, | ||
"create", | ||
|
||
"(" | ||
")" | ||
"Lcom/taobao/weex/bridge/RequestHandler;", | ||
&g_RequestHandler_create); | ||
|
||
jobject ret = | ||
env->CallStaticObjectMethod(RequestHandler_clazz(env), | ||
method_id); | ||
base::android::CheckException(env); | ||
return base::android::ScopedLocalJavaRef<jobject>(env, ret); | ||
} | ||
|
||
static intptr_t g_RequestHandler_send = 0; | ||
static void Java_RequestHandler_send(JNIEnv* env, jobject obj, jstring | ||
instanceId, | ||
jstring url, | ||
jlong nativeCallback) { | ||
/* Must call RegisterNativesImpl() */ | ||
//CHECK_CLAZZ(env, obj, | ||
// RequestHandler_clazz(env)); | ||
jmethodID method_id = | ||
base::android::GetMethod( | ||
env, RequestHandler_clazz(env), | ||
base::android::INSTANCE_METHOD, | ||
"send", | ||
|
||
"(" | ||
"Ljava/lang/String;" | ||
"Ljava/lang/String;" | ||
"J" | ||
")" | ||
"V", | ||
&g_RequestHandler_send); | ||
|
||
env->CallVoidMethod(obj, | ||
method_id, instanceId, url, nativeCallback); | ||
base::android::CheckException(env); | ||
|
||
} | ||
|
||
// Step 3: RegisterNatives. | ||
|
||
static const JNINativeMethod kMethodsRequestHandler[] = { | ||
{ "nativeInvokeOnSuccess", | ||
"(" | ||
"J" | ||
"Ljava/lang/String;" | ||
")" | ||
"V", reinterpret_cast<void*>(InvokeOnSuccess) }, | ||
{ "nativeInvokeOnFailed", | ||
"(" | ||
"J" | ||
")" | ||
"V", reinterpret_cast<void*>(InvokeOnFailed) }, | ||
}; | ||
|
||
static bool RegisterNativesImpl(JNIEnv* env) { | ||
|
||
g_RequestHandler_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( | ||
base::android::GetClass(env, kRequestHandlerClassPath).Get())); | ||
|
||
const int kMethodsRequestHandlerSize = | ||
sizeof(kMethodsRequestHandler)/sizeof(kMethodsRequestHandler[0]); | ||
|
||
if (env->RegisterNatives(RequestHandler_clazz(env), | ||
kMethodsRequestHandler, | ||
kMethodsRequestHandlerSize) < 0) { | ||
//jni_generator::HandleRegistrationError( | ||
// env, RequestHandler_clazz(env), __FILE__); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
#endif // com_taobao_weex_bridge_RequestHandler_JNI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.