diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..60ccf44 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +MultiThreadDownloader \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..217af47 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/copyright/Apache.xml b/.idea/copyright/Apache.xml new file mode 100644 index 0000000..7c21b31 --- /dev/null +++ b/.idea/copyright/Apache.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..f1f8114 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cbaffd1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + Apache + + + + + + + + 1.8 + + + + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..823d9dd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9d32e50 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/Demo/Demo.iml b/Demo/Demo.iml new file mode 100644 index 0000000..5449630 --- /dev/null +++ b/Demo/Demo.iml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Demo/build.gradle b/Demo/build.gradle new file mode 100644 index 0000000..6e30614 --- /dev/null +++ b/Demo/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 22 + buildToolsVersion "22.0.1" + + defaultConfig { + applicationId "cn.aigestudio.downloader.demo" + minSdkVersion 4 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile project(':Downloader') + compile 'com.android.support:support-v4:22.1.1' +} diff --git a/Demo/proguard-rules.pro b/Demo/proguard-rules.pro new file mode 100644 index 0000000..723652e --- /dev/null +++ b/Demo/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this dlLocalFile are appended to flags specified +# in H:\Programming\Android\sdk/tools/proguard/proguard-android.txt +# You can edit the include dirPath and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/Demo/src/main/AndroidManifest.xml b/Demo/src/main/AndroidManifest.xml new file mode 100644 index 0000000..5f7f85c --- /dev/null +++ b/Demo/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/Demo/src/main/java/cn/aigestudio/downloader/demo/DLService.java b/Demo/src/main/java/cn/aigestudio/downloader/demo/DLService.java new file mode 100644 index 0000000..c8bba8c --- /dev/null +++ b/Demo/src/main/java/cn/aigestudio/downloader/demo/DLService.java @@ -0,0 +1,65 @@ +package cn.aigestudio.downloader.demo; + +import android.app.NotificationManager; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.IBinder; +import android.support.v4.app.NotificationCompat; + +import java.io.File; + +import cn.aigestudio.downloader.bizs.DLManager; +import cn.aigestudio.downloader.interfaces.DLTaskListener; +import cn.aigestudio.downloader.utils.FileUtil; +import cn.aigestudio.downloader.utils.LogUtil; + +/** + * 执行下载的Service + * + * @author AigeStudio 2015-05-18 + */ +public class DLService extends Service { + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + String url = intent.getStringExtra("url"); + String path = intent.getStringExtra("path"); + final int id = intent.getIntExtra("id", -1); + final NotificationManager nm = (NotificationManager) getSystemService(Context + .NOTIFICATION_SERVICE); + final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) + .setContentTitle(FileUtil.getFileNameFromUrl(url).replace("/", "")) + .setSmallIcon(R.drawable.ic_launcher); + + DLManager.getInstance(this).dlStart(url, path, new DLTaskListener() { + @Override + public void onProgress(int progress) { + builder.setProgress(100, progress, false); + nm.notify(id, builder.build()); + } + + @Override + public void onFinish(File file) { + LogUtil.i("onFinish"); + installApk(file); + nm.cancel(id); + } + }); + return super.onStartCommand(intent, flags, startId); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void installApk(File file) { + LogUtil.i("installApk"); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); + startActivity(intent); + } +} diff --git a/Demo/src/main/java/cn/aigestudio/downloader/demo/MainActivity.java b/Demo/src/main/java/cn/aigestudio/downloader/demo/MainActivity.java new file mode 100644 index 0000000..1178c08 --- /dev/null +++ b/Demo/src/main/java/cn/aigestudio/downloader/demo/MainActivity.java @@ -0,0 +1,168 @@ +package cn.aigestudio.downloader.demo; + +import android.app.Activity; +import android.app.Notification; +import android.os.Bundle; +import android.os.Environment; +import android.view.View; +import android.widget.Button; +import android.widget.ProgressBar; + +import cn.aigestudio.downloader.bizs.DLManager; +import cn.aigestudio.downloader.interfaces.DLTaskListener; + +public class MainActivity extends Activity { + private static final String URL1 = "http://183.230.95.76:9999/bcs.91.com/pcsuite-dev/apk/c684f54c3f941cd71baef085e2ac8662.apk"; + private static final String URL2 = "http://bcs.apk.r1.91.com/data/upload/apkres/2015/5_17/13/com.tencent.mobileqq_015020349.apk"; + private static final String URL3 = "http://dlsw.baidu.com/sw-search-sp/soft/9e/12035/BaiduHi_V4.7.1.2_setup.1429175376.exe"; + private static final String URL4 = "http://dlsw.baidu.com/sw-search-sp/soft/a2/12282/SinaUC_Release_8.3.4.22616.1396945592.exe"; + private static final String URL5 = "http://dlsw.baidu.com/sw-search-sp/soft/4b/17170/Install_WLMessenger14.0.8117.416.1393467029.exe"; + private static final String URL6 = "http://dlsw.baidu.com/sw-search-sp/soft/a2/25705/sinaSHOW-v1-1.1395901693.dmg"; + + private static final int[] RES_ID_BTN_START = {R.id.main_dl_start_btn1, R.id.main_dl_start_btn2, + R.id.main_dl_start_btn3, R.id.main_dl_start_btn4, R.id.main_dl_start_btn5, + R.id.main_dl_start_btn6}; + private static final int[] RES_ID_BTN_STOP = {R.id.main_dl_stop_btn1, R.id.main_dl_stop_btn2, + R.id.main_dl_stop_btn3, R.id.main_dl_stop_btn4, R.id.main_dl_stop_btn5, + R.id.main_dl_stop_btn6}; + private static final int[] RES_ID_PB = {R.id.main_dl_pb1, R.id.main_dl_pb2, R.id.main_dl_pb3, + R.id.main_dl_pb4, R.id.main_dl_pb5, R.id.main_dl_pb6}; + private static final int[] RES_ID_NOTIFY = {R.id.main_notify_btn1, R.id.main_notify_btn2, + R.id.main_notify_btn3, R.id.main_notify_btn4, R.id.main_notify_btn5, + R.id.main_notify_btn6}; + + private String saveDir; + + private ProgressBar[] pbDLs; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Button[] btnStarts = new Button[RES_ID_BTN_START.length]; + for (int i = 0; i < btnStarts.length; i++) { + btnStarts[i] = (Button) findViewById(RES_ID_BTN_START[i]); + btnStarts[i].setOnClickListener(new MainClickListener()); + } + + Button[] btnStops = new Button[RES_ID_BTN_STOP.length]; + for (int i = 0; i < btnStops.length; i++) { + btnStops[i] = (Button) findViewById(RES_ID_BTN_STOP[i]); + btnStops[i].setOnClickListener(new MainClickListener()); + } + + pbDLs = new ProgressBar[RES_ID_PB.length]; + for (int i = 0; i < pbDLs.length; i++) { + pbDLs[i] = (ProgressBar) findViewById(RES_ID_PB[i]); + pbDLs[i].setMax(100); + pbDLs[i].setOnClickListener(new MainClickListener()); + } + + Button[] btnNotifys = new Button[RES_ID_NOTIFY.length]; + for (int i = 0; i < btnNotifys.length; i++) { + btnNotifys[i] = (Button) findViewById(RES_ID_NOTIFY[i]); + btnNotifys[i].setOnClickListener(new MainClickListener()); + } + + saveDir = Environment.getExternalStorageDirectory() + "/AigeStudio/"; + } + + private class MainClickListener implements View.OnClickListener { + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.main_dl_start_btn1: + DLManager.getInstance(MainActivity.this).dlStart(URL1, saveDir, + new DLTaskListener() { + @Override + public void onProgress(int progress) { + pbDLs[1].setProgress(progress); + } + }); + break; + case R.id.main_dl_stop_btn1: + DLManager.getInstance(MainActivity.this).dlStop(URL1); + break; + case R.id.main_dl_start_btn2: + DLManager.getInstance(MainActivity.this).dlStart(URL2, saveDir, + new DLTaskListener() { + @Override + public void onProgress(int progress) { + pbDLs[2].setProgress(progress); + } + }); + break; + case R.id.main_dl_stop_btn2: + DLManager.getInstance(MainActivity.this).dlStop(URL2); + break; + case R.id.main_dl_start_btn3: + DLManager.getInstance(MainActivity.this).dlStart(URL3, saveDir, + new DLTaskListener() { + @Override + public void onProgress(int progress) { + pbDLs[3].setProgress(progress); + } + }); + break; + case R.id.main_dl_stop_btn3: + DLManager.getInstance(MainActivity.this).dlStop(URL3); + break; + case R.id.main_dl_start_btn4: + DLManager.getInstance(MainActivity.this).dlStart(URL4, saveDir, + new DLTaskListener() { + @Override + public void onProgress(int progress) { + pbDLs[4].setProgress(progress); + } + }); + break; + case R.id.main_dl_stop_btn4: + DLManager.getInstance(MainActivity.this).dlStop(URL4); + break; + case R.id.main_dl_start_btn5: + DLManager.getInstance(MainActivity.this).dlStart(URL5, saveDir, + new DLTaskListener() { + @Override + public void onProgress(int progress) { + pbDLs[5].setProgress(progress); + } + }); + break; + case R.id.main_dl_stop_btn5: + DLManager.getInstance(MainActivity.this).dlStop(URL5); + break; + case R.id.main_dl_start_btn6: + DLManager.getInstance(MainActivity.this).dlStart(URL6, saveDir, + new DLTaskListener() { + @Override + public void onProgress(int progress) { + pbDLs[6].setProgress(progress); + } + }); + break; + case R.id.main_dl_stop_btn6: + DLManager.getInstance(MainActivity.this).dlStop(URL6); + break; + case R.id.main_notify_btn1: + NotificationUtil.notificationForDLAPK(MainActivity.this, URL1); + break; + case R.id.main_notify_btn2: + NotificationUtil.notificationForDLAPK(MainActivity.this, URL2); + break; + case R.id.main_notify_btn3: + NotificationUtil.notificationForDLAPK(MainActivity.this, URL3); + break; + case R.id.main_notify_btn4: + NotificationUtil.notificationForDLAPK(MainActivity.this, URL4); + break; + case R.id.main_notify_btn5: + NotificationUtil.notificationForDLAPK(MainActivity.this, URL5); + break; + case R.id.main_notify_btn6: + NotificationUtil.notificationForDLAPK(MainActivity.this, URL6); + break; + } + } + } +} diff --git a/Demo/src/main/java/cn/aigestudio/downloader/demo/NotificationUtil.java b/Demo/src/main/java/cn/aigestudio/downloader/demo/NotificationUtil.java new file mode 100644 index 0000000..514d2f2 --- /dev/null +++ b/Demo/src/main/java/cn/aigestudio/downloader/demo/NotificationUtil.java @@ -0,0 +1,24 @@ +package cn.aigestudio.downloader.demo; + +import android.content.Context; +import android.content.Intent; +import android.os.Environment; + +/** + * 通知工具类 + * + * @author AigeStudio 2015-05-18 + */ +public final class NotificationUtil { + public static void notificationForDLAPK(Context context, String url) { + notificationForDLAPK(context, url, Environment.getExternalStorageDirectory() + "/AigeStudio/"); + } + + public static void notificationForDLAPK(Context context, String url, String path) { + Intent intent = new Intent(context, DLService.class); + intent.putExtra("url", url); + intent.putExtra("path", path); + intent.putExtra("id", (int) (Math.random() * 1024)); + context.startService(intent); + } +} diff --git a/Demo/src/main/res/drawable-hdpi/ic_launcher.png b/Demo/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..cd9fe5f Binary files /dev/null and b/Demo/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/Demo/src/main/res/drawable-mdpi/ic_launcher.png b/Demo/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..bc3beb8 Binary files /dev/null and b/Demo/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/Demo/src/main/res/drawable-xhdpi/ic_launcher.png b/Demo/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..234724a Binary files /dev/null and b/Demo/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/Demo/src/main/res/drawable-xxhdpi/ic_launcher.png b/Demo/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..6248217 Binary files /dev/null and b/Demo/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/Demo/src/main/res/drawable-xxxhdpi/ic_launcher.png b/Demo/src/main/res/drawable-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..8cca713 Binary files /dev/null and b/Demo/src/main/res/drawable-xxxhdpi/ic_launcher.png differ diff --git a/Demo/src/main/res/layout/activity_main.xml b/Demo/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..9751885 --- /dev/null +++ b/Demo/src/main/res/layout/activity_main.xml @@ -0,0 +1,208 @@ + + + + + + +