-
Notifications
You must be signed in to change notification settings - Fork 204
/
AppData.java
233 lines (199 loc) · 7.22 KB
/
AppData.java
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.bugsnag.android;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* Collects various data on the application state
*/
class AppData {
private static final long startTimeMs = SystemClock.elapsedRealtime();
static final String RELEASE_STAGE_DEVELOPMENT = "development";
static final String RELEASE_STAGE_PRODUCTION = "production";
private final Client client;
private final Context appContext;
private final String packageName;
private String binaryArch = null;
@Nullable
final String appName;
@Nullable
private PackageInfo packageInfo;
@Nullable
private ApplicationInfo applicationInfo;
@Nullable
private PackageManager packageManager;
AppData(Client client) {
this.client = client;
this.appContext = client.appContext;
// cache values which are widely used, expensive to lookup, or unlikely to change
packageName = appContext.getPackageName();
try {
packageManager = appContext.getPackageManager();
packageInfo = packageManager.getPackageInfo(packageName, 0);
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException exception) {
Logger.warn("Could not retrieve package/application information for " + packageName);
}
appName = getAppName();
}
Map<String, Object> getAppDataSummary() {
Map<String, Object> map = new HashMap<>();
Configuration config = client.config;
map.put("type", calculateNotifierType(config));
map.put("releaseStage", guessReleaseStage());
map.put("version", calculateVersionName());
map.put("versionCode", calculateVersionCode());
map.put("codeBundleId", config.getCodeBundleId());
return map;
}
Map<String, Object> getAppData() {
Map<String, Object> map = getAppDataSummary();
map.put("id", packageName);
map.put("buildUUID", client.config.getBuildUUID());
map.put("duration", getDurationMs());
map.put("durationInForeground", calculateDurationInForeground());
map.put("inForeground", client.sessionTracker.isInForeground());
map.put("packageName", packageName);
map.put("binaryArch", binaryArch);
return map;
}
Map<String, Object> getAppDataMetaData() {
Map<String, Object> map = new HashMap<>();
map.put("name", appName);
map.put("packageName", packageName);
map.put("versionName", calculateVersionName());
map.put("activeScreen", getActiveScreenClass());
map.put("memoryUsage", getMemoryUsage());
map.put("lowMemory", isLowMemory());
return map;
}
void setBinaryArch(String binaryArch) {
this.binaryArch = binaryArch;
}
/**
* Get the time in milliseconds since Bugsnag was initialized, which is a
* good approximation for how long the app has been running.
*/
static long getDurationMs() {
return SystemClock.elapsedRealtime() - startTimeMs;
}
/**
* Calculates the duration the app has been in the foreground
*
* @return the duration in ms
*/
private long calculateDurationInForeground() {
long nowMs = System.currentTimeMillis();
return client.sessionTracker.getDurationInForegroundMs(nowMs);
}
String getActiveScreenClass() {
return client.sessionTracker.getContextActivity();
}
@NonNull
private String calculateNotifierType(Configuration config) {
String notifierType = config.getNotifierType();
if (notifierType != null) {
return notifierType;
} else {
return "android";
}
}
/**
* The version code of the running Android app, from android:versionCode
* in AndroidManifest.xml
*/
@Nullable
@SuppressWarnings("deprecation")
private Integer calculateVersionCode() {
if (packageInfo != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// Android P added an android:versionCodeMajor field for anyone who ran out of
// space in android:versionCode. As it's stored in the upper 32 bits
// we want to ignore those, and retrieve the versionCode value only.
return (int) (packageInfo.getLongVersionCode() & 0x0000FFFFL);
} else {
return packageInfo.versionCode;
}
} else {
return null;
}
}
/**
* The version code of the running Android app, from android:versionName
* in AndroidManifest.xml
*/
@Nullable
private String calculateVersionName() {
String configAppVersion = client.config.getAppVersion();
if (configAppVersion != null) {
return configAppVersion;
} else if (packageInfo != null) {
return packageInfo.versionName;
} else {
return null;
}
}
/**
* Guess the release stage of the running Android app by checking the
* android:debuggable flag from AndroidManifest.xml. If the release stage was set in
* {@link Configuration}, this value will be returned instead.
*/
@NonNull
String guessReleaseStage() {
String configStage = client.config.getReleaseStage();
if (configStage != null) {
return configStage;
}
if (applicationInfo != null) {
if ((applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
return RELEASE_STAGE_DEVELOPMENT;
}
}
return RELEASE_STAGE_PRODUCTION;
}
/**
* The name of the running Android app, from android:label in
* AndroidManifest.xml
*/
@Nullable
private String getAppName() {
if (packageManager != null && applicationInfo != null) {
return packageManager.getApplicationLabel(applicationInfo).toString();
} else {
return null;
}
}
/**
* Get the actual memory used by the VM (which may not be the total used
* by the app in the case of NDK usage).
*/
private long getMemoryUsage() {
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
}
/**
* Check if the device is currently running low on memory.
*/
@Nullable
private Boolean isLowMemory() {
try {
ActivityManager activityManager =
(ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager != null) {
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memInfo);
return memInfo.lowMemory;
}
} catch (Exception exception) {
Logger.warn("Could not check lowMemory status");
}
return null;
}
}