Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class BMRLogger {
public static final String EVENT_TAG_APP_EVENT = "app_event";
public static final String PUSH_EVENT = "push_event";
public static final String PAGE_OPEN_EVENT = "page_open";
public static final String CLICK_EVENT = "click";
public static final String FORM_SUBMIT_EVENT = "form_submit";
private BMRLogger() {
}
public static void dispatchFirebaseUserToken(Context context, String token) {
SMR.dispatchFirebaseUserToken(context, token);
}
public static void logFromPref(Activity context) {
if (!AuthStore.isLoggedIn(context)) return;
String eventStr = Pref.getPreferenceString(context, Pref.KEY_EVENT);
if (eventStr == null || eventStr.isEmpty()) return;
Event event = new Gson().fromJson(eventStr, Event.class);
Pref.savePreference(context, Pref.KEY_EVENT, "");
SMR.logOnline(context, event);
}
public static void log(Activity context, String tag, Map<String, String> data) {
if (!AuthStore.isLoggedIn(context) || !NetworkUtils.isConnected(context)) return;
logFromPref(context); // if available on Pref
Event event = EventRegistry.getInstance().getEventByTag(tag);
event.setEventData(data);
SMR.logOnline(context, event);
}
public static void setUser(Activity context) {
if (!AuthStore.isLoggedIn(context)) return;
String username = AuthStore.getAuth(context).getUsername();
SMR.setUser(context, AuthStore.getAuth(context).getName(), username, username + "@" + context.getPackageName() + ".boostmyrevenue.net");
}
public static void logPushMessage(Context context, PushMessage pushMessage) {
Map<String, String> logData = BMRLogger.buildDataMap(context);
logData.put("push_title", pushMessage.getTitle());
logData.put("push_message", pushMessage.getMessage());
logData.put("push_type", pushMessage.getType());
logData.put("push_code", pushMessage.getCode());
Event event = EventRegistry.getInstance().getEventByTag(BMRLogger.PUSH_EVENT);
event.setEventData(logData);
Pref.savePreference(context, Pref.KEY_EVENT, Commons.buildGson().toJson(event));
}
public static Map<String, String> buildDataMap(Context context) {
Map<String, String> data = new HashMap<>();
if (!AuthStore.isLoggedIn(context)) return data;
data.put(Model.USER_ID.getValue(), String.valueOf(AuthStore.getAuth(context).getId()));
data.put(Model.NAME.getValue(), AuthStore.getAuth(context).getName());
data.put(Model.USERNAME.getValue(), AuthStore.getAuth(context).getUsername());
data.put(Model.PHONE.getValue(), AuthStore.getAuth(context).getUsername());
data.put(Model.USER_PERMISSIONS.getValue(), AuthStore.getAuth(context).getGroupsString());
if (AuthStore.getAuth(context).isAdmin())
data.put(Model.USER_TYPE.getValue(), "Admin");
else if (AuthStore.getAuth(context).isStaff())
data.put(Model.USER_TYPE.getValue(), "Staff");
else
data.put(Model.USER_TYPE.getValue(), "user");
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
} else {
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
String lat = String.valueOf(location.getLatitude());
String lng = String.valueOf(location.getLongitude());
if (!lat.isEmpty() && !lng.isEmpty())
data.put(Model.LOCATION_LATLNG.getValue(), lat + "," + lng);
}
}
data.put(Model.APP_NAME.getValue(), context.getString(R.string.app_name));
data.put(Model.APP_PACKAGE.getValue(), context.getPackageName());
data.put(Model.APP_VERSION.getValue(), BuildConfig.VERSION_NAME);
data.put(Model.APP_CATEGORY.getValue(), "strategy game");
data.put(Model.MANUFACTURER.getValue(), Build.MANUFACTURER);
data.put(Model.DISPLAY.getValue(), Build.DISPLAY);
data.put(Model.BOARD.getValue(), Build.BOARD);
data.put(Model.BRAND.getValue(), Build.BRAND);
data.put(Model.VERSION_CODE.getValue(), Build.VERSION.CODENAME);
data.put(Model.SDK_VERSION.getValue(), String.valueOf(Build.VERSION.SDK_INT));
return data;
}
public enum Model {
USER_ID("user_id"),
NAME("name"),
USERNAME("username"),
PHONE("phone"),
USER_TYPE("user_type"),
USER_PERMISSIONS("user_permissions"),
LOCATION_LATLNG("latlng"),
APP_NAME("app_name"),
APP_PACKAGE("app_package"),
APP_VERSION("app_version"),
APP_CATEGORY("app_version"),
MANUFACTURER("manufacturer"),
DISPLAY("display"),
BOARD("board"),
BRAND("brand"),
VERSION_CODE("version_code"),
SDK_VERSION("sdk_version"),
REFERER("referer"),
CATEGORY("category"),
PRODUCT_NAME("product_name");
private String value;
Model(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment