正在查看: Glean v1.1.34 应用的 AnalyticsMessages.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: Glean v1.1.34 应用的 AnalyticsMessages.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.mixpanel.android.mpmetrics;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import com.mixpanel.android.mpmetrics.MPDbAdapter;
import com.mixpanel.android.util.HttpService;
import com.mixpanel.android.util.LegacyVersionUtils;
import com.mixpanel.android.util.MPLog;
import com.mixpanel.android.util.RemoteService;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
class AnalyticsMessages {
private static final int CLEAR_ANONYMOUS_UPDATES = 7;
private static final int EMPTY_QUEUES = 6;
private static final int ENQUEUE_EVENTS = 1;
private static final int ENQUEUE_GROUP = 3;
private static final int ENQUEUE_PEOPLE = 0;
private static final int FLUSH_QUEUE = 2;
private static final int KILL_WORKER = 5;
private static final String LOGTAG = "MixpanelAPI.Messages";
private static final int PUSH_ANONYMOUS_PEOPLE_RECORDS = 4;
private static final int REMOVE_RESIDUAL_IMAGE_FILES = 9;
private static final int REWRITE_EVENT_PROPERTIES = 8;
private static final Map<Context, AnalyticsMessages> sInstances = new HashMap();
protected final MPConfig mConfig;
protected final Context mContext;
private final Worker mWorker = createWorker();
AnalyticsMessages(Context context) {
this.mContext = context;
this.mConfig = getConfig(context);
getPoster().checkIsMixpanelBlocked();
}
protected Worker createWorker() {
return new Worker();
}
public static AnalyticsMessages getInstance(Context context) {
AnalyticsMessages analyticsMessages;
Map<Context, AnalyticsMessages> map = sInstances;
synchronized (map) {
Context applicationContext = context.getApplicationContext();
if (!map.containsKey(applicationContext)) {
analyticsMessages = new AnalyticsMessages(applicationContext);
map.put(applicationContext, analyticsMessages);
} else {
analyticsMessages = map.get(applicationContext);
}
}
return analyticsMessages;
}
public void eventsMessage(EventDescription eventDescription) {
Message obtain = Message.obtain();
obtain.what = 1;
obtain.obj = eventDescription;
this.mWorker.runMessage(obtain);
}
public void peopleMessage(PeopleDescription peopleDescription) {
Message obtain = Message.obtain();
obtain.what = 0;
obtain.obj = peopleDescription;
this.mWorker.runMessage(obtain);
}
public void groupMessage(GroupDescription groupDescription) {
Message obtain = Message.obtain();
obtain.what = 3;
obtain.obj = groupDescription;
this.mWorker.runMessage(obtain);
}
public void pushAnonymousPeopleMessage(PushAnonymousPeopleDescription pushAnonymousPeopleDescription) {
Message obtain = Message.obtain();
obtain.what = 4;
obtain.obj = pushAnonymousPeopleDescription;
this.mWorker.runMessage(obtain);
}
public void clearAnonymousUpdatesMessage(MixpanelDescription mixpanelDescription) {
Message obtain = Message.obtain();
obtain.what = 7;
obtain.obj = mixpanelDescription;
this.mWorker.runMessage(obtain);
}
public void postToServer(MixpanelDescription mixpanelDescription) {
Message obtain = Message.obtain();
obtain.what = 2;
obtain.obj = mixpanelDescription.getToken();
obtain.arg1 = 0;
this.mWorker.runMessage(obtain);
}
public void emptyTrackingQueues(MixpanelDescription mixpanelDescription) {
Message obtain = Message.obtain();
obtain.what = 6;
obtain.obj = mixpanelDescription;
this.mWorker.runMessage(obtain);
}
public void updateEventProperties(UpdateEventsPropertiesDescription updateEventsPropertiesDescription) {
Message obtain = Message.obtain();
obtain.what = 8;
obtain.obj = updateEventsPropertiesDescription;
this.mWorker.runMessage(obtain);
}
public void removeResidualImageFiles(File file) {
Message obtain = Message.obtain();
obtain.what = 9;
obtain.obj = file;
this.mWorker.runMessage(obtain);
}
public void hardKill() {
Message obtain = Message.obtain();
obtain.what = 5;
this.mWorker.runMessage(obtain);
}
boolean isDead() {
return this.mWorker.isDead();
}
protected MPDbAdapter makeDbAdapter(Context context) {
return MPDbAdapter.getInstance(context);
}
protected MPConfig getConfig(Context context) {
return MPConfig.getInstance(context);
}
protected RemoteService getPoster() {
return new HttpService();
}
static class EventDescription extends MixpanelMessageDescription {
private final String mEventName;
private final boolean mIsAutomatic;
private final JSONObject mSessionMetadata;
public EventDescription(String str, JSONObject jSONObject, String str2) {
this(str, jSONObject, str2, false, new JSONObject());
}
public EventDescription(String str, JSONObject jSONObject, String str2, boolean z, JSONObject jSONObject2) {
super(str2, jSONObject);
this.mEventName = str;
this.mIsAutomatic = z;
this.mSessionMetadata = jSONObject2;
}
public String getEventName() {
return this.mEventName;
}
public JSONObject getProperties() {
return getMessage();
}
public JSONObject getSessionMetadata() {
return this.mSessionMetadata;
}
public boolean isAutomatic() {
return this.mIsAutomatic;
}
}
static class PeopleDescription extends MixpanelMessageDescription {
public PeopleDescription(JSONObject jSONObject, String str) {
super(str, jSONObject);
}
public String toString() {
return getMessage().toString();
}
public boolean isAnonymous() {
return !getMessage().has("$distinct_id");
}
}
static class GroupDescription extends MixpanelMessageDescription {
public GroupDescription(JSONObject jSONObject, String str) {
super(str, jSONObject);
}
public String toString() {
return getMessage().toString();
}
}
static class PushAnonymousPeopleDescription extends MixpanelDescription {
private final String mDistinctId;
public PushAnonymousPeopleDescription(String str, String str2) {
super(str2);
this.mDistinctId = str;
}
public String toString() {
return this.mDistinctId;
}
public String getDistinctId() {
return this.mDistinctId;
}
}
static class MixpanelMessageDescription extends MixpanelDescription {
private final JSONObject mMessage;
public MixpanelMessageDescription(String str, JSONObject jSONObject) {
super(str);
if (jSONObject != null && jSONObject.length() > 0) {
Iterator<String> keys = jSONObject.keys();
while (keys.hasNext()) {
String next = keys.next();
try {
jSONObject.get(next).toString();
} catch (AssertionError e) {
jSONObject.remove(next);
MPLog.e(AnalyticsMessages.LOGTAG, "Removing people profile property from update (see https://github.com/mixpanel/mixpanel-android/issues/567)", e);
} catch (JSONException unused) {
}
}
}
this.mMessage = jSONObject;
}
public JSONObject getMessage() {
return this.mMessage;
}
}
static class UpdateEventsPropertiesDescription extends MixpanelDescription {
private final Map<String, String> mProps;
public UpdateEventsPropertiesDescription(String str, Map<String, String> map) {
super(str);
this.mProps = map;
}
public Map<String, String> getProperties() {
return this.mProps;
}
}
static class MixpanelDescription {
private final String mToken;
public MixpanelDescription(String str) {
this.mToken = str;
}
public String getToken() {
return this.mToken;
}
}
public void logAboutMessageToMixpanel(String str) {
MPLog.v(LOGTAG, str + " (Thread " + Thread.currentThread().getId() + ")");
}
public void logAboutMessageToMixpanel(String str, Throwable th) {
MPLog.v(LOGTAG, str + " (Thread " + Thread.currentThread().getId() + ")", th);
}
class Worker {
private SystemInformation mSystemInformation;
private final Object mHandlerLock = new Object();
private long mFlushCount = 0;
private long mAveFlushFrequency = 0;
private long mLastFlushTime = -1;
private Handler mHandler = restartWorkerThread();
public Worker() {
}
public boolean isDead() {
boolean z;
synchronized (this.mHandlerLock) {
z = this.mHandler == null;
}
return z;
}
public void runMessage(Message message) {
synchronized (this.mHandlerLock) {
Handler handler = this.mHandler;
if (handler == null) {
AnalyticsMessages.this.logAboutMessageToMixpanel("Dead mixpanel worker dropping a message: " + message.what);
} else {
handler.sendMessage(message);
}
}
}
protected Handler restartWorkerThread() {
HandlerThread handlerThread = new HandlerThread("com.mixpanel.android.AnalyticsWorker", 10);
handlerThread.start();
return new AnalyticsMessageHandler(handlerThread.getLooper());
}
class AnalyticsMessageHandler extends Handler {
private MPDbAdapter mDbAdapter;
private int mFailedRetries;
private final long mFlushInterval;
private long mTrackEngageRetryAfter;
public AnalyticsMessageHandler(Looper looper) {
super(looper);
this.mDbAdapter = null;
Worker.this.mSystemInformation = SystemInformation.getInstance(AnalyticsMessages.this.mContext);
this.mFlushInterval = AnalyticsMessages.this.mConfig.getFlushInterval();
}
@Override
public void handleMessage(Message message) {
String token;
int i;
if (this.mDbAdapter == null) {
MPDbAdapter makeDbAdapter = AnalyticsMessages.this.makeDbAdapter(AnalyticsMessages.this.mContext);
this.mDbAdapter = makeDbAdapter;
makeDbAdapter.cleanupEvents(System.currentTimeMillis() - AnalyticsMessages.this.mConfig.getDataExpiration(), MPDbAdapter.Table.EVENTS);
this.mDbAdapter.cleanupEvents(System.currentTimeMillis() - AnalyticsMessages.this.mConfig.getDataExpiration(), MPDbAdapter.Table.PEOPLE);
}
try {
if (message.what == 0) {
PeopleDescription peopleDescription = (PeopleDescription) message.obj;
MPDbAdapter.Table table = peopleDescription.isAnonymous() ? MPDbAdapter.Table.ANONYMOUS_PEOPLE : MPDbAdapter.Table.PEOPLE;
AnalyticsMessages.this.logAboutMessageToMixpanel("Queuing people record for sending later");
AnalyticsMessages.this.logAboutMessageToMixpanel(" " + peopleDescription.toString());
token = peopleDescription.getToken();
i = this.mDbAdapter.addJSON(peopleDescription.getMessage(), token, table);
if (peopleDescription.isAnonymous()) {
i = 0;
}
} else if (message.what == 3) {
GroupDescription groupDescription = (GroupDescription) message.obj;
AnalyticsMessages.this.logAboutMessageToMixpanel("Queuing group record for sending later");
AnalyticsMessages.this.logAboutMessageToMixpanel(" " + groupDescription.toString());
token = groupDescription.getToken();
i = this.mDbAdapter.addJSON(groupDescription.getMessage(), token, MPDbAdapter.Table.GROUPS);
} else if (message.what == 1) {
EventDescription eventDescription = (EventDescription) message.obj;
try {
JSONObject prepareEventObject = prepareEventObject(eventDescription);
AnalyticsMessages.this.logAboutMessageToMixpanel("Queuing event for sending later");
AnalyticsMessages.this.logAboutMessageToMixpanel(" " + prepareEventObject.toString());
token = eventDescription.getToken();
try {
i = this.mDbAdapter.addJSON(prepareEventObject, token, MPDbAdapter.Table.EVENTS);
} catch (JSONException e) {
e = e;
MPLog.e(AnalyticsMessages.LOGTAG, "Exception tracking event " + eventDescription.getEventName(), e);
i = -3;
if (i < AnalyticsMessages.this.mConfig.getBulkUploadLimit()) {
}
AnalyticsMessages.this.logAboutMessageToMixpanel("Flushing queue due to bulk upload limit (" + i + ") for project " + token);
Worker.this.updateFlushFrequency();
sendAllData(this.mDbAdapter, token);
}
} catch (JSONException e2) {
e = e2;
token = null;
}
} else if (message.what == 4) {
PushAnonymousPeopleDescription pushAnonymousPeopleDescription = (PushAnonymousPeopleDescription) message.obj;
String distinctId = pushAnonymousPeopleDescription.getDistinctId();
token = pushAnonymousPeopleDescription.getToken();
i = this.mDbAdapter.pushAnonymousUpdatesToPeopleDb(token, distinctId);
} else {
if (message.what == 7) {
token = ((MixpanelDescription) message.obj).getToken();
this.mDbAdapter.cleanupAllEvents(MPDbAdapter.Table.ANONYMOUS_PEOPLE, token);
} else {
if (message.what == 8) {
UpdateEventsPropertiesDescription updateEventsPropertiesDescription = (UpdateEventsPropertiesDescription) message.obj;
MPLog.d(AnalyticsMessages.LOGTAG, this.mDbAdapter.rewriteEventDataWithProperties(updateEventsPropertiesDescription.getProperties(), updateEventsPropertiesDescription.getToken()) + " stored events were updated with new properties.");
} else if (message.what == 2) {
AnalyticsMessages.this.logAboutMessageToMixpanel("Flushing queue due to scheduled or forced flush");
Worker.this.updateFlushFrequency();
token = (String) message.obj;
sendAllData(this.mDbAdapter, token);
} else if (message.what == 6) {
token = ((MixpanelDescription) message.obj).getToken();
this.mDbAdapter.cleanupAllEvents(MPDbAdapter.Table.EVENTS, token);
this.mDbAdapter.cleanupAllEvents(MPDbAdapter.Table.PEOPLE, token);
this.mDbAdapter.cleanupAllEvents(MPDbAdapter.Table.GROUPS, token);
this.mDbAdapter.cleanupAllEvents(MPDbAdapter.Table.ANONYMOUS_PEOPLE, token);
} else if (message.what == 5) {
MPLog.w(AnalyticsMessages.LOGTAG, "Worker received a hard kill. Dumping all events and force-killing. Thread id " + Thread.currentThread().getId());
synchronized (Worker.this.mHandlerLock) {
this.mDbAdapter.deleteDB();
Worker.this.mHandler = null;
Looper.myLooper().quit();
}
} else if (message.what == 9) {
LegacyVersionUtils.removeLegacyResidualImageFiles((File) message.obj);
} else {
MPLog.e(AnalyticsMessages.LOGTAG, "Unexpected message received by Mixpanel worker: " + message);
}
i = -3;
token = null;
}
i = -3;
}
if ((i < AnalyticsMessages.this.mConfig.getBulkUploadLimit() || i == -2) && this.mFailedRetries <= 0 && token != null) {
AnalyticsMessages.this.logAboutMessageToMixpanel("Flushing queue due to bulk upload limit (" + i + ") for project " + token);
Worker.this.updateFlushFrequency();
sendAllData(this.mDbAdapter, token);
} else {
if (i <= 0 || hasMessages(2, token)) {
return;
}
AnalyticsMessages.this.logAboutMessageToMixpanel("Queue depth " + i + " - Adding flush in " + this.mFlushInterval);
if (this.mFlushInterval >= 0) {
Message obtain = Message.obtain();
obtain.what = 2;
obtain.obj = token;
obtain.arg1 = 1;
sendMessageDelayed(obtain, this.mFlushInterval);
}
}
} catch (RuntimeException e3) {
MPLog.e(AnalyticsMessages.LOGTAG, "Worker threw an unhandled exception", e3);
synchronized (Worker.this.mHandlerLock) {
Worker.this.mHandler = null;
try {
Looper.myLooper().quit();
MPLog.e(AnalyticsMessages.LOGTAG, "Mixpanel will not process any more analytics messages", e3);
} catch (Exception e4) {
MPLog.e(AnalyticsMessages.LOGTAG, "Could not halt looper", e4);
}
}
}
}
protected long getTrackEngageRetryAfter() {
return this.mTrackEngageRetryAfter;
}
private void sendAllData(MPDbAdapter mPDbAdapter, String str) {
if (!AnalyticsMessages.this.getPoster().isOnline(AnalyticsMessages.this.mContext, AnalyticsMessages.this.mConfig.getOfflineMode())) {
AnalyticsMessages.this.logAboutMessageToMixpanel("Not flushing data to Mixpanel because the device is not connected to the internet.");
return;
}
sendData(mPDbAdapter, str, MPDbAdapter.Table.EVENTS, AnalyticsMessages.this.mConfig.getEventsEndpoint());
sendData(mPDbAdapter, str, MPDbAdapter.Table.PEOPLE, AnalyticsMessages.this.mConfig.getPeopleEndpoint());
sendData(mPDbAdapter, str, MPDbAdapter.Table.GROUPS, AnalyticsMessages.this.mConfig.getGroupsEndpoint());
}
private void sendData(com.mixpanel.android.mpmetrics.MPDbAdapter r17, java.lang.String r18, com.mixpanel.android.mpmetrics.MPDbAdapter.Table r19, java.lang.String r20) {
throw new UnsupportedOperationException("Method not decompiled: com.mixpanel.android.mpmetrics.AnalyticsMessages.Worker.AnalyticsMessageHandler.sendData(com.mixpanel.android.mpmetrics.MPDbAdapter, java.lang.String, com.mixpanel.android.mpmetrics.MPDbAdapter$Table, java.lang.String):void");
}
private JSONObject getDefaultEventProperties() throws JSONException {
JSONObject jSONObject = new JSONObject();
jSONObject.put("mp_lib", "android");
jSONObject.put("$lib_version", "7.3.2");
jSONObject.put("$os", "Android");
jSONObject.put("$os_version", Build.VERSION.RELEASE == null ? "UNKNOWN" : Build.VERSION.RELEASE);
jSONObject.put("$manufacturer", Build.MANUFACTURER == null ? "UNKNOWN" : Build.MANUFACTURER);
jSONObject.put("$brand", Build.BRAND == null ? "UNKNOWN" : Build.BRAND);
jSONObject.put("$model", Build.MODEL != null ? Build.MODEL : "UNKNOWN");
DisplayMetrics displayMetrics = Worker.this.mSystemInformation.getDisplayMetrics();
jSONObject.put("$screen_dpi", displayMetrics.densityDpi);
jSONObject.put("$screen_height", displayMetrics.heightPixels);
jSONObject.put("$screen_width", displayMetrics.widthPixels);
String appVersionName = Worker.this.mSystemInformation.getAppVersionName();
if (appVersionName != null) {
jSONObject.put("$app_version", appVersionName);
jSONObject.put("$app_version_string", appVersionName);
}
Integer appVersionCode = Worker.this.mSystemInformation.getAppVersionCode();
if (appVersionCode != null) {
String valueOf = String.valueOf(appVersionCode);
jSONObject.put("$app_release", valueOf);
jSONObject.put("$app_build_number", valueOf);
}
Boolean valueOf2 = Boolean.valueOf(Worker.this.mSystemInformation.hasNFC());
if (valueOf2 != null) {
jSONObject.put("$has_nfc", valueOf2.booleanValue());
}
Boolean valueOf3 = Boolean.valueOf(Worker.this.mSystemInformation.hasTelephony());
if (valueOf3 != null) {
jSONObject.put("$has_telephone", valueOf3.booleanValue());
}
String currentNetworkOperator = Worker.this.mSystemInformation.getCurrentNetworkOperator();
if (currentNetworkOperator != null && !currentNetworkOperator.trim().isEmpty()) {
jSONObject.put("$carrier", currentNetworkOperator);
}
Boolean isWifiConnected = Worker.this.mSystemInformation.isWifiConnected();
if (isWifiConnected != null) {
jSONObject.put("$wifi", isWifiConnected.booleanValue());
}
Boolean isBluetoothEnabled = Worker.this.mSystemInformation.isBluetoothEnabled();
if (isBluetoothEnabled != null) {
jSONObject.put("$bluetooth_enabled", isBluetoothEnabled);
}
String bluetoothVersion = Worker.this.mSystemInformation.getBluetoothVersion();
if (bluetoothVersion != null) {
jSONObject.put("$bluetooth_version", bluetoothVersion);
}
return jSONObject;
}
private JSONObject prepareEventObject(EventDescription eventDescription) throws JSONException {
JSONObject jSONObject = new JSONObject();
JSONObject properties = eventDescription.getProperties();
JSONObject defaultEventProperties = getDefaultEventProperties();
defaultEventProperties.put(MPDbAdapter.KEY_TOKEN, eventDescription.getToken());
if (properties != null) {
Iterator<String> keys = properties.keys();
while (keys.hasNext()) {
String next = keys.next();
defaultEventProperties.put(next, properties.get(next));
}
}
jSONObject.put("event", eventDescription.getEventName());
jSONObject.put("properties", defaultEventProperties);
jSONObject.put("$mp_metadata", eventDescription.getSessionMetadata());
return jSONObject;
}
}
public void updateFlushFrequency() {
long currentTimeMillis = System.currentTimeMillis();
long j = this.mFlushCount;
long j2 = 1 + j;
long j3 = this.mLastFlushTime;
if (j3 > 0) {
long j4 = ((currentTimeMillis - j3) + (this.mAveFlushFrequency * j)) / j2;
this.mAveFlushFrequency = j4;
AnalyticsMessages.this.logAboutMessageToMixpanel("Average send frequency approximately " + (j4 / 1000) + " seconds.");
}
this.mLastFlushTime = currentTimeMillis;
this.mFlushCount = j2;
}
}
public long getTrackEngageRetryAfter() {
return ((Worker.AnalyticsMessageHandler) this.mWorker.mHandler).getTrackEngageRetryAfter();
}
}