OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
Ted C
2013/12/26 23:06:15
2013
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content.browser; | |
6 | |
7 import android.content.Context; | |
8 import android.graphics.Color; | |
9 import android.view.KeyEvent; | |
10 import android.view.MotionEvent; | |
11 import android.view.SurfaceHolder; | |
12 import android.view.SurfaceView; | |
13 import android.view.View; | |
14 import android.widget.MediaController; | |
15 | |
16 public class ContentVideoViewLegacy extends ContentVideoView { | |
Ted C
2013/12/26 23:06:15
needs a class level description
| |
17 private MediaController mMediaController; | |
18 private boolean mCanPause; | |
19 private boolean mCanSeekBackward; | |
20 private boolean mCanSeekForward; | |
21 private int mCurrentBufferPercentage; | |
22 | |
23 private static class FullScreenMediaController extends MediaController { | |
24 | |
25 View mVideoView; | |
26 | |
27 public FullScreenMediaController(Context context, View video) { | |
28 super(context); | |
29 mVideoView = video; | |
30 } | |
31 | |
32 @Override | |
33 public void show() { | |
34 super.show(); | |
35 if (mVideoView != null) { | |
36 mVideoView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); | |
37 } | |
38 } | |
39 | |
40 @Override | |
41 public void hide() { | |
42 if (mVideoView != null) { | |
43 mVideoView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE ); | |
44 } | |
45 super.hide(); | |
46 } | |
47 } | |
48 | |
49 ContentVideoViewLegacy(Context context, long nativeContentVideoView, | |
50 ContentVideoViewClient client) { | |
51 super(context, nativeContentVideoView, client); | |
52 setBackgroundColor(Color.BLACK); | |
53 mCurrentBufferPercentage = 0; | |
54 } | |
55 | |
56 @Override | |
57 protected void showContentVideoView() { | |
58 SurfaceView surfaceView = getSurfaceView(); | |
59 surfaceView.setZOrderOnTop(true); | |
60 surfaceView.setOnKeyListener(new OnKeyListener() { | |
61 @Override | |
62 public boolean onKey(View v, int keyCode, KeyEvent event) { | |
63 boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK && | |
64 keyCode != KeyEvent.KEYCODE_VOLUME_ UP && | |
Ted C
2013/12/26 23:06:15
Although this is moved code, these lines should be
| |
65 keyCode != KeyEvent.KEYCODE_VOLUME_ DOWN && | |
66 keyCode != KeyEvent.KEYCODE_VOLUME_ MUTE && | |
67 keyCode != KeyEvent.KEYCODE_CALL && | |
68 keyCode != KeyEvent.KEYCODE_MENU && | |
69 keyCode != KeyEvent.KEYCODE_SEARCH && | |
70 keyCode != KeyEvent.KEYCODE_ENDCALL ; | |
71 if (isInPlaybackState() && isKeyCodeSupported && mMediaControlle r != null) { | |
72 if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK || | |
73 keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) { | |
74 if (isPlaying()) { | |
75 pause(); | |
76 mMediaController.show(); | |
77 } else { | |
78 start(); | |
79 mMediaController.hide(); | |
80 } | |
81 return true; | |
82 } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) { | |
83 if (!isPlaying()) { | |
84 start(); | |
85 mMediaController.hide(); | |
86 } | |
87 return true; | |
88 } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP | |
89 || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) { | |
90 if (isPlaying()) { | |
91 pause(); | |
92 mMediaController.show(); | |
93 } | |
94 return true; | |
95 } else { | |
96 toggleMediaControlsVisiblity(); | |
97 } | |
98 } else if (keyCode == KeyEvent.KEYCODE_BACK && | |
99 event.getAction() == KeyEvent.ACTION_UP) { | |
100 exitFullscreen(false); | |
101 return true; | |
102 } else if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEve nt.KEYCODE_SEARCH) { | |
103 return true; | |
104 } | |
105 return false; | |
106 } | |
107 }); | |
108 surfaceView.setOnTouchListener(new OnTouchListener() { | |
109 @Override | |
110 public boolean onTouch(View v, MotionEvent event) { | |
111 if (isInPlaybackState() && mMediaController != null && | |
112 event.getAction() == MotionEvent.ACTION_DOWN) { | |
113 toggleMediaControlsVisiblity(); | |
114 } | |
115 return true; | |
116 } | |
117 }); | |
118 surfaceView.setFocusable(true); | |
119 surfaceView.setFocusableInTouchMode(true); | |
120 surfaceView.requestFocus(); | |
121 super.showContentVideoView(); | |
122 } | |
123 | |
124 @Override | |
125 public void onMediaPlayerError(int errorType) { | |
126 super.onMediaPlayerError(errorType); | |
127 if (errorType == MEDIA_ERROR_INVALID_CODE) { | |
128 return; | |
Ted C
2013/12/26 23:06:15
this can probably all fit on a single line
| |
129 } | |
130 if (mMediaController != null) { | |
Ted C
2013/12/26 23:06:15
same
| |
131 mMediaController.hide(); | |
132 } | |
133 } | |
134 | |
135 @Override | |
136 protected void onBufferingUpdate(int percent) { | |
137 super.onBufferingUpdate(percent); | |
138 mCurrentBufferPercentage = percent; | |
139 } | |
140 | |
141 @Override | |
142 protected void onUpdateMediaMetadata( | |
143 int videoWidth, | |
144 int videoHeight, | |
145 int duration, | |
146 boolean canPause, | |
147 boolean canSeekBack, | |
148 boolean canSeekForward) { | |
149 super.onUpdateMediaMetadata(videoWidth, videoHeight, duration, | |
150 canPause, canSeekBack, canSeekForward); | |
151 mCanPause = canPause; | |
152 mCanSeekBackward = canSeekBack; | |
153 mCanSeekForward = canSeekForward; | |
154 if (mMediaController != null) { | |
Ted C
2013/12/26 23:06:15
personally, I prefer to exit early in this case an
| |
155 mMediaController.setEnabled(true); | |
156 // If paused , should show the controller forever. | |
157 if (isPlaying()) | |
Ted C
2013/12/26 23:06:15
braces required in java land
| |
158 mMediaController.show(); | |
159 else | |
160 mMediaController.show(0); | |
161 } | |
162 } | |
163 | |
164 @Override | |
165 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
166 super.surfaceChanged(holder, format, width, height); | |
167 SurfaceView surfaceView = getSurfaceView(); | |
168 surfaceView.setFocusable(true); | |
169 surfaceView.setFocusableInTouchMode(true); | |
170 if (isInPlaybackState() && mMediaController != null) { | |
171 mMediaController.show(); | |
172 } | |
173 } | |
174 | |
175 @Override | |
176 protected void openVideo() { | |
177 super.openVideo(); | |
178 | |
179 mCurrentBufferPercentage = 0; | |
180 | |
181 if (mMediaController != null) { | |
Ted C
2013/12/26 23:06:15
make a one liner here too
| |
182 return; | |
183 } | |
184 mMediaController = new FullScreenMediaController(getContext(), this); | |
185 mMediaController.setMediaPlayer(new MediaController.MediaPlayerControl() { | |
186 @Override public boolean canPause() { return mCanPause; } | |
187 @Override public boolean canSeekBackward() { return mCanSeekBackward ; } | |
188 @Override public boolean canSeekForward() { return mCanSeekForward; } | |
189 @Override public int getAudioSessionId() { return 0; } | |
190 @Override public int getBufferPercentage() { return mCurrentBufferPe rcentage; } | |
191 | |
192 @Override | |
193 public int getCurrentPosition() { | |
194 return ContentVideoViewLegacy.this.getCurrentPosition(); | |
195 } | |
196 | |
197 @Override | |
198 public int getDuration() { | |
199 return ContentVideoViewLegacy.this.getDuration(); | |
200 } | |
201 | |
202 @Override | |
203 public boolean isPlaying() { | |
204 return ContentVideoViewLegacy.this.isPlaying(); | |
205 } | |
206 | |
207 @Override | |
208 public void pause() { | |
209 ContentVideoViewLegacy.this.pause(); | |
210 } | |
211 | |
212 @Override | |
213 public void seekTo(int pos) { | |
214 ContentVideoViewLegacy.this.seekTo(pos); | |
215 } | |
216 | |
217 @Override | |
218 public void start() { | |
219 ContentVideoViewLegacy.this.start(); | |
220 } | |
221 }); | |
222 mMediaController.setAnchorView(getSurfaceView()); | |
223 mMediaController.setEnabled(false); | |
224 } | |
225 | |
226 @Override | |
227 protected void onCompletion() { | |
228 super.onCompletion(); | |
229 if (mMediaController != null) { | |
230 mMediaController.hide(); | |
231 } | |
232 } | |
233 | |
234 @Override | |
235 public boolean onTrackballEvent(MotionEvent ev) { | |
236 if (isInPlaybackState() && mMediaController != null) { | |
237 toggleMediaControlsVisiblity(); | |
238 } | |
239 return false; | |
240 } | |
241 | |
242 private void toggleMediaControlsVisiblity() { | |
243 if (mMediaController.isShowing()) { | |
244 mMediaController.hide(); | |
245 } else { | |
246 mMediaController.show(); | |
247 } | |
248 } | |
249 | |
250 @Override | |
251 protected void destroyContentVideoView(boolean nativeViewDestroyed) { | |
252 if (mMediaController != null) { | |
253 mMediaController.setEnabled(false); | |
254 mMediaController.hide(); | |
255 mMediaController = null; | |
256 } | |
257 super.destroyContentVideoView(nativeViewDestroyed); | |
258 } | |
259 | |
260 @Override | |
261 public boolean onTouchEvent(MotionEvent ev) { | |
262 return true; | |
263 } | |
264 | |
265 } | |
OLD | NEW |