• 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 android.app.PendingIntent;
20 import android.content.Context;
21 import android.service.settings.suggestions.Suggestion;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceViewHolder;
24 import android.util.Log;
25 import android.view.View;
26 
27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
29 import com.android.settingslib.suggestions.SuggestionControllerMixin;
30 import com.android.tv.settings.R;
31 
32 /**
33  * Custom preference for Suggestions.
34  */
35 public class SuggestionPreference extends Preference {
36     public static final String SUGGESTION_PREFERENCE_KEY = "suggestion_pref_key";
37     private static final String TAG = "SuggestionPreference";
38     private final MetricsFeatureProvider mMetricsFeatureProvider =
39             new MetricsFeatureProvider();
40 
41     private final Suggestion mSuggestion;
42     private final SuggestionControllerMixin mSuggestionControllerMixin;
43     private String mId;
44     private Callback mCallback;
45 
SuggestionPreference(Suggestion suggestion, Context context, SuggestionControllerMixin suggestionControllerMixin, Callback callback)46     public SuggestionPreference(Suggestion suggestion, Context context,
47             SuggestionControllerMixin 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         View containerView = holder.itemView.findViewById(R.id.main_container);
66         containerView.setOnClickListener(v -> launchSuggestion());
67 
68         // In accessibility mode, item_container get focused instead of main_container,
69         // so we need to add the same listener to item_container.
70         View itemContainerView = holder.itemView.findViewById(R.id.item_container);
71         itemContainerView.setOnClickListener(v -> launchSuggestion());
72 
73         View dismissButton = holder.itemView.findViewById(R.id.dismiss_button);
74         dismissButton.setOnClickListener(v -> {
75             mSuggestionControllerMixin.dismissSuggestion(mSuggestion);
76             if (mCallback != null) {
77                 mCallback.onSuggestionClosed(SuggestionPreference.this);
78             }
79         });
80 
81         mMetricsFeatureProvider.action(getContext(), MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
82                 mId);
83     }
84 
launchSuggestion()85     private void launchSuggestion() {
86         try {
87             mSuggestion.getPendingIntent().send();
88             mSuggestionControllerMixin.launchSuggestion(mSuggestion);
89             mMetricsFeatureProvider.action(getContext(),
90                     MetricsEvent.ACTION_SETTINGS_SUGGESTION, mId);
91         } catch (PendingIntent.CanceledException e) {
92             Log.w(TAG, "Failed to start suggestion " + mSuggestion.getTitle());
93         }
94     }
95 
96     public interface Callback {
97         /** Called when the dismiss button is clicked **/
onSuggestionClosed(Preference preference)98         void onSuggestionClosed(Preference preference);
99     }
100 }
101