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.settingslib.suggestions.SuggestionControllerMixinCompat; 32 import com.android.tv.settings.R; 33 34 /** 35 * Custom preference for Suggestions. 36 */ 37 public class SuggestionPreference extends Preference { 38 public static final String SUGGESTION_PREFERENCE_KEY = "suggestion_pref_key"; 39 private static final String TAG = "SuggestionPreference"; 40 41 private final Suggestion mSuggestion; 42 private final SuggestionControllerMixinCompat mSuggestionControllerMixin; 43 private String mId; 44 private Callback mCallback; 45 SuggestionPreference(Suggestion suggestion, Context context, SuggestionControllerMixinCompat suggestionControllerMixin, Callback callback)46 public SuggestionPreference(Suggestion suggestion, Context context, 47 SuggestionControllerMixinCompat suggestionControllerMixin, Callback callback) { 48 super(context); 49 setLayoutResource(R.layout.suggestion_item); 50 this.mSuggestionControllerMixin = suggestionControllerMixin; 51 this.mSuggestion = suggestion; 52 this.mId = suggestion.getId(); 53 this.mCallback = callback; 54 setKey(SUGGESTION_PREFERENCE_KEY + mId); 55 } 56 getId()57 public String getId() { 58 return mId; 59 } 60 61 @Override onBindViewHolder(final PreferenceViewHolder holder)62 public void onBindViewHolder(final PreferenceViewHolder holder) { 63 super.onBindViewHolder(holder); 64 65 holder.itemView.setOnClickListener(v -> launchSuggestion()); 66 View containerView = holder.itemView.findViewById(R.id.main_container); 67 if (containerView != null) { 68 containerView.setOnClickListener(v -> launchSuggestion()); 69 } 70 71 // In accessibility mode, item_container get focused instead of main_container, 72 // so we need to add the same listener to item_container. 73 View itemContainerView = holder.itemView.findViewById(R.id.item_container); 74 if (itemContainerView != null) { 75 itemContainerView.setOnClickListener(v -> launchSuggestion()); 76 } 77 78 View dismissButton = holder.itemView.findViewById(R.id.dismiss_button); 79 if (dismissButton != null) { 80 dismissButton.setOnClickListener(v -> { 81 mSuggestionControllerMixin.dismissSuggestion(mSuggestion); 82 if (mCallback != null) { 83 mCallback.onSuggestionClosed(SuggestionPreference.this); 84 } 85 }); 86 } 87 } 88 launchSuggestion()89 private void launchSuggestion() { 90 try { 91 mSuggestion.getPendingIntent().send(); 92 mSuggestionControllerMixin.launchSuggestion(mSuggestion); 93 logEntrySelected(TvSettingsEnums.SUGGESTED_SETTINGS); 94 } catch (PendingIntent.CanceledException e) { 95 Log.w(TAG, "Failed to start suggestion " + mSuggestion.getTitle()); 96 } 97 } 98 99 public interface Callback { 100 /** Called when the dismiss button is clicked **/ onSuggestionClosed(Preference preference)101 void onSuggestionClosed(Preference preference); 102 } 103 } 104