Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Add a new component to support Html tags(can custom tag's implement) #2071

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
*/
package com.alibaba.weex.commons.adapter;

import android.net.Uri;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
import com.taobao.weex.adapter.IWXImgLoaderAdapter;
import com.taobao.weex.common.WXImageStrategy;
import com.taobao.weex.dom.WXImageQuality;
import com.taobao.weex.ui.IFComponentHolder;

public class ImageAdapter implements IWXImgLoaderAdapter {

Expand All @@ -41,65 +35,66 @@ public ImageAdapter() {
@Override
public void setImage(final String url, final ImageView view,
WXImageQuality quality, final WXImageStrategy strategy) {
Runnable runnable = new Runnable() {

@Override
public void run() {
if(view==null||view.getLayoutParams()==null){
return;
}
if (TextUtils.isEmpty(url)) {
view.setImageBitmap(null);
return;
}
if (null != strategy){
recordImgLoadAction(strategy.instanceId);
}

String temp = url;
if (url.startsWith("//")) {
temp = "http:" + url;
}

if(!TextUtils.isEmpty(strategy.placeHolder)){
Picasso.Builder builder=new Picasso.Builder(WXEnvironment.getApplication());
Picasso picasso=builder.build();
picasso.load(Uri.parse(strategy.placeHolder)).into(view);

view.setTag(strategy.placeHolder.hashCode(),picasso);
}

Picasso.with(WXEnvironment.getApplication())
.load(temp)
.transform(new BlurTransformation(strategy.blurRadius))
.into(view, new Callback() {
@Override
public void onSuccess() {
if(strategy.getImageListener()!=null){
strategy.getImageListener().onImageFinish(url,view,true,null);
}
recordImgLoadResult(strategy.instanceId,true,null);

if(!TextUtils.isEmpty(strategy.placeHolder)){
((Picasso) view.getTag(strategy.placeHolder.hashCode())).cancelRequest(view);
}
}

@Override
public void onError() {
if(strategy.getImageListener()!=null){
strategy.getImageListener().onImageFinish(url,view,false,null);
}
recordImgLoadResult(strategy.instanceId,false,null);
}
});
}
};
if(Thread.currentThread() == Looper.getMainLooper().getThread()){
runnable.run();
}else {
WXSDKManager.getInstance().postOnUiThread(runnable, 0);
}
Picasso.with(view.getContext()).load(url).into(view);
// Runnable runnable = new Runnable() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this outcommented code left here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change this ImageAdapter because the origin has many conditions about how to display image. It's in demo project, so not the key core code

//
// @Override
// public void run() {
// if(view==null||view.getLayoutParams()==null){
// return;
// }
// if (TextUtils.isEmpty(url)) {
// view.setImageBitmap(null);
// return;
// }
// if (null != strategy){
// recordImgLoadAction(strategy.instanceId);
// }
//
// String temp = url;
// if (url.startsWith("//")) {
// temp = "http:" + url;
// }
//
// if(!TextUtils.isEmpty(strategy.placeHolder)){
// Picasso.Builder builder=new Picasso.Builder(WXEnvironment.getApplication());
// Picasso picasso=builder.build();
// picasso.load(Uri.parse(strategy.placeHolder)).into(view);
//
// view.setTag(strategy.placeHolder.hashCode(),picasso);
// }
//
// Picasso.with(WXEnvironment.getApplication())
// .load(temp)
// .transform(new BlurTransformation(strategy.blurRadius))
// .into(view, new Callback() {
// @Override
// public void onSuccess() {
// if(strategy.getImageListener()!=null){
// strategy.getImageListener().onImageFinish(url,view,true,null);
// }
// recordImgLoadResult(strategy.instanceId,true,null);
//
// if(!TextUtils.isEmpty(strategy.placeHolder)){
// ((Picasso) view.getTag(strategy.placeHolder.hashCode())).cancelRequest(view);
// }
// }
//
// @Override
// public void onError() {
// if(strategy.getImageListener()!=null){
// strategy.getImageListener().onImageFinish(url,view,false,null);
// }
// recordImgLoadResult(strategy.instanceId,false,null);
// }
// });
// }
// };
// if(Thread.currentThread() == Looper.getMainLooper().getThread()){
// runnable.run();
// }else {
// WXSDKManager.getInstance().postOnUiThread(runnable, 0);
// }
}
private void recordImgLoadAction(String instanceId){
WXSDKInstance instance = WXSDKManager.getInstance().getAllInstanceMap().get(instanceId);
Expand Down
13 changes: 13 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/InitConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.taobao.weex.adapter.IWXJsFileLoaderAdapter;
import com.taobao.weex.adapter.IWXSoLoaderAdapter;
import com.taobao.weex.adapter.IWXUserTrackAdapter;
import com.taobao.weex.adapter.IWxHtmlTagAdapter;
import com.taobao.weex.adapter.URIAdapter;
import com.taobao.weex.appfram.storage.IWXStorageAdapter;
import com.taobao.weex.appfram.websocket.IWebSocketAdapterFactory;
Expand All @@ -50,6 +51,7 @@ public class InitConfig {
private IApmGenerator apmGenerater;
private IWXJsFileLoaderAdapter jsFileLoaderAdapter;
private IWXJscProcessManager jscProcessManager;
private IWxHtmlTagAdapter htmlTagAdapter;

public IWXHttpAdapter getHttpAdapter() {
return httpAdapter;
Expand Down Expand Up @@ -111,6 +113,10 @@ public IWXJscProcessManager getJscProcessManager() {
return jscProcessManager;
}

public IWxHtmlTagAdapter getHtmlTagAdapter() {
return htmlTagAdapter;
}

private InitConfig() {
}

Expand All @@ -128,6 +134,7 @@ public static class Builder{
ClassLoaderAdapter classLoaderAdapter;
IApmGenerator apmGenerater;
private IWXJsFileLoaderAdapter jsFileLoaderAdapter;
private IWxHtmlTagAdapter htmlTagAdapter;

public IWXJscProcessManager getJscProcessManager() {
return jscProcessManager;
Expand Down Expand Up @@ -184,6 +191,11 @@ public Builder setSoLoader(IWXSoLoaderAdapter loader) {
return this;
}

public Builder setHtmlTagAdapter(IWxHtmlTagAdapter htmlTagAdapter){
this.htmlTagAdapter = htmlTagAdapter;
return this;
}

public Builder setFramework(String framework){
this.framework=framework;
return this;
Expand Down Expand Up @@ -225,6 +237,7 @@ public InitConfig build(){
config.apmGenerater = this.apmGenerater;
config.jsFileLoaderAdapter = this.jsFileLoaderAdapter;
config.jscProcessManager = this.jscProcessManager;
config.htmlTagAdapter = this.htmlTagAdapter;
return config;
}
}
Expand Down
4 changes: 2 additions & 2 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import com.taobao.weex.common.WXException;
import com.taobao.weex.common.WXInstanceWrap;
import com.taobao.weex.common.WXModule;
import com.taobao.weex.common.WXPerformance;
import com.taobao.weex.http.WXStreamModule;
import com.taobao.weex.ui.ExternalLoaderComponentHolder;
import com.taobao.weex.ui.IExternalComponentGetter;
Expand Down Expand Up @@ -83,7 +82,7 @@
import com.taobao.weex.ui.component.WXText;
import com.taobao.weex.ui.component.WXVideo;
import com.taobao.weex.ui.component.WXWeb;
import com.taobao.weex.ui.component.basic.WXBasicComponent;
import com.taobao.weex.ui.component.html.WxHtmlComponent;
import com.taobao.weex.ui.component.list.HorizontalListComponent;
import com.taobao.weex.ui.component.list.SimpleListComponent;
import com.taobao.weex.ui.component.list.WXCell;
Expand Down Expand Up @@ -369,6 +368,7 @@ private static void register() {
registerComponent(WXBasicComponentType.LOADING, WXLoading.class);
registerComponent(WXBasicComponentType.LOADING_INDICATOR, WXLoadingIndicator.class);
registerComponent(WXBasicComponentType.HEADER, WXHeader.class);
registerComponent(WXBasicComponentType.HTML_TEXT, WxHtmlComponent.class);

registerModule("modal", WXModalUIModule.class);
registerModule("instanceWrap", WXInstanceWrap.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.taobao.weex.adapter.IWXImgLoaderAdapter;
import com.taobao.weex.adapter.IWXJscProcessManager;
import com.taobao.weex.adapter.IWXUserTrackAdapter;
import com.taobao.weex.adapter.IWxHtmlTagAdapter;
import com.taobao.weex.adapter.URIAdapter;
import com.taobao.weex.appfram.websocket.IWebSocketAdapter;
import com.taobao.weex.bridge.EventResult;
Expand Down Expand Up @@ -957,6 +958,10 @@ public IWXImgLoaderAdapter getImgLoaderAdapter() {
return WXSDKManager.getInstance().getIWXImgLoaderAdapter();
}

public IWxHtmlTagAdapter getHtmlTextAdapter() {
return WXSDKManager.getInstance().getWxHtmlTextViewAdapter();
}

public IDrawableLoader getDrawableLoader() {
return WXSDKManager.getInstance().getDrawableLoader();
}
Expand Down
11 changes: 11 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.taobao.weex.adapter.IWXJsFileLoaderAdapter;
import com.taobao.weex.adapter.IWXSoLoaderAdapter;
import com.taobao.weex.adapter.IWXUserTrackAdapter;
import com.taobao.weex.adapter.IWxHtmlTagAdapter;
import com.taobao.weex.adapter.URIAdapter;
import com.taobao.weex.appfram.navigator.IActivityNavBarSetter;
import com.taobao.weex.appfram.navigator.INavigator;
Expand All @@ -54,6 +55,7 @@
import com.taobao.weex.performance.IApmGenerator;
import com.taobao.weex.performance.IWXAnalyzer;
import com.taobao.weex.ui.WXRenderManager;
import com.taobao.weex.ui.component.html.adapter.DefaultHtmlTagAdapter;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXUtils;

Expand Down Expand Up @@ -85,6 +87,7 @@ public class WXSDKManager {
private List<IWXAnalyzer> mWXAnalyzerList;
private IApmGenerator mApmGenerater;
private IWXJsFileLoaderAdapter mWXJsFileLoaderAdapter;
private IWxHtmlTagAdapter mWxHtmlTextViewAdapter;

private ICrashInfoReporter mCrashInfo;

Expand Down Expand Up @@ -404,6 +407,14 @@ void setInitConfig(InitConfig config){
this.mApmGenerater = config.getApmGenerater();
this.mWXJsFileLoaderAdapter = config.getJsFileLoaderAdapter();
this.mWXJscProcessManager = config.getJscProcessManager();
this.mWxHtmlTextViewAdapter = config.getHtmlTagAdapter();
}

public IWxHtmlTagAdapter getWxHtmlTextViewAdapter() {
if (mWxHtmlTextViewAdapter == null) {
mWxHtmlTextViewAdapter = new DefaultHtmlTagAdapter();
}
return mWxHtmlTextViewAdapter;
}

public IWXStorageAdapter getIWXStorageAdapter(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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.adapter;

import android.content.Context;
import android.view.View;

import com.taobao.weex.ui.component.html.WxHtmlComponent;

/** Created by Bruce Too On 2019/1/10. At 09:39 */
public interface IWxHtmlTagAdapter<T extends View> {
/**
* Custom html tags mapped with native view,see the default implement at {@link
* com.taobao.weex.ui.component.html.adapter.DefaultHtmlTagAdapter}
*
* @param context current activity context
* @param tagName tag's name
* @param html html string
* @return the custom native view self
*/
public T getHtmlTagView(Context context, WxHtmlComponent component, String tagName, String html);

public View.OnClickListener getTagViewClickListener(View clickView, String tagName, String html);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class WXBasicComponentType {
public static final String HLIST = "hlist";
public static final String CELL = "cell";
public static final String HEADER = "header";
public static final String HTML_TEXT = "html-text";
public static final String FOOTER = "footer";
public static final String INDICATOR = "indicator";
public static final String VIDEO = "video";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.ui.component.html;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

/**
* Created by Bruce Too
* On 2019/1/9.
* At 13:51
*/
public class AtMostWebView extends WebView {
public AtMostWebView(Context context) {
super(context);
}

public AtMostWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AtMostWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
Loading