Skip to content

Commit

Permalink
Fix 775 on tabris-ios: Enhance the video widget on iOS to cache videos
Browse files Browse the repository at this point in the history
  • Loading branch information
evolanakis committed Oct 26, 2015
1 parent 587259f commit 1270e0f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class VideoTest {
public void setUp() {
Display display = new Display();
parent = new Shell( display );
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
playbackListener = mock( PlaybackListener.class );
video.addPlaybackListener( playbackListener );
presentationListener = mock( PresentationListener.class );
Expand All @@ -79,7 +79,7 @@ public void testPresentationListenerIsSerializable() {

@Test
public void testGetUrl() throws MalformedURLException {
assertEquals( new URL( "http://test.com" ).toString(), video.getURL().toString() );
assertEquals( new URL( "http://localhost/video.mp4" ).toString(), video.getURL().toString() );
}

@Test
Expand All @@ -96,7 +96,7 @@ public void testMallformedUrl() {

@Test( expected = IllegalArgumentException.class )
public void testNullParent() {
new Video( "http://test.com", null );
new Video( "http://localhost/video.mp4", null );
}

@Test
Expand Down Expand Up @@ -408,23 +408,23 @@ public void testPlaybackAdapterPresentationModeChange() {

@Test
public void testHasntPlaybackListenerDefault() {
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

assertFalse( adapter.hasPlaybackListener() );
}

@Test
public void testHasntPresentationListenerDefault() {
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

assertFalse( adapter.hasPresentationListener() );
}

@Test
public void testHasPlaybackListener() {
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
video.addPlaybackListener( mock( PlaybackListener.class ) );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

Expand All @@ -433,7 +433,7 @@ public void testHasPlaybackListener() {

@Test
public void testHasPresentationListener() {
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
video.addPresentationListener( mock( PresentationListener.class ) );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

Expand All @@ -442,7 +442,7 @@ public void testHasPresentationListener() {

@Test
public void testHasPlaybackListenerWithAddAndRemove() {
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
PlaybackListener listener = mock( PlaybackListener.class );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

Expand All @@ -454,7 +454,7 @@ public void testHasPlaybackListenerWithAddAndRemove() {

@Test
public void testHasPresentationListenerWithAddAndRemove() {
video = new Video( "http://test.com", parent );
video = new Video( "http://localhost/video.mp4", parent );
PresentationListener listener = mock( PresentationListener.class );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

Expand All @@ -464,4 +464,35 @@ public void testHasPresentationListenerWithAddAndRemove() {
assertFalse( adapter.hasPresentationListener() );
}

@Test
public void testCacheSizeDefaultsToZero() {
video = new Video( "http://localhost/video.mp4", parent );

assertEquals( 0, video.getCacheSize() );
}

@Test
public void testGetCacheSize() {
video = new Video( "http://localhost/video.mp4", 100, parent );

assertEquals( 100, video.getCacheSize() );
}

@Test(expected = IllegalArgumentException.class)
public void testSetCacheSize_IllegalValue() {
video = new Video( "http://localhost/video.mp4", -1, parent );
}

@Test
public void testClearCache() {
video = new Video( "http://localhost/video.mp4", parent );
PlaybackAdapter adapter = video.getAdapter( PlaybackAdapter.class );

assertFalse( adapter.isClearCacheRequested() );

video.clearCache();

assertTrue( adapter.isClearCacheRequested() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public class Constants {
public static final String TYPE_VIDEO = "tabris.widgets.Video";
public static final String EVENT_PRESENTATION = "Presentation";
public static final String EVENT_PLAYBACK = "Playback";
public static final String METHOD_CLEAR_CACHE = "clearCache";
public static final String PROPERTY_PLAYBACK = "playback";
public static final String PROPERTY_PRESENTATION = "presentation";
public static final String PROPERTY_CACHE_SIZE = "cacheSize";

// Swipe Constants
public static final String TYPE_SWIPE = "tabris.Swipe";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static com.eclipsesource.tabris.internal.Constants.EVENT_PLAYBACK;
import static com.eclipsesource.tabris.internal.Constants.EVENT_PRESENTATION;
import static com.eclipsesource.tabris.internal.Constants.PROPERTY_CACHE_SIZE;
import static com.eclipsesource.tabris.internal.Constants.PROPERTY_PARENT;
import static com.eclipsesource.tabris.internal.Constants.PROPERTY_PLAYBACK;
import static com.eclipsesource.tabris.internal.Constants.PROPERTY_PRESENTATION;
Expand Down Expand Up @@ -76,6 +77,11 @@ public void renderChanges( Widget widget ) throws IOException {
}
renderListener( video, EVENT_PLAYBACK, adapter.hasPlaybackListener(), false );
renderListener( video, EVENT_PRESENTATION, adapter.hasPresentationListener(), false );
if( adapter.isClearCacheRequested() ) {
adapter.resetClearCache();
RemoteObject remoteObject = RemoteObjectFactory.getRemoteObject( video );
remoteObject.call( "clearCache", null );
}
}

@Override
Expand All @@ -84,6 +90,7 @@ public void renderInitialization( Widget widget ) throws IOException {
RemoteObject remoteObject = RemoteObjectFactory.createRemoteObject( video, TYPE_VIDEO );
remoteObject.setHandler( new VideoOperationHandler( video ) );
remoteObject.set( PROPERTY_PARENT, WidgetUtil.getId( video.getParent() ) );
remoteObject.set( PROPERTY_CACHE_SIZE, video.getCacheSize() );
remoteObject.set( PROPERTY_URL, video.getURL().toString() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,43 @@ public static enum Presentation {
private static final float PLAY_SPEED = 1;
private static final float HALT_SPEED = 0;

private URL videoUrl;
private final List<PlaybackListener> playbackListeners = new ArrayList<PlaybackListener>();
private final List<PresentationListener> presentationListeners = new ArrayList<PresentationListener>();
private final Map<PlaybackOptions, Object> playbackOptions = new HashMap<PlaybackOptions, Object>();

private URL videoUrl;
private int cacheSizeMb;
private boolean wantsToClearCache;

/**
* Creates a new Video object.
*
* @param videoUrl a String with the url of the video to play; never null
* @param parent the parent Composite; never null
* @throws IllegalArgumentException when the passed URL string is not a valid
* url.
*/
public Video( String videoUrl, Composite parent ) throws IllegalArgumentException {
this(videoUrl, 0, parent);
}

/**
* <p>
* Creates a new Video object.
* </p>
*
* @throws IllegalArgumentException when the passed URl string is not a valid url.
* @param videoUrl a String with the url of the video to play; never null
* @param cacheSizeMb the maximum allowed size of the client-side video cache
* in megabytes (0 or greater).
* @param parent the parent Composite; never null
* @throws IllegalArgumentException when the passed URl string is not a valid
* url.
* @since 1.4.8
*/
public Video( String videoUrl, Composite parent ) throws IllegalArgumentException {
public Video( String videoUrl, int cacheSizeMb, Composite parent ) throws IllegalArgumentException {
super( parent, SWT.NONE );
initiateDefaultValues();
setCacheSize( cacheSizeMb );
assignUrl( videoUrl );
}

Expand All @@ -167,6 +189,13 @@ private void initiateDefaultValues() {
playbackOptions.put( PlaybackOptions.REPEAT, Boolean.valueOf( false ) );
}

private void setCacheSize( int cacheSizeMb ) {
if (cacheSizeMb < 0) {
throw new IllegalArgumentException( String.valueOf( cacheSizeMb ) );
}
this.cacheSizeMb = cacheSizeMb;
}

private void assignUrl( String videoUrl ) {
try {
this.videoUrl = new URL( videoUrl );
Expand Down Expand Up @@ -385,6 +414,27 @@ public void removePresentationListener( PresentationListener listener ) {
presentationListeners.remove( listener );
}

/**
* Returns the maximum allowed video cache size in megabytes. The default is 0.
*
* @return cache size in megabytes (0 or greater).
* @since 1.4.8
*/
public int getCacheSize() {
return cacheSizeMb;
}

/**
* Instructs the device to remove all cached content from the video cache.
* <p>
* The call is asynchronous, the files are <b>not</b> removed immediately upon returning.
*
* @since 1.4.8
*/
public void clearCache() {
wantsToClearCache = true;
}

private void firePlayback( Playback oldMode, Playback newMode ) {
if( oldMode != newMode ) {
List<PlaybackListener> listeners = new ArrayList<PlaybackListener>( playbackListeners );
Expand Down Expand Up @@ -443,6 +493,12 @@ public boolean hasPresentationListener() {
return !presentationListeners.isEmpty();
}

}
public boolean isClearCacheRequested() {
return wantsToClearCache;
}

public void resetClearCache() {
wantsToClearCache = false;
}
}
}

0 comments on commit 1270e0f

Please sign in to comment.