1 /*
2  * Copyright (C) 2013 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.settings.accessibility;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.graphics.Point;
23 import android.os.Bundle;
24 import android.provider.Settings;
25 
26 import android.media.MediaPlayer;
27 import android.media.MediaPlayer.OnPreparedListener;
28 import android.net.Uri;
29 import android.support.v7.preference.Preference;
30 import android.support.v7.preference.PreferenceScreen;
31 import android.support.v7.preference.PreferenceViewHolder;
32 import android.view.Display;
33 import android.view.ViewTreeObserver;
34 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
35 import android.view.WindowManager;
36 import android.widget.ImageView;
37 import android.widget.MediaController;
38 import android.widget.RelativeLayout.LayoutParams;
39 import android.widget.VideoView;
40 import com.android.internal.logging.MetricsProto.MetricsEvent;
41 import com.android.settings.R;
42 import com.android.settings.widget.ToggleSwitch;
43 import com.android.settings.widget.ToggleSwitch.OnBeforeCheckedChangeListener;
44 
45 public class ToggleScreenMagnificationPreferenceFragment extends ToggleFeaturePreferenceFragment {
46 
47     protected class VideoPreference extends Preference {
48         private ImageView mVideoBackgroundView;
49         private OnGlobalLayoutListener mLayoutListener;
50 
VideoPreference(Context context)51         public VideoPreference(Context context) {
52             super(context);
53         }
54 
55         @Override
onBindViewHolder(PreferenceViewHolder view)56         public void onBindViewHolder(PreferenceViewHolder view) {
57             super.onBindViewHolder(view);
58             Resources res = getPrefContext().getResources();
59             final int backgroundAssetWidth = res.getDimensionPixelSize(
60                     R.dimen.screen_magnification_video_background_width);
61             final int videoAssetWidth = res
62                     .getDimensionPixelSize(R.dimen.screen_magnification_video_width);
63             final int videoAssetHeight = res
64                     .getDimensionPixelSize(R.dimen.screen_magnification_video_height);
65             final int videoAssetMarginTop = res.getDimensionPixelSize(
66                     R.dimen.screen_magnification_video_margin_top);
67             view.setDividerAllowedAbove(false);
68             view.setDividerAllowedBelow(false);
69             mVideoBackgroundView = (ImageView) view.findViewById(R.id.video_background);
70             final VideoView videoView = (VideoView) view.findViewById(R.id.video);
71 
72             // Loop the video.
73             videoView.setOnPreparedListener(new OnPreparedListener() {
74                 @Override
75                 public void onPrepared(MediaPlayer mediaPlayer) {
76                     mediaPlayer.setLooping(true);
77                 }
78             });
79 
80             videoView.setVideoURI(Uri.parse(String.format("%s://%s/%s",
81                     ContentResolver.SCHEME_ANDROID_RESOURCE,
82                     getPrefContext().getPackageName(),
83                     R.raw.accessibility_screen_magnification)));
84             // Make sure video controls (e.g. for pausing) are not displayed.
85             videoView.setMediaController(null);
86 
87             // LayoutListener for adjusting the position of the VideoView on the background image.
88             mLayoutListener = new OnGlobalLayoutListener() {
89                 @Override
90                 public void onGlobalLayout() {
91                     final int backgroundViewWidth = mVideoBackgroundView.getWidth();
92 
93                     LayoutParams videoLp = (LayoutParams) videoView.getLayoutParams();
94                     videoLp.width = videoAssetWidth * backgroundViewWidth / backgroundAssetWidth;
95                     videoLp.height = videoAssetHeight * backgroundViewWidth / backgroundAssetWidth;
96                     videoLp.setMargins(0,
97                             videoAssetMarginTop * backgroundViewWidth / backgroundAssetWidth, 0, 0);
98                     videoView.setLayoutParams(videoLp);
99                     videoView.invalidate();
100                     videoView.start();
101                 }
102             };
103 
104             mVideoBackgroundView.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener);
105         }
106 
107         @Override
onPrepareForRemoval()108         protected void onPrepareForRemoval() {
109             mVideoBackgroundView.getViewTreeObserver()
110                     .removeOnGlobalLayoutListener(mLayoutListener);
111         }
112     }
113 
114     protected VideoPreference mVideoPreference;
115 
116     @Override
onCreate(Bundle savedInstanceState)117     public void onCreate(Bundle savedInstanceState) {
118         super.onCreate(savedInstanceState);
119 
120         mVideoPreference = new VideoPreference(getPrefContext());
121         mVideoPreference.setSelectable(false);
122         mVideoPreference.setPersistent(false);
123         mVideoPreference.setLayoutResource(R.layout.video_preference);
124 
125         final PreferenceScreen preferenceScreen = getPreferenceManager().getPreferenceScreen();
126         preferenceScreen.setOrderingAsAdded(false);
127         mVideoPreference.setOrder(0);
128         mSummaryPreference.setOrder(1);
129         preferenceScreen.addPreference(mVideoPreference);
130     }
131 
132     @Override
onPreferenceToggled(String preferenceKey, boolean enabled)133     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
134         // Do nothing.
135     }
136 
137     @Override
onInstallSwitchBarToggleSwitch()138     protected void onInstallSwitchBarToggleSwitch() {
139         super.onInstallSwitchBarToggleSwitch();
140         mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
141             @Override
142             public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
143                 mSwitchBar.setCheckedInternal(checked);
144                 getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, checked);
145                 onPreferenceToggled(mPreferenceKey, checked);
146                 return false;
147             }
148         });
149     }
150 
151     @Override
onResume()152     public void onResume() {
153         super.onResume();
154 
155         // Temporarily enable Magnification on this screen if it's disabled.
156         if (Settings.Secure.getInt(getContentResolver(),
157                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, 0) == 0) {
158             setMagnificationEnabled(1);
159         }
160 
161         VideoView videoView = (VideoView) getView().findViewById(R.id.video);
162         if (videoView != null) {
163             videoView.start();
164         }
165     }
166 
167     @Override
onPause()168     public void onPause() {
169         super.onPause();
170         if (!mToggleSwitch.isChecked()) {
171             setMagnificationEnabled(0);
172         }
173     }
174 
setMagnificationEnabled(int enabled)175     private void setMagnificationEnabled(int enabled) {
176         Settings.Secure.putInt(getContentResolver(),
177                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, enabled);
178     }
179 
180     @Override
getMetricsCategory()181     protected int getMetricsCategory() {
182         return MetricsEvent.ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFICATION;
183     }
184 
getScreenWidth(Context context)185     private static int getScreenWidth(Context context) {
186         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
187         Display display = wm.getDefaultDisplay();
188         Point size = new Point();
189         display.getSize(size);
190         return size.x;
191     }
192 }
193