From 8052ac0b360a7f9743da9b9f025841fdd51679e5 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Fri, 20 Oct 2017 10:23:48 -0700 Subject: [PATCH] Call SuggestionService.launch when click suggestion. Bug: 68267490 Test: robotests Change-Id: I8614f256fc1a87395b5c9d41e3621164a2ded5e1 --- .../settings/dashboard/DashboardAdapter.java | 8 +-- .../suggestions/SuggestionAdapter.java | 7 ++- .../suggestions/SuggestionController.java | 13 +++++ .../SuggestionControllerMixin.java | 4 ++ .../suggestions/SuggestionAdapterTest.java | 49 ++++++++++--------- 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/com/android/settings/dashboard/DashboardAdapter.java b/src/com/android/settings/dashboard/DashboardAdapter.java index b28ff24bec5..fd2adacc9fd 100644 --- a/src/com/android/settings/dashboard/DashboardAdapter.java +++ b/src/com/android/settings/dashboard/DashboardAdapter.java @@ -430,16 +430,16 @@ public class DashboardAdapter extends RecyclerView.Adapter 0) { conditionOnly = false; - mSuggestionAdapter = new SuggestionAdapter(mContext, (List) - mDashboardData.getItemEntityByPosition(position), + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + (List) mDashboardData.getItemEntityByPosition(position), null, mSuggestionsShownLogged); mSuggestionDismissHandler = new SuggestionDismissController(mContext, holder.data, mSuggestionControllerMixin, mSuggestionParser, mCallback); holder.data.setAdapter(mSuggestionAdapter); } else if (suggestionsV2 != null && suggestionsV2.size() > 0) { conditionOnly = false; - mSuggestionAdapter = new SuggestionAdapter(mContext, null, - (List) mDashboardData.getItemEntityByPosition(position), + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + null, (List) mDashboardData.getItemEntityByPosition(position), mSuggestionsShownLogged); mSuggestionDismissHandler = new SuggestionDismissController(mContext, holder.data, mSuggestionControllerMixin, null /* parser */, mCallback); diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java b/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java index a9d0546664b..293c95af9e9 100644 --- a/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java +++ b/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java @@ -48,11 +48,13 @@ public class SuggestionAdapter extends RecyclerView.Adapter private final List mSuggestionsV2; private final IconCache mCache; private final List mSuggestionsShownLogged; + private final SuggestionControllerMixin mSuggestionControllerMixin; - public SuggestionAdapter(Context context, List suggestions, - List suggestionsV2, + public SuggestionAdapter(Context context, SuggestionControllerMixin suggestionControllerMixin, + List suggestions, List suggestionsV2, List suggestionsShownLogged) { mContext = context; + mSuggestionControllerMixin = suggestionControllerMixin; mSuggestions = suggestions; mSuggestionsV2 = suggestionsV2; mSuggestionsShownLogged = suggestionsShownLogged; @@ -112,6 +114,7 @@ public class SuggestionAdapter extends RecyclerView.Adapter mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_SETTINGS_SUGGESTION, id); try { suggestion.getPendingIntent().send(); + mSuggestionControllerMixin.launchSuggestion(suggestion); } catch (PendingIntent.CanceledException e) { Log.w(TAG, "Failed to start suggestion " + suggestion.getTitle()); } diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionController.java b/src/com/android/settings/dashboard/suggestions/SuggestionController.java index 865e045de74..ef57d836e12 100644 --- a/src/com/android/settings/dashboard/suggestions/SuggestionController.java +++ b/src/com/android/settings/dashboard/suggestions/SuggestionController.java @@ -122,6 +122,19 @@ public class SuggestionController { } } + public void launchSuggestion(Suggestion suggestion) { + if (!isReady()) { + Log.w(TAG, "SuggestionController not ready, cannot launch " + suggestion.getId()); + return; + } + + try { + mRemoteService.launchSuggestion(suggestion); + } catch (RemoteException e) { + Log.w(TAG, "Error when calling launchSuggestion()", e); + } + } + /** * Whether or not the manager is ready */ diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java b/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java index cf015ac66ac..7c2226d5ead 100644 --- a/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java +++ b/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java @@ -115,4 +115,8 @@ public class SuggestionControllerMixin implements SuggestionController.ServiceCo public void dismissSuggestion(Suggestion suggestion) { mSuggestionController.dismissSuggestions(suggestion); } + + public void launchSuggestion(Suggestion suggestion) { + mSuggestionController.launchSuggestion(suggestion); + } } diff --git a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionAdapterTest.java b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionAdapterTest.java index 825b388f018..1a170c8c873 100644 --- a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionAdapterTest.java +++ b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionAdapterTest.java @@ -67,6 +67,8 @@ public class SuggestionAdapterTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private SettingsActivity mActivity; + @Mock + private SuggestionControllerMixin mSuggestionControllerMixin; private FakeFeatureFactory mFeatureFactory; private Context mContext; private SuggestionAdapter mSuggestionAdapter; @@ -108,38 +110,38 @@ public class SuggestionAdapterTest { @Test public void getItemCount_shouldReturnListSize() { - mSuggestionAdapter = new SuggestionAdapter(mContext, mOneSuggestion, - null /* suggestionV2 */, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + mOneSuggestion, null /* suggestionV2 */, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(1); - mSuggestionAdapter = new SuggestionAdapter(mContext, mTwoSuggestions, - null /* suggestionV2 */, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + mTwoSuggestions, null /* suggestionV2 */, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(2); } @Test public void getItemCount_v2_shouldReturnListSize() { - mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, - mOneSuggestionV2, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + null /* suggestions */, mOneSuggestionV2, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(1); - mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, - mTwoSuggestionsV2, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + null /* suggestions */, mTwoSuggestionsV2, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(2); } @Test public void getItemViewType_shouldReturnSuggestionTile() { - mSuggestionAdapter = new SuggestionAdapter(mContext, mOneSuggestion, - null /* suggestionV2 */, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + mOneSuggestion, null /* suggestionV2 */, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemViewType(0)) .isEqualTo(R.layout.suggestion_tile); } @Test public void getItemViewType_v2_shouldReturnSuggestionTile() { - mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, - mOneSuggestionV2, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + null /* suggestions */, mOneSuggestionV2, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemViewType(0)) .isEqualTo(R.layout.suggestion_tile); } @@ -152,8 +154,8 @@ public class SuggestionAdapterTest { .setTitle("123") .setSummary("456") .build()); - mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, - suggestions, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + null /* suggestions */, suggestions, new ArrayList<>()); assertThat(mSuggestionAdapter.getItemViewType(0)) .isEqualTo(R.layout.suggestion_tile_with_button); @@ -164,8 +166,8 @@ public class SuggestionAdapterTest { final View view = spy(LayoutInflater.from(mContext).inflate( R.layout.suggestion_tile, new LinearLayout(mContext), true)); mSuggestionHolder = new DashboardAdapter.DashboardItemHolder(view); - mSuggestionAdapter = new SuggestionAdapter(mContext, mOneSuggestion, - null /* suggestionV2 */, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + mOneSuggestion, null /* suggestionV2 */, new ArrayList<>()); mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0); @@ -177,8 +179,8 @@ public class SuggestionAdapterTest { final View view = spy(LayoutInflater.from(mContext).inflate( R.layout.suggestion_tile, new LinearLayout(mContext), true)); mSuggestionHolder = new DashboardAdapter.DashboardItemHolder(view); - mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestionV1*/, - mOneSuggestionV2, new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin, + null /* suggestionV1*/, mOneSuggestionV2, new ArrayList<>()); // Bind twice mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0); @@ -236,13 +238,14 @@ public class SuggestionAdapterTest { @Test public void onBindViewHolder_v2_itemViewShouldHandleClick() throws PendingIntent.CanceledException { - final List packages = makeSuggestionsV2("pkg1"); - setupSuggestions(mActivity, null /* suggestionV1 */, packages); + final List suggestions = makeSuggestionsV2("pkg1"); + setupSuggestions(mActivity, null /* suggestionV1 */, suggestions); mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0); mSuggestionHolder.itemView.performClick(); - verify(packages.get(0).getPendingIntent()).send(); + verify(mSuggestionControllerMixin).launchSuggestion(suggestions.get(0)); + verify(suggestions.get(0).getPendingIntent()).send(); } @Test @@ -285,8 +288,8 @@ public class SuggestionAdapterTest { private void setupSuggestions(Context context, List suggestions, List suggestionsV2) { - mSuggestionAdapter = new SuggestionAdapter(context, suggestions, suggestionsV2, - new ArrayList<>()); + mSuggestionAdapter = new SuggestionAdapter(context, mSuggestionControllerMixin, + suggestions, suggestionsV2, new ArrayList<>()); mSuggestionHolder = mSuggestionAdapter.onCreateViewHolder( new FrameLayout(RuntimeEnvironment.application), mSuggestionAdapter.getItemViewType(0));