Skip to content

Commit

Permalink
feat: 圈选截图不包含悬浮提示View
Browse files Browse the repository at this point in the history
  • Loading branch information
LitterSun authored and LitterSun committed Oct 22, 2020
1 parent 2ad4f05 commit 81be133
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.growingio.autotest.autotracker.webservices;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
Expand All @@ -33,7 +32,6 @@
import com.growingio.android.sdk.autotrack.view.DecorView;
import com.growingio.android.sdk.autotrack.view.WindowHelper;
import com.growingio.android.sdk.track.log.Logger;
import com.growingio.android.sdk.track.providers.ActivityStateProvider;
import com.growingio.android.sdk.track.webservices.widget.TipView;
import com.growingio.autotest.TestTrackConfiguration;
import com.growingio.autotest.WebServicesTest;
Expand All @@ -48,13 +46,16 @@
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
Expand All @@ -70,6 +71,19 @@ public static void beforeAppOnCreate() {

@Test
public void circleServiceTest() {
String uri = "growing.b6e4218d94f2bffc://growingio/webservice?serviceType=circle&wsUrl=" + Uri.encode(getWsUrl());
Intent intent = new Intent();
intent.setData(Uri.parse(uri));
ActivityScenario.launch(intent);

ActivityScenario.launch(ClickTestActivity.class);
TrackHelper.waitUiThreadForIdleSync();
List<DecorView> decorViews = WindowHelper.get().getAllWindowDecorViews();
View topView = decorViews.get(decorViews.size() - 1).getView();
Truth.assertThat(topView instanceof TipView).isTrue();
TrackHelper.postToUiThread(() -> topView.setVisibility(View.GONE)); //隐藏悬浮窗,防止遮挡其他view
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

HashSet<String> allElements = new HashSet<String>() {{
add("/Page/ActionBarOverlayLayout[0]/FrameLayout[0]/LinearLayout[0]#content_parent");
add("/Page/ActionBarOverlayLayout[0]/FrameLayout[0]/LinearLayout[0]#content_parent/Button[0]#btn_test_click");
Expand Down Expand Up @@ -107,29 +121,19 @@ public void circleServiceTest() {
}
});

String uri = "growing.b6e4218d94f2bffc://growingio/webservice?serviceType=circle&wsUrl=" + Uri.encode(getWsUrl());
Intent intent = new Intent();
intent.setData(Uri.parse(uri));
ActivityScenario.launch(intent);
TrackHelper.waitUiThreadForIdleSync();
Activity foregroundActivity = ActivityStateProvider.get().getForegroundActivity();
foregroundActivity.startActivity(new Intent(foregroundActivity, ClickTestActivity.class));
TrackHelper.waitUiThreadForIdleSync();

DecorView[] decorViews = WindowHelper.get().getAllWindowDecorViews();
View topView = decorViews[decorViews.length - 1].getView();
Truth.assertThat(topView instanceof TipView).isTrue();
TrackHelper.postToUiThread(() -> topView.setVisibility(View.GONE)); //隐藏悬浮窗,防止遮挡其他view
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext());
Awaiter.untilTrue(receivedMessage);
setOnReceivedMessageListener(null);

pressBack();
TrackHelper.postToUiThread(() -> {
topView.setVisibility(View.VISIBLE);
topView.callOnClick();
});
TrackHelper.waitUiThreadForIdleSync();

onView(withText("圈选")).check(matches(isDisplayed()));
onView(withText("您是否要退出圈选?")).check(matches(isDisplayed()));
onView(withText("取消")).perform(click());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

public class Awaiter {
private static final int DEFAULT_TIMEOUT = 5;
private static final int DEFAULT_TIMEOUT = 10;

private Awaiter() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.growingio.android.sdk.autotrack.shadow.WindowManagerShadow;
import com.growingio.android.sdk.track.log.Logger;
import com.growingio.android.sdk.track.providers.ActivityStateProvider;
import com.growingio.android.sdk.track.utils.ActivityUtil;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -107,28 +106,10 @@ private String getSubWindowPrefix(View root) {
}

@NonNull
public DecorView[] getTopActivityViews() {
Activity activity = ActivityStateProvider.get().getForegroundActivity();
if (activity == null) {
return new DecorView[0];
}

List<DecorView> topViews = new ArrayList<>();
for (DecorView decorView : getAllWindowDecorViews()) {
View view = decorView.getView();
if (view == activity.getWindow().getDecorView()
|| view.getContext() == activity
|| ActivityUtil.findActivity(view.getContext()) == activity) {
topViews.add(decorView);
}
}
return topViews.toArray(new DecorView[0]);
}

public DecorView[] getTopWindowViews() {
public List<DecorView> getTopActivityViews() {
List<DecorView> topViews = new ArrayList<>();
Activity activity = ActivityStateProvider.get().getForegroundActivity();
DecorView[] decorViews = getAllWindowDecorViews();
List<DecorView> decorViews = getAllWindowDecorViews();
boolean findTopActivity = false;
for (DecorView decorView : decorViews) {
View view = decorView.getView();
Expand All @@ -140,10 +121,10 @@ public DecorView[] getTopWindowViews() {
topViews.add(decorView);
}
}
return topViews.toArray(new DecorView[0]);
return topViews;
}

public DecorView[] getAllWindowDecorViews() {
public List<DecorView> getAllWindowDecorViews() {
List<DecorView> decorViews = new ArrayList<>();
View[] allViews = WindowHelper.get().getWindowViews();
for (View view : allViews) {
Expand All @@ -158,7 +139,7 @@ public DecorView[] getAllWindowDecorViews() {
decorViews.add(new DecorView(view, area, windowParams));
}
}
return decorViews.toArray(new DecorView[0]);
return decorViews;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import com.growingio.android.sdk.track.listener.ListenerContainer;
import com.growingio.android.sdk.track.log.Logger;
import com.growingio.android.sdk.track.utils.DeviceUtil;
import com.growingio.android.sdk.track.webservices.widget.TipView;

import java.io.IOException;
import java.util.List;

public class ScreenshotProvider extends ListenerContainer<ScreenshotProvider.OnScreenshotRefreshedListener, String> {
private static final String TAG = "ScreenshotProvider";
Expand Down Expand Up @@ -80,11 +82,17 @@ public void onDomChanged() {
}

private void dispatchScreenshot() {
DecorView[] decorViews = WindowHelper.get().getTopActivityViews();
if (decorViews.length == 0) {
List<DecorView> decorViews = WindowHelper.get().getTopActivityViews();
if (decorViews.isEmpty()) {
return;
}
View topView = decorViews[decorViews.length - 1].getView();
for (int i = decorViews.size() - 1; i >= 0; i--) {
if (decorViews.get(i).getView() instanceof TipView) {
decorViews.remove(i);
break;
}
}
View topView = decorViews.get(decorViews.size() - 1).getView();
topView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.growingio.android.sdk.track.utils.DeviceUtil;
import com.growingio.android.sdk.autotrack.view.DecorView;
import com.growingio.android.sdk.autotrack.view.WindowHelper;
import com.growingio.android.sdk.track.ContextProvider;
import com.growingio.android.sdk.autotrack.view.DecorView;
import com.growingio.android.sdk.track.utils.DeviceUtil;
import com.growingio.android.sdk.track.webservices.widget.TipView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public class ScreenshotUtil {
private ScreenshotUtil() {
Expand Down Expand Up @@ -59,14 +61,21 @@ public static Bitmap getScreenshotBitmap(float scale) {
}

public static Bitmap getScreenshotBitmap() {
DecorView[] decorViews = WindowHelper.get().getTopActivityViews();
List<DecorView> decorViews = WindowHelper.get().getTopActivityViews();
for (int i = decorViews.size() - 1; i >= 0; i--) {
if (decorViews.get(i).getView() instanceof TipView) {
decorViews.remove(i);
break;
}
}

DisplayMetrics metrics = DeviceUtil.getDisplayMetrics(ContextProvider.getApplicationContext());
Bitmap bitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);
drawDecorViewsToBitmap(decorViews, bitmap);
return bitmap;
}

private static void drawDecorViewsToBitmap(DecorView[] decorViews, Bitmap bitmap) {
private static void drawDecorViewsToBitmap(List<DecorView> decorViews, Bitmap bitmap) {
if (null != decorViews) {
for (DecorView decorView : decorViews) {
drawDecorViewToBitmap(decorView, bitmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@

import com.growingio.android.sdk.autotrack.hybrid.HybridBridgeProvider;
import com.growingio.android.sdk.autotrack.hybrid.SuperWebView;
import com.growingio.android.sdk.autotrack.view.ViewNode;
import com.growingio.android.sdk.autotrack.page.Page;
import com.growingio.android.sdk.autotrack.page.PageProvider;
import com.growingio.android.sdk.track.utils.DeviceUtil;
import com.growingio.android.sdk.autotrack.view.DecorView;
import com.growingio.android.sdk.autotrack.view.ViewAttributeUtil;
import com.growingio.android.sdk.autotrack.view.ViewHelper;
import com.growingio.android.sdk.autotrack.view.ViewNode;
import com.growingio.android.sdk.autotrack.view.WindowHelper;
import com.growingio.android.sdk.autotrack.webservices.circle.ViewUtil;
import com.growingio.android.sdk.autotrack.view.DecorView;
import com.growingio.android.sdk.track.ContextProvider;
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.utils.ClassExistHelper;
import com.growingio.android.sdk.track.utils.DeviceUtil;
import com.growingio.android.sdk.track.webservices.widget.TipView;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -137,8 +138,11 @@ public Disposable build(Callback<CircleScreenshot> callback) {
mScreenWidth = displayMetrics.widthPixels;
mScreenHeight = displayMetrics.heightPixels;

DecorView[] decorViews = WindowHelper.get().getTopWindowViews();
List<DecorView> decorViews = WindowHelper.get().getTopActivityViews();
for (DecorView decorView : decorViews) {
if (decorView.getView() instanceof TipView) {
continue;
}
checkView2PageElement(decorView.getView());
checkView2ViewElement(decorView.getView());
}
Expand Down

0 comments on commit 81be133

Please sign in to comment.