001package com.baidu.cloud.media.download;
002
003import java.util.Observable;
004
005/**
006 * downloader interface
007 * 
008 * @author shunwei
009 * 
010 */
011public abstract class DownloadableVideoItem extends Observable {
012
013    public static final String[] ERROR_CODE_DESC = {"ERROR_CODE_NO_ERROR", "ERROR_CODE_INVALID_URL",
014            "ERROR_CODE_NETWORK_FAILED", "ERROR_CODE_SDCARD_UNMOUNTED", "ERROR_CODE_M3U8_INVALID_FORMAT",
015            "ERROR_CODE_M3U8_SAVE_FAILED", "ERROR_CODE_M3U8_DRM_INVALID", "ERROR_CODE_TS_SAVE_FAILED"};
016    public static final int ERROR_CODE_NO_ERROR = 0;
017    public static final int ERROR_CODE_INVALID_URL = 1;
018    public static final int ERROR_CODE_NETWORK_FAILED = 2;
019    public static final int ERROR_CODE_SDCARD_UNMOUNTED = 3;
020    public static final int ERROR_CODE_M3U8_INVALID_FORMAT = 4;
021    public static final int ERROR_CODE_M3U8_SAVE_FAILED = 5;
022    public static final int ERROR_CODE_M3U8_DRM_INVALID = 6;
023    public static final int ERROR_CODE_TS_SAVE_FAILED = 7;
024    
025    protected String url;
026
027    protected String folderPath;
028    protected String fileName;
029
030    protected volatile int progress; // 2341 means 23.41%
031
032    protected volatile DownloadStatus downloadStatus = DownloadStatus.NONE;
033    protected String failReason;
034    
035    protected volatile int errorCode;
036
037    /**
038     * 获取错误码
039     * 当DownloadStatus的状态为ERROR时,通过该接口获取错误码
040     * @return
041     */
042    public int getErrorCode() {
043        return errorCode;
044    }
045
046    /**
047     * 下载状态 解析hls文件失败时,返回错误 ERROR;下载ts遇到网络问题时或被强关时,返回 暂停状态;
048     */
049    public static enum DownloadStatus {
050        NONE(0, "first add"), DOWNLOADING(1, "downloading videos"), PAUSED(2, "paused"), COMPLETED(3, "completed"),
051        ERROR(4, "failed to download"), DELETED(5, "delete manually"),
052        PENDING(6, "pending, will start automatically(blocked by Parallel Strategy)");
053
054        private int code;
055        private String msg;
056
057        private DownloadStatus(int code, String msg) {
058            this.code = code;
059            this.msg = msg;
060        }
061
062        public int getCode() {
063            return code;
064        }
065
066        public String getMessage() {
067            return msg;
068        }
069    }
070
071    /**
072     * Get the URL (in String)
073     */
074    public String getUrl() {
075        return url;
076    }
077
078    /**
079     * 获取本地文件的全路径名
080     * @return
081     */
082    public String getLocalAbsolutePath() {
083        if (fileName == null || fileName.equals("")) {
084            return folderPath;
085        } else if (folderPath == null || folderPath.equals("")) {
086            return null;
087        }
088        return folderPath + "/" + fileName;
089    }
090
091    /**
092     * Get the downloaded file's size 注:总大小需要换算 = ts1 * totalTime /
093     * ts1_time;暂不对外提供
094     */
095    // public int getFileSize();
096
097    /**
098     * Get the current progress of the download
099     * 
100     * @return progress the progress of downloading. 0<=progress<=100
101     */
102    public float getProgress() {
103        return ((float) progress) / 100;
104    }
105
106    /**
107     * Get current state of the downloader
108     */
109    public DownloadStatus getStatus() {
110        return downloadStatus;
111    }
112
113    /**
114     * 获取错误描述
115     * 推荐使用getErrorCode接口
116     * @return
117     */
118    public String getFailReason() {
119        return failReason;
120    }
121
122}