From 8c9bacfecdef57f31c53e7bdcce84a8e34796910 Mon Sep 17 00:00:00 2001 From: Orlando Aliaga Date: Thu, 20 Jul 2023 12:22:16 -0400 Subject: [PATCH] New PreyFirebaseCrashlytics New PreyFirebaseCrashlytics --- .../com/prey/actions/picture/PictureUtil.java | 15 +++---- .../exceptions/PreyFirebaseCrashlytics.java | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/prey/exceptions/PreyFirebaseCrashlytics.java diff --git a/app/src/main/java/com/prey/actions/picture/PictureUtil.java b/app/src/main/java/com/prey/actions/picture/PictureUtil.java index e589e367..e702909d 100644 --- a/app/src/main/java/com/prey/actions/picture/PictureUtil.java +++ b/app/src/main/java/com/prey/actions/picture/PictureUtil.java @@ -21,14 +21,13 @@ import androidx.core.app.ActivityCompat; -import com.google.firebase.FirebaseApp; -import com.google.firebase.crashlytics.FirebaseCrashlytics; import com.prey.PreyConfig; import com.prey.PreyLogger; import com.prey.actions.HttpDataService; import com.prey.actions.camera.CameraAction; import com.prey.activities.CheckPasswordHtmlActivity; import com.prey.activities.SimpleCameraActivity; +import com.prey.exceptions.PreyFirebaseCrashlytics; import com.prey.net.http.EntityFile; public class PictureUtil { @@ -45,10 +44,6 @@ public static HttpDataService getPicture(Context ctx) { HttpDataService data = null; int currentVolume = 0; AudioManager mgr = null; - FirebaseApp.initializeApp(ctx); - FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); - crashlytics.setCustomKey("devicekey", PreyConfig.getPreyConfig(ctx).getDeviceId()); - crashlytics.setCustomKey("apikey", PreyConfig.getPreyConfig(ctx).getApiKey()); try { SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmZ"); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M @@ -83,7 +78,7 @@ public static HttpDataService getPicture(Context ctx) { } } catch (Exception e) { PreyLogger.e("report error:" + e.getMessage(), e); - crashlytics.recordException(e); + PreyFirebaseCrashlytics.getInstance(ctx).recordException(e); } attempts++; } while (attempts < maximum); @@ -111,7 +106,7 @@ public static HttpDataService getPicture(Context ctx) { } } catch (Exception e) { PreyLogger.e("report error:" + attempts, e); - crashlytics.recordException(e); + PreyFirebaseCrashlytics.getInstance(ctx).recordException(e); } attempts++; } while (attempts < maximum); @@ -126,7 +121,7 @@ public static HttpDataService getPicture(Context ctx) { ctx.sendBroadcast(new Intent(CheckPasswordHtmlActivity.CLOSE_PREY)); } catch (Exception e) { PreyLogger.e("report error:" + e.getMessage(), e); - crashlytics.recordException(e); + PreyFirebaseCrashlytics.getInstance(ctx).recordException(e); } finally { try { currentVolume = PreyConfig.getPreyConfig(ctx).getVolume(); @@ -136,7 +131,7 @@ public static HttpDataService getPicture(Context ctx) { } } catch (Exception e) { PreyLogger.e("report error:" + e.getMessage(), e); - crashlytics.recordException(e); + PreyFirebaseCrashlytics.getInstance(ctx).recordException(e); } } return data; diff --git a/app/src/main/java/com/prey/exceptions/PreyFirebaseCrashlytics.java b/app/src/main/java/com/prey/exceptions/PreyFirebaseCrashlytics.java new file mode 100644 index 00000000..17597cb4 --- /dev/null +++ b/app/src/main/java/com/prey/exceptions/PreyFirebaseCrashlytics.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Created by Orlando Aliaga + * Copyright 2023 Prey Inc. All rights reserved. + * License: GPLv3 + * Full license at "/LICENSE" + ******************************************************************************/ +package com.prey.exceptions; + +import android.content.Context; + +import androidx.annotation.NonNull; + +import com.google.firebase.FirebaseApp; +import com.google.firebase.crashlytics.FirebaseCrashlytics; + +import com.prey.PreyConfig; + +public class PreyFirebaseCrashlytics { + + private static PreyFirebaseCrashlytics instance = null; + private FirebaseCrashlytics crashlytics = null; + + private PreyFirebaseCrashlytics(Context ctx) { + //initialize FirebaseCrashlytics + FirebaseApp.initializeApp(ctx); + FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); + crashlytics.setCustomKey("devicekey", PreyConfig.getPreyConfig(ctx).getDeviceId()); + crashlytics.setCustomKey("apikey", PreyConfig.getPreyConfig(ctx).getApiKey()); + } + + public static PreyFirebaseCrashlytics getInstance(Context ctx) { + if (instance == null) + instance = new PreyFirebaseCrashlytics(ctx); + return instance; + } + + /** + * Method to report an exception + */ + public void recordException(@NonNull Throwable throwable) { + crashlytics.recordException(throwable); + } +}