summaryrefslogtreecommitdiff
path: root/packages/SystemUI/accessibility/accessibilitymenu/src/com/android/systemui/accessibility/accessibilitymenu/view/A11yMenuOverlayLayout.java
blob: 1be04f854c9a0bea7ba1410b65dc9073733a04e9 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2023 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.systemui.accessibility.accessibilitymenu.view;

import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;

import static java.lang.Math.max;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Insets;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.WindowMetrics;
import android.view.accessibility.AccessibilityManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.systemui.accessibility.accessibilitymenu.AccessibilityMenuService;
import com.android.systemui.accessibility.accessibilitymenu.R;
import com.android.systemui.accessibility.accessibilitymenu.activity.A11yMenuSettingsActivity.A11yMenuPreferenceFragment;
import com.android.systemui.accessibility.accessibilitymenu.model.A11yMenuShortcut;

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

/**
 * Provides functionality for Accessibility menu layout in a11y menu overlay. There are functions to
 * configure or update Accessibility menu layout when orientation and display size changed, and
 * functions to toggle menu visibility when button clicked or screen off.
 */
public class A11yMenuOverlayLayout {

    /** Predefined default shortcuts when large button setting is off. */
    private static final int[] SHORTCUT_LIST_DEFAULT = {
        A11yMenuShortcut.ShortcutId.ID_ASSISTANT_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_A11YSETTING_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_POWER_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_VOLUME_DOWN_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_VOLUME_UP_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_RECENT_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_BRIGHTNESS_DOWN_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_BRIGHTNESS_UP_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_LOCKSCREEN_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_QUICKSETTING_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_NOTIFICATION_VALUE.ordinal(),
        A11yMenuShortcut.ShortcutId.ID_SCREENSHOT_VALUE.ordinal()
    };

    /** Predefined default shortcuts when large button setting is on. */
    private static final int[] LARGE_SHORTCUT_LIST_DEFAULT = {
            A11yMenuShortcut.ShortcutId.ID_ASSISTANT_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_A11YSETTING_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_POWER_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_RECENT_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_VOLUME_DOWN_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_VOLUME_UP_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_BRIGHTNESS_DOWN_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_BRIGHTNESS_UP_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_LOCKSCREEN_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_QUICKSETTING_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_NOTIFICATION_VALUE.ordinal(),
            A11yMenuShortcut.ShortcutId.ID_SCREENSHOT_VALUE.ordinal()
    };



    private final AccessibilityMenuService mService;
    private final WindowManager mWindowManager;
    private final DisplayManager mDisplayManager;
    private ViewGroup mLayout;
    private WindowManager.LayoutParams mLayoutParameter;
    private A11yMenuViewPager mA11yMenuViewPager;
    private Handler mHandler;
    private AccessibilityManager mAccessibilityManager;

    public A11yMenuOverlayLayout(AccessibilityMenuService service) {
        mService = service;
        mWindowManager = mService.getSystemService(WindowManager.class);
        mDisplayManager = mService.getSystemService(DisplayManager.class);
        configureLayout();
        mHandler = new Handler(Looper.getMainLooper());
        mAccessibilityManager = mService.getSystemService(AccessibilityManager.class);
    }

    /** Creates Accessibility menu layout and configure layout parameters. */
    public View configureLayout() {
        return configureLayout(A11yMenuViewPager.DEFAULT_PAGE_INDEX);
    }

    // TODO(b/78292783): Find a better way to inflate layout in the test.
    /**
     * Creates Accessibility menu layout, configure layout parameters and apply index to ViewPager.
     *
     * @param pageIndex the index of the ViewPager to show.
     */
    public View configureLayout(int pageIndex) {

        int lastVisibilityState = View.GONE;
        if (mLayout != null) {
            lastVisibilityState = mLayout.getVisibility();
            mWindowManager.removeView(mLayout);
            mLayout = null;
        }

        if (mLayoutParameter == null) {
            initLayoutParams();
        }

        final Display display = mDisplayManager.getDisplay(DEFAULT_DISPLAY);
        final Context context = mService.createDisplayContext(display).createWindowContext(
                TYPE_ACCESSIBILITY_OVERLAY, null);
        mLayout = new FrameLayout(context);
        updateLayoutPosition();
        inflateLayoutAndSetOnTouchListener(mLayout, context);
        mA11yMenuViewPager = new A11yMenuViewPager(mService, context);
        mA11yMenuViewPager.configureViewPagerAndFooter(mLayout, createShortcutList(), pageIndex);
        mWindowManager.addView(mLayout, mLayoutParameter);
        mLayout.setVisibility(lastVisibilityState);

        return mLayout;
    }

    public void clearLayout() {
        if (mLayout != null) {
            mWindowManager.removeView(mLayout);
            mLayout.setOnTouchListener(null);
            mLayout = null;
        }
    }

    /** Updates view layout with new layout parameters only. */
    public void updateViewLayout() {
        if (mLayout == null || mLayoutParameter == null) {
            return;
        }
        updateLayoutPosition();
        mWindowManager.updateViewLayout(mLayout, mLayoutParameter);
    }

    private void initLayoutParams() {
        mLayoutParameter = new WindowManager.LayoutParams();
        mLayoutParameter.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
        mLayoutParameter.format = PixelFormat.TRANSLUCENT;
        mLayoutParameter.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        mLayoutParameter.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        mLayoutParameter.setTitle(mService.getString(R.string.accessibility_menu_service_name));
    }

    private void inflateLayoutAndSetOnTouchListener(ViewGroup view, Context displayContext) {
        LayoutInflater inflater = LayoutInflater.from(displayContext);
        inflater.inflate(R.layout.paged_menu, view);
        view.setOnTouchListener(mService);
    }

    /**
     * Loads shortcut data from default shortcut ID array.
     *
     * @return A list of default shortcuts
     */
    private List<A11yMenuShortcut> createShortcutList() {
        List<A11yMenuShortcut> shortcutList = new ArrayList<>();

        for (int shortcutId :
                (A11yMenuPreferenceFragment.isLargeButtonsEnabled(mService)
                        ? LARGE_SHORTCUT_LIST_DEFAULT : SHORTCUT_LIST_DEFAULT)) {
            shortcutList.add(new A11yMenuShortcut(shortcutId));
        }
        return shortcutList;
    }

    /** Updates a11y menu layout position by configuring layout params. */
    private void updateLayoutPosition() {
        final Display display = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
        final Configuration configuration = mService.getResources().getConfiguration();
        final int orientation = configuration.orientation;
        if (display != null && orientation == Configuration.ORIENTATION_LANDSCAPE) {
            final boolean ltr = configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
            switch (display.getRotation()) {
                case Surface.ROTATION_0:
                case Surface.ROTATION_180:
                    mLayoutParameter.gravity =
                            (ltr ? Gravity.END : Gravity.START) | Gravity.BOTTOM
                                    | Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
                    mLayoutParameter.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    mLayoutParameter.height = WindowManager.LayoutParams.MATCH_PARENT;
                    mLayoutParameter.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
                    mLayoutParameter.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
                    mLayout.setBackgroundResource(R.drawable.shadow_90deg);
                    break;
                case Surface.ROTATION_90:
                case Surface.ROTATION_270:
                    mLayoutParameter.gravity =
                            (ltr ? Gravity.START : Gravity.END) | Gravity.BOTTOM
                                    | Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;
                    mLayoutParameter.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    mLayoutParameter.height = WindowManager.LayoutParams.MATCH_PARENT;
                    mLayoutParameter.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
                    mLayoutParameter.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
                    mLayout.setBackgroundResource(R.drawable.shadow_270deg);
                    break;
                default:
                    break;
            }
        } else {
            mLayoutParameter.gravity = Gravity.BOTTOM;
            mLayoutParameter.width = WindowManager.LayoutParams.MATCH_PARENT;
            mLayoutParameter.height = WindowManager.LayoutParams.WRAP_CONTENT;
            mLayout.setBackgroundResource(R.drawable.shadow_0deg);
        }

        // Adjusts the y position of a11y menu layout to make the layout not to overlap bottom
        // navigation bar window.
        updateLayoutByWindowInsetsIfNeeded();
        mLayout.setOnApplyWindowInsetsListener(
                (view, insets) -> {
                    if (updateLayoutByWindowInsetsIfNeeded()) {
                        mWindowManager.updateViewLayout(mLayout, mLayoutParameter);
                    }
                    return view.onApplyWindowInsets(insets);
                });
    }

    /**
     * Returns {@code true} if the a11y menu layout params
     * should be updated by {@link WindowManager} immediately due to window insets change.
     * This method adjusts the layout position and size to
     * make a11y menu not to overlap navigation bar window.
     */
    private boolean updateLayoutByWindowInsetsIfNeeded() {
        boolean shouldUpdateLayout = false;
        WindowMetrics windowMetrics = mWindowManager.getCurrentWindowMetrics();
        Insets windowInsets = windowMetrics.getWindowInsets().getInsetsIgnoringVisibility(
                WindowInsets.Type.systemBars() | WindowInsets.Type.displayCutout());
        int xOffset = max(windowInsets.left, windowInsets.right);
        int yOffset = windowInsets.bottom;
        Rect windowBound = windowMetrics.getBounds();
        if (mLayoutParameter.x != xOffset || mLayoutParameter.y != yOffset) {
            mLayoutParameter.x = xOffset;
            mLayoutParameter.y = yOffset;
            shouldUpdateLayout = true;
        }
        // for gestural navigation mode and the landscape mode,
        // the layout height should be decreased by system bar
        // and display cutout inset to fit the new
        // frame size that doesn't overlap the navigation bar window.
        int orientation = mService.getResources().getConfiguration().orientation;
        if (mLayout.getHeight() != mLayoutParameter.height
                && orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mLayoutParameter.height = windowBound.height() - yOffset;
            shouldUpdateLayout = true;
        }
        return shouldUpdateLayout;
    }

    /**
     * Gets the current page index when device configuration changed. {@link
     * AccessibilityMenuService#onConfigurationChanged(Configuration)}
     *
     * @return the current index of the ViewPager.
     */
    public int getPageIndex() {
        if (mA11yMenuViewPager != null) {
            return mA11yMenuViewPager.mViewPager.getCurrentItem();
        }
        return A11yMenuViewPager.DEFAULT_PAGE_INDEX;
    }

    /**
     * Hides a11y menu layout. And return if layout visibility has been changed.
     *
     * @return {@code true} layout visibility is toggled off; {@code false} is unchanged
     */
    public boolean hideMenu() {
        if (mLayout.getVisibility() == View.VISIBLE) {
            mLayout.setVisibility(View.GONE);
            return true;
        }
        return false;
    }

    /** Toggles a11y menu layout visibility. */
    public void toggleVisibility() {
        mLayout.setVisibility((mLayout.getVisibility() == View.VISIBLE) ? View.GONE : View.VISIBLE);
    }

    /** Shows hint text on a minimal Snackbar-like text view. */
    public void showSnackbar(String text) {
        final int animationDurationMs = 300;
        final int timeoutDurationMs = mAccessibilityManager.getRecommendedTimeoutMillis(2000,
                AccessibilityManager.FLAG_CONTENT_TEXT);

        final TextView snackbar = mLayout.findViewById(R.id.snackbar);
        snackbar.setText(text);

        // Remove any existing fade-out animation before starting any new animations.
        mHandler.removeCallbacksAndMessages(null);

        if (snackbar.getVisibility() != View.VISIBLE) {
            snackbar.setAlpha(0f);
            snackbar.setVisibility(View.VISIBLE);
            snackbar.animate().alpha(1f).setDuration(animationDurationMs).setListener(null);
        }
        mHandler.postDelayed(() -> snackbar.animate().alpha(0f).setDuration(
                animationDurationMs).setListener(
                new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(@NonNull Animator animation) {
                            snackbar.setVisibility(View.GONE);
                        }
                    }), timeoutDurationMs);
    }
}