From 8efe7d9f751868cf0d7df9b0f732aa5f0b69f024 Mon Sep 17 00:00:00 2001 From: yuyu lao Date: Sat, 20 Feb 2016 00:06:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=AE=E5=BD=95=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 13 +- .idea/.name | 1 + .idea/compiler.xml | 22 +++ .idea/copyright/profiles_settings.xml | 3 + .idea/encodings.xml | 6 + .idea/gradle.xml | 26 +++ .idea/misc.xml | 53 ++++++ .idea/modules.xml | 10 ++ .idea/runConfigurations.xml | 12 ++ .idea/vcs.xml | 6 + app/.gitignore | 1 + app/build.gradle | 28 +++ proguard-rules.pro => app/proguard-rules.pro | 0 .../arial/downloaddemo/ApplicationTest.java | 13 ++ app/src/main/AndroidManifest.xml | 25 +++ .../arial/downloaddemo/MainActivity.java | 166 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 35 ++++ app/src/main/res/layout/content_main.xml | 65 +++++++ app/src/main/res/menu/menu_main.xml | 9 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-v21/styles.xml | 8 + app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 6 + app/src/main/res/values/strings.xml | 4 + app/src/main/res/values/styles.xml | 17 ++ .../arial/downloaddemo/ExampleUnitTest.java | 15 ++ build.gradle | 57 ++---- downloadutil/.gitignore | 3 + downloadutil/build.gradle | 26 +++ jcenter.gradle => downloadutil/jcenter.gradle | 0 downloadutil/proguard-rules.pro | 17 ++ .../arialyy/downloadutil/ApplicationTest.java | 0 .../src}/main/AndroidManifest.xml | 0 .../arialyy/downloadutil/DownLoadUtil.java | 0 .../downloadutil/DownloadListener.java | 0 .../java/com/arialyy/downloadutil/Util.java | 0 .../downloadutil/inf/IDownloadListener.java | 0 .../src}/main/res/values/strings.xml | 0 .../arialyy/downloadutil/ExampleUnitTest.java | 0 gradle.properties | 18 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53636 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 +++++++++++++++++ gradlew.bat | 90 ++++++++++ settings.gradle | 1 + 50 files changed, 890 insertions(+), 44 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle rename proguard-rules.pro => app/proguard-rules.pro (100%) create mode 100644 app/src/androidTest/java/com/example/arial/downloaddemo/ApplicationTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/example/arial/downloaddemo/MainActivity.java create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/content_main.xml create mode 100644 app/src/main/res/menu/menu_main.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-v21/styles.xml create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/example/arial/downloaddemo/ExampleUnitTest.java create mode 100644 downloadutil/.gitignore create mode 100644 downloadutil/build.gradle rename jcenter.gradle => downloadutil/jcenter.gradle (100%) create mode 100644 downloadutil/proguard-rules.pro rename {src => downloadutil/src}/androidTest/java/com/arialyy/downloadutil/ApplicationTest.java (100%) rename {src => downloadutil/src}/main/AndroidManifest.xml (100%) rename {src => downloadutil/src}/main/java/com/arialyy/downloadutil/DownLoadUtil.java (100%) rename {src => downloadutil/src}/main/java/com/arialyy/downloadutil/DownloadListener.java (100%) rename {src => downloadutil/src}/main/java/com/arialyy/downloadutil/Util.java (100%) rename {src => downloadutil/src}/main/java/com/arialyy/downloadutil/inf/IDownloadListener.java (100%) rename {src => downloadutil/src}/main/res/values/strings.xml (100%) rename {src => downloadutil/src}/test/java/com/arialyy/downloadutil/ExampleUnitTest.java (100%) create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore index 2ce7571b..1034d27f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,12 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store /build -gradle.properties -downloadutil.iml \ No newline at end of file +/captures +/app/build +/downloadutil/build +/downloadutil/gradle.properties + diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..2ac081d5 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +DownloadDemo \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..96cc43ef --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ 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 00000000..e7bedf33 --- /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 00000000..97626ba4 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 00000000..11a97bb3 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,26 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..6b8fbd72 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..d9fb88b4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 00000000..7f68460d --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 00000000..f8b703b0 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 'Google Inc.:Google APIs:23' + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "com.example.arial.downloaddemo" + minSdkVersion 15 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(include: ['*.jar'], dir: 'libs') + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.+' + compile 'com.android.support:design:23.+' + compile project(':downloadutil') +} diff --git a/proguard-rules.pro b/app/proguard-rules.pro similarity index 100% rename from proguard-rules.pro rename to app/proguard-rules.pro diff --git a/app/src/androidTest/java/com/example/arial/downloaddemo/ApplicationTest.java b/app/src/androidTest/java/com/example/arial/downloaddemo/ApplicationTest.java new file mode 100644 index 00000000..d9b0087c --- /dev/null +++ b/app/src/androidTest/java/com/example/arial/downloaddemo/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.example.arial.downloaddemo; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..8dbe4290 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java b/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java new file mode 100644 index 00000000..21d20cdd --- /dev/null +++ b/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java @@ -0,0 +1,166 @@ +package com.example.arial.downloaddemo; + +import android.os.Bundle; +import android.os.Environment; +import android.os.Handler; +import android.os.Message; +import android.support.design.widget.FloatingActionButton; +import android.support.design.widget.Snackbar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.Button; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.arialyy.downloadutil.DownLoadUtil; +import com.arialyy.downloadutil.DownloadListener; +import com.arialyy.downloadutil.Util; + +import java.net.HttpURLConnection; + +public class MainActivity extends AppCompatActivity { + private static final int DOWNLOAD_PRE = 0x01; + private static final int DOWNLOAD_STOP = 0x02; + private static final int DOWNLOAD_FAILE = 0x03; + private static final int DOWNLOAD_CANCEL = 0x04; + private static final int DOWNLOAD_RESUME = 0x05; + private static final int DOWNLOAD_COMPLETE = 0x06; + private ProgressBar mPb; + private String mDownloadUrl = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + private DownLoadUtil mUtil; + private Button mStart, mStop, mCancel; + private TextView mSize; + + private Handler mUpdateHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + switch (msg.what) { + case DOWNLOAD_PRE: + mSize.setText(Util.formatFileSize((Long) msg.obj)); + mStart.setEnabled(false); + break; + case DOWNLOAD_FAILE: + Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); + break; + case DOWNLOAD_STOP: + Toast.makeText(MainActivity.this, "暂停下载", Toast.LENGTH_SHORT).show(); + mStart.setText("恢复"); + mStart.setEnabled(true); + break; + case DOWNLOAD_CANCEL: + mPb.setProgress(0); + Toast.makeText(MainActivity.this, "取消下载", Toast.LENGTH_SHORT).show(); + mStart.setEnabled(true); + mStart.setText("开始"); + break; + case DOWNLOAD_RESUME: + Toast.makeText(MainActivity.this, "恢复下载,恢复位置 ==> " + Util.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT).show(); + mStart.setEnabled(false); + break; + case DOWNLOAD_COMPLETE: + Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show(); + mStart.setEnabled(true); + mCancel.setEnabled(false); + mStop.setEnabled(false); + break; + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + init(); + } + + private void init() { + mPb = (ProgressBar) findViewById(R.id.progressBar); + mStart = (Button) findViewById(R.id.start); + mStop = (Button) findViewById(R.id.stop); + mCancel = (Button) findViewById(R.id.cancel); + mSize = (TextView) findViewById(R.id.size); + mUtil = new DownLoadUtil(); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + start(); + break; + case R.id.stop: + stop(); + break; + case R.id.cancel: + cancel(); + break; + } + } + + private void start() { + mUtil.download(this, mDownloadUrl, Environment.getExternalStorageDirectory().getPath() + "/test.apk" + , new DownloadListener() { + long fileSize = 1; + + @Override + public void onPreDownload(HttpURLConnection connection) { + super.onPreDownload(connection); + mPb.setMax(100); + fileSize = connection.getContentLength(); + mUpdateHandler.obtainMessage(DOWNLOAD_PRE, fileSize).sendToTarget(); + } + + @Override + public void onProgress(long currentLocation) { + super.onProgress(currentLocation); + mPb.setProgress((int) (currentLocation * 100 / fileSize)); + } + + @Override + public void onStop(long stopLocation) { + super.onStop(stopLocation); + mUpdateHandler.obtainMessage(DOWNLOAD_STOP).sendToTarget(); + } + + @Override + public void onCancel() { + super.onCancel(); + mUpdateHandler.obtainMessage(DOWNLOAD_CANCEL).sendToTarget(); + } + + @Override + public void onResume(long resumeLocation) { + super.onResume(resumeLocation); + mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, resumeLocation).sendToTarget(); + } + + @Override + public void onFail() { + super.onFail(); + mUpdateHandler.obtainMessage(DOWNLOAD_FAILE).sendToTarget(); + } + + @Override + public void onComplete() { + super.onComplete(); + mUpdateHandler.obtainMessage(DOWNLOAD_COMPLETE).sendToTarget(); + } + }); + } + + private void stop() { + mUtil.stopDownload(); + } + + private void cancel() { + mUtil.cancelDownload(); + } + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..41173feb --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml new file mode 100644 index 00000000..481a2119 --- /dev/null +++ b/app/src/main/res/layout/content_main.xml @@ -0,0 +1,65 @@ + + + + + + + + + +