Java Code Examples for com.lzy.okgo.model.Progress

The following examples show how to use com.lzy.okgo.model.Progress. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: okhttp-OkGo   Source File: UploadAdapter.java    License: Apache License 2.0 6 votes vote down vote up
@OnClick(R.id.upload)
public void upload() {
    Progress progress = task.progress;
    switch (progress.status) {
        case Progress.PAUSE:
        case Progress.NONE:
        case Progress.ERROR:
            task.start();
            break;
        case Progress.LOADING:
            task.pause();
            break;
        case Progress.FINISH:
            break;
    }
    refresh(progress);
}
 
Example 2
Source Project: okhttp-OkGo   Source File: DownloadAdapter.java    License: Apache License 2.0 6 votes vote down vote up
@OnClick(R.id.start)
public void start() {
    Progress progress = task.progress;
    switch (progress.status) {
        case Progress.PAUSE:
        case Progress.NONE:
        case Progress.ERROR:
            task.start();
            break;
        case Progress.LOADING:
            task.pause();
            break;
        case Progress.FINISH:
            if (ApkUtils.isAvailable(context, new File(progress.filePath))) {
                ApkUtils.uninstall(context, ApkUtils.getPackageName(context, progress.filePath));
            } else {
                ApkUtils.install(context, new File(progress.filePath));
            }
            break;
    }
    refresh(progress);
}
 
Example 3
Source Project: okhttp-OkGo   Source File: DownloadListActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_download_list);
    initToolBar(toolbar, true, "开始下载");

    initData();
    OkDownload.getInstance().setFolder(Environment.getExternalStorageDirectory().getAbsolutePath() + "/aaa/");
    OkDownload.getInstance().getThreadPool().setCorePoolSize(3);

    folder.setText(String.format("下载路径: %s", OkDownload.getInstance().getFolder()));
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

    //从数据库中恢复数据
    List<Progress> progressList = DownloadManager.getInstance().getAll();
    OkDownload.restore(progressList);
    adapter = new DownloadListAdapter(this);
    recyclerView.setAdapter(adapter);

    checkSDCardPermission();
}
 
Example 4
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 6 votes vote down vote up
public void start() {
    if (OkDownload.getInstance().getTask(progress.tag) == null || DownloadManager.getInstance().get(progress.tag) == null) {
        throw new IllegalStateException("you must call DownloadTask#save() before DownloadTask#start()!");
    }
    if (progress.status == Progress.NONE || progress.status == Progress.PAUSE || progress.status == Progress.ERROR) {
        postOnStart(progress);
        postWaiting(progress);
        priorityRunnable = new PriorityRunnable(progress.priority, this);
        executor.execute(priorityRunnable);
    } else if (progress.status == Progress.FINISH) {
        if (progress.filePath == null) {
            postOnError(progress, new StorageException("the file of the task with tag:" + progress.tag + " may be invalid or damaged, please call the method restart() to download again!"));
        } else {
            File file = new File(progress.filePath);
            if (file.exists() && file.length() == progress.totalSize) {
                postOnFinish(progress, new File(progress.filePath));
            } else {
                postOnError(progress, new StorageException("the file " + progress.filePath + " may be invalid or damaged, please call the method restart() to download again!"));
            }
        }
    } else {
        OkLogger.w("the task with tag " + progress.tag + " is already in the download queue, current task status is " + progress.status);
    }
}
 
Example 5
Source Project: DevUtils   Source File: OkGoCallback.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 上传过程中的进度回调, get请求不回调, UI 线程
 */
@Override
public void uploadProgress(Progress progress) {
    super.uploadProgress(progress);

    DevLogger.dTag(TAG, "上传过程中的进度回调: " + url + ", progress: " + progress);
}
 
Example 6
Source Project: okhttp-OkGo   Source File: UploadAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public void bind() {
    Progress progress = task.progress;
    ImageItem item = (ImageItem) progress.extra1;
    Glide.with(context).load(item.path).error(R.mipmap.ic_launcher).into(icon);
    name.setText(item.name);
    priority.setText(String.format("优先级:%s", progress.priority));
}
 
Example 7
Source Project: okhttp-OkGo   Source File: DownloadAdapter.java    License: Apache License 2.0 5 votes vote down vote up
public void refresh(Progress progress) {
    String currentSize = Formatter.formatFileSize(context, progress.currentSize);
    String totalSize = Formatter.formatFileSize(context, progress.totalSize);
    downloadSize.setText(currentSize + "/" + totalSize);
    priority.setText(String.format("优先级:%s", progress.priority));
    switch (progress.status) {
        case Progress.NONE:
            netSpeed.setText("停止");
            download.setText("下载");
            break;
        case Progress.PAUSE:
            netSpeed.setText("暂停中");
            download.setText("继续");
            break;
        case Progress.ERROR:
            netSpeed.setText("下载出错");
            download.setText("出错");
            break;
        case Progress.WAITING:
            netSpeed.setText("等待中");
            download.setText("等待");
            break;
        case Progress.FINISH:
            netSpeed.setText("下载完成");
            download.setText("完成");
            break;
        case Progress.LOADING:
            String speed = Formatter.formatFileSize(context, progress.speed);
            netSpeed.setText(String.format("%s/s", speed));
            download.setText("暂停");
            break;
    }
    tvProgress.setText(numberFormat.format(progress.fraction));
    pbProgress.setMax(10000);
    pbProgress.setProgress((int) (progress.fraction * 10000));
}
 
Example 8
Source Project: okhttp-OkGo   Source File: DesActivity.java    License: Apache License 2.0 5 votes vote down vote up
@OnClick(R.id.start)
public void start() {
    if (task == null) {

        //这里只是演示,表示请求可以传参,怎么传都行,和okgo使用方法一样
        GetRequest<File> request = OkGo.<File>get(apk.url)//
                .headers("aaa", "111")//
                .params("bbb", "222");

        task = OkDownload.request(apk.url, request)//
                .priority(apk.priority)//
                .extra1(apk)//
                .save()//
                .register(new DesListener("DesListener"))//
                .register(new LogDownloadListener());
    }
    switch (task.progress.status) {
        case Progress.PAUSE:
        case Progress.NONE:
        case Progress.ERROR:
            task.start();
            break;
        case Progress.LOADING:
            task.pause();
            break;
        case Progress.FINISH:
            File file = new File(task.progress.filePath);
            if (ApkUtils.isAvailable(this, file)) {
                ApkUtils.uninstall(this, ApkUtils.getPackageName(this, file.getAbsolutePath()));
            } else {
                ApkUtils.install(this, file);
            }
            break;
    }
}
 
Example 9
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
public UploadTask(String tag, Request<T, ? extends Request> request) {
    HttpUtils.checkNotNull(tag, "tag == null");
    progress = new Progress();
    progress.tag = tag;
    progress.url = request.getBaseUrl();
    progress.status = Progress.NONE;
    progress.totalSize = -1;
    progress.request = request;

    executor = OkUpload.getInstance().getThreadPool().getExecutor();
    listeners = new HashMap<>();
}
 
Example 10
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
public UploadTask<T> start() {
    if (OkUpload.getInstance().getTask(progress.tag) == null || UploadManager.getInstance().get(progress.tag) == null) {
        throw new IllegalStateException("you must call UploadTask#save() before UploadTask#start()!");
    }
    if (progress.status != Progress.WAITING && progress.status != Progress.LOADING) {
        postOnStart(progress);
        postWaiting(progress);
        priorityRunnable = new PriorityRunnable(progress.priority, this);
        executor.execute(priorityRunnable);
    } else {
        OkLogger.w("the task with tag " + progress.tag + " is already in the upload queue, current task status is " + progress.status);
    }
    return this;
}
 
Example 11
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
public void restart() {
    pause();
    progress.status = Progress.NONE;
    progress.currentSize = 0;
    progress.fraction = 0;
    progress.speed = 0;
    UploadManager.getInstance().replace(progress);
    start();
}
 
Example 12
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postWaiting(final Progress progress) {
    progress.speed = 0;
    progress.status = Progress.WAITING;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (UploadListener<T> listener : listeners.values()) {
                listener.onProgress(progress);
            }
        }
    });
}
 
Example 13
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postPause(final Progress progress) {
    progress.speed = 0;
    progress.status = Progress.PAUSE;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (UploadListener<T> listener : listeners.values()) {
                listener.onProgress(progress);
            }
        }
    });
}
 
Example 14
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postOnError(final Progress progress, final Throwable throwable) {
    progress.speed = 0;
    progress.status = Progress.ERROR;
    progress.exception = throwable;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (UploadListener<T> listener : listeners.values()) {
                listener.onProgress(progress);
                listener.onError(progress);
            }
        }
    });
}
 
Example 15
Source Project: okhttp-OkGo   Source File: UploadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postOnFinish(final Progress progress, final T t) {
    progress.speed = 0;
    progress.fraction = 1.0f;
    progress.status = Progress.FINISH;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (UploadListener<T> listener : listeners.values()) {
                listener.onProgress(progress);
                listener.onFinish(t, progress);
            }
        }
    });
}
 
Example 16
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
public DownloadTask(String tag, Request<File, ? extends Request> request) {
    HttpUtils.checkNotNull(tag, "tag == null");
    progress = new Progress();
    progress.tag = tag;
    progress.folder = OkDownload.getInstance().getFolder();
    progress.url = request.getBaseUrl();
    progress.status = Progress.NONE;
    progress.totalSize = -1;
    progress.request = request;

    executor = OkDownload.getInstance().getThreadPool().getExecutor();
    listeners = new HashMap<>();
}
 
Example 17
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
public void restart() {
    pause();
    IOUtils.delFileOrFolder(progress.filePath);
    progress.status = Progress.NONE;
    progress.currentSize = 0;
    progress.fraction = 0;
    progress.speed = 0;
    DownloadManager.getInstance().replace(progress);
    start();
}
 
Example 18
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postOnStart(final Progress progress) {
    progress.speed = 0;
    progress.status = Progress.NONE;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (DownloadListener listener : listeners.values()) {
                listener.onStart(progress);
            }
        }
    });
}
 
Example 19
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postWaiting(final Progress progress) {
    progress.speed = 0;
    progress.status = Progress.WAITING;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (DownloadListener listener : listeners.values()) {
                listener.onProgress(progress);
            }
        }
    });
}
 
Example 20
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postPause(final Progress progress) {
    progress.speed = 0;
    progress.status = Progress.PAUSE;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (DownloadListener listener : listeners.values()) {
                listener.onProgress(progress);
            }
        }
    });
}
 
Example 21
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postLoading(final Progress progress) {
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (DownloadListener listener : listeners.values()) {
                listener.onProgress(progress);
            }
        }
    });
}
 
Example 22
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postOnError(final Progress progress, final Throwable throwable) {
    progress.speed = 0;
    progress.status = Progress.ERROR;
    progress.exception = throwable;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (DownloadListener listener : listeners.values()) {
                listener.onProgress(progress);
                listener.onError(progress);
            }
        }
    });
}
 
Example 23
Source Project: okhttp-OkGo   Source File: DownloadTask.java    License: Apache License 2.0 5 votes vote down vote up
private void postOnFinish(final Progress progress, final File file) {
    progress.speed = 0;
    progress.fraction = 1.0f;
    progress.status = Progress.FINISH;
    updateDatabase(progress);
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (DownloadListener listener : listeners.values()) {
                listener.onProgress(progress);
                listener.onFinish(file, progress);
            }
        }
    });
}
 
Example 24
Source Project: okhttp-OkGo   Source File: OkDownload.java    License: Apache License 2.0 5 votes vote down vote up
/** 从数据库中恢复任务 */
public static DownloadTask restore(Progress progress) {
    Map<String, DownloadTask> taskMap = OkDownload.getInstance().getTaskMap();
    DownloadTask task = taskMap.get(progress.tag);
    if (task == null) {
        task = new DownloadTask(progress);
        taskMap.put(progress.tag, task);
    }
    return task;
}
 
Example 25
Source Project: okhttp-OkGo   Source File: OkDownload.java    License: Apache License 2.0 5 votes vote down vote up
/** 从数据库中恢复任务 */
public static List<DownloadTask> restore(List<Progress> progressList) {
    Map<String, DownloadTask> taskMap = OkDownload.getInstance().getTaskMap();
    List<DownloadTask> tasks = new ArrayList<>();
    for (Progress progress : progressList) {
        DownloadTask task = taskMap.get(progress.tag);
        if (task == null) {
            task = new DownloadTask(progress);
            taskMap.put(progress.tag, task);
        }
        tasks.add(task);
    }
    return tasks;
}
 
Example 26
Source Project: okhttp-OkGo   Source File: OkUpload.java    License: Apache License 2.0 5 votes vote down vote up
/** 从数据库中恢复任务 */
public static <T> UploadTask<T> restore(Progress progress) {
    Map<String, UploadTask<?>> taskMap = OkUpload.getInstance().getTaskMap();
    //noinspection unchecked
    UploadTask<T> task = (UploadTask<T>) taskMap.get(progress.tag);
    if (task == null) {
        task = new UploadTask<>(progress);
        taskMap.put(progress.tag, task);
    }
    return task;
}
 
Example 27
Source Project: okhttp-OkGo   Source File: OkUpload.java    License: Apache License 2.0 5 votes vote down vote up
/** 从数据库中恢复任务 */
public static List<UploadTask<?>> restore(List<Progress> progressList) {
    Map<String, UploadTask<?>> taskMap = OkUpload.getInstance().getTaskMap();
    List<UploadTask<?>> tasks = new ArrayList<>();
    for (Progress progress : progressList) {
        UploadTask<?> task = taskMap.get(progress.tag);
        if (task == null) {
            task = new UploadTask<>(progress);
            taskMap.put(progress.tag, task);
        }
        tasks.add(task);
    }
    return tasks;
}
 
Example 28
Source Project: okhttp-OkGo   Source File: ProgressRequestBody.java    License: Apache License 2.0 5 votes vote down vote up
private void onProgress(final Progress progress) {
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (callback != null) {
                callback.uploadProgress(progress);
            }
        }
    });
}
 
Example 29
Source Project: okhttp-OkGo   Source File: FileConvert.java    License: Apache License 2.0 5 votes vote down vote up
private void onProgress(final Progress progress) {
    HttpUtils.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            callback.downloadProgress(progress);   //进度回调的方法
        }
    });
}
 
Example 30
Source Project: BaseProject   Source File: DownloadManager.java    License: Apache License 2.0 4 votes vote down vote up
/** 获取下载任务 */
public Progress get(String tag) {
    return queryOne(Progress.TAG + "=?", new String[]{tag});
}