AI2 Component  (Version nb184)
FullScreenVideoUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2018 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
13 
14 import android.R;
15 import android.app.Dialog;
16 import android.media.MediaPlayer;
17 import android.media.MediaPlayer.OnCompletionListener;
18 import android.media.MediaPlayer.OnPreparedListener;
19 import android.os.Bundle;
20 import android.os.Handler;
21 import android.util.Log;
22 import android.view.Gravity;
23 import android.view.MotionEvent;
24 import android.view.View;
25 import android.view.View.OnTouchListener;
26 import android.view.ViewGroup;
27 import android.view.ViewGroup.LayoutParams;
28 import android.widget.FrameLayout;
29 import android.widget.VideoView;
30 
31 import java.io.IOException;
32 
39 public class FullScreenVideoUtil implements OnCompletionListener,
40  OnPreparedListener {
41 
42  // Constants
43  public static final int FULLSCREEN_VIDEO_DIALOG_FLAG = 189;
44 
45  public static final int FULLSCREEN_VIDEO_ACTION_SEEK = 190;
46 
47  public static final int FULLSCREEN_VIDEO_ACTION_PLAY = 191;
48 
49  public static final int FULLSCREEN_VIDEO_ACTION_PAUSE = 192;
50 
51  public static final int FULLSCREEN_VIDEO_ACTION_STOP = 193;
52 
53  public static final int FULLSCREEN_VIDEO_ACTION_SOURCE = 194;
54 
55  public static final int FULLSCREEN_VIDEO_ACTION_FULLSCREEN = 195;
56 
57  public static final int FULLSCREEN_VIDEO_ACTION_DURATION = 196;
58 
59  public static final String VIDEOPLAYER_FULLSCREEN = "FullScreenKey";
60 
61  public static final String VIDEOPLAYER_PLAYING = "PlayingKey";
62 
63  public static final String VIDEOPLAYER_POSITION = "PositionKey";
64 
65  public static final String VIDEOPLAYER_SOURCE = "SourceKey";
66 
67  public static final String ACTION_SUCESS = "ActionSuccess";
68 
69  public static final String ACTION_DATA = "ActionData";
70 
71  // The Dialog and other components used in the Dialog for displaying the
72  // video.
73 
74  private Dialog mFullScreenVideoDialog;
75  private FrameLayout mFullScreenVideoHolder;
76  private VideoView mFullScreenVideoView;
77  private CustomMediaController mFullScreenVideoController;
78  private FrameLayout.LayoutParams mMediaControllerParams = new FrameLayout.LayoutParams(
79  LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
80 
81  private Form mForm;
82 
83  // The player whose video is currently being shown.
84  private VideoPlayer mFullScreenPlayer = null;
85 
86  // The data passed in by the player that requested
87  // fullscreen display of its video.
88  private Bundle mFullScreenVideoBundle;
89 
90  // Used for showing a preview of a paused video.
91  private Handler mHandler;
92 
101  public FullScreenVideoUtil(Form form, Handler handler) {
102 
103  mForm = form;
104  mHandler = handler;
105 
106  mFullScreenVideoDialog = new Dialog(mForm,
107  R.style.Theme_NoTitleBar_Fullscreen) {
108  public void onBackPressed() {
109  // Allows the user to force exiting full-screen.
110  Bundle values = new Bundle();
111  values.putInt(VIDEOPLAYER_POSITION,
112  mFullScreenVideoView.getCurrentPosition());
113  values.putBoolean(VIDEOPLAYER_PLAYING,
114  mFullScreenVideoView.isPlaying());
115  values.putString(VIDEOPLAYER_SOURCE,
116  mFullScreenVideoBundle.getString(VIDEOPLAYER_SOURCE));
117  mFullScreenPlayer.fullScreenKilled(values);
118  super.onBackPressed();
119  }
120 
121  public void onStart() {
122  super.onStart();
123  // Prepare the Dialog media.
124  startDialog();
125  }
126  };
127  }
128 
164  public synchronized Bundle performAction(int action, VideoPlayer source,
165  Object data) {
166  Log.i("Form.fullScreenVideoAction", "Actions:" + action + " Source:"
167  + source + ": Current Source:" + mFullScreenPlayer + " Data:" + data);
168  Bundle result = new Bundle();
169  result.putBoolean(ACTION_SUCESS, true);
170  if (source == mFullScreenPlayer) {
171  switch (action) {
173  return doFullScreenVideoAction(source, (Bundle) data);
175  if (showing()) {
176  mFullScreenVideoView.pause();
177  return result;
178  }
179  result.putBoolean(ACTION_SUCESS, false);
180  return result;
182  if (showing()) {
183  mFullScreenVideoView.start();
184  return result;
185  }
186  result.putBoolean(ACTION_SUCESS, false);
187  return result;
189  if (showing()) {
190  mFullScreenVideoView.seekTo((Integer) data);
191  return result;
192  }
193  result.putBoolean(ACTION_SUCESS, false);
194  return result;
196  if (showing()) {
197  mFullScreenVideoView.stopPlayback();
198  return result;
199  }
200  result.putBoolean(ACTION_SUCESS, false);
201  return result;
203  if (showing()) {
204  result.putBoolean(ACTION_SUCESS,setSource((String) data, true));
205  return result;
206  }
207  result.putBoolean(ACTION_SUCESS, false);
208  return result;
210  if (showing()) {
211  result.putInt(ACTION_DATA, mFullScreenVideoView.getDuration());
212  return result;
213  }
214  result.putBoolean(ACTION_SUCESS, false);
215  return result;
216  }
217  } else if (action == FULLSCREEN_VIDEO_ACTION_FULLSCREEN) {
218  // There may be a dialog already being shown.
219  if (showing() && mFullScreenPlayer != null) {
220  Bundle values = new Bundle();
221  values.putInt(VIDEOPLAYER_POSITION,
222  mFullScreenVideoView.getCurrentPosition());
223  values.putBoolean(VIDEOPLAYER_PLAYING,
224  mFullScreenVideoView.isPlaying());
225  values.putString(VIDEOPLAYER_SOURCE,
226  mFullScreenVideoBundle
227  .getString(VIDEOPLAYER_SOURCE));
228  mFullScreenPlayer.fullScreenKilled(values);
229  }
230  return doFullScreenVideoAction(source, (Bundle) data);
231  }
232 
233  // This should never be called.
234  result.putBoolean(ACTION_SUCESS, false);
235  return result;
236  }
237 
238  /*
239  * Displays or hides a full-screen video.
240  */
241  private Bundle doFullScreenVideoAction(VideoPlayer source, Bundle data) {
242  Log.i("Form.doFullScreenVideoAction", "Source:" + source + " Data:" + data);
243  Bundle result = new Bundle();
244  result.putBoolean(ACTION_SUCESS, true);
245  if (data.getBoolean(VIDEOPLAYER_FULLSCREEN) == true) {
246  mFullScreenPlayer = source;
247  mFullScreenVideoBundle = data;
248  if (!mFullScreenVideoDialog.isShowing()) {
249  mForm.showDialog(FULLSCREEN_VIDEO_DIALOG_FLAG);
250  return result;
251  } else {
252  mFullScreenVideoView.pause();
253  result.putBoolean(ACTION_SUCESS, setSource(
254  mFullScreenVideoBundle.getString(VIDEOPLAYER_SOURCE),false));
255  return result;
256  }
257  } else {
258  if (showing()) {
259  result.putBoolean(VIDEOPLAYER_PLAYING,
260  mFullScreenVideoView.isPlaying());
261  result.putInt(VIDEOPLAYER_POSITION,
262  mFullScreenVideoView.getCurrentPosition());
263  result.putString(VIDEOPLAYER_SOURCE,
264  mFullScreenVideoBundle
265  .getString(VIDEOPLAYER_SOURCE));
266 
267  mFullScreenPlayer = null;
268  mFullScreenVideoBundle = null;
269 
270  mForm.dismissDialog(FULLSCREEN_VIDEO_DIALOG_FLAG);
271  return result;
272  }
273  }
274  result.putBoolean(ACTION_SUCESS, false);
275  return result;
276  }
277 
283  public Dialog createFullScreenVideoDialog() {
284 
285  if (mFullScreenVideoBundle == null)
286  Log.i("Form.createFullScreenVideoDialog", "mFullScreenVideoBundle is null");
287 
288  mFullScreenVideoView = new VideoView(mForm);
289  mFullScreenVideoHolder = new FrameLayout(mForm);
290  mFullScreenVideoController = new CustomMediaController(mForm);
291 
292  mFullScreenVideoView.setId(mFullScreenVideoView.hashCode());
293  mFullScreenVideoHolder.setId(mFullScreenVideoHolder.hashCode());
294 
295  mFullScreenVideoView.setMediaController(mFullScreenVideoController);
296 
297  mFullScreenVideoView.setOnTouchListener(new OnTouchListener() {
298 
299  @Override
300  public boolean onTouch(View arg0, MotionEvent arg1) {
301  Log.i("FullScreenVideoUtil..onTouch", "Video Touched!!");
302  return false;
303  }
304  });
305  mFullScreenVideoController.setAnchorView(mFullScreenVideoView);
306 
307  String orientation = mForm.ScreenOrientation();
308  if (orientation.equals("landscape")
309  || orientation.equals("sensorLandscape")
310  || orientation.equals("reverseLandscape")) {
311  mFullScreenVideoView.setLayoutParams(new FrameLayout.LayoutParams(
312  FrameLayout.LayoutParams.WRAP_CONTENT,
313  FrameLayout.LayoutParams.FILL_PARENT, Gravity.CENTER));
314  } else {
315  mFullScreenVideoView.setLayoutParams(new FrameLayout.LayoutParams(
316  FrameLayout.LayoutParams.FILL_PARENT,
317  FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
318  }
319  mFullScreenVideoHolder
320  .setLayoutParams(new ViewGroup.LayoutParams(
321  ViewGroup.LayoutParams.FILL_PARENT,
322  ViewGroup.LayoutParams.FILL_PARENT));
323 
324  mFullScreenVideoHolder.addView(mFullScreenVideoView);
325 
326  // Add the MediaController to the Dialog
327  mFullScreenVideoController.addTo(mFullScreenVideoHolder,
328  mMediaControllerParams);
329 
330  mFullScreenVideoDialog.setContentView(mFullScreenVideoHolder);
331  return mFullScreenVideoDialog;
332  }
333 
341  public void prepareFullScreenVideoDialog(Dialog dia) {
342  mFullScreenVideoView.setOnPreparedListener(this);
343  mFullScreenVideoView.setOnCompletionListener(this);
344  }
345 
349  public boolean dialogInitialized() {
350  return mFullScreenVideoDialog != null;
351  }
352 
357  public boolean showing() {
358  return dialogInitialized() && mFullScreenVideoDialog.isShowing();
359  }
360 
377  public boolean setSource(String source, boolean clearSeek) {
378  try {
379  if (clearSeek) {
380  mFullScreenVideoBundle.putInt(VIDEOPLAYER_POSITION,
381  0);
382  }
383  MediaUtil.loadVideoView(mFullScreenVideoView, mForm, (String) source);
384 
385  mFullScreenVideoBundle.putString(VIDEOPLAYER_SOURCE,
386  source);
387  return true;
388  } catch (PermissionException e) {
389  mForm.dispatchPermissionDeniedEvent(mFullScreenPlayer, "Source", e);
390  return false;
391  } catch (IOException e) {
392  mForm.dispatchErrorOccurredEvent(mFullScreenPlayer, "Source",
394  return false;
395  }
396  }
397 
401  @Override
402  public void onCompletion(MediaPlayer arg0) {
403  if (mFullScreenPlayer != null) {
404  mFullScreenPlayer.Completed();
405  }
406  }
407 
411  @Override
412  public void onPrepared(MediaPlayer arg0) {
413  Log.i(
414  "FullScreenVideoUtil..onPrepared",
415  "Seeking to:"
416  + mFullScreenVideoBundle
417  .getInt(VIDEOPLAYER_POSITION));
418  mFullScreenVideoView.seekTo(mFullScreenVideoBundle
419  .getInt(VIDEOPLAYER_POSITION));
420  if (mFullScreenVideoBundle
421  .getBoolean(VIDEOPLAYER_PLAYING)) {
422  mFullScreenVideoView.start();
423  } else {
424  mFullScreenVideoView.start();
425  mHandler.postDelayed(new Runnable() {
426 
427  @Override
428  public void run() {
429  mFullScreenVideoView.pause();
430  }
431  }, 100);
432  }
433  }
434 
438  public void startDialog() {
439  try {
440  MediaUtil.loadVideoView(mFullScreenVideoView, mForm,
441  mFullScreenVideoBundle
442  .getString(VIDEOPLAYER_SOURCE));
443  } catch (PermissionException e) {
444  mForm.dispatchPermissionDeniedEvent(mFullScreenPlayer, "Source", e);
445  } catch (IOException e) {
446  mForm.dispatchErrorOccurredEvent(mFullScreenPlayer, "Source",
447  ErrorMessages.ERROR_UNABLE_TO_LOAD_MEDIA, mFullScreenVideoBundle
448  .getString(VIDEOPLAYER_SOURCE));
449  return;
450  }
451  }
452 }
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_SOURCE
static final int FULLSCREEN_VIDEO_ACTION_SOURCE
Definition: FullScreenVideoUtil.java:53
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_STOP
static final int FULLSCREEN_VIDEO_ACTION_STOP
Definition: FullScreenVideoUtil.java:51
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_UNABLE_TO_LOAD_MEDIA
static final int ERROR_UNABLE_TO_LOAD_MEDIA
Definition: ErrorMessages.java:93
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.setSource
boolean setSource(String source, boolean clearSeek)
Definition: FullScreenVideoUtil.java:377
com.google.appinventor.components.runtime.util.FullScreenVideoUtil
Definition: FullScreenVideoUtil.java:39
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.ACTION_DATA
static final String ACTION_DATA
Definition: FullScreenVideoUtil.java:69
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FullScreenVideoUtil
FullScreenVideoUtil(Form form, Handler handler)
Definition: FullScreenVideoUtil.java:101
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.showing
boolean showing()
Definition: FullScreenVideoUtil.java:357
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.dialogInitialized
boolean dialogInitialized()
Definition: FullScreenVideoUtil.java:349
com.google.appinventor.components
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.VIDEOPLAYER_SOURCE
static final String VIDEOPLAYER_SOURCE
Definition: FullScreenVideoUtil.java:65
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.VIDEOPLAYER_POSITION
static final String VIDEOPLAYER_POSITION
Definition: FullScreenVideoUtil.java:63
com.google.appinventor.components.runtime.util.MediaUtil
Definition: MediaUtil.java:53
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.onCompletion
void onCompletion(MediaPlayer arg0)
Definition: FullScreenVideoUtil.java:402
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.createFullScreenVideoDialog
Dialog createFullScreenVideoDialog()
Definition: FullScreenVideoUtil.java:283
com.google.appinventor.components.runtime.Form.dispatchPermissionDeniedEvent
void dispatchPermissionDeniedEvent(final Component component, final String functionName, final PermissionException exception)
Definition: Form.java:987
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.startDialog
void startDialog()
Definition: FullScreenVideoUtil.java:438
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.ACTION_SUCESS
static final String ACTION_SUCESS
Definition: FullScreenVideoUtil.java:67
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_DURATION
static final int FULLSCREEN_VIDEO_ACTION_DURATION
Definition: FullScreenVideoUtil.java:57
com.google.appinventor.components.runtime.VideoPlayer
Definition: VideoPlayer.java:121
com.google.appinventor.components.runtime.Form.ScreenOrientation
String ScreenOrientation()
Definition: Form.java:1434
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_PAUSE
static final int FULLSCREEN_VIDEO_ACTION_PAUSE
Definition: FullScreenVideoUtil.java:49
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.performAction
synchronized Bundle performAction(int action, VideoPlayer source, Object data)
Definition: FullScreenVideoUtil.java:164
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_DIALOG_FLAG
static final int FULLSCREEN_VIDEO_DIALOG_FLAG
Definition: FullScreenVideoUtil.java:43
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.prepareFullScreenVideoDialog
void prepareFullScreenVideoDialog(Dialog dia)
Definition: FullScreenVideoUtil.java:341
com.google.appinventor.components.runtime.VideoPlayer.Completed
void Completed()
Definition: VideoPlayer.java:388
com.google.appinventor.components.runtime.errors.PermissionException
Definition: PermissionException.java:16
com.google.appinventor.components.runtime.VideoPlayer.fullScreenKilled
void fullScreenKilled(Bundle data)
Definition: VideoPlayer.java:589
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_FULLSCREEN
static final int FULLSCREEN_VIDEO_ACTION_FULLSCREEN
Definition: FullScreenVideoUtil.java:55
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_PLAY
static final int FULLSCREEN_VIDEO_ACTION_PLAY
Definition: FullScreenVideoUtil.java:47
com.google
com.google.appinventor.components.runtime.util.MediaUtil.loadVideoView
static void loadVideoView(VideoView videoView, Form form, String mediaPath)
Definition: MediaUtil.java:793
com
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.VIDEOPLAYER_FULLSCREEN
static final String VIDEOPLAYER_FULLSCREEN
Definition: FullScreenVideoUtil.java:59
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.util.CustomMediaController.addTo
boolean addTo(ViewGroup parent, ViewGroup.LayoutParams params)
Definition: CustomMediaController.java:75
com.google.appinventor.components.runtime.util.CustomMediaController.setAnchorView
void setAnchorView(View anchorView)
Definition: CustomMediaController.java:93
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.onPrepared
void onPrepared(MediaPlayer arg0)
Definition: FullScreenVideoUtil.java:412
com.google.appinventor.components.runtime.util.CustomMediaController
Definition: CustomMediaController.java:26
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.FULLSCREEN_VIDEO_ACTION_SEEK
static final int FULLSCREEN_VIDEO_ACTION_SEEK
Definition: FullScreenVideoUtil.java:45
com.google.appinventor.components.runtime.Form
Definition: Form.java:126
com.google.appinventor.components.runtime.util.FullScreenVideoUtil.VIDEOPLAYER_PLAYING
static final String VIDEOPLAYER_PLAYING
Definition: FullScreenVideoUtil.java:61
com.google.appinventor