use of org.edx.mobile.model.download.NativeDownloadModel in project edx-app-android by edx.
the class DownloadCompleteReceiver method handleDownloadCompleteIntent.
private void handleDownloadCompleteIntent(final Intent data) {
if (data.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
final long id = data.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id != -1) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
logger.debug("Received download notification for id: " + id);
// check if download was SUCCESSFUL
NativeDownloadModel nm = environment.getDownloadManager().getDownload(id);
if (nm == null || nm.status != DownloadManager.STATUS_SUCCESSFUL) {
logger.debug("Download seems failed or cancelled for id : " + id);
environment.getDownloadManager().removeDownloads(id);
return;
} else {
logger.debug("Download successful for id : " + id);
}
// mark download as completed
environment.getStorage().markDownloadAsComplete(id, new DataCallback<VideoModel>() {
@Override
public void onResult(VideoModel result) {
if (result != null) {
DownloadEntry download = (DownloadEntry) result;
AnalyticsRegistry analyticsRegistry = environment.getAnalyticsRegistry();
analyticsRegistry.trackDownloadComplete(download.videoId, download.eid, download.lmsUrl);
}
}
@Override
public void onFail(Exception ex) {
logger.error(ex);
}
});
}
});
}
}
}
use of org.edx.mobile.model.download.NativeDownloadModel in project edx-app-android by edx.
the class IDownloadManagerImpl method getProgressDetailsForDownloads.
@Override
public synchronized NativeDownloadModel getProgressDetailsForDownloads(long[] dmids) {
// Need to check first if the download manager service is enabled
if (!isDownloadManagerEnabled())
return null;
final NativeDownloadModel downloadProgressModel = new NativeDownloadModel();
final Query query = new Query();
query.setFilterById(dmids);
final Cursor c = dm.query(query);
if (c.moveToFirst()) {
downloadProgressModel.downloadCount = c.getCount();
do {
downloadProgressModel.downloaded += c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
downloadProgressModel.size += c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
} while (c.moveToNext());
c.close();
return downloadProgressModel;
}
c.close();
return null;
}
use of org.edx.mobile.model.download.NativeDownloadModel in project edx-app-android by edx.
the class IDownloadManagerImpl method getDownload.
@Override
public synchronized NativeDownloadModel getDownload(long dmid) {
// Need to check first if the download manager service is enabled
if (!isDownloadManagerEnabled())
return null;
try {
Query query = new Query();
query.setFilterById(dmid);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
long downloaded = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
long size = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
String filepath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
c.close();
NativeDownloadModel ndm = new NativeDownloadModel();
ndm.dmid = dmid;
ndm.downloaded = downloaded;
ndm.size = size;
ndm.filepath = filepath;
ndm.status = status;
return ndm;
}
c.close();
} catch (Exception e) {
logger.error(e);
}
return null;
}
use of org.edx.mobile.model.download.NativeDownloadModel in project edx-app-android by edx.
the class Storage method addDownload.
public long addDownload(VideoModel model) {
if (model.getVideoUrl() == null || model.getVideoUrl().length() <= 0) {
return -1;
}
VideoModel videoByUrl = db.getVideoByVideoUrl(model.getVideoUrl(), null);
db.addVideoData(model, null);
if (model.isVideoForWebOnly())
// we may need to return different error code.
return -1;
if (videoByUrl == null || videoByUrl.getDmId() < 0) {
boolean downloadPreference = pref.isDownloadOverWifiOnly();
if (NetworkUtil.isOnZeroRatedNetwork(context, config)) {
// If the device has zero rated network, then allow downloading
// on mobile network even if user has "Only on wifi" settings as ON
downloadPreference = false;
}
// Fail the download if download directory isn't available
final File downloadDirectory = pref.getDownloadDirectory();
if (downloadDirectory == null)
return -1;
// there is no any download ever marked for this URL
// so, add a download and map download info to given video
long dmid = dm.addDownload(downloadDirectory, model.getVideoUrl(), downloadPreference, model.getTitle());
if (dmid == -1) {
// Download did not start for the video because of an issue in DownloadManager
return -1;
}
NativeDownloadModel download = dm.getDownload(dmid);
if (download != null) {
// copy download info
model.setDownloadingInfo(download);
}
} else {
// download for this URL already exists, just map download info to given video
model.setDownloadInfo(videoByUrl);
}
db.updateDownloadingVideoInfoByVideoId(model, new DataCallback<Integer>() {
@Override
public void onResult(Integer noOfRows) {
if (noOfRows > 1) {
logger.warn("Should have updated only one video, " + "but seems more than one videos are updated");
}
logger.debug("Video download info updated for " + noOfRows + " videos");
}
@Override
public void onFail(Exception ex) {
logger.error(ex);
}
});
return model.getDmId();
}
use of org.edx.mobile.model.download.NativeDownloadModel in project edx-app-android by edx.
the class DownloadTests method testAddDownload.
@Test
public void testAddDownload() throws Exception {
File dir = null;
try {
dir = new UserPrefs(RuntimeEnvironment.application, MainApplication.getEnvironment(RuntimeEnvironment.application).getLoginPrefs()).getDownloadDirectory();
} catch (Exception ex) {
// it happens in CI environment and we should skip the test.
print("dir is null, it happens in CI environment and we should skip the test.");
}
if (dir == null)
return;
String url = "https://s3.amazonaws.com/edx-course-videos/edx-edx101/EDXSPCPJSP13-H010000_100.mp4";
// test add new download
long dmid = dm.addDownload(dir, url, true, "Video 1 title");
long dmid2 = dm.addDownload(dir, url, false, "Video 2 title");
assertTrue("invalid dmid=" + dmid, dmid > 0);
assertTrue("invalid dmid2=" + dmid2, dmid2 > 0);
print("new download dmid: " + dmid);
print("new download dmid2: " + dmid2);
// wait for downloads to begin
Thread.sleep(20 * 1000);
// test get download info
NativeDownloadModel model = dm.getDownload(dmid);
assertNotNull(model);
print("downloading [dmid=" + dmid + "] to: " + model.filepath);
int progress = dm.getProgressForDownload(dmid);
assertTrue(progress >= 0 && progress <= 100);
print("progress=" + progress);
int averageProgress = dm.getAverageProgressForDownloads(new long[] { dmid, dmid2 });
assertTrue(averageProgress >= 0 && averageProgress <= 100);
print("averageProgress=" + averageProgress);
// test remove downloads
boolean removed = dm.removeDownloads(dmid) > 0;
assertTrue("failed to remove dmid: " + dmid, removed);
removed = dm.removeDownloads(dmid2) > 0;
assertTrue("failed to remove dmid: " + dmid2, removed);
model = dm.getDownload(dmid);
assertNull("download not removed, dmid: " + dmid, model);
print("removed the downloads");
}
Aggregations