summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PhoneState.java
blob: a7f574bf40fe2d78be8ce9c1ff26904a20ed0e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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.TelemetryUtils;

/**
 * Class that holds kinds of states of the current phone.
 */
class PhoneState {
  private static final String NO_CARRIER = "EMPTY_CARRIER";
  private static final int NO_NETWORK = -1;
  private String created;
  private String cellularNetworkType;
  private Orientation orientation;
  private String carrier;
  private int batteryLevel;
  private boolean pluggedIn;
  private boolean wifi;
  private float accessibilityFontScale;
  private float resolution;

  PhoneState() {

  }

  PhoneState(@NonNull Context context) {
    this.created = TelemetryUtils.obtainCurrentDate();
    this.batteryLevel = TelemetryUtils.obtainBatteryLevel(context);
    this.pluggedIn = TelemetryUtils.isPluggedIn(context);
    this.cellularNetworkType = TelemetryUtils.obtainCellularNetworkType(context);
    this.orientation = Orientation.getOrientation(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(@NonNull 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(@NonNull Context context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.density;
  }

  private boolean isConnectedToWifi(@NonNull 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.getOrientation();
  }

  void setOrientation(Orientation 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;
  }

  enum Orientation {
    ORIENTATION_PORTRAIT("Portrait"),
    ORIENTATION_LANDSCAPE("Landscape");
    private String orientation;

    Orientation(String orientation) {
      this.orientation = orientation;
    }

    public static Orientation getOrientation(int index) {
      if (Configuration.ORIENTATION_PORTRAIT == index) {
        return ORIENTATION_PORTRAIT;
      }
      return ORIENTATION_LANDSCAPE;
    }

    public String getOrientation() {
      return orientation;
    }
  }
}