diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 79db29b..fbb8a74 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,13 +14,20 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + + \ No newline at end of file diff --git a/app/src/main/java/me/jessyan/progressmanager/demo/AdvanceActivity.java b/app/src/main/java/me/jessyan/progressmanager/demo/AdvanceActivity.java new file mode 100644 index 0000000..60eb971 --- /dev/null +++ b/app/src/main/java/me/jessyan/progressmanager/demo/AdvanceActivity.java @@ -0,0 +1,358 @@ +/** + * Copyright 2017 JessYan + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.jessyan.progressmanager.demo; + +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.ImageView; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.bumptech.glide.load.engine.DiskCacheStrategy; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import me.jessyan.progressmanager.ProgressListener; +import me.jessyan.progressmanager.ProgressManager; +import me.jessyan.progressmanager.body.ProgressInfo; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +/** + * ================================================ + * 这里为了展示本框架的高级功能,使用同一个 url 地址根据 Post 请求参数的不同而下载或上传不同的资源 + * + * @see {@link #initListener} + * Created by JessYan on 08/06/2017 12:59 + * Contact me + * Follow me + * ================================================ + */ +public class AdvanceActivity extends AppCompatActivity implements View.OnClickListener { + private static final String TAG = "AdvanceActivity"; + private ImageView mImageView; + private OkHttpClient mOkHttpClient; + private ProgressBar mGlideProgress; + private ProgressBar mDownloadProgress; + private ProgressBar mUploadProgress; + private TextView mGlideProgressText; + private TextView mDownloadProgressText; + private TextView mUploadProgressText; + + private ProgressInfo mLastDownloadingInfo; + private ProgressInfo mLastUploadingingInfo; + private Handler mHandler; + private String mNewImageUrl; + private String mNewDownloadUrl; + private String mNewUploadUrl; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mOkHttpClient = ((BaseApplication) getApplicationContext()).getOkHttpClient(); + mHandler = new Handler(); + initView(); + initListener(); + //在 Activity 中显示进度条的同时,也在 Fragment 中显示对应 url 的进度条,为了展示此框架的多端同步更新某一个进度信息 + getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, + AdvanceFragment.newInstance(mNewImageUrl, mNewDownloadUrl, mNewUploadUrl)).commit(); + } + + + private void initView() { + setContentView(R.layout.activity_advance); + mImageView = findViewById(R.id.imageView); + mGlideProgress = findViewById(R.id.glide_progress); + mDownloadProgress = findViewById(R.id.download_progress); + mUploadProgress = findViewById(R.id.upload_progress); + mGlideProgressText = findViewById(R.id.glide_progress_text); + mDownloadProgressText = findViewById(R.id.download_progress_text); + mUploadProgressText = findViewById(R.id.upload_progress_text); + findViewById(R.id.glide_start).setOnClickListener(this); + findViewById(R.id.download_start).setOnClickListener(this); + findViewById(R.id.upload_start).setOnClickListener(this); + } + + private void initListener() { + //图片和下载 (上传也同样支持) 使用同一个 url 地址,是为了展示高级功能 + //高级功能是为了应对当需要使用同一个 url 地址根据 Post 请求参数的不同而下载或上传不同资源的情况 + //"http://jessyancoding.github.io/images/RxCache.png" 会重定向到 "http://jessyan.me/images/RxCache.png" + //所以也展示了高级功能同时完美兼容重定向 + //这里需要注意的是虽然使用的是新的 url 地址进行上传或下载,但实际请求服务器的 url 地址,还是原始的 url 地址 + //在监听器内部已经进行了处理,所以高级功能并不会影响服务器的请求 + + //Glide 加载监听 + mNewImageUrl = ProgressManager + .getInstance() + .addDiffResponseListenerOnSameUrl("http://jessyancoding.github.io/images/RxCache.png", getGlideListener()); + + + //Okhttp/Retofit 下载监听 + mNewDownloadUrl = ProgressManager + .getInstance() + .addDiffResponseListenerOnSameUrl("http://jessyancoding.github.io/images/RxCache.png", getDownloadListener()); + + + //Okhttp/Retofit 上传监听 + mNewUploadUrl = ProgressManager + .getInstance() + .addDiffRequestListenerOnSameUrl("http://upload.qiniu.com/", "test", getUploadListener()); + } + + + @NonNull + private ProgressListener getGlideListener() { + return new ProgressListener() { + @Override + public void onProgress(ProgressInfo progressInfo) { + int progress = progressInfo.getPercent(); + mGlideProgress.setProgress(progress); + mGlideProgressText.setText(progress + "%"); + Log.d(TAG, "--Glide-- " + progress + " % " + progressInfo.getSpeed() + " byte/s " + progressInfo.toString()); + if (progressInfo.isFinish()) { + //说明已经加载完成 + Log.d(TAG, "--Glide-- finish"); + } + } + + @Override + public void onError(long id, Exception e) { + mHandler.post(new Runnable() { + @Override + public void run() { + mGlideProgress.setProgress(0); + mGlideProgressText.setText("error"); + } + }); + } + }; + } + + @NonNull + private ProgressListener getUploadListener() { + return new ProgressListener() { + @Override + public void onProgress(ProgressInfo progressInfo) { + // 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束, + // 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息 + // 这里我就取最新的上传进度用来展示,顺便展示下 id 的用法 + + if (mLastUploadingingInfo == null) { + mLastUploadingingInfo = progressInfo; + } + + //因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新 + if (progressInfo.getId() < mLastUploadingingInfo.getId()) { + return; + } else if (progressInfo.getId() > mLastUploadingingInfo.getId()) { + mLastUploadingingInfo = progressInfo; + } + + int progress = mLastUploadingingInfo.getPercent(); + mUploadProgress.setProgress(progress); + mUploadProgressText.setText(progress + "%"); + Log.d(TAG, "--Upload-- " + progress + " % " + mLastUploadingingInfo.getSpeed() + " byte/s " + mLastUploadingingInfo.toString()); + if (mLastUploadingingInfo.isFinish()) { + //说明已经上传完成 + Log.d(TAG, "--Upload-- finish"); + } + } + + @Override + public void onError(long id, Exception e) { + mHandler.post(new Runnable() { + @Override + public void run() { + mUploadProgress.setProgress(0); + mUploadProgressText.setText("error"); + } + }); + } + }; + } + + @NonNull + private ProgressListener getDownloadListener() { + return new ProgressListener() { + @Override + public void onProgress(ProgressInfo progressInfo) { + // 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束, + // 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息 + // 这里我就取最新的下载进度用来展示,顺便展示下 id 的用法 + + if (mLastDownloadingInfo == null) { + mLastDownloadingInfo = progressInfo; + } + + //因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新 + if (progressInfo.getId() < mLastDownloadingInfo.getId()) { + return; + } else if (progressInfo.getId() > mLastDownloadingInfo.getId()) { + mLastDownloadingInfo = progressInfo; + } + + int progress = mLastDownloadingInfo.getPercent(); + mDownloadProgress.setProgress(progress); + mDownloadProgressText.setText(progress + "%"); + Log.d(TAG, "--Download-- " + progress + " % " + mLastDownloadingInfo.getSpeed() + " byte/s " + mLastDownloadingInfo.toString()); + if (mLastDownloadingInfo.isFinish()) { + //说明已经下载完成 + Log.d(TAG, "--Download-- finish"); + } + } + + @Override + public void onError(long id, Exception e) { + mHandler.post(new Runnable() { + @Override + public void run() { + mDownloadProgress.setProgress(0); + mDownloadProgressText.setText("error"); + } + }); + } + }; + } + + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.glide_start: + glideStart(); + break; + case R.id.download_start: + downloadStart(); + break; + case R.id.upload_start: + uploadStart(); + break; + } + } + + /** + * 点击开始上传资源,为了演示,就不做重复点击的处理,即允许用户在还有进度没完成的情况下,使用同一个 url 开始新的上传 + */ + private void uploadStart() { + new Thread(new Runnable() { + @Override + public void run() { + try { + //为了方便就不动态申请权限了,直接将文件放到CacheDir()中 + File file = new File(getCacheDir(), "a.java"); + //读取Assets里面的数据,作为上传源数据 + writeToFile(getAssets().open("a.java"), file); + + Request request = new Request.Builder() + .url(mNewUploadUrl) + .post(RequestBody.create(MediaType.parse("multipart/form-data"), file)) + .build(); + + Response response = mOkHttpClient.newCall(request).execute(); + response.body(); + } catch (IOException e) { + e.printStackTrace(); + //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法 + ProgressManager.getInstance().notifyOnErorr(mNewUploadUrl, e); + } + } + }).start(); + } + + /** + * 点击开始下载资源,为了演示,就不做重复点击的处理,即允许用户在还有进度没完成的情况下,使用同一个 url 开始新的下载 + */ + private void downloadStart() { + new Thread(new Runnable() { + @Override + public void run() { + try { + Request request = new Request.Builder() + .url(mNewDownloadUrl) + .build(); + + Response response = mOkHttpClient.newCall(request).execute(); + + InputStream is = response.body().byteStream(); + //为了方便就不动态申请权限了,直接将文件放到CacheDir()中 + File file = new File(getCacheDir(), "download"); + FileOutputStream fos = new FileOutputStream(file); + BufferedInputStream bis = new BufferedInputStream(is); + byte[] buffer = new byte[1024]; + int len; + while ((len = bis.read(buffer)) != -1) { + fos.write(buffer, 0, len); + } + fos.flush(); + fos.close(); + bis.close(); + is.close(); + + + } catch (IOException e) { + e.printStackTrace(); + //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法 + ProgressManager.getInstance().notifyOnErorr(mNewDownloadUrl, e); + } + } + }).start(); + } + + /** + * 点击开始 Glide 加载图片,为了演示,就不做重复点击的处理,但是 Glide 自己对重复加载做了处理 + * 即重复加载同一个 Url 时,停止还在请求当中的进度,再开启新的加载 + */ + private void glideStart() { + GlideApp.with(this) + .load(mNewImageUrl) + .centerCrop() + .placeholder(R.color.colorPrimary) + .diskCacheStrategy(DiskCacheStrategy.NONE) + .into(mImageView); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + //记得释放引用 + mNewImageUrl = null; + mNewDownloadUrl = null; + mNewUploadUrl = null; + } + + public static File writeToFile(InputStream in, File file) throws IOException { + FileOutputStream out = new FileOutputStream(file); + byte[] buf = new byte[1024]; + int num = 0; + while ((num = in.read(buf)) != -1) { + out.write(buf, 0, buf.length); + } + out.close(); + return file; + } +} diff --git a/app/src/main/java/me/jessyan/progressmanager/demo/AdvanceFragment.java b/app/src/main/java/me/jessyan/progressmanager/demo/AdvanceFragment.java new file mode 100644 index 0000000..e8195b7 --- /dev/null +++ b/app/src/main/java/me/jessyan/progressmanager/demo/AdvanceFragment.java @@ -0,0 +1,214 @@ +/** + * Copyright 2017 JessYan + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.jessyan.progressmanager.demo; + +import android.os.Bundle; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ProgressBar; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.Arrays; + +import me.jessyan.progressmanager.ProgressListener; +import me.jessyan.progressmanager.ProgressManager; +import me.jessyan.progressmanager.body.ProgressInfo; + +/** + * ================================================ + * Created by JessYan on 08/06/2017 12:59 + * Contact me + * Follow me + * ================================================ + */ +public class AdvanceFragment extends Fragment { + private static final String TAG = "AdvanceFragment"; + + private ProgressBar mGlideProgress; + private ProgressBar mDownloadProgress; + private ProgressBar mUploadProgress; + private TextView mGlideProgressText; + private TextView mDownloadProgressText; + private TextView mUploadProgressText; + private View mRootView; + + private ProgressInfo mLastDownloadingInfo; + private ProgressInfo mLastUploadingingInfo; + private Handler mHandler; + private static final String URL_BUNDLE_KEY = "url_bundle_key"; + + public static AdvanceFragment newInstance(String imageUrl, String downloadUrl, String uploadUrl) { + AdvanceFragment fragment = new AdvanceFragment(); + Bundle bundle = new Bundle(); + ArrayList list = new ArrayList<>(Arrays.asList(imageUrl, downloadUrl, uploadUrl)); + bundle.putStringArrayList(URL_BUNDLE_KEY, list); + fragment.setArguments(bundle); + return fragment; + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + mRootView = inflater.inflate(R.layout.fragment_main, container, false); + return mRootView; + } + + + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + mHandler = new Handler(); + initView(); + initData(); + } + + + private void initView() { + mGlideProgress = mRootView.findViewById(R.id.glide_progress); + mDownloadProgress = mRootView.findViewById(R.id.download_progress); + mUploadProgress = mRootView.findViewById(R.id.upload_progress); + mGlideProgressText = mRootView.findViewById(R.id.glide_progress_text); + mDownloadProgressText = mRootView.findViewById(R.id.download_progress_text); + mUploadProgressText = mRootView.findViewById(R.id.upload_progress_text); + } + + private void initData() { + Bundle arguments = getArguments(); + ArrayList list = arguments.getStringArrayList(URL_BUNDLE_KEY); + if (list == null || list.isEmpty()) + return; + //Glide 加载监听 + ProgressManager.getInstance().addResponseListener(list.get(0), getGlideListener()); + + //Okhttp/Retofit 下载监听 + ProgressManager.getInstance().addResponseListener(list.get(1), getDownloadListener()); + + //Okhttp/Retofit 上传监听 + ProgressManager.getInstance().addRequestListener(list.get(2), getUploadListener()); + list.clear(); //清理 list 的引用 + } + + + @NonNull + private ProgressListener getUploadListener() { + return new ProgressListener() { + @Override + public void onProgress(ProgressInfo progressInfo) { + // 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束, + // 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息 + // 这里我就取最新的上传进度用来展示,顺便展示下 id 的用法 + + if (mLastUploadingingInfo == null) { + mLastUploadingingInfo = progressInfo; + } + + //因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新 + if (progressInfo.getId() < mLastUploadingingInfo.getId()) { + return; + } else if (progressInfo.getId() > mLastUploadingingInfo.getId()) { + mLastUploadingingInfo = progressInfo; + } + + + int progress = mLastUploadingingInfo.getPercent(); + mUploadProgress.setProgress(progress); + mUploadProgressText.setText(progress + "%"); + Log.d(TAG, "--Upload-- " + progress + " %"); + } + + @Override + public void onError(long id, Exception e) { + mHandler.post(new Runnable() { + @Override + public void run() { + mUploadProgress.setProgress(0); + mUploadProgressText.setText("error"); + } + }); + } + }; + } + + @NonNull + private ProgressListener getDownloadListener() { + return new ProgressListener() { + @Override + public void onProgress(ProgressInfo progressInfo) { + // 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束, + // 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息 + // 这里我就取最新的下载进度用来展示,顺便展示下 id 的用法 + + if (mLastDownloadingInfo == null) { + mLastDownloadingInfo = progressInfo; + } + + //因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新 + if (progressInfo.getId() < mLastDownloadingInfo.getId()) { + return; + } else if (progressInfo.getId() > mLastDownloadingInfo.getId()) { + mLastDownloadingInfo = progressInfo; + } + + int progress = mLastDownloadingInfo.getPercent(); + mDownloadProgress.setProgress(progress); + mDownloadProgressText.setText(progress + "%"); + Log.d(TAG, "--Download-- " + progress + " %"); + } + + @Override + public void onError(long id, Exception e) { + mHandler.post(new Runnable() { + @Override + public void run() { + mDownloadProgress.setProgress(0); + mDownloadProgressText.setText("error"); + } + }); + } + }; + } + + @NonNull + private ProgressListener getGlideListener() { + return new ProgressListener() { + @Override + public void onProgress(ProgressInfo progressInfo) { + int progress = progressInfo.getPercent(); + mGlideProgress.setProgress(progress); + mGlideProgressText.setText(progress + "%"); + Log.d(TAG, "--Glide-- " + progress + " %"); + } + + @Override + public void onError(long id, Exception e) { + mHandler.post(new Runnable() { + @Override + public void run() { + mGlideProgress.setProgress(0); + mGlideProgressText.setText("error"); + } + }); + } + }; + } +} diff --git a/app/src/main/java/me/jessyan/progressmanager/demo/MainActivity.java b/app/src/main/java/me/jessyan/progressmanager/demo/MainActivity.java index 5231466..3e5cb56 100644 --- a/app/src/main/java/me/jessyan/progressmanager/demo/MainActivity.java +++ b/app/src/main/java/me/jessyan/progressmanager/demo/MainActivity.java @@ -15,6 +15,7 @@ */ package me.jessyan.progressmanager.demo; +import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; @@ -45,6 +46,8 @@ /** * ================================================ + * 这里展示框架的基本功能,高级功能请看 {@link AdvanceActivity} + *

* Created by JessYan on 08/06/2017 12:59 * Contact me * Follow me @@ -53,14 +56,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "MainActivity"; /** - * 我这里为了方便展示,直接使用了全局静态常量,这样 {@link WeakHashMap} 中的 {@code key} 和 {@code value} 永远不会回收,建议日常使用时使用全局变量 - * 最重要的是使用 String mUrl = new String("url");, 而不是 String mUrl = "url"; + * 全局持有 url 是为了使用 {@link WeakHashMap} 的特性,在 {@link ProgressManager} 中顶部有介绍 + * 使用 String mUrl = new String("url");, 而不是 String mUrl = "url"; * 为什么这样做? 因为如果直接使用 String mUrl = "url", 这个 url 字符串会被加入全局字符串常量池, 池中的字符串将不会被回收 * 既然 {@code key} 没被回收, 那 {@link WeakHashMap} 中的值也不会被移除 + * 在 {@link #onDestroy()} 中一定记得释放被引用的 url (将 url 设为 null), 这样框架就能在 java 虚拟机 GC 时释放对应的监听器 */ - public static final String IMAGE_URL = new String("http://jessyancoding.github.io/images/RxCache.png"); - public static final String DOWNLOAD_URL = new String("http://jessyancoding.github.io/images/RxCache.png"); - public static final String UPLOAD_URL = new String("http://upload.qiniu.com/"); + public String mImageUrl = new String("https://raw.githubusercontent.com/JessYanCoding/MVPArmsTemplate/master/art/step.png"); + public String mDownloadUrl = new String("https://raw.githubusercontent.com/JessYanCoding/MVPArmsTemplate/master/art/MVPArms.gif"); + public String mUploadUrl = new String("http://upload.qiniu.com/"); private ImageView mImageView; private OkHttpClient mOkHttpClient; @@ -74,9 +78,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe private ProgressInfo mLastDownloadingInfo; private ProgressInfo mLastUploadingingInfo; private Handler mHandler; - private String mNewImageUrl; - private String mNewDownloadUrl; - private String mNewUploadUrl; @Override @@ -86,36 +87,38 @@ protected void onCreate(Bundle savedInstanceState) { mHandler = new Handler(); initView(); initListener(); - //在 Activity 中显示进度条的同时,也在 Fragment 中显示,为了展示此框架的多端同步更新某一个进度信息 - getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new MainFragment()).commit(); + //在 Activity 中显示进度条的同时,也在 Fragment 中显示对应 url 的进度条,为了展示此框架的多端同步更新某一个进度信息 + getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, + MainFragment.newInstance(mImageUrl, mDownloadUrl, mUploadUrl)).commit(); } private void initView() { setContentView(R.layout.activity_main); - mImageView = (ImageView) findViewById(R.id.imageView); - mGlideProgress = (ProgressBar) findViewById(R.id.glide_progress); - mDownloadProgress = (ProgressBar) findViewById(R.id.download_progress); - mUploadProgress = (ProgressBar) findViewById(R.id.upload_progress); - mGlideProgressText = (TextView) findViewById(R.id.glide_progress_text); - mDownloadProgressText = (TextView) findViewById(R.id.download_progress_text); - mUploadProgressText = (TextView) findViewById(R.id.upload_progress_text); + mImageView = findViewById(R.id.imageView); + mGlideProgress = findViewById(R.id.glide_progress); + mDownloadProgress = findViewById(R.id.download_progress); + mUploadProgress = findViewById(R.id.upload_progress); + mGlideProgressText = findViewById(R.id.glide_progress_text); + mDownloadProgressText = findViewById(R.id.download_progress_text); + mUploadProgressText = findViewById(R.id.upload_progress_text); findViewById(R.id.glide_start).setOnClickListener(this); findViewById(R.id.download_start).setOnClickListener(this); findViewById(R.id.upload_start).setOnClickListener(this); + findViewById(R.id.advance).setOnClickListener(this); } private void initListener() { //Glide 加载监听 - mNewImageUrl = ProgressManager.getInstance().addDiffResponseListenerOnSameUrl(IMAGE_URL, getGlideListener()); + ProgressManager.getInstance().addResponseListener(mImageUrl, getGlideListener()); //Okhttp/Retofit 下载监听 - mNewDownloadUrl = ProgressManager.getInstance().addDiffResponseListenerOnSameUrl(DOWNLOAD_URL, getDownloadListener()); + ProgressManager.getInstance().addResponseListener(mDownloadUrl, getDownloadListener()); //Okhttp/Retofit 上传监听 - mNewUploadUrl = ProgressManager.getInstance().addDiffRequestListenerOnSameUrl(UPLOAD_URL, getUploadListener()); + ProgressManager.getInstance().addRequestListener(mUploadUrl, getUploadListener()); } @@ -246,6 +249,9 @@ public void onClick(View v) { case R.id.upload_start: uploadStart(); break; + case R.id.advance: + startActivity(new Intent(getApplicationContext(),AdvanceActivity.class)); + break; } } @@ -263,7 +269,7 @@ public void run() { writeToFile(getAssets().open("a.java"), file); Request request = new Request.Builder() - .url(mNewUploadUrl) + .url(mUploadUrl) .post(RequestBody.create(MediaType.parse("multipart/form-data"), file)) .build(); @@ -272,7 +278,7 @@ public void run() { } catch (IOException e) { e.printStackTrace(); //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法 - ProgressManager.getInstance().notifyOnErorr(mNewUploadUrl, e); + ProgressManager.getInstance().notifyOnErorr(mUploadUrl, e); } } }).start(); @@ -287,7 +293,7 @@ private void downloadStart() { public void run() { try { Request request = new Request.Builder() - .url(mNewDownloadUrl) + .url(mDownloadUrl) .build(); Response response = mOkHttpClient.newCall(request).execute(); @@ -311,7 +317,7 @@ public void run() { } catch (IOException e) { e.printStackTrace(); //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法 - ProgressManager.getInstance().notifyOnErorr(mNewDownloadUrl, e); + ProgressManager.getInstance().notifyOnErorr(mDownloadUrl, e); } } }).start(); @@ -323,7 +329,7 @@ public void run() { */ private void glideStart() { GlideApp.with(this) - .load(mNewImageUrl) + .load(mImageUrl) .centerCrop() .placeholder(R.color.colorPrimary) .diskCacheStrategy(DiskCacheStrategy.NONE) @@ -333,9 +339,10 @@ private void glideStart() { @Override protected void onDestroy() { super.onDestroy(); - mNewImageUrl = null;// TODO: 10/10/2017 释放资源 - mNewDownloadUrl = null; - mNewUploadUrl = null; + //记得释放引用 + mImageUrl = null; + mDownloadUrl = null; + mUploadUrl = null; } public static File writeToFile(InputStream in, File file) throws IOException { diff --git a/app/src/main/java/me/jessyan/progressmanager/demo/MainFragment.java b/app/src/main/java/me/jessyan/progressmanager/demo/MainFragment.java index e99819d..3cba842 100644 --- a/app/src/main/java/me/jessyan/progressmanager/demo/MainFragment.java +++ b/app/src/main/java/me/jessyan/progressmanager/demo/MainFragment.java @@ -1,12 +1,12 @@ /** * Copyright 2017 JessYan - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -27,13 +27,12 @@ import android.widget.ProgressBar; import android.widget.TextView; -import me.jessyan.progressmanager.body.ProgressInfo; +import java.util.ArrayList; +import java.util.Arrays; + import me.jessyan.progressmanager.ProgressListener; import me.jessyan.progressmanager.ProgressManager; - -import static me.jessyan.progressmanager.demo.MainActivity.DOWNLOAD_URL; -import static me.jessyan.progressmanager.demo.MainActivity.IMAGE_URL; -import static me.jessyan.progressmanager.demo.MainActivity.UPLOAD_URL; +import me.jessyan.progressmanager.body.ProgressInfo; /** * ================================================ @@ -56,6 +55,16 @@ public class MainFragment extends Fragment { private ProgressInfo mLastDownloadingInfo; private ProgressInfo mLastUploadingingInfo; private Handler mHandler; + private static final String URL_BUNDLE_KEY = "url_bundle_key"; + + public static MainFragment newInstance(String imageUrl, String downloadUrl, String uploadUrl) { + MainFragment fragment = new MainFragment(); + Bundle bundle = new Bundle(); + ArrayList list = new ArrayList<>(Arrays.asList(imageUrl, downloadUrl, uploadUrl)); + bundle.putStringArrayList(URL_BUNDLE_KEY, list); + fragment.setArguments(bundle); + return fragment; + } @Nullable @Override @@ -75,23 +84,28 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { private void initView() { - mGlideProgress = (ProgressBar) mRootView.findViewById(R.id.glide_progress); - mDownloadProgress = (ProgressBar) mRootView.findViewById(R.id.download_progress); - mUploadProgress = (ProgressBar) mRootView.findViewById(R.id.upload_progress); - mGlideProgressText = (TextView) mRootView.findViewById(R.id.glide_progress_text); - mDownloadProgressText = (TextView) mRootView.findViewById(R.id.download_progress_text); - mUploadProgressText = (TextView) mRootView.findViewById(R.id.upload_progress_text); + mGlideProgress = mRootView.findViewById(R.id.glide_progress); + mDownloadProgress = mRootView.findViewById(R.id.download_progress); + mUploadProgress = mRootView.findViewById(R.id.upload_progress); + mGlideProgressText = mRootView.findViewById(R.id.glide_progress_text); + mDownloadProgressText = mRootView.findViewById(R.id.download_progress_text); + mUploadProgressText = mRootView.findViewById(R.id.upload_progress_text); } private void initData() { + Bundle arguments = getArguments(); + ArrayList list = arguments.getStringArrayList(URL_BUNDLE_KEY); + if (list == null || list.isEmpty()) + return; //Glide 加载监听 - ProgressManager.getInstance().addResponseListener(IMAGE_URL, getGlideListener()); + ProgressManager.getInstance().addResponseListener(list.get(0), getGlideListener()); //Okhttp/Retofit 下载监听 - ProgressManager.getInstance().addResponseListener(DOWNLOAD_URL, getDownloadListener()); + ProgressManager.getInstance().addResponseListener(list.get(1), getDownloadListener()); //Okhttp/Retofit 上传监听 - ProgressManager.getInstance().addRequestListener(UPLOAD_URL, getUploadListener()); + ProgressManager.getInstance().addRequestListener(list.get(2), getUploadListener()); + list.clear();//清理 list 里的引用 } diff --git a/app/src/main/res/layout/activity_advance.xml b/app/src/main/res/layout/activity_advance.xml new file mode 100644 index 0000000..bedec0b --- /dev/null +++ b/app/src/main/res/layout/activity_advance.xml @@ -0,0 +1,203 @@ + + + + + + + + +