Call SuggestionService.launch when click suggestion.

Bug: 68267490
Test: robotests
Change-Id: I8614f256fc1a87395b5c9d41e3621164a2ded5e1
This commit is contained in:
Fan Zhang
2017-10-20 10:23:48 -07:00
parent f2b3825a09
commit 8052ac0b36
5 changed files with 52 additions and 29 deletions

View File

@@ -430,16 +430,16 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
if (position == SUGGESTION_CONDITION_HEADER_POSITION) { if (position == SUGGESTION_CONDITION_HEADER_POSITION) {
if (suggestions != null && suggestions.size() > 0) { if (suggestions != null && suggestions.size() > 0) {
conditionOnly = false; conditionOnly = false;
mSuggestionAdapter = new SuggestionAdapter(mContext, (List<Tile>) mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
mDashboardData.getItemEntityByPosition(position), (List<Tile>) mDashboardData.getItemEntityByPosition(position),
null, mSuggestionsShownLogged); null, mSuggestionsShownLogged);
mSuggestionDismissHandler = new SuggestionDismissController(mContext, mSuggestionDismissHandler = new SuggestionDismissController(mContext,
holder.data, mSuggestionControllerMixin, mSuggestionParser, mCallback); holder.data, mSuggestionControllerMixin, mSuggestionParser, mCallback);
holder.data.setAdapter(mSuggestionAdapter); holder.data.setAdapter(mSuggestionAdapter);
} else if (suggestionsV2 != null && suggestionsV2.size() > 0) { } else if (suggestionsV2 != null && suggestionsV2.size() > 0) {
conditionOnly = false; conditionOnly = false;
mSuggestionAdapter = new SuggestionAdapter(mContext, null, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
(List<Suggestion>) mDashboardData.getItemEntityByPosition(position), null, (List<Suggestion>) mDashboardData.getItemEntityByPosition(position),
mSuggestionsShownLogged); mSuggestionsShownLogged);
mSuggestionDismissHandler = new SuggestionDismissController(mContext, mSuggestionDismissHandler = new SuggestionDismissController(mContext,
holder.data, mSuggestionControllerMixin, null /* parser */, mCallback); holder.data, mSuggestionControllerMixin, null /* parser */, mCallback);

View File

@@ -48,11 +48,13 @@ public class SuggestionAdapter extends RecyclerView.Adapter<DashboardItemHolder>
private final List<Suggestion> mSuggestionsV2; private final List<Suggestion> mSuggestionsV2;
private final IconCache mCache; private final IconCache mCache;
private final List<String> mSuggestionsShownLogged; private final List<String> mSuggestionsShownLogged;
private final SuggestionControllerMixin mSuggestionControllerMixin;
public SuggestionAdapter(Context context, List<Tile> suggestions, public SuggestionAdapter(Context context, SuggestionControllerMixin suggestionControllerMixin,
List<Suggestion> suggestionsV2, List<Tile> suggestions, List<Suggestion> suggestionsV2,
List<String> suggestionsShownLogged) { List<String> suggestionsShownLogged) {
mContext = context; mContext = context;
mSuggestionControllerMixin = suggestionControllerMixin;
mSuggestions = suggestions; mSuggestions = suggestions;
mSuggestionsV2 = suggestionsV2; mSuggestionsV2 = suggestionsV2;
mSuggestionsShownLogged = suggestionsShownLogged; mSuggestionsShownLogged = suggestionsShownLogged;
@@ -112,6 +114,7 @@ public class SuggestionAdapter extends RecyclerView.Adapter<DashboardItemHolder>
mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_SETTINGS_SUGGESTION, id); mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_SETTINGS_SUGGESTION, id);
try { try {
suggestion.getPendingIntent().send(); suggestion.getPendingIntent().send();
mSuggestionControllerMixin.launchSuggestion(suggestion);
} catch (PendingIntent.CanceledException e) { } catch (PendingIntent.CanceledException e) {
Log.w(TAG, "Failed to start suggestion " + suggestion.getTitle()); Log.w(TAG, "Failed to start suggestion " + suggestion.getTitle());
} }

View File

@@ -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 * Whether or not the manager is ready
*/ */

View File

@@ -115,4 +115,8 @@ public class SuggestionControllerMixin implements SuggestionController.ServiceCo
public void dismissSuggestion(Suggestion suggestion) { public void dismissSuggestion(Suggestion suggestion) {
mSuggestionController.dismissSuggestions(suggestion); mSuggestionController.dismissSuggestions(suggestion);
} }
public void launchSuggestion(Suggestion suggestion) {
mSuggestionController.launchSuggestion(suggestion);
}
} }

View File

@@ -67,6 +67,8 @@ public class SuggestionAdapterTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private SettingsActivity mActivity; private SettingsActivity mActivity;
@Mock
private SuggestionControllerMixin mSuggestionControllerMixin;
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
private Context mContext; private Context mContext;
private SuggestionAdapter mSuggestionAdapter; private SuggestionAdapter mSuggestionAdapter;
@@ -108,38 +110,38 @@ public class SuggestionAdapterTest {
@Test @Test
public void getItemCount_shouldReturnListSize() { public void getItemCount_shouldReturnListSize() {
mSuggestionAdapter = new SuggestionAdapter(mContext, mOneSuggestion, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
null /* suggestionV2 */, new ArrayList<>()); mOneSuggestion, null /* suggestionV2 */, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(1); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(1);
mSuggestionAdapter = new SuggestionAdapter(mContext, mTwoSuggestions, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
null /* suggestionV2 */, new ArrayList<>()); mTwoSuggestions, null /* suggestionV2 */, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(2); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(2);
} }
@Test @Test
public void getItemCount_v2_shouldReturnListSize() { public void getItemCount_v2_shouldReturnListSize() {
mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
mOneSuggestionV2, new ArrayList<>()); null /* suggestions */, mOneSuggestionV2, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(1); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(1);
mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
mTwoSuggestionsV2, new ArrayList<>()); null /* suggestions */, mTwoSuggestionsV2, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(2); assertThat(mSuggestionAdapter.getItemCount()).isEqualTo(2);
} }
@Test @Test
public void getItemViewType_shouldReturnSuggestionTile() { public void getItemViewType_shouldReturnSuggestionTile() {
mSuggestionAdapter = new SuggestionAdapter(mContext, mOneSuggestion, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
null /* suggestionV2 */, new ArrayList<>()); mOneSuggestion, null /* suggestionV2 */, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemViewType(0)) assertThat(mSuggestionAdapter.getItemViewType(0))
.isEqualTo(R.layout.suggestion_tile); .isEqualTo(R.layout.suggestion_tile);
} }
@Test @Test
public void getItemViewType_v2_shouldReturnSuggestionTile() { public void getItemViewType_v2_shouldReturnSuggestionTile() {
mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
mOneSuggestionV2, new ArrayList<>()); null /* suggestions */, mOneSuggestionV2, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemViewType(0)) assertThat(mSuggestionAdapter.getItemViewType(0))
.isEqualTo(R.layout.suggestion_tile); .isEqualTo(R.layout.suggestion_tile);
} }
@@ -152,8 +154,8 @@ public class SuggestionAdapterTest {
.setTitle("123") .setTitle("123")
.setSummary("456") .setSummary("456")
.build()); .build());
mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestions */, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
suggestions, new ArrayList<>()); null /* suggestions */, suggestions, new ArrayList<>());
assertThat(mSuggestionAdapter.getItemViewType(0)) assertThat(mSuggestionAdapter.getItemViewType(0))
.isEqualTo(R.layout.suggestion_tile_with_button); .isEqualTo(R.layout.suggestion_tile_with_button);
@@ -164,8 +166,8 @@ public class SuggestionAdapterTest {
final View view = spy(LayoutInflater.from(mContext).inflate( final View view = spy(LayoutInflater.from(mContext).inflate(
R.layout.suggestion_tile, new LinearLayout(mContext), true)); R.layout.suggestion_tile, new LinearLayout(mContext), true));
mSuggestionHolder = new DashboardAdapter.DashboardItemHolder(view); mSuggestionHolder = new DashboardAdapter.DashboardItemHolder(view);
mSuggestionAdapter = new SuggestionAdapter(mContext, mOneSuggestion, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
null /* suggestionV2 */, new ArrayList<>()); mOneSuggestion, null /* suggestionV2 */, new ArrayList<>());
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0); mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
@@ -177,8 +179,8 @@ public class SuggestionAdapterTest {
final View view = spy(LayoutInflater.from(mContext).inflate( final View view = spy(LayoutInflater.from(mContext).inflate(
R.layout.suggestion_tile, new LinearLayout(mContext), true)); R.layout.suggestion_tile, new LinearLayout(mContext), true));
mSuggestionHolder = new DashboardAdapter.DashboardItemHolder(view); mSuggestionHolder = new DashboardAdapter.DashboardItemHolder(view);
mSuggestionAdapter = new SuggestionAdapter(mContext, null /* suggestionV1*/, mSuggestionAdapter = new SuggestionAdapter(mContext, mSuggestionControllerMixin,
mOneSuggestionV2, new ArrayList<>()); null /* suggestionV1*/, mOneSuggestionV2, new ArrayList<>());
// Bind twice // Bind twice
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0); mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
@@ -236,13 +238,14 @@ public class SuggestionAdapterTest {
@Test @Test
public void onBindViewHolder_v2_itemViewShouldHandleClick() public void onBindViewHolder_v2_itemViewShouldHandleClick()
throws PendingIntent.CanceledException { throws PendingIntent.CanceledException {
final List<Suggestion> packages = makeSuggestionsV2("pkg1"); final List<Suggestion> suggestions = makeSuggestionsV2("pkg1");
setupSuggestions(mActivity, null /* suggestionV1 */, packages); setupSuggestions(mActivity, null /* suggestionV1 */, suggestions);
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0); mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
mSuggestionHolder.itemView.performClick(); mSuggestionHolder.itemView.performClick();
verify(packages.get(0).getPendingIntent()).send(); verify(mSuggestionControllerMixin).launchSuggestion(suggestions.get(0));
verify(suggestions.get(0).getPendingIntent()).send();
} }
@Test @Test
@@ -285,8 +288,8 @@ public class SuggestionAdapterTest {
private void setupSuggestions(Context context, List<Tile> suggestions, private void setupSuggestions(Context context, List<Tile> suggestions,
List<Suggestion> suggestionsV2) { List<Suggestion> suggestionsV2) {
mSuggestionAdapter = new SuggestionAdapter(context, suggestions, suggestionsV2, mSuggestionAdapter = new SuggestionAdapter(context, mSuggestionControllerMixin,
new ArrayList<>()); suggestions, suggestionsV2, new ArrayList<>());
mSuggestionHolder = mSuggestionAdapter.onCreateViewHolder( mSuggestionHolder = mSuggestionAdapter.onCreateViewHolder(
new FrameLayout(RuntimeEnvironment.application), new FrameLayout(RuntimeEnvironment.application),
mSuggestionAdapter.getItemViewType(0)); mSuggestionAdapter.getItemViewType(0));