Updated Bugsnag

This commit is contained in:
M66B 2023-11-09 08:30:10 +01:00
parent 92e119abec
commit ceac8badde
15 changed files with 163 additions and 56 deletions

View File

@ -506,7 +506,7 @@ dependencies {
def dnsjava_version = "2.1.9" def dnsjava_version = "2.1.9"
def openpgp_version = "12.0" def openpgp_version = "12.0"
def badge_version = "1.1.22" def badge_version = "1.1.22"
def bugsnag_version = "5.28.2" def bugsnag_version = "5.31.3"
def biweekly_version = "0.6.7" def biweekly_version = "0.6.7"
def vcard_version = "0.12.1" def vcard_version = "0.12.1"
def relinker_version = "1.4.5" def relinker_version = "1.4.5"

View File

@ -33,6 +33,7 @@ internal class AppDataCollector(
private val processName = findProcessName() private val processName = findProcessName()
private val releaseStage = config.releaseStage private val releaseStage = config.releaseStage
private val versionName = config.appVersion ?: config.packageInfo?.versionName private val versionName = config.appVersion ?: config.packageInfo?.versionName
private val installerPackage = getInstallerPackageName()
fun generateApp(): App = fun generateApp(): App =
App(config, binaryArch, packageName, releaseStage, versionName, codeBundleId) App(config, binaryArch, packageName, releaseStage, versionName, codeBundleId)
@ -74,6 +75,7 @@ internal class AppDataCollector(
map["totalMemory"] = totalMemory map["totalMemory"] = totalMemory
map["freeMemory"] = freeMemory map["freeMemory"] = freeMemory
map["memoryLimit"] = runtime.maxMemory() map["memoryLimit"] = runtime.maxMemory()
map["installerPackage"] = installerPackage
} }
/** /**
@ -130,6 +132,20 @@ internal class AppDataCollector(
} }
} }
/**
* The name of installer / vendor package of the app
*/
fun getInstallerPackageName(): String? {
try {
if (VERSION.SDK_INT >= VERSION_CODES.R)
return packageManager?.getInstallSourceInfo(packageName)?.installingPackageName
@Suppress("DEPRECATION")
return packageManager?.getInstallerPackageName(packageName)
} catch (e: Exception) {
return null
}
}
/** /**
* Finds the name of the current process, or null if this cannot be found. * Finds the name of the current process, or null if this cannot be found.
*/ */

View File

@ -66,7 +66,7 @@ internal class BugsnagEventMapper(
// populate session // populate session
val sessionMap = map["session"] as? Map<String, Any?> val sessionMap = map["session"] as? Map<String, Any?>
sessionMap?.let { sessionMap?.let {
event.session = Session(it, logger) event.session = Session(it, logger, apiKey)
} }
// populate threads // populate threads

View File

@ -7,6 +7,7 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.location.LocationManager import android.location.LocationManager
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.os.Build
import android.os.RemoteException import android.os.RemoteException
import android.os.storage.StorageManager import android.os.storage.StorageManager
import java.lang.RuntimeException import java.lang.RuntimeException
@ -21,7 +22,11 @@ internal fun Context.registerReceiverSafe(
logger: Logger? = null logger: Logger? = null
): Intent? { ): Intent? {
try { try {
return registerReceiver(receiver, filter) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED)
} else {
registerReceiver(receiver, filter)
}
} catch (exc: SecurityException) { } catch (exc: SecurityException) {
logger?.w("Failed to register receiver", exc) logger?.w("Failed to register receiver", exc)
} catch (exc: RemoteException) { } catch (exc: RemoteException) {

View File

@ -242,21 +242,26 @@ internal class DeviceDataCollector(
/** /**
* Get the amount of memory remaining on the device * Get the amount of memory remaining on the device
*/ */
private fun calculateFreeMemory(): Long? { fun calculateFreeMemory(): Long? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
val freeMemory = appContext.getActivityManager() try {
?.let { am -> ActivityManager.MemoryInfo().also { am.getMemoryInfo(it) } } val freeMemory = appContext.getActivityManager()
?.availMem ?.let { am -> ActivityManager.MemoryInfo().also { am.getMemoryInfo(it) } }
?.availMem
if (freeMemory != null) { if (freeMemory != null) {
return freeMemory return freeMemory
}
} catch (e: Throwable) {
return null
} }
} }
return runCatching { return try {
@Suppress("PrivateApi") @Suppress("PrivateApi")
AndroidProcess::class.java.getDeclaredMethod("getFreeMemory").invoke(null) as Long? AndroidProcess::class.java.getDeclaredMethod("getFreeMemory").invoke(null) as Long?
}.getOrNull() } catch (e: Throwable) {
null
}
} }
/** /**

View File

@ -7,7 +7,7 @@ import java.io.IOException
*/ */
class Notifier @JvmOverloads constructor( class Notifier @JvmOverloads constructor(
var name: String = "Android Bugsnag Notifier", var name: String = "Android Bugsnag Notifier",
var version: String = "5.30.0", var version: String = "5.31.3",
var url: String = "https://bugsnag.com" var url: String = "https://bugsnag.com"
) : JsonStream.Streamable { ) : JsonStream.Streamable {

View File

@ -34,7 +34,7 @@ internal class PluginClient(
private fun instantiatePlugin(clz: String, isWarningEnabled: Boolean): Plugin? { private fun instantiatePlugin(clz: String, isWarningEnabled: Boolean): Plugin? {
return try { return try {
val pluginClz = Class.forName(clz) val pluginClz = Class.forName(clz)
pluginClz.newInstance() as Plugin pluginClz.getDeclaredConstructor().newInstance() as Plugin
} catch (exc: ClassNotFoundException) { } catch (exc: ClassNotFoundException) {
if (isWarningEnabled) { if (isWarningEnabled) {
logger.d("Plugin '$clz' is not on the classpath - functionality will not be enabled.") logger.d("Plugin '$clz' is not on the classpath - functionality will not be enabled.")

View File

@ -34,17 +34,19 @@ public final class Session implements JsonStream.Streamable, UserAware {
private final AtomicBoolean tracked = new AtomicBoolean(false); private final AtomicBoolean tracked = new AtomicBoolean(false);
final AtomicBoolean isPaused = new AtomicBoolean(false); final AtomicBoolean isPaused = new AtomicBoolean(false);
private String apiKey;
static Session copySession(Session session) { static Session copySession(Session session) {
Session copy = new Session(session.id, session.startedAt, session.user, Session copy = new Session(session.id, session.startedAt, session.user,
session.unhandledCount.get(), session.handledCount.get(), session.notifier, session.unhandledCount.get(), session.handledCount.get(), session.notifier,
session.logger); session.logger, session.getApiKey());
copy.tracked.set(session.tracked.get()); copy.tracked.set(session.tracked.get());
copy.autoCaptured.set(session.isAutoCaptured()); copy.autoCaptured.set(session.isAutoCaptured());
return copy; return copy;
} }
Session(Map<String, Object> map, Logger logger) { Session(Map<String, Object> map, Logger logger, String apiKey) {
this(null, null, logger); this(null, null, logger, apiKey);
setId((String) map.get("id")); setId((String) map.get("id"));
String timestamp = (String) map.get("startedAt"); String timestamp = (String) map.get("startedAt");
@ -61,25 +63,28 @@ public final class Session implements JsonStream.Streamable, UserAware {
} }
Session(String id, Date startedAt, User user, boolean autoCaptured, Session(String id, Date startedAt, User user, boolean autoCaptured,
Notifier notifier, Logger logger) { Notifier notifier, Logger logger, String apiKey) {
this(null, notifier, logger); this(null, notifier, logger, apiKey);
this.id = id; this.id = id;
this.startedAt = new Date(startedAt.getTime()); this.startedAt = new Date(startedAt.getTime());
this.user = user; this.user = user;
this.autoCaptured.set(autoCaptured); this.autoCaptured.set(autoCaptured);
this.apiKey = apiKey;
} }
Session(String id, Date startedAt, User user, int unhandledCount, int handledCount, Session(String id, Date startedAt, User user, int unhandledCount, int handledCount,
Notifier notifier, Logger logger) { Notifier notifier, Logger logger, String apiKey) {
this(id, startedAt, user, false, notifier, logger); this(id, startedAt, user, false, notifier, logger, apiKey);
this.unhandledCount.set(unhandledCount); this.unhandledCount.set(unhandledCount);
this.handledCount.set(handledCount); this.handledCount.set(handledCount);
this.tracked.set(true); this.tracked.set(true);
this.apiKey = apiKey;
} }
Session(File file, Notifier notifier, Logger logger) { Session(File file, Notifier notifier, Logger logger, String apiKey) {
this.file = file; this.file = file;
this.logger = logger; this.logger = logger;
this.apiKey = SessionFilenameInfo.findApiKeyInFilename(file, apiKey);
if (notifier != null) { if (notifier != null) {
Notifier copy = new Notifier(notifier.getName(), Notifier copy = new Notifier(notifier.getName(),
notifier.getVersion(), notifier.getUrl()); notifier.getVersion(), notifier.getUrl());
@ -211,8 +216,10 @@ public final class Session implements JsonStream.Streamable, UserAware {
* *
* @return whether the payload is v2 * @return whether the payload is v2
*/ */
boolean isV2Payload() {
return file != null && file.getName().endsWith("_v2.json"); boolean isLegacyPayload() {
return !(file != null
&& (file.getName().endsWith("_v2.json") || file.getName().endsWith("_v3.json")));
} }
Notifier getNotifier() { Notifier getNotifier() {
@ -222,10 +229,10 @@ public final class Session implements JsonStream.Streamable, UserAware {
@Override @Override
public void toStream(@NonNull JsonStream writer) throws IOException { public void toStream(@NonNull JsonStream writer) throws IOException {
if (file != null) { if (file != null) {
if (isV2Payload()) { if (!isLegacyPayload()) {
serializeV2Payload(writer); serializePayload(writer);
} else { } else {
serializeV1Payload(writer); serializeLegacyPayload(writer);
} }
} else { } else {
writer.beginObject(); writer.beginObject();
@ -239,11 +246,11 @@ public final class Session implements JsonStream.Streamable, UserAware {
} }
} }
private void serializeV2Payload(@NonNull JsonStream writer) throws IOException { private void serializePayload(@NonNull JsonStream writer) throws IOException {
writer.value(file); writer.value(file);
} }
private void serializeV1Payload(@NonNull JsonStream writer) throws IOException { private void serializeLegacyPayload(@NonNull JsonStream writer) throws IOException {
writer.beginObject(); writer.beginObject();
writer.name("notifier").value(notifier); writer.name("notifier").value(notifier);
writer.name("app").value(app); writer.name("app").value(app);
@ -261,4 +268,25 @@ public final class Session implements JsonStream.Streamable, UserAware {
writer.name("user").value(user); writer.name("user").value(user);
writer.endObject(); writer.endObject();
} }
/**
* The API key used for session sent to Bugsnag. Even though the API key is set when Bugsnag
* is initialized, you may choose to send certain sessions to a different Bugsnag project.
*/
public void setApiKey(@NonNull String apiKey) {
if (apiKey != null) {
this.apiKey = apiKey;
} else {
logNull("apiKey");
}
}
/**
* The API key used for session sent to Bugsnag. Even though the API key is set when Bugsnag
* is initialized, you may choose to send certain sessions to a different Bugsnag project.
*/
@NonNull
public String getApiKey() {
return apiKey;
}
} }

View File

@ -1,5 +1,6 @@
package com.bugsnag.android package com.bugsnag.android
import com.bugsnag.android.internal.ImmutableConfig
import java.io.File import java.io.File
import java.util.UUID import java.util.UUID
@ -11,12 +12,13 @@ import java.util.UUID
* timestamp - to sort error reports by time of capture * timestamp - to sort error reports by time of capture
*/ */
internal data class SessionFilenameInfo( internal data class SessionFilenameInfo(
var apiKey: String,
val timestamp: Long, val timestamp: Long,
val uuid: String, val uuid: String
) { ) {
fun encode(): String { fun encode(): String {
return toFilename(timestamp, uuid) return toFilename(apiKey, timestamp, uuid)
} }
internal companion object { internal companion object {
@ -27,29 +29,63 @@ internal data class SessionFilenameInfo(
* Generates a filename for the session in the format * Generates a filename for the session in the format
* "[UUID][timestamp]_v2.json" * "[UUID][timestamp]_v2.json"
*/ */
fun toFilename(timestamp: Long, uuid: String): String { fun toFilename(apiKey: String, timestamp: Long, uuid: String): String {
return "${uuid}${timestamp}_v2.json" return "${apiKey}_${uuid}${timestamp}_v3.json"
} }
@JvmStatic @JvmStatic
fun defaultFilename(): String { fun defaultFilename(
return toFilename(System.currentTimeMillis(), UUID.randomUUID().toString()) obj: Any,
config: ImmutableConfig
): SessionFilenameInfo {
val sanitizedApiKey = when (obj) {
is Session -> obj.apiKey
else -> config.apiKey
}
return SessionFilenameInfo(
sanitizedApiKey,
System.currentTimeMillis(),
UUID.randomUUID().toString()
)
} }
fun fromFile(file: File): SessionFilenameInfo { fun fromFile(file: File, defaultApiKey: String): SessionFilenameInfo {
return SessionFilenameInfo( return SessionFilenameInfo(
findApiKeyInFilename(file, defaultApiKey),
findTimestampInFilename(file), findTimestampInFilename(file),
findUuidInFilename(file) findUuidInFilename(file)
) )
} }
private fun findUuidInFilename(file: File): String { @JvmStatic
return file.name.substring(0, uuidLength - 1) fun findUuidInFilename(file: File): String {
var fileName = file.name
if (isFileV3(file)) {
fileName = file.name.substringAfter('_')
}
return fileName.takeIf { it.length >= uuidLength }?.take(uuidLength) ?: ""
} }
@JvmStatic @JvmStatic
fun findTimestampInFilename(file: File): Long { fun findTimestampInFilename(file: File): Long {
return file.name.substring(uuidLength, file.name.indexOf("_")).toLongOrNull() ?: -1 var fileName = file.name
if (isFileV3(file)) {
fileName = file.name.substringAfter('_')
}
return fileName.drop(findUuidInFilename(file).length)
.substringBefore('_')
.toLongOrNull() ?: -1
} }
@JvmStatic
fun findApiKeyInFilename(file: File?, defaultApiKey: String): String {
if (file == null || !isFileV3(file)) {
return defaultApiKey
}
return file.name.substringBefore('_').takeUnless { it.isEmpty() } ?: defaultApiKey
}
internal fun isFileV3(file: File): Boolean = file.name.endsWith("_v3.json")
} }
} }

View File

@ -17,6 +17,7 @@ import java.util.UUID;
*/ */
class SessionStore extends FileStore { class SessionStore extends FileStore {
private final ImmutableConfig config;
static final Comparator<File> SESSION_COMPARATOR = new Comparator<File>() { static final Comparator<File> SESSION_COMPARATOR = new Comparator<File>() {
@Override @Override
public int compare(File lhs, File rhs) { public int compare(File lhs, File rhs) {
@ -43,12 +44,15 @@ class SessionStore extends FileStore {
SESSION_COMPARATOR, SESSION_COMPARATOR,
logger, logger,
delegate); delegate);
this.config = config;
} }
@NonNull @NonNull
@Override @Override
String getFilename(Object object) { String getFilename(Object object) {
return SessionFilenameInfo.defaultFilename(); SessionFilenameInfo sessionInfo
= SessionFilenameInfo.defaultFilename(object, config);
return sessionInfo.encode();
} }
public boolean isTooOld(File file) { public boolean isTooOld(File file) {

View File

@ -89,7 +89,10 @@ class SessionTracker extends BaseObservable {
return null; return null;
} }
String id = UUID.randomUUID().toString(); String id = UUID.randomUUID().toString();
Session session = new Session(id, date, user, autoCaptured, client.getNotifier(), logger); Session session = new Session(
id, date, user, autoCaptured,
client.getNotifier(), logger, configuration.getApiKey()
);
if (trackSessionIfNeeded(session)) { if (trackSessionIfNeeded(session)) {
return session; return session;
} else { } else {
@ -157,7 +160,7 @@ class SessionTracker extends BaseObservable {
Session session = null; Session session = null;
if (date != null && sessionId != null) { if (date != null && sessionId != null) {
session = new Session(sessionId, date, user, unhandledCount, handledCount, session = new Session(sessionId, date, user, unhandledCount, handledCount,
client.getNotifier(), logger); client.getNotifier(), logger, configuration.getApiKey());
notifySessionStartObserver(session); notifySessionStartObserver(session);
} else { } else {
updateState(StateEvent.PauseSession.INSTANCE); updateState(StateEvent.PauseSession.INSTANCE);
@ -256,9 +259,11 @@ class SessionTracker extends BaseObservable {
void flushStoredSession(File storedFile) { void flushStoredSession(File storedFile) {
logger.d("SessionTracker#flushStoredSession() - attempting delivery"); logger.d("SessionTracker#flushStoredSession() - attempting delivery");
Session payload = new Session(storedFile, client.getNotifier(), logger); Session payload = new Session(
storedFile, client.getNotifier(), logger, configuration.getApiKey()
);
if (!payload.isV2Payload()) { // collect data here if (payload.isLegacyPayload()) { // collect data here
payload.setApp(client.getAppDataCollector().generateApp()); payload.setApp(client.getAppDataCollector().generateApp());
payload.setDevice(client.getDeviceDataCollector().generateDevice()); payload.setDevice(client.getDeviceDataCollector().generateDevice());
} }
@ -330,7 +335,7 @@ class SessionTracker extends BaseObservable {
} }
DeliveryStatus deliverSessionPayload(Session payload) { DeliveryStatus deliverSessionPayload(Session payload) {
DeliveryParams params = configuration.getSessionApiDeliveryParams(); DeliveryParams params = configuration.getSessionApiDeliveryParams(payload);
Delivery delivery = configuration.getDelivery(); Delivery delivery = configuration.getDelivery();
return delivery.deliver(payload, params); return delivery.deliver(payload, params);
} }

View File

@ -2,32 +2,38 @@ package com.bugsnag.android
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.SharedPreferences
/** /**
* Reads legacy information left in SharedPreferences and migrates it to the new location. * Reads legacy information left in SharedPreferences and migrates it to the new location.
*/ */
internal class SharedPrefMigrator(context: Context) : DeviceIdPersistence { internal class SharedPrefMigrator(context: Context) : DeviceIdPersistence {
private val prefs = context private val prefs: SharedPreferences? =
.getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE) try {
context.getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE)
} catch (e: RuntimeException) {
null
}
/** /**
* This implementation will never create an ID; it will only fetch one if present. * This implementation will never create an ID; it will only fetch one if present.
*/ */
override fun loadDeviceId(requestCreateIfDoesNotExist: Boolean) = prefs.getString(INSTALL_ID_KEY, null) override fun loadDeviceId(requestCreateIfDoesNotExist: Boolean) =
prefs?.getString(INSTALL_ID_KEY, null)
fun loadUser(deviceId: String?) = User( fun loadUser(deviceId: String?) = User(
prefs.getString(USER_ID_KEY, deviceId), prefs?.getString(USER_ID_KEY, deviceId),
prefs.getString(USER_EMAIL_KEY, null), prefs?.getString(USER_EMAIL_KEY, null),
prefs.getString(USER_NAME_KEY, null) prefs?.getString(USER_NAME_KEY, null)
) )
fun hasPrefs() = prefs.contains(INSTALL_ID_KEY) fun hasPrefs() = prefs?.contains(INSTALL_ID_KEY) == true
@SuppressLint("ApplySharedPref") @SuppressLint("ApplySharedPref")
fun deleteLegacyPrefs() { fun deleteLegacyPrefs() {
if (hasPrefs()) { if (hasPrefs()) {
prefs.edit().clear().commit() prefs?.edit()?.clear()?.commit()
} }
} }

View File

@ -66,6 +66,7 @@ internal class SystemBroadcastReceiver(
) { ) {
val extras = intent.extras val extras = intent.extras
extras?.keySet()?.forEach { key -> extras?.keySet()?.forEach { key ->
@Suppress("DEPRECATION")
val valObj = extras[key] ?: return@forEach val valObj = extras[key] ?: return@forEach
val strVal = valObj.toString() val strVal = valObj.toString()
if (isAndroidKey(key)) { // shorten the Intent action if (isAndroidKey(key)) { // shorten the Intent action

View File

@ -18,6 +18,7 @@ import com.bugsnag.android.EventPayload
import com.bugsnag.android.Logger import com.bugsnag.android.Logger
import com.bugsnag.android.ManifestConfigLoader.Companion.BUILD_UUID import com.bugsnag.android.ManifestConfigLoader.Companion.BUILD_UUID
import com.bugsnag.android.NoopLogger import com.bugsnag.android.NoopLogger
import com.bugsnag.android.Session
import com.bugsnag.android.Telemetry import com.bugsnag.android.Telemetry
import com.bugsnag.android.ThreadSendPolicy import com.bugsnag.android.ThreadSendPolicy
import com.bugsnag.android.errorApiHeaders import com.bugsnag.android.errorApiHeaders
@ -65,8 +66,8 @@ data class ImmutableConfig(
DeliveryParams(endpoints.notify, errorApiHeaders(payload)) DeliveryParams(endpoints.notify, errorApiHeaders(payload))
@JvmName("getSessionApiDeliveryParams") @JvmName("getSessionApiDeliveryParams")
internal fun getSessionApiDeliveryParams() = internal fun getSessionApiDeliveryParams(session: Session) =
DeliveryParams(endpoints.sessions, sessionApiHeaders(apiKey)) DeliveryParams(endpoints.sessions, sessionApiHeaders(session.apiKey))
/** /**
* Returns whether the given throwable should be discarded * Returns whether the given throwable should be discarded

View File

@ -10,7 +10,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:8.1.3' classpath 'com.android.tools.build:gradle:8.1.3'
// https://github.com/bugsnag/bugsnag-android-gradle-plugin // https://github.com/bugsnag/bugsnag-android-gradle-plugin
// https://mvnrepository.com/artifact/com.bugsnag/bugsnag-android-gradle-plugin // https://mvnrepository.com/artifact/com.bugsnag/bugsnag-android-gradle-plugin
classpath "com.bugsnag:bugsnag-android-gradle-plugin:8.0.1" classpath "com.bugsnag:bugsnag-android-gradle-plugin:8.1.0"
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin // https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.8.20" classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.8.20"