From 0f75eb550e337c6c99b0158ce6997f17bd7ce1a1 Mon Sep 17 00:00:00 2001 From: Yi-Ling Chuang Date: Wed, 8 May 2019 11:34:40 +0800 Subject: [PATCH] Add null check before setting the visibility of swipe background Some contextual cards does not allow card dismissal, so they won't have swipe background declared in the layout file. Add null check to prevent from NPE. Fixes: 132209310 Test: robotests & launch Settings Change-Id: I31f897f445c4901d007c8187fe69aea416b915d1 --- .../slices/SliceContextualCardRenderer.java | 11 +++++---- .../SliceContextualCardRendererTest.java | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java b/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java index f9753484180..b83a712bc54 100644 --- a/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java +++ b/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRenderer.java @@ -112,8 +112,10 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life final View swipeBackground = holder.itemView.findViewById(R.id.dismissal_swipe_background); sliceLiveData.removeObservers(mLifecycleOwner); - // set the background to Gone in case the holder is reused. - swipeBackground.setVisibility(View.GONE); + // set the background to GONE in case the holder is reused. + if (swipeBackground != null) { + swipeBackground.setVisibility(View.GONE); + } sliceLiveData.observe(mLifecycleOwner, slice -> { if (slice == null) { Log.w(TAG, "Slice is null"); @@ -140,8 +142,9 @@ public class SliceContextualCardRenderer implements ContextualCardRenderer, Life default: mFullCardHelper.bindView(holder, card, slice); } - - swipeBackground.setVisibility(View.VISIBLE); + if (swipeBackground != null) { + swipeBackground.setVisibility(View.VISIBLE); + } }); switch (holder.getItemViewType()) { diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRendererTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRendererTest.java index 70761cf52d5..fb04dac9dd2 100644 --- a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRendererTest.java +++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/SliceContextualCardRendererTest.java @@ -16,11 +16,13 @@ package com.android.settings.homepage.contextualcards.slices; +import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_DEFERRED_SETUP; import static com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer.VIEW_TYPE_FULL_WIDTH; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import android.app.Activity; @@ -160,6 +162,15 @@ public class SliceContextualCardRendererTest { assertThat(swipeBg.getVisibility()).isEqualTo(View.GONE); } + @Test + public void bindView_deferredSetupCard_shouldNotCrash() { + final RecyclerView.ViewHolder viewHolder = getDeferredSetupViewHolder(); + final ContextualCard card = buildContextualCard(TEST_SLICE_URI); + mRenderer.mSliceLiveDataMap.put(TEST_SLICE_URI, mSliceLiveData); + + mRenderer.bindView(viewHolder, card); + } + @Test public void viewClick_keepCard_shouldShowSlice() { final RecyclerView.ViewHolder viewHolder = getSliceViewHolder(); @@ -246,6 +257,18 @@ public class SliceContextualCardRendererTest { return mRenderer.createViewHolder(view, VIEW_TYPE_FULL_WIDTH); } + private RecyclerView.ViewHolder getDeferredSetupViewHolder() { + final RecyclerView recyclerView = new RecyclerView(mActivity); + recyclerView.setLayoutManager(new LinearLayoutManager(mActivity)); + final View view = LayoutInflater.from(mActivity) + .inflate(VIEW_TYPE_DEFERRED_SETUP, recyclerView, false); + final RecyclerView.ViewHolder viewHolder = spy( + new SliceDeferredSetupCardRendererHelper.DeferredSetupCardViewHolder(view)); + doReturn(VIEW_TYPE_DEFERRED_SETUP).when(viewHolder).getItemViewType(); + + return viewHolder; + } + private ContextualCard buildContextualCard(Uri sliceUri) { return new ContextualCard.Builder() .setName("test_name")