summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/SettingsPreferenceFragment.java
blob: 3a6d0ebb6774cb4643119f74f5bcf2cd564c3316 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (C) 2017 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;

import static androidx.lifecycle.Lifecycle.Event.ON_CREATE;
import static androidx.lifecycle.Lifecycle.Event.ON_DESTROY;
import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;

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

import android.animation.AnimatorInflater;
import android.annotation.CallSuper;
import android.app.tvsettings.TvSettingsEnums;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.leanback.preference.LeanbackPreferenceFragmentCompat;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceGroupAdapter;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder;
import androidx.recyclerview.widget.RecyclerView;

import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.tv.settings.library.overlay.FlavorUtils;
import com.android.tv.settings.util.SettingsPreferenceUtil;
import com.android.tv.settings.widget.SettingsViewModel;
import com.android.tv.settings.widget.TsPreference;
import com.android.tv.twopanelsettings.TwoPanelSettingsFragment;

import java.util.Collections;

/**
 * A {@link LeanbackPreferenceFragmentCompat} that has hooks to observe fragment lifecycle events
 * and allow for instrumentation.
 */
public abstract class SettingsPreferenceFragment extends LeanbackPreferenceFragmentCompat
        implements LifecycleOwner,
        TwoPanelSettingsFragment.PreviewableComponentCallback {
    private final Lifecycle mLifecycle = new Lifecycle(this);

    // Rename getLifecycle() to getSettingsLifecycle() as androidx Fragment has already implemented
    // getLifecycle(), overriding here would cause unexpected crash in framework.
    @NonNull
    public Lifecycle getSettingsLifecycle() {
        return mLifecycle;
    }

    public SettingsPreferenceFragment() {
    }

    @CallSuper
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mLifecycle.onAttach(context);
    }

    @CallSuper
    @Override
    public void onCreate(Bundle savedInstanceState) {
        mLifecycle.onCreate(savedInstanceState);
        mLifecycle.handleLifecycleEvent(ON_CREATE);
        super.onCreate(savedInstanceState);
        if (getCallbackFragment() != null
                && !(getCallbackFragment() instanceof TwoPanelSettingsFragment)) {
            logPageFocused(getPageId(), true);
        }
    }

    // While the default of relying on text language to determine gravity works well in general,
    // some page titles (e.g., SSID as Wifi details page title) are dynamic and can be in different
    // languages. This can cause some complex gravity issues. For example, Wifi details page in RTL
    // showing an English SSID title would by default align the title to the left, which is
    // incorrectly considered as START in RTL.
    // We explicitly set the title gravity to RIGHT in RTL cases to remedy this issue.
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Set list view listeners after the fragment view is created
        if (getCallbackFragment() instanceof TwoPanelSettingsFragment) {
            TwoPanelSettingsFragment parentFragment =
                    (TwoPanelSettingsFragment) getCallbackFragment();
            parentFragment.addListenerForFragment(this);
        }
        if (view != null) {
            TextView titleView = view.findViewById(R.id.decor_title);
            // We rely on getResources().getConfiguration().getLayoutDirection() instead of
            // view.isLayoutRtl() as the latter could return false in some complex scenarios even if
            // it is RTL.
            if (titleView != null
                    && getResources().getConfiguration().getLayoutDirection()
                    == View.LAYOUT_DIRECTION_RTL) {
                titleView.setGravity(Gravity.RIGHT);
            }
            if (FlavorUtils.isTwoPanel(getContext())) {
                ViewGroup decor = view.findViewById(R.id.decor_title_container);
                if (decor != null) {
                    decor.setOutlineProvider(null);
                }
            } else {
                // We only want to set the title in this location for one-panel settings.
                // TwoPanelSettings behavior is handled moveToPanel in TwoPanelSettingsFragment
                // since we only want the active/main panel to announce its title.
                // For some reason, setAccessibiltyPaneTitle interferes with the initial a11y focus
                // of this screen.
                if (getActivity().getWindow() != null) {
                    getActivity().getWindow().setTitle(getPreferenceScreen().getTitle());
                    view.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
                }

                // Only the one-panel settings should be set as unrestricted keep-clear areas
                // because they are a side panel, so the PiP can be moved next to it.
                view.addOnLayoutChangeListener((v, l, t, r, b, oldL, oldT, oldR, oldB) -> {
                    view.setUnrestrictedPreferKeepClearRects(
                            Collections.singletonList(new Rect(0, 0, r - l, b - t)));
                });

            }
            removeAnimationClipping(view);
        }
        SettingsViewModel settingsViewModel = new ViewModelProvider(this.getActivity(),
                ViewModelProvider.AndroidViewModelFactory.getInstance(
                        this.getActivity().getApplication())).get(SettingsViewModel.class);
        iteratePreferenceAndSetObserver(settingsViewModel, getPreferenceScreen());
    }

    private void iteratePreferenceAndSetObserver(SettingsViewModel viewModel,
            PreferenceGroup preferenceGroup) {
        for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
            Preference pref = preferenceGroup.getPreference(i);
            if (pref instanceof TsPreference
                    && ((TsPreference) pref).updatableFromGoogleSettings()) {
                viewModel.getVisibilityLiveData(
                        SettingsPreferenceUtil.getCompoundKey(this, pref))
                        .observe(getViewLifecycleOwner(), (Boolean b) -> pref.setVisible(b));
            }
            if (pref instanceof PreferenceGroup) {
                iteratePreferenceAndSetObserver(viewModel, (PreferenceGroup) pref);
            }
        }
    }

    protected void removeAnimationClipping(View v) {
        if (v instanceof ViewGroup) {
            ((ViewGroup) v).setClipChildren(false);
            ((ViewGroup) v).setClipToPadding(false);
            for (int index = 0; index < ((ViewGroup) v).getChildCount(); index++) {
                View child = ((ViewGroup) v).getChildAt(index);
                removeAnimationClipping(child);
            }
        }
    }

    @Override
    protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
        return new PreferenceGroupAdapter(preferenceScreen) {
            @Override
            @NonNull
            public PreferenceViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
                    int viewType) {
                PreferenceViewHolder vh = super.onCreateViewHolder(parent, viewType);
                if (FlavorUtils.isTwoPanel(getContext())) {
                    vh.itemView.setStateListAnimator(AnimatorInflater.loadStateListAnimator(
                            getContext(), R.animator.preference));
                }
                vh.itemView.setOnTouchListener((v, e) -> {
                    if (e.getActionMasked() == MotionEvent.ACTION_DOWN) {
                        vh.itemView.requestFocus();
                        v.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
                                KeyEvent.KEYCODE_DPAD_CENTER));
                        return true;
                    } else if (e.getActionMasked() == MotionEvent.ACTION_UP) {
                        v.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
                                KeyEvent.KEYCODE_DPAD_CENTER));
                        return true;
                    }
                    return false;
                });
                vh.itemView.setFocusable(true);
                vh.itemView.setFocusableInTouchMode(true);
                return vh;
            }
        };
    }

    @Override
    public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
        mLifecycle.setPreferenceScreen(preferenceScreen);
        super.setPreferenceScreen(preferenceScreen);
    }

    @CallSuper
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mLifecycle.onSaveInstanceState(outState);
    }

    @CallSuper
    @Override
    public void onStart() {
        mLifecycle.handleLifecycleEvent(ON_START);
        super.onStart();
    }

    @CallSuper
    @Override
    public void onResume() {
        super.onResume();
        mLifecycle.handleLifecycleEvent(ON_RESUME);
    }

    // This should only be invoked if the parent Fragment is TwoPanelSettingsFragment.
    @CallSuper
    @Override
    public void onArriveAtMainPanel(boolean forward) {
        logPageFocused(getPageId(), forward);
    }

    @CallSuper
    @Override
    public void onPause() {
        mLifecycle.handleLifecycleEvent(ON_PAUSE);
        super.onPause();
    }

    @CallSuper
    @Override
    public void onStop() {
        mLifecycle.handleLifecycleEvent(ON_STOP);
        super.onStop();
    }

    @CallSuper
    @Override
    public void onDestroy() {
        mLifecycle.handleLifecycleEvent(ON_DESTROY);
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (getCallbackFragment() instanceof TwoPanelSettingsFragment) {
            TwoPanelSettingsFragment parentFragment =
                    (TwoPanelSettingsFragment) getCallbackFragment();
            parentFragment.removeListenerForFragment(this);
        }
    }

    @CallSuper
    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
        mLifecycle.onCreateOptionsMenu(menu, inflater);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @CallSuper
    @Override
    public void onPrepareOptionsMenu(final Menu menu) {
        mLifecycle.onPrepareOptionsMenu(menu);
        super.onPrepareOptionsMenu(menu);
    }

    @CallSuper
    @Override
    public boolean onOptionsItemSelected(final MenuItem menuItem) {
        boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
        if (!lifecycleHandled) {
            return super.onOptionsItemSelected(menuItem);
        }
        return lifecycleHandled;
    }

    /** Subclasses should override this to use their own PageId for statsd logging. */
    protected int getPageId() {
        return TvSettingsEnums.PAGE_CLASSIC_DEFAULT;
    }
}