Skip to content

Commit

Permalink
fix circler webview bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacm authored and tianhui12 committed Apr 27, 2022
1 parent b14c9ab commit 881d64f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 75 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ buildscript {
],

'androidx' : [
'appcompat' : 'androidx.appcompat:appcompat:1.0.0',
'fragment' : 'androidx.fragment:fragment:1.0.0',
'recyclerview': 'androidx.recyclerview:recyclerview:1.0.0',
'appcompat' : 'androidx.appcompat:appcompat:1.3.1',
'fragment' : 'androidx.fragment:fragment:1.3.1',
'recyclerview' : 'androidx.recyclerview:recyclerview:1.2.1',
"swiperefreshlayout": "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
],

'growingio': [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.growingio.android.sdk.track.modelloader.ModelLoader;
import com.growingio.android.sdk.track.modelloader.ModelLoaderFactory;
import com.growingio.android.sdk.track.modelloader.data.HybridBridge;
import com.growingio.android.sdk.track.utils.ClassExistHelper;

/**
* <p>
Expand Down Expand Up @@ -57,9 +58,9 @@ public HybridDataFetcher(HybridBridge eventData) {
public Boolean executeData() {
if (bridge.getView() instanceof WebView) {
HybridBridgeProvider.get().bridgeForWebView(SuperWebView.make((WebView) bridge.getView()));
} else if (bridge.getView() instanceof com.tencent.smtt.sdk.WebView) {
} else if (ClassExistHelper.instanceOfX5WebView(bridge.getView())) {
HybridBridgeProvider.get().bridgeForWebView(SuperWebView.makeX5((com.tencent.smtt.sdk.WebView) bridge.getView()));
} else if (bridge.getView() instanceof com.uc.webview.export.WebView) {
} else if (ClassExistHelper.instanceOfUcWebView(bridge.getView())) {
HybridBridgeProvider.get().bridgeForWebView(SuperWebView.makeUC((com.uc.webview.export.WebView) bridge.getView()));
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ public Disposable getWebViewDomTree(SuperWebView<?> webView, final Callback<JSON
if (callback == null) {
return Disposable.EMPTY_DISPOSABLE;
}
final Disposable disposable = new HandlerDisposable().schedule(new Runnable() {
@Override
public void run() {
Logger.e(TAG, "getWebViewDomTree timeout");
callback.onFailed();
}
final Disposable disposable = new HandlerDisposable().schedule(() -> {
Logger.e(TAG, "getWebViewDomTree timeout");
callback.onFailed();
}, EVALUATE_JAVASCRIPT_TIMEOUT);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Expand Down Expand Up @@ -126,8 +123,8 @@ public void onReceiveValue(String value) {
Logger.e(TAG, "You need use after Android 4.4 to getWebViewDomTree");
if (!disposable.isDisposed()) {
disposable.dispose();
callback.onFailed();
}
callback.onFailed();
}
return disposable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.growingio.android.sdk.track.modelloader.ModelLoaderFactory;
import com.growingio.android.sdk.track.modelloader.data.HybridDom;
import com.growingio.android.sdk.track.modelloader.data.HybridJson;
import com.growingio.android.sdk.track.utils.ClassExistHelper;

import org.json.JSONObject;

Expand Down Expand Up @@ -67,6 +68,7 @@ public HybridDataFetcher(HybridDom eventData) {
public void loadData(DataCallback<? super HybridJson> callback) {
if (dom.getView() == null) {
callback.onLoadFailed(new NullPointerException("webview is null"));
return;
}
SuperWebView<? extends View> superWebView = getSuperWebView();
if (superWebView == null) {
Expand All @@ -89,9 +91,9 @@ public void onFailed() {
public SuperWebView<? extends View> getSuperWebView() {
if (dom.getView() instanceof WebView) {
return SuperWebView.make((WebView) dom.getView());
} else if (dom.getView() instanceof com.tencent.smtt.sdk.WebView) {
} else if (ClassExistHelper.instanceOfX5WebView(dom.getView())) {
return SuperWebView.makeX5((com.tencent.smtt.sdk.WebView) dom.getView());
} else if (dom.getView() instanceof com.uc.webview.export.WebView) {
} else if (ClassExistHelper.instanceOfUcWebView(dom.getView())) {
return SuperWebView.makeUC((com.uc.webview.export.WebView) dom.getView());
} else {
return null;
Expand Down
1 change: 1 addition & 0 deletions growingio-tracker-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {

compileOnly libraries.androidx.appcompat
compileOnly libraries.androidx.recyclerview
compileOnly libraries.androidx.swiperefreshlayout
compileOnly libraries.android.appcompat
compileOnly libraries.android.recyclerview
//compileOnly libraries.others.x5_webview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.growingio.android.sdk.autotrack.view.ViewNode;
import com.growingio.android.sdk.autotrack.view.ViewUtil;
import com.growingio.android.sdk.track.async.Callback;
import com.growingio.android.sdk.track.async.Disposable;
import com.growingio.android.sdk.track.async.UnsubscribedDisposable;
import com.growingio.android.sdk.track.modelloader.LoadDataFetcher;
import com.growingio.android.sdk.track.modelloader.ModelLoader;
import com.growingio.android.sdk.track.modelloader.data.HybridDom;
Expand Down Expand Up @@ -109,10 +107,9 @@ public static final class Builder {
private long mSnapshotKey;
private final List<ViewElement> mViewElements = new ArrayList<>();
private final List<PageElement> mPages = new ArrayList<>();
private final AtomicInteger mWebViewCount = new AtomicInteger(0);
private final AtomicInteger mScreenLock = new AtomicInteger(0);
private int mViewCount = 0;
private Callback<CircleScreenshot> mScreenshotResultCallback;
private Disposable mBuildDisposable;

public Builder setScale(float scale) {
mScale = scale;
Expand All @@ -129,19 +126,21 @@ public Builder setSnapshotKey(long snapshotKey) {
return this;
}

public Disposable build(Callback<CircleScreenshot> callback) {
if (callback == null) {
return Disposable.EMPTY_DISPOSABLE;
}
public void build(Callback<CircleScreenshot> callback) {
List<DecorView> decorViews = WindowHelper.get().getTopActivityViews();
return build(decorViews, callback);
build(decorViews, callback);
}

public Disposable build(List<DecorView> decorViews, Callback<CircleScreenshot> callback) {
public void build(List<DecorView> decorViews, Callback<CircleScreenshot> callback) {
if (callback == null) {
return Disposable.EMPTY_DISPOSABLE;
return;
}

if (decorViews == null || decorViews.isEmpty()) {
callResultOnFailed();
return;
}
mBuildDisposable = new UnsubscribedDisposable();

mScreenshotResultCallback = callback;

DisplayMetrics displayMetrics = DeviceUtil.getDisplayMetrics(TrackerContext.get().getApplicationContext());
Expand All @@ -152,31 +151,16 @@ public Disposable build(List<DecorView> decorViews, Callback<CircleScreenshot> c
if (decorView.getView() instanceof TipView) {
continue;
}
checkView2PageElement(decorView.getView());
checkView2ViewElement(decorView.getView());
}
if (mWebViewCount.get() == 0) {
callResultOnSuccess();
}
return mBuildDisposable;
}

private void callResultOnSuccess() {
if (!mBuildDisposable.isDisposed()) {
mBuildDisposable.dispose();
if (mScreenshotResultCallback != null) {
mScreenshotResultCallback.onSuccess(new CircleScreenshot(this));
if (isViewInvisible(decorView.getView())) {
continue;
}
}
}

private void callResultOnFailed() {
if (!mBuildDisposable.isDisposed()) {
mBuildDisposable.dispose();
if (mScreenshotResultCallback != null) {
mScreenshotResultCallback.onFailed();
}
mScreenLock.incrementAndGet();
checkView2PageElement(decorView.getView());
checkView2ViewElement(decorView.getView());
}
callResultOnSuccess();
}

private ViewElement.Builder createViewElementBuilder(ViewNode viewNode) {
Expand All @@ -199,14 +183,11 @@ private ViewElement.Builder createViewElementBuilder(ViewNode viewNode) {

private void checkView2ViewElement(View view) {
ViewNode topViewNode = ViewHelper.getTopViewNode(view, null);
if (disposeWebView(topViewNode)) {
return;
}

traverseViewNode(topViewNode);
}

private void traverseViewNode(ViewNode viewNode) {
if (isViewInvisible(viewNode.getView())) return;
if (!disposeWebView(viewNode) && ViewUtil.canCircle(viewNode.getView())) {
mViewElements.add(createViewElementBuilder(viewNode).build());
}
Expand All @@ -225,28 +206,40 @@ private boolean disposeWebView(ViewNode viewNode) {
if (ClassExistHelper.isWebView(viewNode.getView())) {
ModelLoader<HybridDom, HybridJson> modelLoader = ScreenshotProvider.get().getHybridModelLoader();
if (modelLoader == null) return false;
mWebViewCount.incrementAndGet();
mScreenLock.incrementAndGet();
LoadDataFetcher<HybridJson> loadDataFetcher = (LoadDataFetcher<HybridJson>) modelLoader.buildLoadData(new HybridDom(viewNode.getView())).fetcher;
loadDataFetcher.loadData(new LoadDataFetcher.DataCallback<HybridJson>() {
@Override
public void onDataReady(HybridJson data) {
ViewElement.Builder elementBuilder = createViewElementBuilder(viewNode);
mViewElements.add(elementBuilder.setWebView(data.getJsonObject()).build());
if (mWebViewCount.decrementAndGet() == 0) {
callResultOnSuccess();
}
callResultOnSuccess();
}

@Override
public void onLoadFailed(Exception e) {
callResultOnFailed();
callResultOnSuccess();
}
});
return true;
}
return false;
}

private void callResultOnSuccess() {
if (mScreenLock.decrementAndGet() <= 0) {
if (mScreenshotResultCallback != null) {
mScreenshotResultCallback.onSuccess(new CircleScreenshot(this));
}
}
}

private void callResultOnFailed() {
if (mScreenshotResultCallback != null) {
mScreenshotResultCallback.onFailed();
}
}

private void checkView2PageElement(View view) {
Page<?> viewPage = ViewAttributeUtil.getViewPage(view);
if (viewPage != null) {
Expand All @@ -273,5 +266,9 @@ private void checkView2PageElement(View view) {
}
}
}

private boolean isViewInvisible(View view) {
return view.getVisibility() == View.GONE || view.getWidth() <= 0 || view.getHeight() <= 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,25 @@ public interface OnScreenshotRefreshedListener {
}

private long mSnapshotKey = 0;
private Disposable mCircleScreenshotDisposable;

public void sendScreenshotRefreshed(String screenshotBase64, float scale) {
if (mCircleScreenshotDisposable != null) {
mCircleScreenshotDisposable.dispose();
}

mCircleScreenshotDisposable = new CircleScreenshot.Builder()
CircleScreenshot.Builder builder = new CircleScreenshot.Builder()
.setScale(scale)
.setScreenshot(screenshotBase64)
.setSnapshotKey(mSnapshotKey++)
.build(new Callback<CircleScreenshot>() {
@Override
public void onSuccess(CircleScreenshot result) {
Logger.d(TAG, "Create circle screenshot successfully");
sendScreenshot(result);
}

@Override
public void onFailed() {
Logger.e(TAG, "Create circle screenshot failed");
}
});
.setSnapshotKey(mSnapshotKey++);

builder.build(new Callback<CircleScreenshot>() {
@Override
public void onSuccess(CircleScreenshot result) {
Logger.d(TAG, "Create circle screenshot successfully");
sendScreenshot(result);
}

@Override
public void onFailed() {
Logger.e(TAG, "Create circle screenshot failed");
}
});
}

public void sendScreenshot(CircleScreenshot result) {
Expand Down

0 comments on commit 881d64f

Please sign in to comment.