Giter Club home page Giter Club logo

yc-downloader's Introduction

YC-Downloader

中文文档

A multi-thread, multi-task and multi-process downloader. It supports HTTP, download speed limit.

An app using this library is available at https://github.com/SirLYC/EveryDownload

Features

  • HTTP/HTTPS download
  • multi-thread download
  • download thread and disk-io thread separated
  • multi download task
  • support for HTTP (resume from break-point)
  • message control to avoid ui frame drops
  • multi-process support
  • download speed limit
  • auto-retry(connect and download)
  • other protocol download maybe...

Project

sample module

A simple apk instance which uses download library.

downloader module

Download library.

Install

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.SirLYC:Yuchuan-Downloader:latest.release'
}

Check release notes here

Step3. Install YCDownloader

In manifest:

<manifest>
    <!--required-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <!--not required but important-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    ...
    <application>
        ...   
        <!--One of them is required-->
        <!--When you want service to run in your app process.-->
        <service android:name="com.lyc.downloader.LocalDownloadService"/>
    
        <!--When you want service to run In another process. Attribute process must be defined and different from you package name-->
        <service android:name="com.lyc.downloader.RemoteDownloadService"
                 android:process=":remote"/>
        ...
    </application>
</manifest>

It's recommended to install in Application class

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        val config = Configuration.Builder()
            // If service running in another process;
            // Default value:
            // Decided by service in your manifest;
            // If both remote and local services are defined in your manifest,
            // remote one will be selected;
            .setMultiProcess(false)
            // If allow download. Default true;
            .setAllowDownload(true)
            // If avoid frame drop. Default true;
            .setAvoidFrameDrop(true)
            // Default 4;
            .setMaxRunningTask(4)
            // Send progress update message to main thread interval time in nano. Default 333ms;
            .setSendMessageIntervalNanos(TimeUnit.MILLISECONDS.toNanos(333))
            // Speed limit(bytes/s). If <= 0, no limit;
//            .setSpeedLimit(2048 * 1024)
            .setSpeedLimit(0)
            .build()

        YCDownloader.install(this, config)

        // multiProcess is selected by YCDownloader;
        // Other params are default values;
//        YCDownloader.install(this)
    }
}

Proguard

Proguard is already defined in the library, you don't have to specify for this library.

Main API

You can check all apis in file YCDownloader.java

Start download

private SubmitListener submitListener = new SubmitListener() {
        @Override
        public void submitSuccess(DownloadInfo downloadInfo) {
            Log.d("Submit", "success submit, id = " + downloadInfo.getId());
        }

        @Override
        public void submitFail(Exception e) {
            Log.e("Submit", "submit failed", e);
        }
};
// path: parent directory to store your file
// filename: can be null; if not null, downloader will use it to save your file
YCDownloader.submit(url, path, filename, submitListener);

Listen to download progress or state change

DownloadListener downloadListener = ...;
YCDownloader.registerDownloadListener(downloadListener);

// you should unregister it to avoid memory leak
// such as Activity.OnDestroy
YCDownloader.unregisterDownloadListener(downloadListener);

Query download info

// attention: these methods should be called in worker thread
// query by id
YCDownloader.queryDownloadInfo(long id);
// state != CANCELED && state != FINISH
YCDownloader.queryActiveDownloadInfoList();
// state == DELETED
YCDownloader.queryDeletedDownloadInfoList();
// state == FINISH
YCDownloader.queryFinishedDownloadInfoList();

Configuration

You can change configurations when downloader is running.These methods are safe to call in any thread.

// limit your running task to 4
YCDownloader.setMaxRunningTask(4);

// limit your download speed to 512KB
// param <= 0 means no limit, run as fast as possible
YCDownloader.setSpeedLimit(512 * 1024);

// if true, the speed of sending progress-update message will slow down
YCDownloader.setAvoidFrameDrop(true);
// send progress update message interval at least 500ms
// only valid when setAvoidFrameDrop(true)
YCDownloader.setSendMessageInterval(500, TimeUnit.MILLISECONDS);

// If false, all running tasks are in WAITING state
YCDownloaer.setAllowDownload(true)

// Use configuration to update
YCDownloader.updateByConfiguration(Configuration);

Important classes

YCDownloader: com.lyc.downloader.YCDownloader

Export main apis.

DownloadListener: com.lyc.downloader.DownloadListener

Callback of the download progress and state change of every downloadTask. All methods are called in main thread.

DownloadTasksChangeListener: com.lyc.downloader.DownloadTasksChangeListener

Callback of the change in downloadTasks: created or removed. It's a good feature to implement a function like eventBus. All methods are called in main thread.

SubmitListener: com.lyc.downloader.SubmitListener

Notify task submit state: success or fail. All methods are called in main thread.

DownloadInfo: com.lyc.downloader.db.DownloadInfo

Database entity, also Parcelable to pass between multi-process.

Configuration: com.lyc.downloader.Configuration

An immutable class to pass params of downloader.

Permissions

<!--required-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--not required but important-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Licence

MIT License

Copyright (c) 2019 Liu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

yc-downloader's People

Contributors

sirlyc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yc-downloader's Issues

URL需要上传多个header 的情况

想请教下 URL需要上传多个header 的情况 需要如何处理
自己继承DownloadManager 发现Request 是私有的,无法在子类里面实现 只能在doPivotCall 方法中添加header 或者在DownloadManager 的buildBaseRequest 方法中实现 除了下载源码 下来还有其他的方式么?

crash

only IDownloadService$StubImp can be transferred to download service! 这块有时会崩溃,方便留个联系方式吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.