Created
June 26, 2015 22:27
-
-
Save mgp/969f29f1c47402504990 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.khanacademy.core.net.videodownloadmanager; | |
import org.khanacademy.core.net.downloadmanager.DownloadManager; | |
import org.khanacademy.core.net.downloadmanager.okhttp.OkHttpDownloadManager; | |
import org.khanacademy.core.net.downloadmanager.okhttp.OkHttpDownloadManager.ResourceFunctions; | |
import org.khanacademy.core.topictree.models.Video; | |
import org.khanacademy.core.topictree.models.Video.DownloadFormat; | |
import com.squareup.okhttp.OkHttpClient; | |
import java.io.File; | |
import java.net.URI; | |
/** | |
* Downloads videos using OkHttp. | |
*/ | |
public final class OkHttpVideoDownloadManager { | |
private static final ResourceFunctions<Video, String> RESOURCE_FUNCTIONS = new ResourceFunctions<Video, String>() { | |
private static final String MP4_EXTENSION = "mp4"; | |
@Override | |
public String getKey(final Video video) { | |
return video.contentId; | |
} | |
@Override | |
public URI getUri(final Video video) { | |
return video.downloadUrls.get(DownloadFormat.MP4); | |
} | |
@Override | |
public File getFile(final File storageDirectory, final Video video) { | |
final String filename = String.format("%s.%s", video.contentId, MP4_EXTENSION); | |
return new File(storageDirectory, filename); | |
} | |
}; | |
/** | |
* @return a {@link DownloadManager} for {@link Video} instances | |
*/ | |
public static DownloadManager<Video> createVideoDownloadManager( | |
final File storageDirectory, final OkHttpClient okHttpClient) { | |
return new OkHttpDownloadManager<>(storageDirectory, okHttpClient, RESOURCE_FUNCTIONS); | |
} | |
// Static factory methods only. | |
private OkHttpVideoDownloadManager() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment