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.media.AudioManager;
24 import android.media.MediaPlayer;
25 import android.media.MediaPlayer.OnPreparedListener;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.provider.Settings;
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.OnGlobalLayoutListener;
34 import android.view.WindowManager;
35 import android.widget.ImageView;
36 import android.widget.RelativeLayout.LayoutParams;
37 import android.widget.Switch;
38 import android.widget.VideoView;
39 
40 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
41 import com.android.settings.R;
42 import com.android.settings.widget.SwitchBar;
43 
44 public class ToggleScreenMagnificationPreferenceFragment extends
45         ToggleFeaturePreferenceFragment implements SwitchBar.OnSwitchChangeListener {
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             // Make sure the VideoView does not request audio focus.
81             videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE);
82 
83             // Resolve and set the video content
84             Bundle args = getArguments();
85             if ((args != null) && args.containsKey(
86                     AccessibilitySettings.EXTRA_VIDEO_RAW_RESOURCE_ID)) {
87                 videoView.setVideoURI(Uri.parse(String.format("%s://%s/%s",
88                         ContentResolver.SCHEME_ANDROID_RESOURCE,
89                         getPrefContext().getPackageName(),
90                         args.getInt(AccessibilitySettings.EXTRA_VIDEO_RAW_RESOURCE_ID))));
91             }
92 
93             // Make sure video controls (e.g. for pausing) are not displayed.
94             videoView.setMediaController(null);
95 
96             // LayoutListener for adjusting the position of the VideoView on the background image.
97             mLayoutListener = new OnGlobalLayoutListener() {
98                 @Override
99                 public void onGlobalLayout() {
100                     final int backgroundViewWidth = mVideoBackgroundView.getWidth();
101 
102                     LayoutParams videoLp = (LayoutParams) videoView.getLayoutParams();
103                     videoLp.width = videoAssetWidth * backgroundViewWidth / backgroundAssetWidth;
104                     videoLp.height = videoAssetHeight * backgroundViewWidth / backgroundAssetWidth;
105                     videoLp.setMargins(0,
106                             videoAssetMarginTop * backgroundViewWidth / backgroundAssetWidth, 0, 0);
107                     videoView.setLayoutParams(videoLp);
108                     videoView.invalidate();
109                     videoView.start();
110                 }
111             };
112 
113             mVideoBackgroundView.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener);
114         }
115 
116         @Override
onPrepareForRemoval()117         protected void onPrepareForRemoval() {
118             mVideoBackgroundView.getViewTreeObserver()
119                     .removeOnGlobalLayoutListener(mLayoutListener);
120         }
121     }
122 
123     protected VideoPreference mVideoPreference;
124     protected Preference mConfigWarningPreference;
125 
126     private boolean mLaunchFromSuw = false;
127     private boolean mInitialSetting = false;
128 
129     @Override
onCreate(Bundle savedInstanceState)130     public void onCreate(Bundle savedInstanceState) {
131         super.onCreate(savedInstanceState);
132 
133         mVideoPreference = new VideoPreference(getPrefContext());
134         mVideoPreference.setSelectable(false);
135         mVideoPreference.setPersistent(false);
136         mVideoPreference.setLayoutResource(R.layout.magnification_video_preference);
137 
138         mConfigWarningPreference = new Preference(getPrefContext());
139         mConfigWarningPreference.setSelectable(false);
140         mConfigWarningPreference.setPersistent(false);
141         mConfigWarningPreference.setVisible(false);
142         mConfigWarningPreference.setIcon(R.drawable.ic_warning_24dp);
143 
144         final PreferenceScreen preferenceScreen = getPreferenceManager().getPreferenceScreen();
145         preferenceScreen.setOrderingAsAdded(false);
146         mVideoPreference.setOrder(0);
147         mSummaryPreference.setOrder(1);
148         mConfigWarningPreference.setOrder(2);
149         preferenceScreen.addPreference(mVideoPreference);
150         preferenceScreen.addPreference(mConfigWarningPreference);
151     }
152 
153     @Override
onResume()154     public void onResume() {
155         super.onResume();
156 
157         VideoView videoView = (VideoView) getView().findViewById(R.id.video);
158         if (videoView != null) {
159             videoView.start();
160         }
161 
162         updateConfigurationWarningIfNeeded();
163     }
164 
165     @Override
getMetricsCategory()166     public int getMetricsCategory() {
167         // TODO: Distinguish between magnification modes
168         return MetricsEvent.ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFICATION;
169     }
170 
171     @Override
onSwitchChanged(Switch switchView, boolean isChecked)172     public void onSwitchChanged(Switch switchView, boolean isChecked) {
173         onPreferenceToggled(mPreferenceKey, isChecked);
174     }
175 
176     @Override
onPreferenceToggled(String preferenceKey, boolean enabled)177     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
178         Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? 1 : 0);
179         updateConfigurationWarningIfNeeded();
180     }
181 
182     @Override
onInstallSwitchBarToggleSwitch()183     protected void onInstallSwitchBarToggleSwitch() {
184         super.onInstallSwitchBarToggleSwitch();
185 
186         mSwitchBar.setCheckedInternal(
187                 Settings.Secure.getInt(getContentResolver(), mPreferenceKey, 0) == 1);
188         mSwitchBar.addOnSwitchChangeListener(this);
189     }
190 
191     @Override
onRemoveSwitchBarToggleSwitch()192     protected void onRemoveSwitchBarToggleSwitch() {
193         super.onRemoveSwitchBarToggleSwitch();
194         mSwitchBar.removeOnSwitchChangeListener(this);
195     }
196 
197     @Override
onProcessArguments(Bundle arguments)198     protected void onProcessArguments(Bundle arguments) {
199         super.onProcessArguments(arguments);
200         if (arguments == null) {
201             return;
202         }
203 
204         if (arguments.containsKey(AccessibilitySettings.EXTRA_VIDEO_RAW_RESOURCE_ID)) {
205             mVideoPreference.setVisible(true);
206             final int resId = arguments.getInt(
207                     AccessibilitySettings.EXTRA_VIDEO_RAW_RESOURCE_ID);
208         } else {
209             mVideoPreference.setVisible(false);
210         }
211 
212         if (arguments.containsKey(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW)) {
213             mLaunchFromSuw = arguments.getBoolean(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW);
214         }
215 
216         if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
217             mInitialSetting = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED);
218         }
219     }
220 
updateConfigurationWarningIfNeeded()221     private void updateConfigurationWarningIfNeeded() {
222         final CharSequence warningMessage =
223                 MagnificationPreferenceFragment.getConfigurationWarningStringForSecureSettingsKey(
224                         mPreferenceKey, getPrefContext());
225         if (warningMessage != null) {
226             mConfigWarningPreference.setSummary(warningMessage);
227         }
228         mConfigWarningPreference.setVisible(warningMessage != null);
229     }
230 
getScreenWidth(Context context)231     private static int getScreenWidth(Context context) {
232         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
233         Display display = wm.getDefaultDisplay();
234         Point size = new Point();
235         display.getSize(size);
236         return size.x;
237     }
238 }
239