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