• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.suggestions;
18 
19 import android.content.Context;
20 import android.service.settings.suggestions.Suggestion;
21 import android.view.View;
22 
23 import androidx.preference.PreferenceViewHolder;
24 
25 import com.android.car.settings.R;
26 import com.android.car.ui.preference.CarUiPreference;
27 
28 import java.util.Objects;
29 
30 /**
31  * Custom preference for Suggestions.
32  */
33 public class SuggestionPreference extends CarUiPreference {
34 
35     public static final String SUGGESTION_PREFERENCE_KEY = "suggestion_pref_key";
36 
37     /**
38      * Callback for actions performed on a suggestion preference.
39      */
40     public interface Callback {
41         /** Called when the suggestion should be launched. */
launchSuggestion(SuggestionPreference preference)42         void launchSuggestion(SuggestionPreference preference);
43 
44         /** Called when the suggestion should be dismissed. */
dismissSuggestion(SuggestionPreference preference)45         void dismissSuggestion(SuggestionPreference preference);
46     }
47 
48     private final Callback mCallback;
49     private Suggestion mSuggestion;
50 
SuggestionPreference(Context context, Suggestion suggestion, Callback callback)51     public SuggestionPreference(Context context, Suggestion suggestion, Callback callback) {
52         super(context);
53         setLayoutResource(R.layout.suggestion_preference);
54         mCallback = callback;
55         setKey(SUGGESTION_PREFERENCE_KEY + suggestion.getId());
56         updateSuggestion(suggestion);
57     }
58 
59     /**
60      * Returns the {@link Suggestion} represented by this preference.
61      */
getSuggestion()62     public Suggestion getSuggestion() {
63         return mSuggestion;
64     }
65 
66     /**
67      * Updates the icon, title, and summary of the preference with those of the given
68      * {@link Suggestion}.
69      *
70      * @param suggestion the updated suggestion to represent
71      * @throws IllegalArgumentException if the given suggestion has a different id than the
72      *         suggestion passed to the constructor
73      */
updateSuggestion(Suggestion suggestion)74     public void updateSuggestion(Suggestion suggestion) {
75         if (mSuggestion != null && !Objects.equals(mSuggestion.getId(), suggestion.getId())) {
76             throw new IllegalArgumentException(
77                     "Suggestion preference update must have a matching id");
78         }
79         mSuggestion = suggestion;
80         setIcon((suggestion.getIcon() != null) ? suggestion.getIcon().loadDrawable(getContext())
81                 : null);
82         setTitle(suggestion.getTitle());
83         setSummary(suggestion.getSummary());
84     }
85 
86     @Override
onBindViewHolder(final PreferenceViewHolder holder)87     public void onBindViewHolder(final PreferenceViewHolder holder) {
88         super.onBindViewHolder(holder);
89 
90         holder.itemView.setOnClickListener(v -> mCallback.launchSuggestion(this));
91 
92         View dismissButton = holder.findViewById(R.id.dismiss_button);
93         dismissButton.setOnClickListener(v -> mCallback.dismissSuggestion(this));
94     }
95 }
96