1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.settings.suggestions; 18 19 import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected; 20 21 import android.app.PendingIntent; 22 import android.app.tvsettings.TvSettingsEnums; 23 import android.content.Context; 24 import android.service.settings.suggestions.Suggestion; 25 import android.util.Log; 26 import android.view.View; 27 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceViewHolder; 30 31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 32 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 33 import com.android.settingslib.suggestions.SuggestionControllerMixin; 34 import com.android.tv.settings.R; 35 36 /** 37 * Custom preference for Suggestions. 38 */ 39 public class SuggestionPreference extends Preference { 40 public static final String SUGGESTION_PREFERENCE_KEY = "suggestion_pref_key"; 41 private static final String TAG = "SuggestionPreference"; 42 private final MetricsFeatureProvider mMetricsFeatureProvider = 43 new MetricsFeatureProvider(); 44 45 private final Suggestion mSuggestion; 46 private final SuggestionControllerMixin mSuggestionControllerMixin; 47 private String mId; 48 private Callback mCallback; 49 SuggestionPreference(Suggestion suggestion, Context context, SuggestionControllerMixin suggestionControllerMixin, Callback callback)50 public SuggestionPreference(Suggestion suggestion, Context context, 51 SuggestionControllerMixin suggestionControllerMixin, Callback callback) { 52 super(context); 53 setLayoutResource(R.layout.suggestion_item); 54 this.mSuggestionControllerMixin = suggestionControllerMixin; 55 this.mSuggestion = suggestion; 56 this.mId = suggestion.getId(); 57 this.mCallback = callback; 58 setKey(SUGGESTION_PREFERENCE_KEY + mId); 59 } 60 getId()61 public String getId() { 62 return mId; 63 } 64 65 @Override onBindViewHolder(final PreferenceViewHolder holder)66 public void onBindViewHolder(final PreferenceViewHolder holder) { 67 super.onBindViewHolder(holder); 68 69 holder.itemView.setOnClickListener(v -> launchSuggestion()); 70 View containerView = holder.itemView.findViewById(R.id.main_container); 71 if (containerView != null) { 72 containerView.setOnClickListener(v -> launchSuggestion()); 73 } 74 75 // In accessibility mode, item_container get focused instead of main_container, 76 // so we need to add the same listener to item_container. 77 View itemContainerView = holder.itemView.findViewById(R.id.item_container); 78 if (itemContainerView != null) { 79 itemContainerView.setOnClickListener(v -> launchSuggestion()); 80 } 81 82 View dismissButton = holder.itemView.findViewById(R.id.dismiss_button); 83 if (dismissButton != null) { 84 dismissButton.setOnClickListener(v -> { 85 mSuggestionControllerMixin.dismissSuggestion(mSuggestion); 86 if (mCallback != null) { 87 mCallback.onSuggestionClosed(SuggestionPreference.this); 88 } 89 }); 90 } 91 92 mMetricsFeatureProvider.action(getContext(), MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, 93 mId); 94 } 95 launchSuggestion()96 private void launchSuggestion() { 97 try { 98 mSuggestion.getPendingIntent().send(); 99 mSuggestionControllerMixin.launchSuggestion(mSuggestion); 100 logEntrySelected(TvSettingsEnums.SUGGESTED_SETTINGS); 101 mMetricsFeatureProvider.action(getContext(), 102 MetricsEvent.ACTION_SETTINGS_SUGGESTION, mId); 103 } catch (PendingIntent.CanceledException e) { 104 Log.w(TAG, "Failed to start suggestion " + mSuggestion.getTitle()); 105 } 106 } 107 108 public interface Callback { 109 /** Called when the dismiss button is clicked **/ onSuggestionClosed(Preference preference)110 void onSuggestionClosed(Preference preference); 111 } 112 } 113