From d8b167ebfa6ea8469a99857fa9f8effb9b3348b3 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Fri, 14 May 2021 17:05:02 +0200 Subject: [PATCH] spotbugs: improve exception handling Signed-off-by: Andy Scherzinger --- .../talk/controllers/ContactsController.java | 79 +++++++++---------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java b/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java index e76028942..fd9d5243d 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java @@ -75,6 +75,7 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.parceler.Parcels; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -129,7 +130,6 @@ public class ContactsController extends BaseController implements SearchView.OnQ @BindView(R.id.loading_content) LinearLayout loadingContent; - @BindView(R.id.recycler_view) RecyclerView recyclerView; @@ -527,45 +527,49 @@ public class ContactsController extends BaseController implements SearchView.OnQ EnumActorTypeConverter actorTypeConverter = new EnumActorTypeConverter(); try { - AutocompleteOverall autocompleteOverall = LoganSquare.parse(responseBody.string(), AutocompleteOverall.class); - autocompleteUsersHashSet.addAll(autocompleteOverall.getOcs().getData()); + AutocompleteOverall autocompleteOverall = LoganSquare.parse( + responseBody.string(), + AutocompleteOverall.class); + autocompleteUsersHashSet.addAll(autocompleteOverall.getOcs().getData()); - for (AutocompleteUser autocompleteUser : autocompleteUsersHashSet) { - if (!autocompleteUser.getId().equals(currentUser.getUserId()) && !existingParticipants.contains(autocompleteUser.getId())) { - participant = new Participant(); - participant.setActorId(autocompleteUser.getId()); - participant.setActorType(actorTypeConverter.getFromString(autocompleteUser.getSource())); - participant.setDisplayName(autocompleteUser.getLabel()); - participant.setSource(autocompleteUser.getSource()); + for (AutocompleteUser autocompleteUser : autocompleteUsersHashSet) { + if (!autocompleteUser.getId().equals(currentUser.getUserId()) + && !existingParticipants.contains(autocompleteUser.getId())) { + participant = new Participant(); + participant.setActorId(autocompleteUser.getId()); + participant.setActorType(actorTypeConverter.getFromString(autocompleteUser.getSource())); + participant.setDisplayName(autocompleteUser.getLabel()); + participant.setSource(autocompleteUser.getSource()); - String headerTitle; - - if (participant.getActorType() == Participant.ActorType.GROUPS) { - headerTitle = getResources().getString(R.string.nc_groups); - } else if (participant.getActorType() == Participant.ActorType.CIRCLES) { - headerTitle = getResources().getString(R.string.nc_circles); - } else { - headerTitle = participant.getDisplayName().substring(0, 1).toUpperCase(); - } - - GenericTextHeaderItem genericTextHeaderItem; - if (!userHeaderItems.containsKey(headerTitle)) { - genericTextHeaderItem = new GenericTextHeaderItem(headerTitle); - userHeaderItems.put(headerTitle, genericTextHeaderItem); - } - - - UserItem newContactItem = new UserItem(participant, currentUser, - userHeaderItems.get(headerTitle)); - - if (!contactItems.contains(newContactItem)) { - newUserItemList.add(newContactItem); - } + String headerTitle; + if (participant.getActorType() == Participant.ActorType.GROUPS) { + headerTitle = getResources().getString(R.string.nc_groups); + } else if (participant.getActorType() == Participant.ActorType.CIRCLES) { + headerTitle = getResources().getString(R.string.nc_circles); + } else { + headerTitle = participant.getDisplayName().substring(0, 1).toUpperCase(); } + + GenericTextHeaderItem genericTextHeaderItem; + if (!userHeaderItems.containsKey(headerTitle)) { + genericTextHeaderItem = new GenericTextHeaderItem(headerTitle); + userHeaderItems.put(headerTitle, genericTextHeaderItem); + } + + UserItem newContactItem = new UserItem( + participant, + currentUser, + userHeaderItems.get(headerTitle) + ); + + if (!contactItems.contains(newContactItem)) { + newUserItemList.add(newContactItem); + } + } } - } catch (Exception exception) { - Log.e(TAG, "Parsing response body failed while getting contacts"); + } catch (IOException ioe) { + Log.e(TAG, "Parsing response body failed while getting contacts", ioe); } userHeaderItems = new HashMap<>(); @@ -575,7 +579,6 @@ public class ContactsController extends BaseController implements SearchView.OnQ String firstName; String secondName; - if (o1 instanceof UserItem) { firstName = ((UserItem) o1).getModel().getDisplayName(); } else { @@ -627,7 +630,6 @@ public class ContactsController extends BaseController implements SearchView.OnQ String firstName; String secondName; - if (o1 instanceof UserItem) { firstName = ((UserItem) o1).getModel().getDisplayName(); } else { @@ -653,7 +655,6 @@ public class ContactsController extends BaseController implements SearchView.OnQ return firstName.compareToIgnoreCase(secondName); }); - if (newUserItemList.size() > 0) { adapter.updateDataSet(newUserItemList); } else { @@ -664,7 +665,6 @@ public class ContactsController extends BaseController implements SearchView.OnQ swipeRefreshLayout.setRefreshing(false); } } - } @Override @@ -673,7 +673,6 @@ public class ContactsController extends BaseController implements SearchView.OnQ swipeRefreshLayout.setRefreshing(false); } dispose(contactsQueryDisposable); - } @Override