Skip to content

Commit

Permalink
perf: 优化事件模型继承链
Browse files Browse the repository at this point in the history
  • Loading branch information
LitterSun authored and 小肥阳 committed Aug 31, 2020
1 parent 6f289f5 commit 0c34b53
Show file tree
Hide file tree
Showing 28 changed files with 499 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private static void initAutotrackSDKInUI(AutotrackConfiguration autotrackConfigu
GrowingAutotracker autotrack = new GrowingAutotracker();
PageProvider.get().start();
ViewChangeProvider.get().start();
sInstance = autotrack;
LogUtil.d(TAG, "Autotracker module init success in ui thread");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ public static void viewOnChange(View view) {
}

private static void sendChangeEvent(ViewNode viewNode) {
ViewElementEvent.EventBuilder event = new ViewElementEvent.EventBuilder();
ViewElementEvent.Builder event = new ViewElementEvent.Builder();
Page<?> page = PageProvider.get().findPage(viewNode.getView());
TrackMainThread.trackMain().postEventToTrackMain(
new ViewElementEvent.EventBuilder()
new ViewElementEvent.Builder()
.setEventType(AutotrackEventType.VIEW_CHANGE)
.setPageName(page.path())
.setPageShowTimestamp(page.getShowTimestamp())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ public static void menuItemOnClick(MenuItem menuItem) {
}

private static void sendClickEvent(ViewNode viewNode) {
ViewElementEvent.EventBuilder event = new ViewElementEvent.EventBuilder();
Page<?> page = PageProvider.get().findPage(viewNode.getView());
TrackMainThread.trackMain().postEventToTrackMain(
new ViewElementEvent.EventBuilder()
new ViewElementEvent.Builder()
.setEventType(AutotrackEventType.VIEW_CLICK)
.setPageName(page.path())
.setPageShowTimestamp(page.getShowTimestamp())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private AutoTrackEventGenerator() {

public static void generatePageEvent(String pageName, String title, long timestamp) {
TrackMainThread.trackMain().postEventToTrackMain(
new PageEvent.EventBuilder()
new PageEvent.Builder()
.setPageName(pageName)
.setTitle(title)
.setTimestamp(timestamp)
Expand All @@ -35,7 +35,7 @@ public static void generatePageEvent(String pageName, String title, long timesta

public static void generatePageAttributesEvent(String pageName, long pageShowTimestamp, Map<String, String> attributes) {
TrackMainThread.trackMain().postEventToTrackMain(
new PageAttributesEvent.EventBuilder()
new PageAttributesEvent.Builder()
.setPageName(pageName)
.setPageShowTimestamp(pageShowTimestamp)
.setAttributes(attributes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,76 @@

package com.growingio.android.sdk.autotrack.events;

import com.growingio.android.sdk.autotrack.events.base.BasePageAttributesEvent;
import com.growingio.android.sdk.track.events.base.BaseAttributesEvent;

public class PageAttributesEvent extends BasePageAttributesEvent {
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class PageAttributesEvent extends BaseAttributesEvent {
private static final long serialVersionUID = 1L;

protected PageAttributesEvent(EventBuilder eventBuilder) {
private final String mPageName;
private final long mPageShowTimestamp;

protected PageAttributesEvent(Builder eventBuilder) {
super(eventBuilder);
mPageName = eventBuilder.mPageName;
mPageShowTimestamp = eventBuilder.mPageShowTimestamp;
}

public static final class EventBuilder extends BasePageAttributesEvent.EventBuilder<PageAttributesEvent> {
public String getPageName() {
return mPageName;
}

public long getPageShowTimestamp() {
return mPageShowTimestamp;
}

EventBuilder() {
@Override
public JSONObject toJSONObject() {
JSONObject json = super.toJSONObject();
try {
json.put("pageName", mPageName);
json.put("pageShowTimestamp", mPageShowTimestamp);
} catch (JSONException ignored) {
}
return json;
}

public static class Builder extends BaseAttributesEvent.Builder<PageAttributesEvent> {
private String mPageName;
private long mPageShowTimestamp;

public Builder() {
super();
}

public Builder setPageName(String pageName) {
mPageName = pageName;
return this;
}

public Builder setPageShowTimestamp(long pageShowTimestamp) {
mPageShowTimestamp = pageShowTimestamp;
return this;
}

@Override
public Builder setAttributes(Map<String, String> attributes) {
super.setAttributes(attributes);
return this;
}

@Override
public String getEventType() {
return AutotrackEventType.PAGE_ATTRIBUTES;
}

@Override
public PageAttributesEvent build() {
return new PageAttributesEvent(this);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,106 @@

package com.growingio.android.sdk.autotrack.events;

import com.growingio.android.sdk.autotrack.events.base.BasePageEvent;
import android.app.Activity;

public class PageEvent extends BasePageEvent {
import com.growingio.android.sdk.track.events.base.BaseEvent;
import com.growingio.android.sdk.track.providers.ActivityStateProvider;

import org.json.JSONException;
import org.json.JSONObject;

public class PageEvent extends BaseEvent {
private static final long serialVersionUID = 1L;

protected PageEvent(EventBuilder eventBuilder) {
private static final String ORIENTATION_PORTRAIT = "PORTRAIT";
private static final String ORIENTATION_LANDSCAPE = "LANDSCAPE";

private final String mPageName;
private final String mOrientation;
private final String mTitle;
private final String mReferralPage;

protected PageEvent(Builder eventBuilder) {
super(eventBuilder);
mPageName = eventBuilder.mPageName;
mOrientation = eventBuilder.mOrientation;
mTitle = eventBuilder.mTitle;
mReferralPage = eventBuilder.mReferralPage;
}

public String getPageName() {
return mPageName;
}

public String getOrientation() {
return mOrientation;
}

public String getTitle() {
return mTitle;
}

public String getReferralPage() {
return mReferralPage;
}

@Override
public JSONObject toJSONObject() {
JSONObject json = super.toJSONObject();
try {
json.put("pageName", mPageName);
json.put("orientation", mOrientation);
json.put("title", mTitle);
json.put("referralPage", mReferralPage);
} catch (JSONException ignored) {
}
return json;
}

public static class EventBuilder extends BasePageEvent.EventBuilder<PageEvent> {
public static class Builder extends BaseBuilder<PageEvent> {
private String mPageName;
private String mOrientation;
private String mTitle;
private String mReferralPage;

EventBuilder() {
public Builder() {
super();
mReferralPage = "";
Activity activity = ActivityStateProvider.get().getResumedActivity();
if (activity != null) {
mOrientation = activity.getResources().getConfiguration().orientation == 1
? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
}
}

public Builder setPageName(String pageName) {
mPageName = pageName;
return this;
}

public Builder setTitle(String title) {
mTitle = title;
return this;
}

public Builder setReferralPage(String referralPage) {
mReferralPage = referralPage;
return this;
}

@Override
public String getEventType() {
return AutotrackEventType.PAGE;
}

@Override
public PageEvent build() {
return new PageEvent(this);
}

public Builder setTimestamp(long timestamp) {
mTimestamp = timestamp;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
* limitations under the License.
*/

package com.growingio.android.sdk.autotrack.events.base;
package com.growingio.android.sdk.autotrack.events;

import com.growingio.android.sdk.autotrack.events.AutotrackEventType;
import com.growingio.android.sdk.track.events.base.BaseAttributesEvent;
import com.growingio.android.sdk.track.events.CustomEvent;

import org.json.JSONException;
import org.json.JSONObject;

public abstract class BasePageAttributesEvent extends BaseAttributesEvent {
import java.util.Map;

public class PageLevelCustomEvent extends CustomEvent {
private static final long serialVersionUID = 1L;

private final String mPageName;
private final long mPageShowTimestamp;

protected BasePageAttributesEvent(EventBuilder<?> eventBuilder) {
protected PageLevelCustomEvent(Builder eventBuilder) {
super(eventBuilder);
mPageName = eventBuilder.mPageName;
mPageShowTimestamp = eventBuilder.mPageShowTimestamp;
Expand All @@ -51,35 +54,38 @@ public JSONObject toJSONObject() {
return json;
}

public abstract static class EventBuilder<T extends BasePageAttributesEvent> extends BaseAttributesEvent.EventBuilder<T> {
public static class Builder extends CustomEvent.Builder {
private String mPageName;
private long mPageShowTimestamp;

public EventBuilder() {
super();
public Builder() {
}

public String getPageName() {
return mPageName;
public Builder setPageName(String pageName) {
mPageName = pageName;
return this;
}

public EventBuilder<T> setPageName(String pageName) {
mPageName = pageName;
public Builder setPageShowTimestamp(long pageShowTimestamp) {
mPageShowTimestamp = pageShowTimestamp;
return this;
}

public long getPageShowTimestamp() {
return mPageShowTimestamp;
@Override
public Builder setEventName(String eventName) {
super.setEventName(eventName);
return this;
}

public EventBuilder<T> setPageShowTimestamp(long pageShowTimestamp) {
mPageShowTimestamp = pageShowTimestamp;
@Override
public Builder setAttributes(Map<String, String> attributes) {
super.setAttributes(attributes);
return this;
}

@Override
public String getEventType() {
return AutotrackEventType.PAGE_ATTRIBUTES;
public PageLevelCustomEvent build() {
return new PageLevelCustomEvent(this);
}
}
}
Loading

0 comments on commit 0c34b53

Please sign in to comment.