Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autotrack switch #213

Merged
merged 9 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ protobuf = "3.22.3"
powermock = "2.0.9"

# !!! SDK VERSION !!!
growingio = "4.2.0"
growingioCode = "40200"
growingioPlugin = "4.1.0"
growingio = "4.2.1"
growingioCode = "40201"
growingioPlugin = "4.2.0"

[plugins]
android-application = { id = "com.android.application", version.ref = "pluginGradle" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class AutotrackConfig implements Configurable {
private boolean enableFragmentTag = false;
private final AutotrackOptions mAutotrackOptions = new AutotrackOptions();

private boolean autotrackEnabled = true;
private int pageXmlRes = 0;
private final List<PageRule> pageRules = new ArrayList<>();

Expand Down Expand Up @@ -144,4 +145,13 @@ public List<PageRule> getPageRules() {
public int getPageXmlRes() {
return pageXmlRes;
}

public boolean isAutotrack() {
return autotrackEnabled;
}

public AutotrackConfig setAutotrack(boolean enabled) {
this.autotrackEnabled = enabled;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void run() {

private final ViewTreeStatusObserver viewTreeStatusObserver;
private ActivityStateProvider activityStateProvider;
private AutotrackConfig autotrackConfig;

public ImpressionProvider() {
viewTreeStatusObserver = new ViewTreeStatusObserver(this);
Expand All @@ -68,10 +69,13 @@ public ImpressionProvider() {
@Override
public void setup(TrackerContext context) {

AutotrackConfig configuration = context.getConfigurationProvider().getConfiguration(AutotrackConfig.class);
impressionScale = configuration == null ? 0f : configuration.getImpressionScale();

autotrackConfig = context.getConfigurationProvider().getConfiguration(AutotrackConfig.class);
impressionScale = autotrackConfig == null ? 0f : autotrackConfig.getImpressionScale();
activityStateProvider = context.getActivityStateProvider();

if (autotrackConfig == null || !autotrackConfig.isAutotrack()) {
return;
}
activityStateProvider.registerActivityLifecycleListener(this);
}

Expand Down Expand Up @@ -165,6 +169,10 @@ public void trackViewImpression(View view, String impressionEventName, Map<Strin
if (view == null || TextUtils.isEmpty(impressionEventName)) {
return;
}
if (autotrackConfig == null || !autotrackConfig.isAutotrack()) {
Logger.d(TAG, "autotrack is not enabled");
return;
}
if (ViewAttributeUtil.isIgnoredView(view)) {
Logger.w(TAG, "Current view is set to ignore");
return;
Expand Down Expand Up @@ -215,6 +223,10 @@ public void stopTrackViewImpression(View trackedView) {
if (trackedView == null) {
return;
}
if (autotrackConfig == null || !autotrackConfig.isAutotrack()) {
Logger.d(TAG, "autotrack is not enabled");
return;
}
Activity activity = findViewActivity(trackedView);
if (activity == null) {
Logger.e(TAG, "TrackedView context activity is NULL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ public class ViewChangeProvider implements IActivityLifecycle, OnViewStateChange

@Override
public void setup(TrackerContext context) {
activityStateProvider = context.getActivityStateProvider();
autotrackConfig = context.getConfigurationProvider().getConfiguration(AutotrackConfig.class);
activityStateProvider.registerActivityLifecycleListener(this);
activityStateProvider = context.getActivityStateProvider();
viewNodeProvider = context.getProvider(ViewNodeProvider.class);

if (autotrackConfig == null || !autotrackConfig.isAutotrack()) return;
activityStateProvider.registerActivityLifecycleListener(this);
viewTreeStatusObserver = new ViewTreeStatusObserver(false, false, true, false, this,
R.id.growing_tracker_monitoring_focus_change);
}
Expand All @@ -67,7 +68,7 @@ public void shutdown() {
@Override
public void onActivityLifecycle(ActivityLifecycleEvent event) {
Activity activity = event.getActivity();
if (activity == null) {
if (activity == null || viewTreeStatusObserver == null) {
return;
}
if (event.eventType == ActivityLifecycleEvent.EVENT_TYPE.ON_RESUMED) {
Expand Down Expand Up @@ -138,6 +139,11 @@ private void sendChangeEvent(View view) {
Logger.e(TAG, "Autotracker do not initialized successfully");
}

if (autotrackConfig == null || !autotrackConfig.isAutotrack()) {
Logger.d(TAG, "autotrack is not enabled");
return;
}

if (view == null) {
Logger.e(TAG, "viewOnChange:View is NULL");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public void viewOnClick(View view) {
return;
}

if (autotrackConfig == null || !autotrackConfig.isAutotrack()) {
Logger.d(TAG, "autotrack is not enabled");
return;
}

if (view == null) {
Logger.e(TAG, "viewOnClick: view is NULL");
return;
Expand All @@ -207,6 +212,11 @@ public void menuItemOnClick(Activity activity, MenuItem menuItem) {
return;
}

if (autotrackConfig == null || !autotrackConfig.isAutotrack()) {
Logger.d(TAG, "autotrack is not enabled");
return;
}

if (activity == null || menuItem == null) {
Logger.e(TAG, "menuItemOnClick: activity or menuItem is NULL");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public String getClassName() {

@Override
public boolean isAutotrack() {
if (pageConfig == null || !pageConfig.isAutotrack()) {
return false;
}
// cdp downgrade when activity page is enabled
if (pageConfig != null && pageConfig.isActivityPageEnabled()) {
if (pageConfig.isActivityPageEnabled()) {
return !isIgnored();
}
return super.isAutotrack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ private String transformSwitcherTag(String tag) {

@Override
public boolean isAutotrack() {
if (pageConfig == null || !pageConfig.isAutotrack()) {
return false;
}
// cdp downgrade when fragment page is enabled
if (pageConfig != null && pageConfig.isFragmentPageEnabled()) {
if (pageConfig.isFragmentPageEnabled()) {
return !isIgnored();
}
return super.isAutotrack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ class PageConfig {
private boolean isFragmentPageEnabled = true;
private boolean isDowngrade = false;

public PageConfig(List<PageRule> pageRuleList, boolean isActivityPageEnabled, boolean isFragmentPageEnabled, boolean enableFragmentTag, boolean isDowngrade) {
private boolean autotrack = true;

public PageConfig(List<PageRule> pageRuleList, boolean isActivityPageEnabled, boolean isFragmentPageEnabled, boolean enableFragmentTag, boolean isDowngrade,boolean autotrack) {
this.pageRuleList = pageRuleList;
this.isActivityPageEnabled = isActivityPageEnabled;
this.isFragmentPageEnabled = isFragmentPageEnabled;
this.enableFragmentTag = enableFragmentTag;
this.isDowngrade = isDowngrade;
this.autotrack = autotrack;
}

public boolean isEnableFragmentTag() {
Expand All @@ -52,4 +55,8 @@ public boolean isFragmentPageEnabled() {
public boolean isDowngrade() {
return isDowngrade;
}

public boolean isAutotrack() {
return autotrack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ private PageProvider() {
@Override
public void setup(TrackerContext context) {
activityStateProvider = context.getActivityStateProvider();
activityStateProvider.registerActivityLifecycleListener(this);
loadPageConfig(context);
}

private void loadPageConfig(TrackerContext context) {
ConfigurationProvider configurationProvider = context.getConfigurationProvider();
AutotrackConfig autotrackConfig = configurationProvider.getConfiguration(AutotrackConfig.class);
boolean isDowngrade = configurationProvider.isDowngrade();

loadPageConfig(context, autotrackConfig, configurationProvider.isDowngrade());

activityStateProvider.registerActivityLifecycleListener(this);
}

private void loadPageConfig(TrackerContext context, AutotrackConfig autotrackConfig, boolean isDowngrade) {
if (autotrackConfig != null) {
boolean isActivityPageEnabled = autotrackConfig.getAutotrackOptions().isActivityPageEnabled();
boolean isFragmentPageEnabled = autotrackConfig.getAutotrackOptions().isFragmentPageEnabled();
Expand All @@ -88,15 +90,17 @@ private void loadPageConfig(TrackerContext context) {
autotrackConfig.getPageRules().addAll(0, pageRuleList);

List<PageRule> pageRules = Collections.unmodifiableList(autotrackConfig.getPageRules());
pageConfig = new PageConfig(pageRules, isActivityPageEnabled, isFragmentPageEnabled, enableFragmentTag, isDowngrade);
pageConfig = new PageConfig(pageRules, isActivityPageEnabled, isFragmentPageEnabled, enableFragmentTag, isDowngrade, autotrackConfig.isAutotrack());
} else {
pageConfig = new PageConfig(null, isDowngrade, isDowngrade, false, isDowngrade);
pageConfig = new PageConfig(null, isDowngrade, isDowngrade, false, isDowngrade, true);
}
}


@Override
public void shutdown() {
ALL_PAGE_TREE.clear();
CACHE_PAGES.clear();
activityStateProvider.unregisterActivityLifecycleListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.android.material.tabs.TabLayout;
import com.growingio.android.sdk.autotrack.shadow.ListMenuItemViewShadow;
import com.growingio.android.sdk.track.TrackMainThread;
import com.growingio.android.sdk.track.log.Logger;
import com.growingio.android.sdk.track.utils.ClassExistHelper;

public class ViewUtil {
Expand Down Expand Up @@ -107,8 +108,13 @@ public static String getWidgetContent(View widget) {
return compoundButtonContentValue((CompoundButton) widget);
}
if (ClassExistHelper.hasClass("com.google.android.material.tabs.TabLayout")) {
if (widget instanceof TabLayout.TabView) {
return tabViewContentValue((TabLayout.TabView) widget);
try {
if (widget instanceof TabLayout.TabView) {
return tabViewContentValue((TabLayout.TabView) widget);
}
} catch (IllegalAccessError e) {
// TabLayout.TabView在1.1.0版本才开始修改为public访问权限
Logger.e("ViewUtil", "TabLayout version is low.");
}
}
if (ClassExistHelper.hasClass("com.google.android.material.slider.Slider")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,21 @@ public void registerComponent(LibraryGioModule module, Configurable config) {
addComponent(module, config);
}

/**
* uninstall module
*/
protected void uninstallComponent(Class modelClazz, Class dataClazz, Class<? extends Configurable> configClazz, Class<? extends TrackerLifecycleProvider> providerClazz) {
if (modelClazz == null) return;
if (configClazz != null) {
trackerContext.getConfigurationProvider().removeConfiguration(configClazz);
}
trackerContext.getRegistry().unregister(modelClazz, dataClazz);
if (providerClazz != null) {
TrackerLifecycleProvider provider = trackerContext.getProviderStore().remove(providerClazz);
if (provider != null) provider.shutdown();
}
}

private void addComponent(LibraryGioModule module, Configurable config) {
if (module == null) return;
if (config != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class EventResponse {
private final boolean succeeded;
private final InputStream stream; //it's useless
private final InputStream stream;
private final long usedBytes; //it's useless

public EventResponse(boolean succeeded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public void addConfiguration(Configurable config) {
}
}

public void removeConfiguration(Class configClazz) {
if (configClazz != null) {
sModuleConfigs.remove(configClazz);
}
}

public String printAllConfigurationInfo() {
StringBuilder info = new StringBuilder();
if (!mCoreConfiguration.isDebugEnabled()) {
Expand Down
1 change: 1 addition & 0 deletions growingio-webservice/circler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ android {
}

dependencies {
testImplementation libs.google.material
testImplementation libs.bundles.test
testImplementation libs.okhttp3.mockwebserver
testImplementation libs.androidx.test.core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.growingio.android.circler;

import com.growingio.android.sdk.TrackerContext;
import com.growingio.android.sdk.autotrack.AutotrackConfig;
import com.growingio.android.sdk.track.modelloader.ModelLoader;
import com.growingio.android.sdk.track.modelloader.ModelLoaderFactory;
import com.growingio.android.sdk.track.middleware.webservice.Circler;
Expand Down Expand Up @@ -72,6 +73,11 @@ public Factory(TrackerContext context) {

@Override
public ModelLoader<Circler, WebService> build() {
// circler is enabled when autotrack is enabled
AutotrackConfig config = context.getConfigurationProvider().getConfiguration(AutotrackConfig.class);
if (config == null || !config.isAutotrack()) {
return null;
}
return new CirclerDataLoader(getsInternalClient(), context);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import com.google.common.truth.Truth;
import com.google.common.util.concurrent.Uninterruptibles;
import com.growingio.android.circler.shadow.ShadowThreadUtils;
import com.growingio.android.sdk.Configurable;
import com.growingio.android.sdk.CoreConfiguration;
import com.growingio.android.sdk.Tracker;
import com.growingio.android.sdk.TrackerContext;
import com.growingio.android.sdk.autotrack.AutotrackConfig;
import com.growingio.android.sdk.track.modelloader.DataFetcher;
import com.growingio.android.sdk.track.modelloader.ModelLoader;
import com.growingio.android.sdk.track.middleware.webservice.Circler;
import com.growingio.android.sdk.track.middleware.webservice.WebService;
import com.growingio.android.sdk.track.providers.TrackerLifecycleProviderFactory;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -39,6 +43,7 @@
import org.robolectric.annotation.Config;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -78,6 +83,10 @@ private void sendMessage(WebSocket webSocket, String message) {

@Before
public void setup() {
Map<Class<? extends Configurable>, Configurable> sModuleConfigs = new HashMap<>();
sModuleConfigs.put(AutotrackConfig.class, new AutotrackConfig());
TrackerLifecycleProviderFactory.create().createConfigurationProviderWithConfig(new CoreConfiguration("growing.project", "growing.test"), sModuleConfigs);

Tracker tracker = new Tracker(application);
CirclerLibraryGioModule dModule = new CirclerLibraryGioModule();
tracker.registerComponent(dModule);
Expand Down
Loading