EventHandler.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.atmob.voiceai.handlers;
  2. import android.app.Application;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Looper;
  6. import android.text.TextUtils;
  7. import com.atmob.common.runtime.ContextUtil;
  8. import com.atmob.voiceai.BuildConfig;
  9. import com.atmob.voiceai.sdk.firebase.FirebaseHelper;
  10. import com.atmob.voiceai.utils.ToastUtil;
  11. import java.util.Set;
  12. import dagger.hilt.EntryPoint;
  13. import dagger.hilt.InstallIn;
  14. import dagger.hilt.android.EntryPointAccessors;
  15. import dagger.hilt.components.SingletonComponent;
  16. public final class EventHandler {
  17. private static final boolean isToastReport = false;
  18. private static volatile EventHandler INSTANCE;
  19. private final FirebaseHelper firebaseHelper;
  20. private static Handler MAIN_HANDLER;
  21. static {
  22. if (isToastReport) {
  23. MAIN_HANDLER = new Handler(Looper.getMainLooper());
  24. }
  25. }
  26. private EventHandler() {
  27. Application application = ContextUtil.getApplication();
  28. EventHandlerEntryPoint entryPoint = EntryPointAccessors.fromApplication(application, EventHandlerEntryPoint.class);
  29. firebaseHelper = entryPoint.firebaseHelper();
  30. }
  31. private static EventHandler getInstance() {
  32. if (INSTANCE == null) {
  33. synchronized (EventHandler.class) {
  34. if (INSTANCE == null) {
  35. INSTANCE = new EventHandler();
  36. }
  37. }
  38. }
  39. return INSTANCE;
  40. }
  41. public static void report(String eventId) {
  42. report(eventId, null);
  43. }
  44. public static void report(String eventId, Bundle params) {
  45. if (TextUtils.isEmpty(eventId)) {
  46. return;
  47. }
  48. if (isToastReport) {
  49. MAIN_HANDLER.post(() -> {
  50. StringBuilder sb = new StringBuilder();
  51. sb.append(eventId);
  52. if (params != null) {
  53. sb.append(" ");
  54. Set<String> keySet = params.keySet();
  55. for (String key : keySet) {
  56. sb.append(key);
  57. sb.append(":");
  58. sb.append(params.get(key));
  59. sb.append(" ");
  60. }
  61. }
  62. ToastUtil.show(sb.toString(), ToastUtil.LENGTH_SHORT);
  63. });
  64. }
  65. if (BuildConfig.DEBUG) {
  66. return;
  67. }
  68. getInstance().firebaseHelper.reportEvent(eventId, params);
  69. }
  70. @EntryPoint
  71. @InstallIn(SingletonComponent.class)
  72. interface EventHandlerEntryPoint {
  73. FirebaseHelper firebaseHelper();
  74. }
  75. }