• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.systemui.accessibility;
18 
19 import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY;
20 
21 import android.annotation.MainThread;
22 import android.content.Context;
23 import android.hardware.display.DisplayManager;
24 import android.view.Display;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.systemui.dagger.SysUISingleton;
28 
29 /**
30  * A class to control {@link MagnificationModeSwitch}. It should show the button UI with following
31  * conditions:
32  * <ol>
33  *   <li> Both full-screen and window magnification mode are capable.</li>
34  *   <li> The magnification scale is changed by a user.</li>
35  * <ol>
36  */
37 @SysUISingleton
38 public class ModeSwitchesController {
39 
40     private final DisplayIdIndexSupplier<MagnificationModeSwitch> mSwitchSupplier;
41 
ModeSwitchesController(Context context)42     public ModeSwitchesController(Context context) {
43         mSwitchSupplier = new SwitchSupplier(context,
44                 context.getSystemService(DisplayManager.class));
45     }
46 
47     @VisibleForTesting
ModeSwitchesController(DisplayIdIndexSupplier<MagnificationModeSwitch> switchSupplier)48     ModeSwitchesController(DisplayIdIndexSupplier<MagnificationModeSwitch> switchSupplier) {
49         mSwitchSupplier = switchSupplier;
50     }
51 
52     /**
53      * Shows a button that a user can click the button to switch magnification mode. And the
54      * button would be dismissed automatically after the button is displayed for a period of time.
55      *
56      * @param displayId The logical display id
57      * @param mode      The magnification mode
58      * @see android.provider.Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW
59      * @see android.provider.Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN
60      */
61     @MainThread
showButton(int displayId, int mode)62     void showButton(int displayId, int mode) {
63         final MagnificationModeSwitch magnificationModeSwitch =
64                 mSwitchSupplier.get(displayId);
65         if (magnificationModeSwitch == null) {
66             return;
67         }
68         magnificationModeSwitch.showButton(mode);
69     }
70 
71     /**
72      * Removes magnification mode switch button immediately.
73      *
74      * @param displayId The logical display id
75      */
removeButton(int displayId)76     void removeButton(int displayId) {
77         final MagnificationModeSwitch magnificationModeSwitch =
78                 mSwitchSupplier.get(displayId);
79         if (magnificationModeSwitch == null) {
80             return;
81         }
82         magnificationModeSwitch.removeButton();
83     }
84 
85     /**
86      * Called when the configuration has changed, and it updates magnification button UI.
87      *
88      * @param configDiff a bit mask of the differences between the configurations
89      */
90     @MainThread
onConfigurationChanged(int configDiff)91     void onConfigurationChanged(int configDiff) {
92         mSwitchSupplier.forEach(
93                 switchController -> switchController.onConfigurationChanged(configDiff));
94     }
95 
96     private static class SwitchSupplier extends DisplayIdIndexSupplier<MagnificationModeSwitch> {
97 
98         private final Context mContext;
99 
100         /**
101          * @param context        Context
102          * @param displayManager DisplayManager
103          */
SwitchSupplier(Context context, DisplayManager displayManager)104         SwitchSupplier(Context context, DisplayManager displayManager) {
105             super(displayManager);
106             mContext = context;
107         }
108 
109         @Override
createInstance(Display display)110         protected MagnificationModeSwitch createInstance(Display display) {
111             final Context uiContext = mContext.createWindowContext(display,
112                     TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY, /* options */ null);
113             return new MagnificationModeSwitch(uiContext);
114         }
115     }
116 }
117