joshdholtz / Sentry-Android

[Deprecated] Use official "raven-java" library
https://github.com/getsentry/sentry-java
MIT License
180 stars 48 forks source link

Add some Android device details by default to Events #59

Closed marcomorain closed 8 years ago

marcomorain commented 8 years ago

Hi @joshdholtz,

I'm using code like this to build up a default set of device data to send along with crashes.

private static Map<String, String> readDeviceData(Context context) {
  final Map<String, String> deviceData = new HashMap<>();
  try {
    deviceData.put("Device", Build.DEVICE);
    deviceData.put("Brand", Build.BRAND);
    deviceData.put("Model", Build.MODEL);
    deviceData.put("Android Version", Build.VERSION.RELEASE);
    //deviceData.put("Android SDK", Build.VERSION.SDK); // Android 3
    deviceData.put("Android SDK", Integer.toString(Build.VERSION.SDK_INT)); // Android 4+
    deviceData.put("Manufacturer", Build.MANUFACTURER); // Android 4+

    // Read package information
    final String packageName = context.getPackageName();
    final PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
    deviceData.put("Package Name", packageName);
    deviceData.put("Package Version Name", packageInfo.versionName);
    deviceData.put("Package Version Code", Integer.toString(packageInfo.versionCode));

    // Read screen resolution in the format "800x600"
    // Normalised to have wider side first.
    final Object windowManager = context.getSystemService(Context.WINDOW_SERVICE);
    if (windowManager != null && windowManager instanceof WindowManager) {
        final DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager)windowManager).getDefaultDisplay().getMetrics(metrics);
        deviceData.put("ScreenResolution",
                        String.format("%sx%s",
                                Math.max(metrics.widthPixels, metrics.heightPixels),
                                Math.min(metrics.widthPixels, metrics.heightPixels)));
            }

    } catch (Exception e) {
      Log.d("Sentry", "Error reading device data", e);
    }
    return deviceData;
}

I use a SentryEventCaptureListener to add this Map to the SentryEventBuilder before capture.

I thought you might be able to add something like this to all events by default, or to create a function like

class Sentry {
   ... // snip

  public static SentryEventCaptureListener CreateDeviceDataCaptureListener() {
    final Map<String, String> deviceData = readDeviceData(context);
    return new Sentry.SentryEventCaptureListener() {
            @Override
            public Sentry.SentryEventBuilder beforeCapture(Sentry.SentryEventBuilder builder) {
                return builder.setExtra(deviceData);
            }
        }
  }
}

Data in Sentry:

sentry
marcomorain commented 8 years ago

Hi @joshdholtz - and interest in me turning this into a PR or adding it to the README as an example of usage?

joshdholtz commented 8 years ago

@marcomorain A PR would be greatly appreciated if you don't mind 😊

marcomorain commented 8 years ago

@joshdholtz done!

marcomorain commented 8 years ago

Hi @joshdholtz - have you had any time to review this?