|
21 | 21 | import android.animation.ObjectAnimator; |
22 | 22 | import android.animation.TypeEvaluator; |
23 | 23 | import android.animation.ValueAnimator; |
| 24 | +import android.content.BroadcastReceiver; |
24 | 25 | import android.content.Context; |
| 26 | +import android.content.Intent; |
| 27 | +import android.content.IntentFilter; |
25 | 28 | import android.graphics.Canvas; |
26 | 29 | import android.graphics.Color; |
27 | 30 | import android.graphics.PixelFormat; |
@@ -144,6 +147,7 @@ public final class ScreenMagnifier implements EventStreamTransformation { |
144 | 147 |
|
145 | 148 | private final MagnificationController mMagnificationController; |
146 | 149 | private final DisplayContentObserver mDisplayContentObserver; |
| 150 | + private final ScreenStateObserver mScreenStateObserver; |
147 | 151 | private final Viewport mViewport; |
148 | 152 |
|
149 | 153 | private final int mTapTimeSlop = ViewConfiguration.getTapTimeout(); |
@@ -187,6 +191,8 @@ public ScreenMagnifier(Context context) { |
187 | 191 | mDisplayContentObserver = new DisplayContentObserver(mContext, mViewport, |
188 | 192 | mMagnificationController, mWindowManagerService, mDisplayProvider, |
189 | 193 | mLongAnimationDuration, mWindowAnimationScale); |
| 194 | + mScreenStateObserver = new ScreenStateObserver(mContext, mViewport, |
| 195 | + mMagnificationController); |
190 | 196 |
|
191 | 197 | mGestureDetector = new GestureDetector(context); |
192 | 198 |
|
@@ -247,6 +253,7 @@ public void onDestroy() { |
247 | 253 | mViewport.setFrameShown(false, true); |
248 | 254 | mDisplayProvider.destroy(); |
249 | 255 | mDisplayContentObserver.destroy(); |
| 256 | + mScreenStateObserver.destroy(); |
250 | 257 | } |
251 | 258 |
|
252 | 259 | private void handleMotionEventStateDelegating(MotionEvent event, int policyFlags) { |
@@ -786,6 +793,12 @@ private float getPersistedScale() { |
786 | 793 | DEFAULT_MAGNIFICATION_SCALE); |
787 | 794 | } |
788 | 795 |
|
| 796 | + private static boolean isScreenMagnificationAutoUpdateEnabled(Context context) { |
| 797 | + return (Settings.Secure.getInt(context.getContentResolver(), |
| 798 | + Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE, |
| 799 | + DEFAULT_SCREEN_MAGNIFICATION_AUTO_UPDATE) == 1); |
| 800 | + } |
| 801 | + |
789 | 802 | private static final class MotionEventInfo { |
790 | 803 |
|
791 | 804 | private static final int MAX_POOL_SIZE = 10; |
@@ -844,6 +857,54 @@ private void clear() { |
844 | 857 | } |
845 | 858 | } |
846 | 859 |
|
| 860 | + private static final class ScreenStateObserver extends BroadcastReceiver { |
| 861 | + |
| 862 | + private static final int MESSAGE_ON_SCREEN_STATE_CHANGE = 1; |
| 863 | + |
| 864 | + private final Handler mHandler = new Handler() { |
| 865 | + @Override |
| 866 | + public void handleMessage(Message message) { |
| 867 | + switch (message.what) { |
| 868 | + case MESSAGE_ON_SCREEN_STATE_CHANGE: { |
| 869 | + String action = (String) message.obj; |
| 870 | + handleOnScreenStateChange(action); |
| 871 | + } break; |
| 872 | + } |
| 873 | + } |
| 874 | + }; |
| 875 | + |
| 876 | + private final Context mContext; |
| 877 | + private final Viewport mViewport; |
| 878 | + private final MagnificationController mMagnificationController; |
| 879 | + |
| 880 | + public ScreenStateObserver(Context context, Viewport viewport, |
| 881 | + MagnificationController magnificationController) { |
| 882 | + mContext = context; |
| 883 | + mViewport = viewport; |
| 884 | + mMagnificationController = magnificationController; |
| 885 | + mContext.registerReceiver(this, new IntentFilter(Intent.ACTION_SCREEN_OFF)); |
| 886 | + } |
| 887 | + |
| 888 | + public void destroy() { |
| 889 | + mContext.unregisterReceiver(this); |
| 890 | + } |
| 891 | + |
| 892 | + @Override |
| 893 | + public void onReceive(Context context, Intent intent) { |
| 894 | + mHandler.obtainMessage(MESSAGE_ON_SCREEN_STATE_CHANGE, |
| 895 | + intent.getAction()).sendToTarget(); |
| 896 | + } |
| 897 | + |
| 898 | + private void handleOnScreenStateChange(String action) { |
| 899 | + if (action.equals(Intent.ACTION_SCREEN_OFF) |
| 900 | + && mMagnificationController.isMagnifying() |
| 901 | + && isScreenMagnificationAutoUpdateEnabled(mContext)) { |
| 902 | + mMagnificationController.reset(false); |
| 903 | + mViewport.setFrameShown(false, false); |
| 904 | + } |
| 905 | + } |
| 906 | + } |
| 907 | + |
847 | 908 | private static final class DisplayContentObserver { |
848 | 909 |
|
849 | 910 | private static final int MESSAGE_SHOW_VIEWPORT_FRAME = 1; |
@@ -972,7 +1033,7 @@ private void handleOnWindowTransition(int transition, WindowInfo info) { |
972 | 1033 | switch (transition) { |
973 | 1034 | case WindowManagerPolicy.TRANSIT_ENTER: |
974 | 1035 | case WindowManagerPolicy.TRANSIT_SHOW: { |
975 | | - if (!magnifying || !screenMagnificationAutoUpdateEnabled(mContext)) { |
| 1036 | + if (!magnifying || !isScreenMagnificationAutoUpdateEnabled(mContext)) { |
976 | 1037 | break; |
977 | 1038 | } |
978 | 1039 | final int type = info.type; |
@@ -1060,18 +1121,12 @@ private void ensureRectangleInMagnifiedRegionBounds(Rect magnifiedRegionBounds, |
1060 | 1121 |
|
1061 | 1122 | private void resetMagnificationIfNeeded() { |
1062 | 1123 | if (mMagnificationController.isMagnifying() |
1063 | | - && screenMagnificationAutoUpdateEnabled(mContext)) { |
| 1124 | + && isScreenMagnificationAutoUpdateEnabled(mContext)) { |
1064 | 1125 | mMagnificationController.reset(true); |
1065 | 1126 | mViewport.setFrameShown(false, true); |
1066 | 1127 | } |
1067 | 1128 | } |
1068 | 1129 |
|
1069 | | - private boolean screenMagnificationAutoUpdateEnabled(Context context) { |
1070 | | - return (Settings.Secure.getInt(context.getContentResolver(), |
1071 | | - Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE, |
1072 | | - DEFAULT_SCREEN_MAGNIFICATION_AUTO_UPDATE) == 1); |
1073 | | - } |
1074 | | - |
1075 | 1130 | private String windowTransitionToString(int transition) { |
1076 | 1131 | switch (transition) { |
1077 | 1132 | case WindowManagerPolicy.TRANSIT_UNSET: { |
|
0 commit comments