Skip to content

Commit b9ac6e8

Browse files
committed
more clean up
1 parent bbd5126 commit b9ac6e8

10 files changed

Lines changed: 358 additions & 275 deletions

File tree

android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public class RNOneSignal extends NativeOneSignalSpec
8585
private boolean hasSetPushSubscriptionObserver = false;
8686
private boolean hasSetUserStateObserver = false;
8787

88-
private HashMap<String, INotificationWillDisplayEvent> notificationWillDisplayCache;
89-
private HashMap<String, INotificationWillDisplayEvent> preventDefaultCache;
88+
private final HashMap<String, INotificationWillDisplayEvent> notificationWillDisplayCache = new HashMap<>();
89+
private final HashMap<String, INotificationWillDisplayEvent> preventDefaultCache = new HashMap<>();
9090

9191
private boolean hasAddedNotificationForegroundListener = false;
9292
private boolean hasAddedInAppMessageLifecycleListener = false;
@@ -96,26 +96,26 @@ public class RNOneSignal extends NativeOneSignalSpec
9696
// Static reference to track current instance for cleanup on reload
9797
private static RNOneSignal currentInstance = null;
9898

99-
private IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener() {
99+
private final IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener() {
100100
@Override
101101
public void onClick(IInAppMessageClickEvent event) {
102102
try {
103103
emitOnInAppMessageClicked(
104104
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageClickEventToMap(event)));
105105
} catch (JSONException e) {
106-
e.printStackTrace();
106+
logJSONException("onInAppMessageClicked", e);
107107
}
108108
}
109109
};
110110

111-
private IInAppMessageLifecycleListener rnInAppLifecycleListener = new IInAppMessageLifecycleListener() {
111+
private final IInAppMessageLifecycleListener rnInAppLifecycleListener = new IInAppMessageLifecycleListener() {
112112
@Override
113113
public void onWillDisplay(IInAppMessageWillDisplayEvent event) {
114114
try {
115115
emitOnInAppMessageWillDisplay(
116116
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageWillDisplayEventToMap(event)));
117117
} catch (JSONException e) {
118-
e.printStackTrace();
118+
logJSONException("onInAppMessageWillDisplay", e);
119119
}
120120
}
121121

@@ -125,7 +125,7 @@ public void onDidDisplay(IInAppMessageDidDisplayEvent event) {
125125
emitOnInAppMessageDidDisplay(
126126
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageDidDisplayEventToMap(event)));
127127
} catch (JSONException e) {
128-
e.printStackTrace();
128+
logJSONException("onInAppMessageDidDisplay", e);
129129
}
130130
}
131131

@@ -135,7 +135,7 @@ public void onWillDismiss(IInAppMessageWillDismissEvent event) {
135135
emitOnInAppMessageWillDismiss(
136136
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageWillDismissEventToMap(event)));
137137
} catch (JSONException e) {
138-
e.printStackTrace();
138+
logJSONException("onInAppMessageWillDismiss", e);
139139
}
140140
}
141141

@@ -145,23 +145,27 @@ public void onDidDismiss(IInAppMessageDidDismissEvent event) {
145145
emitOnInAppMessageDidDismiss(
146146
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageDidDismissEventToMap(event)));
147147
} catch (JSONException e) {
148-
e.printStackTrace();
148+
logJSONException("onInAppMessageDidDismiss", e);
149149
}
150150
}
151151
};
152152

153-
private INotificationClickListener rnNotificationClickListener = new INotificationClickListener() {
153+
private final INotificationClickListener rnNotificationClickListener = new INotificationClickListener() {
154154
@Override
155155
public void onClick(INotificationClickEvent event) {
156156
try {
157157
emitOnNotificationClicked(
158158
RNUtils.convertHashMapToWritableMap(RNUtils.convertNotificationClickEventToMap(event)));
159159
} catch (JSONException e) {
160-
e.printStackTrace();
160+
logJSONException("onNotificationClicked", e);
161161
}
162162
}
163163
};
164164

165+
private void logJSONException(String eventName, JSONException exception) {
166+
Logging.error("Failed to serialize payload for " + eventName, exception);
167+
}
168+
165169
private void removeObservers() {
166170
if (!oneSignalInitDone) {
167171
Logging.debug("OneSignal React-Native SDK not initialized yet. Could not remove observers.", null);
@@ -193,8 +197,6 @@ private void removeObservers() {
193197
public RNOneSignal(ReactApplicationContext reactContext) {
194198
super(reactContext);
195199
reactContext.addLifecycleEventListener(this);
196-
notificationWillDisplayCache = new HashMap<String, INotificationWillDisplayEvent>();
197-
preventDefaultCache = new HashMap<String, INotificationWillDisplayEvent>();
198200

199201
// Clean up previous instance if it exists (handles reload scenario)
200202
if (currentInstance != null && currentInstance != this) {
@@ -375,7 +377,7 @@ public void onWillDisplay(INotificationWillDisplayEvent event) {
375377
Logging.error("InterruptedException: " + e.toString(), null);
376378
}
377379
} catch (JSONException e) {
378-
e.printStackTrace();
380+
logJSONException("onNotificationWillDisplay", e);
379381
}
380382
}
381383

@@ -410,7 +412,7 @@ public void addPermissionObserver() {
410412
}
411413
}
412414

413-
public void removePermissionObserver() {
415+
private void removePermissionObserver() {
414416
if (hasSetPermissionObserver) {
415417
OneSignal.getNotifications().removePermissionObserver(this);
416418
hasSetPermissionObserver = false;
@@ -423,7 +425,7 @@ public void onNotificationPermissionChange(boolean permission) {
423425
emitOnPermissionChanged(RNUtils.convertHashMapToWritableMap(RNUtils.convertPermissionToMap(permission)));
424426
Logging.debug("Sending permission change event", null);
425427
} catch (JSONException e) {
426-
e.printStackTrace();
428+
logJSONException("onPermissionChanged", e);
427429
}
428430
}
429431

@@ -568,11 +570,11 @@ public void onPushSubscriptionChange(PushSubscriptionChangedState pushSubscripti
568570
RNUtils.convertPushSubscriptionChangedStateToMap(pushSubscriptionChangedState)));
569571
Logging.debug("Sending subscription change event", null);
570572
} catch (JSONException e) {
571-
e.printStackTrace();
573+
logJSONException("onSubscriptionChanged", e);
572574
}
573575
}
574576

575-
public void removePushSubscriptionObserver() {
577+
private void removePushSubscriptionObserver() {
576578
if (hasSetPushSubscriptionObserver) {
577579
OneSignal.getUser().getPushSubscription().removeObserver(this);
578580
hasSetPushSubscriptionObserver = false;
@@ -713,11 +715,11 @@ public void onUserStateChange(UserChangedState state) {
713715
emitOnUserStateChanged(RNUtils.convertHashMapToWritableMap(RNUtils.convertUserChangedStateToMap(state)));
714716
Logging.debug("Sending user state change event", null);
715717
} catch (JSONException e) {
716-
e.printStackTrace();
718+
logJSONException("onUserStateChanged", e);
717719
}
718720
}
719721

720-
public void removeUserStateObserver() {
722+
private void removeUserStateObserver() {
721723
if (hasSetUserStateObserver) {
722724
OneSignal.getUser().removeObserver(this);
723725
hasSetUserStateObserver = false;

0 commit comments

Comments
 (0)