summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java162
1 files changed, 162 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java
new file mode 100644
index 0000000000..8cd388357b
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java
@@ -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;
+ }
+}