From ae53f976509f4f28fb2f9afa36ab15a8ccc9dd09 Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Mon, 27 Mar 2017 14:34:30 -0700 Subject: [PATCH] Fix null pointer on dismissing suggestion. Add null check for context when getting the suggestion identifier, since this is called from async task and context might becomes null. Change-Id: If17036803de1634891b659a4fc6bd4b8a76ef5fb Fix: 36605386 Test: make RunSettingsRoboTests --- .../suggestions/SuggestionFeatureProviderImpl.java | 3 ++- .../suggestions/SuggestionFeatureProviderImplTest.java | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java b/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java index 779a8aaf34a..f3f8af92f81 100644 --- a/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java +++ b/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java @@ -83,7 +83,8 @@ public class SuggestionFeatureProviderImpl implements SuggestionFeatureProvider @Override public String getSuggestionIdentifier(Context context, Tile suggestion) { - if (suggestion.intent == null || suggestion.intent.getComponent() == null) { + if (suggestion.intent == null || suggestion.intent.getComponent() == null + || context == null) { return "unknown_suggestion"; } String packageName = suggestion.intent.getComponent().getPackageName(); diff --git a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java index 3efe15a86d4..6183acca6e5 100644 --- a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java +++ b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java @@ -107,6 +107,15 @@ public class SuggestionFeatureProviderImplTest { .isNotEmpty(); } + @Test + public void getSuggestionIdentifier_nullContext_shouldNotCrash() { + final Tile suggestion = new Tile(); + suggestion.intent = new Intent() + .setClassName(RuntimeEnvironment.application.getPackageName(), "123"); + assertThat(mProvider.getSuggestionIdentifier(null, suggestion)) + .isNotEmpty(); + } + @Test public void dismissSuggestion_hasMoreDismissCount_shouldNotDisableComponent() { when(mSuggestionParser.dismissSuggestion(any(Tile.class), anyBoolean())) @@ -120,7 +129,6 @@ public class SuggestionFeatureProviderImplTest { verify(mContext, never()).getPackageManager(); } - @Test public void dismissSuggestion_noContext_shouldDoNothing() { mProvider.dismissSuggestion(null, mSuggestionParser, mSuggestion);