Polish codes and add test cases

This commit is contained in:
kevin
2019-04-09 14:53:40 +08:00
committed by Łukasz Paczos
parent c6793dd1a2
commit 2b576d24f0
10 changed files with 573 additions and 534 deletions

View File

@@ -1,86 +1,85 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.content.Context;
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.TelemetryUtils;
class MapClickEvent extends Event implements Parcelable {
private static final String MAP_CLICK = "map.click";
@SerializedName("event")
private final String event;
@SerializedName("created")
private String created;
@SerializedName("gesture")
/**
* Event that represents users' gestures on map, for the details of gestures,
* please refer to {@link com.mapbox.mapboxsdk.constants.TelemetryConstants}
*/
@SuppressLint("ParcelCreator")
class MapClickEvent extends Event {
private final String event = "map.click";
private final String created;
private final String gesture;
@SerializedName("lat")
private double latitude;
@SerializedName("lng")
private double longitude;
@SerializedName("zoom")
private double zoom;
@SerializedName("orientation")
private String orientation = null;
@SerializedName("batteryLevel")
private Integer batteryLevel;
@SerializedName("pluggedIn")
private Boolean pluggedIn;
@SerializedName("carrier")
private String carrier = null;
@SerializedName("cellularNetworkType")
private String cellularNetworkType;
@SerializedName("wifi")
private Boolean wifi = null;
private final String cellularNetworkType;
private final String carrier;
private final String orientation;
private final double latitude;
private final double longitude;
private final double zoom;
private final int batteryLevel;
private final boolean pluggedIn;
private final boolean wifi;
MapClickEvent(MapState mapState) {
this.event = MAP_CLICK;
MapClickEvent(PhoneState phoneState, MapState mapState) {
this.gesture = mapState.getGesture();
this.latitude = mapState.getLatitude();
this.longitude = mapState.getLongitude();
this.zoom = mapState.getZoom();
this.created = TelemetryUtils.obtainCurrentDate();
this.batteryLevel = 0;
this.pluggedIn = false;
this.cellularNetworkType = "";
this.created = phoneState.getCreated();
this.batteryLevel = phoneState.getBatteryLevel();
this.pluggedIn = phoneState.isPluggedIn();
this.cellularNetworkType = phoneState.getCellularNetworkType();
this.orientation = phoneState.getOrientation();
this.carrier = phoneState.getCarrier();
this.wifi = phoneState.isWifi();
}
MapClickEvent setDeviceInfo(Context context) {
this.batteryLevel = TelemetryUtils.obtainBatteryLevel(context);
this.pluggedIn = TelemetryUtils.isPluggedIn(context);
this.cellularNetworkType = TelemetryUtils.obtainCellularNetworkType(context);
return this;
String getCreated() {
return created;
}
void setOrientation(String orientation) {
this.orientation = orientation;
String getGesture() {
return gesture;
}
void setCarrier(String carrier) {
this.carrier = carrier;
String getCellularNetworkType() {
return cellularNetworkType;
}
void setWifi(boolean wifi) {
this.wifi = wifi;
String getCarrier() {
return carrier;
}
private MapClickEvent(Parcel in) {
event = in.readString();
created = in.readString();
gesture = in.readString();
latitude = in.readDouble();
longitude = in.readDouble();
zoom = in.readDouble();
orientation = in.readString();
batteryLevel = in.readInt();
pluggedIn = in.readByte() != 0x00;
carrier = in.readString();
cellularNetworkType = in.readString();
byte wifiVal = in.readByte();
wifi = wifiVal == 0x02 ? null : wifiVal != 0x00;
String getOrientation() {
return orientation;
}
double getLatitude() {
return latitude;
}
double getLongitude() {
return longitude;
}
double getZoom() {
return zoom;
}
int getBatteryLevel() {
return batteryLevel;
}
boolean isPluggedIn() {
return pluggedIn;
}
boolean isWifi() {
return wifi;
}
@Override
@@ -90,33 +89,5 @@ class MapClickEvent extends Event implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(event);
dest.writeString(created);
dest.writeString(gesture);
dest.writeDouble(latitude);
dest.writeDouble(longitude);
dest.writeDouble(zoom);
dest.writeString(orientation);
dest.writeInt(batteryLevel);
dest.writeByte((byte) (pluggedIn ? 0x01 : 0x00));
dest.writeString(carrier);
dest.writeString(cellularNetworkType);
if (wifi == null) {
dest.writeByte((byte) (0x02));
} else {
dest.writeByte((byte) (wifi ? 0x01 : 0x00));
}
}
public static final Creator<MapClickEvent> CREATOR = new Creator<MapClickEvent>() {
@Override
public MapClickEvent createFromParcel(Parcel in) {
return new MapClickEvent(in);
}
@Override
public MapClickEvent[] newArray(int size) {
return new MapClickEvent[size];
}
};
}

View File

@@ -1,82 +1,78 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.content.Context;
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.TelemetryUtils;
class MapDragendEvent extends Event implements Parcelable {
private static final String MAP_DRAGEND = "map.dragend";
/**
* When user drag map should send this event.
*/
@SuppressLint("ParcelCreator")
class MapDragendEvent extends Event {
private final String event = "map.dragend";
private final String created;
private final String orientation;
private final String carrier;
private final String cellularNetworkType;
private final int batteryLevel;
private final double lat;
private final double lng;
private final double zoom;
private final boolean pluggedIn;
private final boolean wifi;
@SerializedName("event")
private final String event;
@SerializedName("created")
private String created;
@SerializedName("lat")
private double latitude;
@SerializedName("lng")
private double longitude;
@SerializedName("zoom")
private double zoom;
@SerializedName("orientation")
private String orientation = null;
@SerializedName("batteryLevel")
private int batteryLevel;
@SerializedName("pluggedIn")
private Boolean pluggedIn;
@SerializedName("carrier")
private String carrier = null;
@SerializedName("cellularNetworkType")
private String cellularNetworkType;
@SerializedName("wifi")
private Boolean wifi = null;
MapDragendEvent(MapState mapState) {
this.event = MAP_DRAGEND;
this.latitude = mapState.getLatitude();
this.longitude = mapState.getLongitude();
MapDragendEvent(PhoneState phoneState, MapState mapState) {
this.lat = mapState.getLatitude();
this.lng = mapState.getLongitude();
this.zoom = mapState.getZoom();
this.created = TelemetryUtils.obtainCurrentDate();
this.batteryLevel = 0;
this.pluggedIn = false;
this.cellularNetworkType = "";
this.created = phoneState.getCreated();
this.batteryLevel = phoneState.getBatteryLevel();
this.pluggedIn = phoneState.isPluggedIn();
this.cellularNetworkType = phoneState.getCellularNetworkType();
this.wifi = phoneState.isWifi();
this.orientation = phoneState.getOrientation();
this.carrier = phoneState.getCarrier();
}
MapDragendEvent setDeviceInfo(Context context) {
this.batteryLevel = TelemetryUtils.obtainBatteryLevel(context);
this.pluggedIn = TelemetryUtils.isPluggedIn(context);
this.cellularNetworkType = TelemetryUtils.obtainCellularNetworkType(context);
return this;
String getCreated() {
return created;
}
void setOrientation(String orientation) {
this.orientation = orientation;
String getOrientation() {
return orientation;
}
void setCarrier(String carrier) {
this.carrier = carrier;
String getCarrier() {
return carrier;
}
void setWifi(boolean wifi) {
this.wifi = wifi;
String getCellularNetworkType() {
return cellularNetworkType;
}
private MapDragendEvent(Parcel in) {
event = in.readString();
created = in.readString();
latitude = in.readDouble();
longitude = in.readDouble();
zoom = in.readDouble();
orientation = in.readString();
batteryLevel = in.readInt();
pluggedIn = in.readByte() != 0x00;
carrier = in.readString();
cellularNetworkType = in.readString();
byte wifiVal = in.readByte();
wifi = wifiVal == 0x02 ? null : wifiVal != 0x00;
int getBatteryLevel() {
return batteryLevel;
}
double getLat() {
return lat;
}
double getLng() {
return lng;
}
double getZoom() {
return zoom;
}
boolean isPluggedIn() {
return pluggedIn;
}
boolean isWifi() {
return wifi;
}
@Override
@@ -86,32 +82,5 @@ class MapDragendEvent extends Event implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(event);
dest.writeString(created);
dest.writeDouble(latitude);
dest.writeDouble(longitude);
dest.writeDouble(zoom);
dest.writeString(orientation);
dest.writeInt(batteryLevel);
dest.writeByte((byte) (pluggedIn ? 0x01 : 0x00));
dest.writeString(carrier);
dest.writeString(cellularNetworkType);
if (wifi == null) {
dest.writeByte((byte) (0x02));
} else {
dest.writeByte((byte) (wifi ? 0x01 : 0x00));
}
}
public static final Creator<MapDragendEvent> CREATOR = new Creator<MapDragendEvent>() {
@Override
public MapDragendEvent createFromParcel(Parcel in) {
return new MapDragendEvent(in);
}
@Override
public MapDragendEvent[] newArray(int size) {
return new MapDragendEvent[size];
}
};
}

View File

@@ -1,141 +1,50 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.content.Context;
import android.content.res.Configuration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.annotation.NonNull;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.TelemetryUtils;
import java.util.HashMap;
import java.util.Map;
class MapEventFactory {
public class MapEventFactory {
private static final String LANDSCAPE = "Landscape";
private static final String PORTRAIT = "Portrait";
private static final String NO_CARRIER = "EMPTY_CARRIER";
private static final int NO_NETWORK = -1;
private static final Map<Integer, String> ORIENTATIONS = new HashMap<Integer, String>() {
{
put(Configuration.ORIENTATION_LANDSCAPE, LANDSCAPE);
put(Configuration.ORIENTATION_PORTRAIT, PORTRAIT);
}
};
private final Context appContext;
MapEventFactory(Context appContext) {
this.appContext = appContext;
}
Event buildMapLoadEvent() {
static MapLoadEvent buildMapLoadEvent(@NonNull PhoneState phoneState) {
String userId = TelemetryUtils.retrieveVendorId();
MapLoadEvent mapLoadEvent = new MapLoadEvent(userId).setDeviceInfo(appContext);
mapLoadEvent.setOrientation(obtainOrientation(appContext));
mapLoadEvent.setAccessibilityFontScale(obtainAccessibilityFontScaleSize(appContext));
mapLoadEvent.setCarrier(obtainCellularCarrier(appContext));
mapLoadEvent.setResolution(obtainDisplayDensity(appContext));
mapLoadEvent.setWifi(isConnectedToWifi(appContext));
return mapLoadEvent;
return new MapLoadEvent(userId, phoneState);
}
Event buildOfflineDownloadStartEvent(String shapeForOfflineRegion,
Double minZoom, Double maxZoom,
String styleURL) {
static OfflineDownloadStartEvent buildOfflineDownloadStartEvent(PhoneState phoneState,
String shapeForOfflineRegion,
Double minZoom, Double maxZoom,
String styleURL) {
OfflineDownloadStartEvent offlineEvent =
new OfflineDownloadStartEvent(shapeForOfflineRegion, minZoom, maxZoom);
new OfflineDownloadStartEvent(phoneState, shapeForOfflineRegion, minZoom, maxZoom);
offlineEvent.setStyleURL(styleURL);
return offlineEvent;
}
public Event buildOfflineDownloadCompleteEvent(String shapeForOfflineRegion,
Double minZoom, Double maxZoom,
String styleURL,
Long sizeOfResourcesCompleted,
Long numberOfTilesCompleted,
String state) {
static OfflineDownloadEndEvent buildOfflineDownloadCompleteEvent(PhoneState phoneState,
String shapeForOfflineRegion,
Double minZoom, Double maxZoom,
String styleURL,
Long sizeOfResourcesCompleted,
Long numberOfTilesCompleted,
String state) {
OfflineDownloadEndEvent offlineEvent =
new OfflineDownloadEndEvent(shapeForOfflineRegion, minZoom, maxZoom);
new OfflineDownloadEndEvent(phoneState, shapeForOfflineRegion, minZoom, maxZoom);
offlineEvent.setStyleURL(styleURL);
offlineEvent.setSizeOfResourcesCompleted(sizeOfResourcesCompleted);
offlineEvent.setNumberOfTilesCompleted(numberOfTilesCompleted);
offlineEvent.setState(state);
return offlineEvent;
}
MapClickEvent buildMapClickEvent(@NonNull MapState mapState) {
MapClickEvent mapClickEvent = new MapClickEvent(mapState).setDeviceInfo(appContext);
mapClickEvent.setOrientation(obtainOrientation(appContext));
mapClickEvent.setCarrier(obtainCellularCarrier(appContext));
mapClickEvent.setWifi(isConnectedToWifi(appContext));
return mapClickEvent;
static MapClickEvent buildMapClickEvent(@NonNull PhoneState phoneState, @NonNull MapState mapState) {
return new MapClickEvent(phoneState, mapState);
}
MapDragendEvent buildMapDragendEvent(@NonNull MapState mapState) {
MapDragendEvent mapDragendEvent = new MapDragendEvent(mapState).setDeviceInfo(appContext);
mapDragendEvent.setOrientation(obtainOrientation(appContext));
mapDragendEvent.setCarrier(obtainCellularCarrier(appContext));
mapDragendEvent.setWifi(isConnectedToWifi(appContext));
return mapDragendEvent;
}
private String obtainOrientation(Context context) {
return ORIENTATIONS.get(context.getResources().getConfiguration().orientation);
}
private float obtainAccessibilityFontScaleSize(Context context) {
return context.getResources().getConfiguration().fontScale;
}
private String obtainCellularCarrier(Context context) {
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager == null) {
return NO_CARRIER;
}
String carrierName = manager.getNetworkOperatorName();
if (TextUtils.isEmpty(carrierName)) {
return NO_CARRIER;
}
return carrierName;
}
private float obtainDisplayDensity(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.density;
}
private boolean isConnectedToWifi(Context context) {
try {
WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//noinspection MissingPermission
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
return isWifiConnected(wifiMgr, wifiInfo);
} catch (Exception exception) {
return false;
}
}
private boolean isWifiConnected(WifiManager wifiMgr, WifiInfo wifiInfo) {
return wifiMgr.isWifiEnabled() && networkConnected(wifiInfo);
}
private boolean networkConnected(WifiInfo wifiInfo) {
return wifiInfo.getNetworkId() != NO_NETWORK;
static MapDragendEvent buildMapDragendEvent(@NonNull PhoneState phoneState, @NonNull MapState mapState) {
return new MapDragendEvent(phoneState, mapState);
}
}

View File

@@ -1,112 +1,101 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.content.Context;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.TelemetryUtils;
import com.mapbox.mapboxsdk.BuildConfig;
class MapLoadEvent extends Event implements Parcelable {
private static final String MAP_LOAD = "map.load";
private static final String OPERATING_SYSTEM = "Android - " + Build.VERSION.RELEASE;
/**
* Event will be sent while map is loaded.
*/
@SuppressLint("ParcelCreator")
class MapLoadEvent extends Event {
private final String event = "map.load";
private final String operatingSystem = "Android - " + Build.VERSION.RELEASE;
private final String sdkIdentifier = BuildConfig.MAPBOX_SDK_IDENTIFIER;
private final String sdkVersion = BuildConfig.MAPBOX_SDK_VERSION;
private final String model = Build.MODEL;
private final String created;
private final String userId;
private final String carrier;
private final String cellularNetworkType;
private final String orientation;
private final float resolution;
private final float accessibilityFontScale;
private final int batteryLevel;
private final boolean pluggedIn;
private final boolean wifi;
@SerializedName("event")
private final String event;
@SerializedName("created")
private String created;
@SerializedName("userId")
private String userId;
@SerializedName("model")
private String model = null;
@SerializedName("operatingSystem")
private String operatingSystem = null;
@SerializedName("resolution")
private Float resolution = null;
@SerializedName("accessibilityFontScale")
private Float accessibilityFontScale = null;
@SerializedName("orientation")
private String orientation = null;
@SerializedName("batteryLevel")
private Integer batteryLevel;
@SerializedName("pluggedIn")
private Boolean pluggedIn;
@SerializedName("carrier")
private String carrier = null;
@SerializedName("cellularNetworkType")
private String cellularNetworkType;
@SerializedName("wifi")
private Boolean wifi = null;
@SerializedName("sdkIdentifier")
private String sdkIdentifier = null;
@SerializedName("sdkVersion")
private String sdkVersion = null;
MapLoadEvent(String userId) {
this.event = MAP_LOAD;
this.model = Build.MODEL;
this.operatingSystem = OPERATING_SYSTEM;
this.created = TelemetryUtils.obtainCurrentDate();
MapLoadEvent(String userId, PhoneState phoneState) {
this.created = phoneState.getCreated();
this.userId = userId;
this.batteryLevel = 0;
this.pluggedIn = false;
this.cellularNetworkType = "";
this.batteryLevel = phoneState.getBatteryLevel();
this.pluggedIn = phoneState.isPluggedIn();
this.cellularNetworkType = phoneState.getCellularNetworkType();
this.carrier = phoneState.getCarrier();
this.resolution = phoneState.getResolution();
this.accessibilityFontScale = phoneState.getAccessibilityFontScale();
this.wifi = phoneState.isWifi();
this.orientation = phoneState.getOrientation();
}
MapLoadEvent setDeviceInfo(Context context) {
this.batteryLevel = TelemetryUtils.obtainBatteryLevel(context);
this.pluggedIn = TelemetryUtils.isPluggedIn(context);
this.cellularNetworkType = TelemetryUtils.obtainCellularNetworkType(context);
return this;
String getOperatingSystem() {
return operatingSystem;
}
void setResolution(float resolution) {
this.resolution = resolution;
String getSdkIdentifier() {
return sdkIdentifier;
}
void setAccessibilityFontScale(float accessibilityFontScale) {
this.accessibilityFontScale = accessibilityFontScale;
String getSdkVersion() {
return sdkVersion;
}
void setOrientation(String orientation) {
this.orientation = orientation;
String getModel() {
return model;
}
void setCarrier(String carrier) {
this.carrier = carrier;
String getCreated() {
return created;
}
void setWifi(boolean wifi) {
this.wifi = wifi;
String getUserId() {
return userId;
}
void setSdkIdentifier(String sdkIdentifier) {
this.sdkIdentifier = sdkIdentifier;
String getCarrier() {
return carrier;
}
void setSdkVersion(String sdkVersion) {
this.sdkVersion = sdkVersion;
String getCellularNetworkType() {
return cellularNetworkType;
}
private MapLoadEvent(Parcel in) {
event = in.readString();
created = in.readString();
userId = in.readString();
model = in.readString();
operatingSystem = in.readString();
resolution = in.readByte() == 0x00 ? null : in.readFloat();
accessibilityFontScale = in.readByte() == 0x00 ? null : in.readFloat();
orientation = in.readString();
batteryLevel = in.readInt();
pluggedIn = in.readByte() != 0x00;
carrier = in.readString();
cellularNetworkType = in.readString();
byte wifiVal = in.readByte();
wifi = wifiVal == 0x02 ? null : wifiVal != 0x00;
sdkIdentifier = in.readString();
sdkVersion = in.readString();
String getOrientation() {
return orientation;
}
float getResolution() {
return resolution;
}
float getAccessibilityFontScale() {
return accessibilityFontScale;
}
int getBatteryLevel() {
return batteryLevel;
}
boolean isPluggedIn() {
return pluggedIn;
}
boolean isWifi() {
return wifi;
}
@Override
@@ -116,47 +105,5 @@ class MapLoadEvent extends Event implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(event);
dest.writeString(created);
dest.writeString(userId);
dest.writeString(model);
dest.writeString(operatingSystem);
if (resolution == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeFloat(resolution);
}
if (accessibilityFontScale == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeFloat(accessibilityFontScale);
}
dest.writeString(orientation);
dest.writeInt(batteryLevel);
dest.writeByte((byte) (pluggedIn ? 0x01 : 0x00));
dest.writeString(carrier);
dest.writeString(cellularNetworkType);
if (wifi == null) {
dest.writeByte((byte) (0x02));
} else {
dest.writeByte((byte) (wifi ? 0x01 : 0x00));
}
dest.writeString(sdkIdentifier);
dest.writeString(sdkVersion);
}
@SuppressWarnings("unused")
public static final Creator<MapLoadEvent> CREATOR = new Creator<MapLoadEvent>() {
@Override
public MapLoadEvent createFromParcel(Parcel in) {
return new MapLoadEvent(in);
}
@Override
public MapLoadEvent[] newArray(int size) {
return new MapLoadEvent[size];
}
};
}

View File

@@ -6,13 +6,13 @@ public class MapState {
private double zoom;
private String gesture;
public MapState(double latitude, double longitude, double zoom) {
MapState(double latitude, double longitude, double zoom) {
this.latitude = latitude;
this.longitude = longitude;
this.zoom = zoom;
}
public void setGesture(String gesture) {
void setGesture(String gesture) {
this.gesture = gesture;
}

View File

@@ -1,79 +1,78 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.TelemetryUtils;
public class OfflineDownloadEndEvent extends Event implements Parcelable {
private static final String OFFLINE_DOWNLOAD_COMPLETE = "map.offlineDownload.end";
@SerializedName("event")
private final String event;
@SerializedName("created")
/**
* Event will be sent while offline download end.
*/
@SuppressLint("ParcelCreator")
public class OfflineDownloadEndEvent extends Event {
private final String event = "map.offlineDownload.end";
private final String created;
@SerializedName("minZoom")
private final Double minZoom;
@SerializedName("maxZoom")
private final Double maxZoom;
@SerializedName("shapeForOfflineRegion")
private final String shapeForOfflineRegion;
@SerializedName("styleURL")
private String styleURL;
@SerializedName("sizeOfResourcesCompleted")
private Long sizeOfResourcesCompleted;
@SerializedName("numberOfTilesCompleted")
private Long numberOfTilesCompleted;
@SerializedName("state")
private String state;
public void setStyleURL(String styleURL) {
void setStyleURL(String styleURL) {
this.styleURL = styleURL;
}
public void setSizeOfResourcesCompleted(Long sizeOfResourcesCompleted) {
void setSizeOfResourcesCompleted(Long sizeOfResourcesCompleted) {
this.sizeOfResourcesCompleted = sizeOfResourcesCompleted;
}
public void setNumberOfTilesCompleted(Long numberOfTilesCompleted) {
void setNumberOfTilesCompleted(Long numberOfTilesCompleted) {
this.numberOfTilesCompleted = numberOfTilesCompleted;
}
public void setState(String state) {
void setState(String state) {
this.state = state;
}
OfflineDownloadEndEvent(String shapeForOfflineRegion, Double minZoom, Double maxZoom) {
this.event = OFFLINE_DOWNLOAD_COMPLETE;
this.created = TelemetryUtils.obtainCurrentDate();
OfflineDownloadEndEvent(PhoneState phoneState, String shapeForOfflineRegion, Double minZoom, Double maxZoom) {
this.created = phoneState.getCreated();
this.shapeForOfflineRegion = shapeForOfflineRegion;
this.minZoom = minZoom;
this.maxZoom = maxZoom;
}
private OfflineDownloadEndEvent(Parcel in) {
event = in.readString();
created = in.readString();
shapeForOfflineRegion = in.readString();
minZoom = in.readDouble();
maxZoom = in.readDouble();
styleURL = in.readString();
sizeOfResourcesCompleted = in.readLong();
numberOfTilesCompleted = in.readLong();
state = in.readString();
String getCreated() {
return created;
}
Double getMinZoom() {
return minZoom;
}
Double getMaxZoom() {
return maxZoom;
}
String getShapeForOfflineRegion() {
return shapeForOfflineRegion;
}
String getStyleURL() {
return styleURL;
}
Long getSizeOfResourcesCompleted() {
return sizeOfResourcesCompleted;
}
Long getNumberOfTilesCompleted() {
return numberOfTilesCompleted;
}
String getState() {
return state;
}
@Override
@@ -83,26 +82,5 @@ public class OfflineDownloadEndEvent extends Event implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(event);
dest.writeString(created);
dest.writeString(shapeForOfflineRegion);
dest.writeDouble(minZoom);
dest.writeDouble(maxZoom);
dest.writeString(styleURL);
dest.writeLong(sizeOfResourcesCompleted);
dest.writeLong(numberOfTilesCompleted);
dest.writeString(state);
}
public static final Creator<OfflineDownloadEndEvent> CREATOR = new Creator<OfflineDownloadEndEvent>() {
@Override
public OfflineDownloadEndEvent createFromParcel(Parcel in) {
return new OfflineDownloadEndEvent(in);
}
@Override
public OfflineDownloadEndEvent[] newArray(int size) {
return new OfflineDownloadEndEvent[size];
}
};
}

View File

@@ -1,54 +1,51 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import com.mapbox.android.telemetry.Event;
import com.mapbox.android.telemetry.TelemetryUtils;
public class OfflineDownloadStartEvent extends Event implements Parcelable {
private static final String OFFLINE_DOWNLOAD_START = "map.offlineDownload.start";
@SerializedName("event")
private final String event;
@SerializedName("created")
/**
* Event will be sent while offline download start.
*/
@SuppressLint("ParcelCreator")
public class OfflineDownloadStartEvent extends Event {
private final String event = "map.offlineDownload.start";
private final String created;
@SerializedName("minZoom")
private final Double minZoom;
@SerializedName("maxZoom")
private final Double maxZoom;
@SerializedName("shapeForOfflineRegion")
private final String shapeForOfflineRegion;
@SerializedName("styleURL")
private String styleURL;
public void setStyleURL(String styleURL) {
void setStyleURL(String styleURL) {
this.styleURL = styleURL;
}
OfflineDownloadStartEvent(String shapeForOfflineRegion, Double minZoom, Double maxZoom) {
this.event = OFFLINE_DOWNLOAD_START;
this.created = TelemetryUtils.obtainCurrentDate();
OfflineDownloadStartEvent(PhoneState phoneState, String shapeForOfflineRegion, Double minZoom, Double maxZoom) {
this.created = phoneState.getCreated();
this.shapeForOfflineRegion = shapeForOfflineRegion;
this.minZoom = minZoom;
this.maxZoom = maxZoom;
}
private OfflineDownloadStartEvent(Parcel in) {
event = in.readString();
created = in.readString();
shapeForOfflineRegion = in.readString();
minZoom = in.readDouble();
maxZoom = in.readDouble();
styleURL = in.readString();
String getCreated() {
return created;
}
Double getMinZoom() {
return minZoom;
}
Double getMaxZoom() {
return maxZoom;
}
String getShapeForOfflineRegion() {
return shapeForOfflineRegion;
}
String getStyleURL() {
return styleURL;
}
@Override
@@ -58,23 +55,5 @@ public class OfflineDownloadStartEvent extends Event implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(event);
dest.writeString(created);
dest.writeString(shapeForOfflineRegion);
dest.writeDouble(minZoom);
dest.writeDouble(maxZoom);
dest.writeString(styleURL);
}
public static final Creator<OfflineDownloadStartEvent> CREATOR = new Creator<OfflineDownloadStartEvent>() {
@Override
public OfflineDownloadStartEvent createFromParcel(Parcel in) {
return new OfflineDownloadStartEvent(in);
}
@Override
public OfflineDownloadStartEvent[] newArray(int size) {
return new OfflineDownloadStartEvent[size];
}
};
}

View File

@@ -0,0 +1,162 @@
package com.mapbox.mapboxsdk.module.telemetry;
import android.content.Context;
import android.content.res.Configuration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import com.mapbox.android.telemetry.TelemetryUtils;
import java.util.HashMap;
import java.util.Map;
/**
* Class that holds kinds of states of the current phone.
*/
class PhoneState {
static final String LANDSCAPE = "Landscape";
static final String PORTRAIT = "Portrait";
private static final String NO_CARRIER = "EMPTY_CARRIER";
private static final int NO_NETWORK = -1;
private static final Map<Integer, String> ORIENTATIONS = new HashMap<Integer, String>() {
{
put(Configuration.ORIENTATION_LANDSCAPE, LANDSCAPE);
put(Configuration.ORIENTATION_PORTRAIT, PORTRAIT);
}
};
private String created;
private String cellularNetworkType;
private String orientation;
private String carrier;
private int batteryLevel;
private boolean pluggedIn;
private boolean wifi;
private float accessibilityFontScale;
private float resolution;
PhoneState() {
}
PhoneState(Context context) {
this.created = TelemetryUtils.obtainCurrentDate();
this.batteryLevel = TelemetryUtils.obtainBatteryLevel(context);
this.pluggedIn = TelemetryUtils.isPluggedIn(context);
this.cellularNetworkType = TelemetryUtils.obtainCellularNetworkType(context);
this.orientation = ORIENTATIONS.get(context.getResources().getConfiguration().orientation);
this.accessibilityFontScale = context.getResources().getConfiguration().fontScale;
this.carrier = obtainCellularCarrier(context);
this.resolution = obtainDisplayDensity(context);
this.wifi = isConnectedToWifi(context);
}
private String obtainCellularCarrier(Context context) {
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager == null) {
return NO_CARRIER;
}
String carrierName = manager.getNetworkOperatorName();
if (TextUtils.isEmpty(carrierName)) {
return NO_CARRIER;
}
return carrierName;
}
private float obtainDisplayDensity(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.density;
}
private boolean isConnectedToWifi(Context context) {
try {
WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiMgr == null) {
return false;
}
//noinspection MissingPermission
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
return wifiMgr.isWifiEnabled() && wifiInfo.getNetworkId() != NO_NETWORK;
} catch (Exception exception) {
return false;
}
}
String getCreated() {
return created;
}
void setCreated(String created) {
this.created = created;
}
int getBatteryLevel() {
return batteryLevel;
}
void setBatteryLevel(int batteryLevel) {
this.batteryLevel = batteryLevel;
}
boolean isPluggedIn() {
return pluggedIn;
}
void setPluggedIn(boolean pluggedIn) {
this.pluggedIn = pluggedIn;
}
String getCellularNetworkType() {
return cellularNetworkType;
}
void setCellularNetworkType(String cellularNetworkType) {
this.cellularNetworkType = cellularNetworkType;
}
String getOrientation() {
return orientation;
}
void setOrientation(String orientation) {
this.orientation = orientation;
}
String getCarrier() {
return carrier;
}
void setCarrier(String carrier) {
this.carrier = carrier;
}
boolean isWifi() {
return wifi;
}
void setWifi(boolean wifi) {
this.wifi = wifi;
}
float getAccessibilityFontScale() {
return accessibilityFontScale;
}
void setAccessibilityFontScale(float accessibilityFontScale) {
this.accessibilityFontScale = accessibilityFontScale;
}
float getResolution() {
return resolution;
}
void setResolution(float resolution) {
this.resolution = resolution;
}
}

View File

@@ -21,18 +21,16 @@ public class TelemetryImpl implements TelemetryDefinition {
@Nullable
private MapboxTelemetry telemetry;
private MapEventFactory mapEventFactory;
private Context appContext;
public TelemetryImpl() {
Context appContext = Mapbox.getApplicationContext();
appContext = Mapbox.getApplicationContext();
String accessToken = Mapbox.getAccessToken();
telemetry = new MapboxTelemetry(appContext, accessToken, BuildConfig.MAPBOX_EVENTS_USER_AGENT);
TelemetryEnabler.State telemetryState = TelemetryEnabler.retrieveTelemetryStateFromPreferences();
if (TelemetryEnabler.State.ENABLED.equals(telemetryState)) {
telemetry.enable();
}
mapEventFactory = new MapEventFactory(appContext);
}
/**
@@ -43,7 +41,7 @@ public class TelemetryImpl implements TelemetryDefinition {
AppUserTurnstile turnstileEvent = new AppUserTurnstile(BuildConfig.MAPBOX_SDK_IDENTIFIER,
BuildConfig.MAPBOX_SDK_VERSION);
telemetry.push(turnstileEvent);
telemetry.push(mapEventFactory.buildMapLoadEvent());
telemetry.push(MapEventFactory.buildMapLoadEvent(new PhoneState(appContext)));
}
/**
@@ -58,7 +56,7 @@ public class TelemetryImpl implements TelemetryDefinition {
public void onGestureInteraction(String eventType, double latitude, double longitude, double zoom) {
MapState state = new MapState(latitude, longitude, zoom);
state.setGesture(eventType);
telemetry.push(mapEventFactory.buildMapClickEvent(state));
telemetry.push(MapEventFactory.buildMapClickEvent(new PhoneState(appContext), state));
}
/**
@@ -98,7 +96,7 @@ public class TelemetryImpl implements TelemetryDefinition {
@Override
public void onCreateOfflineRegion(@NonNull OfflineRegionDefinition offlineDefinition) {
telemetry.push(mapEventFactory.buildOfflineDownloadStartEvent(
telemetry.push(MapEventFactory.buildOfflineDownloadStartEvent(new PhoneState(appContext),
offlineDefinition instanceof OfflineTilePyramidRegionDefinition ? "tileregion" : "shaperegion",
offlineDefinition.getMinZoom(),
offlineDefinition.getMaxZoom(),