summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/help/HelpFragment.java
blob: 1b43ada88e236eae4fea846c8fe34260af332a76 (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
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.help;

import static android.content.Context.ACCESSIBILITY_SERVICE;

import static com.android.tv.settings.overlay.FlavorUtils.FLAVOR_CLASSIC;
import static com.android.tv.settings.overlay.FlavorUtils.FLAVOR_TWO_PANEL;
import static com.android.tv.settings.overlay.FlavorUtils.FLAVOR_VENDOR;
import static com.android.tv.settings.overlay.FlavorUtils.FLAVOR_X;
import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;

import android.app.tvsettings.TvSettingsEnums;
import android.content.ComponentName;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityManager;

import androidx.annotation.Keep;
import androidx.leanback.widget.VerticalGridView;
import androidx.preference.Preference;

import com.android.settingslib.accessibility.AccessibilityUtils;
import com.android.tv.settings.MainFragment;
import com.android.tv.settings.R;
import com.android.tv.settings.SettingsPreferenceFragment;
import com.android.tv.settings.overlay.FlavorUtils;

import java.util.Set;

/**
 * The "Help & feedback" screen in TV settings.
 */
@Keep
public class HelpFragment extends SettingsPreferenceFragment {

    private static final String KEY_SEND_FEEDBACK = "feedback";
    private static final String KEY_HELP = "help_center";
    private static final String TALKBACK_SERVICE_NAME = ".TalkBackService";

    private int getPreferenceScreenResId() {
        switch (FlavorUtils.getFlavor(getContext())) {
            case FLAVOR_CLASSIC:
            case FLAVOR_TWO_PANEL:
                return R.xml.help_and_feedback;
            case FLAVOR_X:
            case FLAVOR_VENDOR:
                return R.xml.help_and_feedback_x;
            default:
                return R.xml.help_and_feedback;
        }
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(getPreferenceScreenResId(), null);

        final Preference sendFeedbackPref = findPreference(KEY_SEND_FEEDBACK);
        if (sendFeedbackPref != null) {
            sendFeedbackPref.setVisible(!FlavorUtils.getFeatureFactory(getContext())
                    .getBasicModeFeatureProvider().isBasicMode(getContext()));
        }

        final Preference helpPref = findPreference(KEY_HELP);
        if (helpPref != null) {
            final ResolveInfo info = MainFragment.systemIntentIsHandled(getActivity(),
                    helpPref.getIntent());
            helpPref.setVisible(info != null);
        }
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        // Workaround to only allow click when the input focus and a11y focus are on the same item.
        // This should only apply when the TalkBack service is on since other a11y services may not
        // utilize a11y focus (ex. Switch Access).
        VerticalGridView listView = (VerticalGridView) getListView();
        if (!listView.getChildAt(listView.getSelectedPosition()).isAccessibilityFocused()
                && isTalkBackEnabled()) {
            return true;
        }
        switch (preference.getKey()) {
            case KEY_SEND_FEEDBACK:
                logEntrySelected(TvSettingsEnums.FEEDBACK_SEND);
                return super.onPreferenceTreeClick(preference);
            case KEY_HELP:
                FlavorUtils.getFeatureFactory(getActivity()).getSupportFeatureProvider()
                        .startSupport(getActivity());
                return true;
            default:
                return super.onPreferenceTreeClick(preference);
        }
    }


    private boolean isTalkBackEnabled() {
        AccessibilityManager am =
                (AccessibilityManager) getContext().getSystemService(ACCESSIBILITY_SERVICE);
        boolean isAccessibilityEnabled = am.isEnabled();

        if (isAccessibilityEnabled) {
            final Set<ComponentName> enabledServices =
                    AccessibilityUtils.getEnabledServicesFromSettings(getActivity());

            for (final ComponentName componentName : enabledServices) {
                if (TextUtils.equals(componentName.getShortClassName(), TALKBACK_SERVICE_NAME)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    protected int getPageId() {
        return TvSettingsEnums.FEEDBACK;
    }
}