From eb1e55470a27bf267cc1d7a64213cda879408a7d Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 15 May 2021 11:46:15 +0900 Subject: [PATCH] Fix null menuItemView warnings --- .../directmessages/DirectMessageInboxFragment.java | 12 ++++++++++-- .../DirectMessageThreadFragment.java | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java index 60a7482e..d8a98087 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageInboxFragment.java @@ -16,6 +16,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.view.menu.ActionMenuItemView; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; @@ -27,6 +28,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.android.material.badge.BadgeDrawable; import com.google.android.material.badge.BadgeUtils; +import com.google.android.material.internal.ToolbarUtils; import com.google.android.material.snackbar.Snackbar; import java.util.List; @@ -102,7 +104,9 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh super.onPause(); unregisterReceiver(); isPendingRequestTotalBadgeAttached = false; - if (pendingRequestTotalBadgeDrawable != null) { + @SuppressLint("RestrictedApi") final ActionMenuItemView menuItemView = ToolbarUtils + .getActionMenuItemView(fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); + if (pendingRequestTotalBadgeDrawable != null && menuItemView != null) { BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); pendingRequestTotalBadgeDrawable = null; } @@ -217,7 +221,11 @@ public class DirectMessageInboxFragment extends Fragment implements SwipeRefresh pendingRequestTotalBadgeDrawable = BadgeDrawable.create(context); } if (count == null || count == 0) { - BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); + @SuppressLint("RestrictedApi") final ActionMenuItemView menuItemView = ToolbarUtils + .getActionMenuItemView(fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); + if (menuItemView != null) { + BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); + } isPendingRequestTotalBadgeAttached = false; pendingRequestTotalBadgeDrawable.setNumber(0); pendingRequestsMenuItem.setVisible(false); diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index 99d72a57..d4db3192 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -31,6 +31,7 @@ import androidx.activity.OnBackPressedDispatcher; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; +import androidx.appcompat.view.menu.ActionMenuItemView; import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsAnimationCompat; @@ -56,6 +57,7 @@ import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat; import com.google.android.material.badge.BadgeDrawable; import com.google.android.material.badge.BadgeUtils; +import com.google.android.material.internal.ToolbarUtils; import com.google.android.material.snackbar.Snackbar; import com.google.common.collect.ImmutableList; @@ -554,7 +556,11 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact } isPendingRequestCountBadgeAttached = false; if (pendingRequestCountBadgeDrawable != null) { - BadgeUtils.detachBadgeDrawable(pendingRequestCountBadgeDrawable, fragmentActivity.getToolbar(), R.id.info); + @SuppressLint("RestrictedApi") final ActionMenuItemView menuItemView = ToolbarUtils + .getActionMenuItemView(fragmentActivity.getToolbar(), R.id.info); + if (menuItemView != null) { + BadgeUtils.detachBadgeDrawable(pendingRequestCountBadgeDrawable, fragmentActivity.getToolbar(), R.id.info); + } pendingRequestCountBadgeDrawable = null; } } @@ -838,7 +844,11 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact pendingRequestCountBadgeDrawable = BadgeDrawable.create(context); } if (count == null || count == 0) { - BadgeUtils.detachBadgeDrawable(pendingRequestCountBadgeDrawable, fragmentActivity.getToolbar(), R.id.info); + @SuppressLint("RestrictedApi") final ActionMenuItemView menuItemView = ToolbarUtils + .getActionMenuItemView(fragmentActivity.getToolbar(), R.id.info); + if (menuItemView != null) { + BadgeUtils.detachBadgeDrawable(pendingRequestCountBadgeDrawable, fragmentActivity.getToolbar(), R.id.info); + } isPendingRequestCountBadgeAttached = false; pendingRequestCountBadgeDrawable.setNumber(0); return;