Search in sources :

Example 1 with HasDownloadEntry

use of org.edx.mobile.model.course.HasDownloadEntry in project edx-app-android by edx.

the class VideoDownloadHelper method startDownloadVideos.

private void startDownloadVideos(List<? extends HasDownloadEntry> model, FragmentActivity activity, DownloadManagerCallback callback) {
    long downloadSize = 0;
    ArrayList<DownloadEntry> downloadList = new ArrayList<>();
    int downloadCount = 0;
    for (HasDownloadEntry v : model) {
        DownloadEntry de = v.getDownloadEntry(storage);
        if (!TextUtils.isEmpty(v.getDownloadUrl())) {
            // Prefer download url to download
            de.url = v.getDownloadUrl();
        }
        if (null == de || de.downloaded == DownloadEntry.DownloadedState.DOWNLOADING || de.downloaded == DownloadEntry.DownloadedState.DOWNLOADED || de.isVideoForWebOnly) {
            continue;
        } else {
            downloadSize = downloadSize + de.getSize();
            downloadList.add(de);
            downloadCount++;
        }
    }
    if (downloadSize > MemoryUtil.getAvailableExternalMemory(activity)) {
        ((BaseFragmentActivity) activity).showInfoMessage(activity.getString(R.string.file_size_exceeded));
        callback.updateListUI();
        EventBus.getDefault().post(new BulkVideosDownloadCancelledEvent());
    } else {
        if (isDownloadSizeWithinLimit(downloadSize, MemoryUtil.GB) && !downloadList.isEmpty()) {
            startDownload(downloadList, activity, callback);
            final DownloadEntry downloadEntry = downloadList.get(0);
            analyticsRegistry.trackSubSectionBulkVideoDownload(downloadEntry.getSectionName(), downloadEntry.getChapterName(), downloadEntry.getEnrollmentId(), downloadCount);
        } else {
            showDownloadSizeExceedDialog(downloadList, downloadCount, activity, callback);
        }
    }
}
Also used : BaseFragmentActivity(org.edx.mobile.base.BaseFragmentActivity) ArrayList(java.util.ArrayList) BulkVideosDownloadCancelledEvent(org.edx.mobile.module.storage.BulkVideosDownloadCancelledEvent) HasDownloadEntry(org.edx.mobile.model.course.HasDownloadEntry) DownloadEntry(org.edx.mobile.model.db.DownloadEntry) HasDownloadEntry(org.edx.mobile.model.course.HasDownloadEntry)

Example 2 with HasDownloadEntry

use of org.edx.mobile.model.course.HasDownloadEntry in project edx-app-android by edx.

the class ApiTests method testGetCourseStructure.

@Test
public void testGetCourseStructure() throws Exception {
    login();
    // General overall testing of CourseComponent API without recursion
    EnrolledCoursesResponse e = executeStrict(courseAPI.getEnrolledCourses()).get(0);
    final String courseId = e.getCourse().getId();
    final CourseStructureV1Model model = executeStrict(courseAPI.getCourseStructure(courseId));
    final CourseComponent courseComponent = (CourseComponent) CourseAPI.normalizeCourseStructure(model, courseId);
    assertNotNull(courseComponent);
    assertNotNull(courseComponent.getRoot());
    assertEquals(courseId, courseComponent.getCourseId());
    List<IBlock> children = courseComponent.getChildren();
    assertNotNull(children);
    List<CourseComponent> childContainers = new ArrayList<>();
    List<CourseComponent> childLeafs = new ArrayList<>();
    for (IBlock c : children) {
        assertTrue(c instanceof CourseComponent);
        final CourseComponent child = (CourseComponent) c;
        assertEquals(child, courseComponent.find(new Filter<CourseComponent>() {

            @Override
            public boolean apply(CourseComponent component) {
                return child.getId().equals(component.getId());
            }
        }));
        List<IBlock> grandchildren = child.getChildren();
        for (IBlock gc : grandchildren) {
            assertTrue(gc instanceof CourseComponent);
            final CourseComponent grandchild = (CourseComponent) c;
            assertEquals(grandchild, courseComponent.find(new Filter<CourseComponent>() {

                @Override
                public boolean apply(CourseComponent component) {
                    return grandchild.getId().equals(component.getId());
                }
            }));
        }
        assertNull(child.find(new Filter<CourseComponent>() {

            @Override
            public boolean apply(CourseComponent component) {
                return courseComponent.getId().equals(component.getId());
            }
        }));
        if (child.isContainer()) {
            childContainers.add(child);
        } else {
            childLeafs.add(child);
        }
    }
    assertEquals(childContainers, courseComponent.getChildContainers());
    assertEquals(childLeafs, courseComponent.getChildLeafs());
    assertTrue(courseComponent.isLastChild());
    int childrenSize = children.size();
    assertTrue(childrenSize > 0);
    assertTrue(((CourseComponent) children.get(childrenSize - 1)).isLastChild());
    BlockType blockType = courseComponent.getType();
    assertSame(courseComponent, courseComponent.getAncestor(Integer.MAX_VALUE));
    assertSame(courseComponent, courseComponent.getAncestor(EnumSet.of(blockType)));
    List<VideoBlockModel> videos = courseComponent.getVideos();
    assertNotNull(videos);
    for (HasDownloadEntry video : videos) {
        assertNotNull(video);
        assertTrue(video instanceof CourseComponent);
        CourseComponent videoComponent = (CourseComponent) video;
        assertFalse(videoComponent.isContainer());
        assertEquals(BlockType.VIDEO, videoComponent.getType());
    }
    for (BlockType type : BlockType.values()) {
        EnumSet<BlockType> typeSet = EnumSet.of(type);
        List<CourseComponent> typeComponents = new ArrayList<>();
        courseComponent.fetchAllLeafComponents(typeComponents, typeSet);
        for (CourseComponent typeComponent : typeComponents) {
            assertEquals(type, typeComponent.getType());
            verifyModelParsing(typeComponent);
        }
        if (type != blockType) {
            assertNotSame(courseComponent, courseComponent.getAncestor(EnumSet.of(type)));
        }
    }
    BlockPath path = courseComponent.getPath();
    assertNotNull(path);
    assertEquals(1, path.getPath().size());
    assertSame(courseComponent, path.get(0));
    List<CourseComponent> leafComponents = new ArrayList<>();
    courseComponent.fetchAllLeafComponents(leafComponents, EnumSet.allOf(BlockType.class));
    for (CourseComponent leafComponent : leafComponents) {
        BlockPath leafPath = leafComponent.getPath();
        assertNotNull(leafPath);
        int pathSize = leafPath.getPath().size();
        assertTrue(pathSize > 1);
        CourseComponent component = leafComponent;
        for (int i = pathSize - 1; i >= 0; i--) {
            assertSame(component, leafPath.get(i));
            component = component.getParent();
        }
    }
}
Also used : IBlock(org.edx.mobile.model.course.IBlock) BlockPath(org.edx.mobile.model.course.BlockPath) ArrayList(java.util.ArrayList) VideoBlockModel(org.edx.mobile.model.course.VideoBlockModel) CourseComponent(org.edx.mobile.model.course.CourseComponent) HasDownloadEntry(org.edx.mobile.model.course.HasDownloadEntry) Filter(org.edx.mobile.model.Filter) BlockType(org.edx.mobile.model.course.BlockType) EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) CourseStructureV1Model(org.edx.mobile.model.course.CourseStructureV1Model) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 HasDownloadEntry (org.edx.mobile.model.course.HasDownloadEntry)2 BaseFragmentActivity (org.edx.mobile.base.BaseFragmentActivity)1 Filter (org.edx.mobile.model.Filter)1 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)1 BlockPath (org.edx.mobile.model.course.BlockPath)1 BlockType (org.edx.mobile.model.course.BlockType)1 CourseComponent (org.edx.mobile.model.course.CourseComponent)1 CourseStructureV1Model (org.edx.mobile.model.course.CourseStructureV1Model)1 IBlock (org.edx.mobile.model.course.IBlock)1 VideoBlockModel (org.edx.mobile.model.course.VideoBlockModel)1 DownloadEntry (org.edx.mobile.model.db.DownloadEntry)1 BulkVideosDownloadCancelledEvent (org.edx.mobile.module.storage.BulkVideosDownloadCancelledEvent)1 Test (org.junit.Test)1