Skip to content

Commit f3e6a07

Browse files
committed
Discard only problematic Objects when serializing/deserializing LiveNoti data arrays
1 parent c503e87 commit f3e6a07

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

.idea/deploymentTargetSelector.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/noti/main/service/livenoti/LiveNotiProcess.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.noti.main.service.livenoti;
22

33
import android.content.Context;
4-
import android.content.pm.PackageManager;
54
import android.service.notification.StatusBarNotification;
65
import android.util.Log;
76

@@ -60,7 +59,7 @@ public static void onProcessReceive(Map<String, String> map, Context context) {
6059
}
6160
}
6261

63-
public static String toStringLiveNotificationList(Context context) throws JsonProcessingException, PackageManager.NameNotFoundException {
62+
public static String toStringLiveNotificationList(Context context) throws JsonProcessingException {
6463
if(mOnNotificationListListener != null) {
6564
ArrayList<StatusBarNotification> statusBarNotifications = new ArrayList<>(Arrays.asList(mOnNotificationListListener.onRequested()));
6665
ArrayList<String> keyList = new ArrayList<>();
@@ -72,8 +71,15 @@ public static String toStringLiveNotificationList(Context context) throws JsonPr
7271
continue;
7372
}
7473

75-
finalDataList.add(new LiveNotificationData(context, statusBarNotification).toString());
76-
keyList.add(statusBarNotification.getKey());
74+
try {
75+
finalDataList.add(new LiveNotificationData(context, statusBarNotification).toString());
76+
keyList.add(statusBarNotification.getKey());
77+
} catch (Exception e) {
78+
if(BuildConfig.DEBUG) {
79+
Log.e("LiveNotiProcess", "Error while serializing LiveNotification data: " + statusBarNotification.getKey());
80+
e.printStackTrace();
81+
}
82+
}
7783
}
7884

7985
return new ObjectMapper().writeValueAsString(finalDataList.toArray());

app/src/main/java/com/noti/main/service/livenoti/LiveNotiRequests.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import android.content.Context;
66
import android.content.SharedPreferences;
7+
import android.util.Log;
78

89
import com.noti.main.Application;
910
import com.noti.main.service.NotiListenerService;
@@ -17,6 +18,7 @@
1718

1819
import java.io.IOException;
1920
import java.security.NoSuchAlgorithmException;
21+
import java.util.ArrayList;
2022
import java.util.Map;
2123
import java.util.concurrent.ConcurrentHashMap;
2224

@@ -122,10 +124,17 @@ public static void getLiveNotificationData(Context context, Map<String, String>
122124
ResultPacket resultPacket = ResultPacket.parseFrom(response.toString());
123125
if(resultPacket.isResultOk()) {
124126
String[] rawArray = new ObjectMapper().readValue(resultPacket.getExtraData(), String[].class);
125-
LiveNotificationData[] liveNotiArray = new LiveNotificationData[rawArray.length];
126-
for (int i = 0; i < rawArray.length; i++) {
127-
liveNotiArray[i] = LiveNotificationData.parseFrom(rawArray[i]);
127+
ArrayList<LiveNotificationData> dataArrayList = new ArrayList<>();
128+
for (String s : rawArray) {
129+
try {
130+
dataArrayList.add(LiveNotificationData.parseFrom(s));
131+
} catch (Exception e) {
132+
Log.d("LiveNotiProcess", "Error parsing LiveNotification => Raw data: " + s);
133+
}
128134
}
135+
136+
LiveNotificationData[] liveNotiArray = new LiveNotificationData[dataArrayList.size()];
137+
dataArrayList.toArray(liveNotiArray);
129138
LiveNotiProcess.callLiveNotificationUploadCompleteListener(true, liveNotiArray);
130139
} else {
131140
throw new IOException(resultPacket.getErrorCause());

app/src/main/java/com/noti/main/service/livenoti/LiveNotificationData.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public LiveNotificationData() {
4747
// Default constructor for creating instance by ObjectMapper
4848
}
4949

50-
public LiveNotificationData(Context context, StatusBarNotification statusBarNotification) throws PackageManager.NameNotFoundException {
50+
public LiveNotificationData(Context context, StatusBarNotification statusBarNotification) throws Exception {
5151
this.postTime = statusBarNotification.getPostTime();
5252
this.key = statusBarNotification.getKey();
5353

@@ -82,8 +82,12 @@ public LiveNotificationData(Context context, StatusBarNotification statusBarNoti
8282

8383
if(iconBitmap != null) {
8484
iconBitmap.setHasAlpha(true);
85-
this.smallIcon = CompressStringUtil.compressString(CompressStringUtil.getStringFromBitmap(
86-
NotiListenerService.getResizedBitmap(iconBitmap,52,52, Color.BLACK)));
85+
Bitmap resizedBitmap = NotiListenerService.getResizedBitmap(iconBitmap,52,52, Color.BLACK);
86+
if(resizedBitmap != null) {
87+
this.smallIcon = CompressStringUtil.compressString(CompressStringUtil.getStringFromBitmap(resizedBitmap));
88+
} else {
89+
this.smallIcon = "";
90+
}
8791
}
8892
}
8993
} else {

0 commit comments

Comments
 (0)