summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/SuggestionQuickSettingPrefsContainer.java
blob: 2841e8608a35da47c820b08de379e47fbe60b912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.android.tv.settings;

import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;

import android.app.tvsettings.TvSettingsEnums;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.service.settings.suggestions.Suggestion;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;

import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.suggestions.SuggestionControllerMixinCompat;
import com.android.settingslib.utils.IconCache;
import com.android.tv.settings.R;
import com.android.tv.settings.overlay.FlavorUtils;
import com.android.tv.settings.suggestions.SuggestionPreference;
import com.android.tv.settings.system.SecurityFragment;
import com.android.tv.settings.widget.SwitchWithSoundPreference;

import java.util.ArrayList;
import java.util.List;

public class SuggestionQuickSettingPrefsContainer  {
    @VisibleForTesting static final String KEY_QUICK_SETTINGS = "quick_settings";
    private static final String TAG = "SuggestionQuickSettingPrefsContainer";
    private static final String KEY_SUGGESTIONS_LIST = "suggestions";
    @VisibleForTesting PreferenceCategory mSuggestionsList;
    IconCache mIconCache;
    private SuggestionControllerMixinCompat mSuggestionControllerMixin;
    /** Controllers for the Quick Settings section. */
    private List<AbstractPreferenceController> mPreferenceControllers;

    private HotwordSwitchController mHotwordSwitchController;
    private TakeBugReportController mTakeBugReportController;
    private PreferenceCategory mQuickSettingsList;
    private SwitchWithSoundPreference mHotwordSwitch;
    private Preference mTakeBugReportPreference;
    private MainFragment mainFragment;

    SuggestionQuickSettingPrefsContainer(MainFragment mainFragment) {
        this.mainFragment = mainFragment;
    }

    void onCreate() {
        mIconCache = new IconCache(mainFragment.getContext());
        if (!isRestricted()) {
            ComponentName componentName = new ComponentName("com.android.settings.intelligence",
                    "com.android.settings.intelligence.suggestions.SuggestionService");
            mSuggestionControllerMixin =
                    new SuggestionControllerMixinCompat(mainFragment.getContext(),
                    mainFragment, mainFragment.getSettingsLifecycle(), componentName);
        }
    }

    void onDestroy() {
        if (mHotwordSwitchController != null) {
            mHotwordSwitchController.unregister();
        }
    }

    void showOrHideQuickSettings() {
        if (shouldShowQuickSettings()) {
            showQuickSettings();
        } else {
            hideQuickSettings();
        }
    }

    void onCreatePreferences() {
        if (mHotwordSwitchController != null) {
            mHotwordSwitchController.init(mainFragment);
        }
    }

    void onSuggestionReady(List<Suggestion> data) {
        // Suggestion category is handled differently in basic mode
        if (data == null || data.size() == 0
                || FlavorUtils.getFeatureFactory(mainFragment.getContext())
                .getBasicModeFeatureProvider()
                .isBasicMode(mainFragment.getContext())) {
            if (mSuggestionsList != null) {
                mainFragment.getPreferenceScreen().removePreference(mSuggestionsList);
                mSuggestionsList = null;
            }
            return;
        }

        if (mSuggestionsList == null) {
            mSuggestionsList = new PreferenceCategory(
                    mainFragment.getPreferenceManager().getContext());
            mSuggestionsList.setKey(KEY_SUGGESTIONS_LIST);
            mSuggestionsList.setTitle(R.string.header_category_suggestions);
            mSuggestionsList.setOrder(0); // always at top
            mainFragment.getPreferenceScreen().addPreference(mSuggestionsList);
        }
        updateSuggestionList(data);
    }

    @VisibleForTesting
    void updateSuggestionList(List<Suggestion> suggestions) {
        // Remove suggestions that are not in the new list.
        for (int i = 0; i < mSuggestionsList.getPreferenceCount(); i++) {
            SuggestionPreference pref = (SuggestionPreference) mSuggestionsList.getPreference(i);
            boolean isInNewSuggestionList = false;
            for (Suggestion suggestion : suggestions) {
                if (pref.getId().equals(suggestion.getId())) {
                    isInNewSuggestionList = true;
                    break;
                }
            }
            if (!isInNewSuggestionList) {
                mSuggestionsList.removePreference(pref);
            }
        }

        // Add suggestions that are not in the old list and update the existing suggestions.
        for (Suggestion suggestion : suggestions) {
            Preference curPref = mainFragment.findPreference(
                    SuggestionPreference.SUGGESTION_PREFERENCE_KEY + suggestion.getId());
            if (curPref == null) {
                SuggestionPreference newSuggPref =
                        new SuggestionPreference(suggestion,
                                mainFragment.getPreferenceManager().getContext(),
                                mSuggestionControllerMixin, mainFragment);
                newSuggPref.setIcon(mIconCache.getIcon(suggestion.getIcon()));
                newSuggPref.setTitle(suggestion.getTitle());
                newSuggPref.setSummary(suggestion.getSummary());
                mSuggestionsList.addPreference(newSuggPref);
            } else {
                // Even though the id of suggestion might not change, the details could change.
                // So we need to update icon, title and summary for the suggestions.
                curPref.setIcon(mIconCache.getIcon(suggestion.getIcon()));
                curPref.setTitle(suggestion.getTitle());
                curPref.setSummary(suggestion.getSummary());
            }
        }
    }

    boolean isRestricted() {
        return SecurityFragment.isRestrictedProfileInEffect(mainFragment.getContext());
    }

    void onSuggestionClosed(Preference preference) {
        if (mSuggestionsList == null || mSuggestionsList.getPreferenceCount() == 0) {
            return;
        } else if (mSuggestionsList.getPreferenceCount() == 1) {
            mainFragment.getPreferenceScreen().removePreference(mSuggestionsList);
        } else {
            mSuggestionsList.removePreference(preference);
        }
    }

    void onHotwordStateChanged() {
        if (mHotwordSwitch != null) {
            mHotwordSwitchController.updateState(mHotwordSwitch);
        }
        showOrHideQuickSettings();
    }

    void onHotwordEnable() {
        try {
            Intent intent = new Intent(HotwordSwitchController.ACTION_HOTWORD_ENABLE);
            intent.setPackage(HotwordSwitchController.ASSISTANT_PGK_NAME);
            mainFragment.startActivityForResult(intent, 0);
        } catch (ActivityNotFoundException e) {
            Log.w(TAG, "Unable to find hotwording activity.", e);
        }
    }

    void onHotwordDisable() {
        try {
            Intent intent = new Intent(HotwordSwitchController.ACTION_HOTWORD_DISABLE);
            intent.setPackage(HotwordSwitchController.ASSISTANT_PGK_NAME);
            mainFragment.startActivityForResult(intent, 0);
        } catch (ActivityNotFoundException e) {
            Log.w(TAG, "Unable to find hotwording activity.", e);
        }
    }

    private boolean quickSettingsEnabled() {
        return mainFragment.getContext().getResources().getBoolean(
                R.bool.config_quick_settings_enabled);
    }

    /** @return true if there is at least one available item in quick settings. */
    private boolean shouldShowQuickSettings() {
        for (AbstractPreferenceController controller : mPreferenceControllers) {
            if (controller.isAvailable()) {
                return true;
            }
        }
        return false;
    }


    /** Creates the quick settings category and its children. */
    private void showQuickSettings() {
        if (mQuickSettingsList != null) {
            return;
        }
        mQuickSettingsList = new PreferenceCategory(
                mainFragment.getPreferenceManager().getContext());
        mQuickSettingsList.setKey(KEY_QUICK_SETTINGS);
        mQuickSettingsList.setTitle(R.string.header_category_quick_settings);
        mQuickSettingsList.setOrder(1); // at top, but below suggested settings
        mainFragment.getPreferenceScreen().addPreference(mQuickSettingsList);
        if (mHotwordSwitchController != null && mHotwordSwitchController.isAvailable()) {
            mHotwordSwitch = new SwitchWithSoundPreference(
                    mainFragment.getPreferenceManager().getContext());
            mHotwordSwitch.setKey(HotwordSwitchController.KEY_HOTWORD_SWITCH);
            mHotwordSwitch.setOnPreferenceClickListener(preference -> {
                logEntrySelected(TvSettingsEnums.QUICK_SETTINGS);
                return false;
            });
            mHotwordSwitchController.updateState(mHotwordSwitch);
            mQuickSettingsList.addPreference(mHotwordSwitch);
        }
        if (mTakeBugReportController != null && mTakeBugReportController.isAvailable()) {
            mTakeBugReportPreference = new Preference(
                    mainFragment.getPreferenceManager().getContext());
            mTakeBugReportPreference.setKey(TakeBugReportController.KEY_TAKE_BUG_REPORT);
            mTakeBugReportPreference.setOnPreferenceClickListener(preference -> {
                logEntrySelected(TvSettingsEnums.QUICK_SETTINGS);
                return false;
            });
            mTakeBugReportController.updateState(mTakeBugReportPreference);
            mQuickSettingsList.addPreference(mTakeBugReportPreference);
        }
    }

    /** Removes the quick settings category and all its children. */
    private void hideQuickSettings() {
        Preference quickSettingsPref = mainFragment.findPreference(KEY_QUICK_SETTINGS);
        if (quickSettingsPref == null) {
            return;
        }
        mQuickSettingsList.removeAll();
        mainFragment.getPreferenceScreen().removePreference(mQuickSettingsList);
        mQuickSettingsList = null;
    }

    List<AbstractPreferenceController> onCreatePreferenceControllers(Context context) {
        mPreferenceControllers = new ArrayList<>(2);
        if (quickSettingsEnabled()) {
            mHotwordSwitchController = new HotwordSwitchController(context);
            mTakeBugReportController = new TakeBugReportController(context);
            mPreferenceControllers.add(mHotwordSwitchController);
            mPreferenceControllers.add(mTakeBugReportController);
        }
        return mPreferenceControllers;
    }

}