Search in sources :

Example 1 with NativeDownloadModel

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);
                        }
                    });
                }
            });
        }
    }
}
Also used : AnalyticsRegistry(org.edx.mobile.module.analytics.AnalyticsRegistry) NativeDownloadModel(org.edx.mobile.model.download.NativeDownloadModel) DataCallback(org.edx.mobile.module.db.DataCallback) VideoModel(org.edx.mobile.model.VideoModel) DownloadEntry(org.edx.mobile.model.db.DownloadEntry)

Example 2 with NativeDownloadModel

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;
}
Also used : Query(android.app.DownloadManager.Query) NativeDownloadModel(org.edx.mobile.model.download.NativeDownloadModel) Cursor(android.database.Cursor)

Example 3 with NativeDownloadModel

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;
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) NativeDownloadModel(org.edx.mobile.model.download.NativeDownloadModel)

Example 4 with NativeDownloadModel

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();
}
Also used : NativeDownloadModel(org.edx.mobile.model.download.NativeDownloadModel) VideoModel(org.edx.mobile.model.VideoModel) File(java.io.File)

Example 5 with NativeDownloadModel

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");
}
Also used : NativeDownloadModel(org.edx.mobile.model.download.NativeDownloadModel) File(java.io.File) UserPrefs(org.edx.mobile.module.prefs.UserPrefs) Test(org.junit.Test)

Aggregations

NativeDownloadModel (org.edx.mobile.model.download.NativeDownloadModel)5 Query (android.app.DownloadManager.Query)2 Cursor (android.database.Cursor)2 File (java.io.File)2 VideoModel (org.edx.mobile.model.VideoModel)2 DownloadEntry (org.edx.mobile.model.db.DownloadEntry)1 AnalyticsRegistry (org.edx.mobile.module.analytics.AnalyticsRegistry)1 DataCallback (org.edx.mobile.module.db.DataCallback)1 UserPrefs (org.edx.mobile.module.prefs.UserPrefs)1 Test (org.junit.Test)1