Skip to content

Commit 5728621

Browse files
committed
Let Demo support v1.5.0
1 parent 6905c95 commit 5728621

File tree

7 files changed

+857
-47
lines changed

7 files changed

+857
-47
lines changed

app/src/main/AndroidManifest.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@
1414
android:roundIcon="@mipmap/ic_launcher_round"
1515
android:supportsRtl="true"
1616
android:theme="@style/AppTheme">
17-
<activity android:name=".MainActivity">
17+
<activity
18+
android:name=".MainActivity"
19+
android:label="ProgressManager Base"
20+
>
1821
<intent-filter>
1922
<action android:name="android.intent.action.MAIN"/>
2023

2124
<category android:name="android.intent.category.LAUNCHER"/>
2225
</intent-filter>
2326
</activity>
27+
<activity
28+
android:name=".AdvanceActivity"
29+
android:label="ProgressManager Advance"
30+
/>
2431
</application>
2532

2633
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
/**
2+
* Copyright 2017 JessYan
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.jessyan.progressmanager.demo;
17+
18+
import android.os.Bundle;
19+
import android.os.Handler;
20+
import android.support.annotation.NonNull;
21+
import android.support.v7.app.AppCompatActivity;
22+
import android.util.Log;
23+
import android.view.View;
24+
import android.widget.ImageView;
25+
import android.widget.ProgressBar;
26+
import android.widget.TextView;
27+
28+
import com.bumptech.glide.load.engine.DiskCacheStrategy;
29+
30+
import java.io.BufferedInputStream;
31+
import java.io.File;
32+
import java.io.FileOutputStream;
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
36+
import me.jessyan.progressmanager.ProgressListener;
37+
import me.jessyan.progressmanager.ProgressManager;
38+
import me.jessyan.progressmanager.body.ProgressInfo;
39+
import okhttp3.MediaType;
40+
import okhttp3.OkHttpClient;
41+
import okhttp3.Request;
42+
import okhttp3.RequestBody;
43+
import okhttp3.Response;
44+
45+
/**
46+
* ================================================
47+
* 这里为了展示本框架的高级功能,使用同一个 url 地址根据 Post 请求参数的不同而下载或上传不同的资源
48+
*
49+
* @see {@link #initListener}
50+
* Created by JessYan on 08/06/2017 12:59
51+
* <a href="mailto:[email protected]">Contact me</a>
52+
* <a href="https://github.com/JessYanCoding">Follow me</a>
53+
* ================================================
54+
*/
55+
public class AdvanceActivity extends AppCompatActivity implements View.OnClickListener {
56+
private static final String TAG = "AdvanceActivity";
57+
private ImageView mImageView;
58+
private OkHttpClient mOkHttpClient;
59+
private ProgressBar mGlideProgress;
60+
private ProgressBar mDownloadProgress;
61+
private ProgressBar mUploadProgress;
62+
private TextView mGlideProgressText;
63+
private TextView mDownloadProgressText;
64+
private TextView mUploadProgressText;
65+
66+
private ProgressInfo mLastDownloadingInfo;
67+
private ProgressInfo mLastUploadingingInfo;
68+
private Handler mHandler;
69+
private String mNewImageUrl;
70+
private String mNewDownloadUrl;
71+
private String mNewUploadUrl;
72+
73+
74+
@Override
75+
protected void onCreate(Bundle savedInstanceState) {
76+
super.onCreate(savedInstanceState);
77+
mOkHttpClient = ((BaseApplication) getApplicationContext()).getOkHttpClient();
78+
mHandler = new Handler();
79+
initView();
80+
initListener();
81+
//在 Activity 中显示进度条的同时,也在 Fragment 中显示对应 url 的进度条,为了展示此框架的多端同步更新某一个进度信息
82+
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
83+
AdvanceFragment.newInstance(mNewImageUrl, mNewDownloadUrl, mNewUploadUrl)).commit();
84+
}
85+
86+
87+
private void initView() {
88+
setContentView(R.layout.activity_advance);
89+
mImageView = findViewById(R.id.imageView);
90+
mGlideProgress = findViewById(R.id.glide_progress);
91+
mDownloadProgress = findViewById(R.id.download_progress);
92+
mUploadProgress = findViewById(R.id.upload_progress);
93+
mGlideProgressText = findViewById(R.id.glide_progress_text);
94+
mDownloadProgressText = findViewById(R.id.download_progress_text);
95+
mUploadProgressText = findViewById(R.id.upload_progress_text);
96+
findViewById(R.id.glide_start).setOnClickListener(this);
97+
findViewById(R.id.download_start).setOnClickListener(this);
98+
findViewById(R.id.upload_start).setOnClickListener(this);
99+
}
100+
101+
private void initListener() {
102+
//图片和下载 (上传也同样支持) 使用同一个 url 地址,是为了展示高级功能
103+
//高级功能是为了应对当需要使用同一个 url 地址根据 Post 请求参数的不同而下载或上传不同资源的情况
104+
//"http://jessyancoding.github.io/images/RxCache.png" 会重定向到 "http://jessyan.me/images/RxCache.png"
105+
//所以也展示了高级功能同时完美兼容重定向
106+
//这里需要注意的是虽然使用的是新的 url 地址进行上传或下载,但实际请求服务器的 url 地址,还是原始的 url 地址
107+
//在监听器内部已经进行了处理,所以高级功能并不会影响服务器的请求
108+
109+
//Glide 加载监听
110+
mNewImageUrl = ProgressManager
111+
.getInstance()
112+
.addDiffResponseListenerOnSameUrl("http://jessyancoding.github.io/images/RxCache.png", getGlideListener());
113+
114+
115+
//Okhttp/Retofit 下载监听
116+
mNewDownloadUrl = ProgressManager
117+
.getInstance()
118+
.addDiffResponseListenerOnSameUrl("http://jessyancoding.github.io/images/RxCache.png", getDownloadListener());
119+
120+
121+
//Okhttp/Retofit 上传监听
122+
mNewUploadUrl = ProgressManager
123+
.getInstance()
124+
.addDiffRequestListenerOnSameUrl("http://upload.qiniu.com/", "test", getUploadListener());
125+
}
126+
127+
128+
@NonNull
129+
private ProgressListener getGlideListener() {
130+
return new ProgressListener() {
131+
@Override
132+
public void onProgress(ProgressInfo progressInfo) {
133+
int progress = progressInfo.getPercent();
134+
mGlideProgress.setProgress(progress);
135+
mGlideProgressText.setText(progress + "%");
136+
Log.d(TAG, "--Glide-- " + progress + " % " + progressInfo.getSpeed() + " byte/s " + progressInfo.toString());
137+
if (progressInfo.isFinish()) {
138+
//说明已经加载完成
139+
Log.d(TAG, "--Glide-- finish");
140+
}
141+
}
142+
143+
@Override
144+
public void onError(long id, Exception e) {
145+
mHandler.post(new Runnable() {
146+
@Override
147+
public void run() {
148+
mGlideProgress.setProgress(0);
149+
mGlideProgressText.setText("error");
150+
}
151+
});
152+
}
153+
};
154+
}
155+
156+
@NonNull
157+
private ProgressListener getUploadListener() {
158+
return new ProgressListener() {
159+
@Override
160+
public void onProgress(ProgressInfo progressInfo) {
161+
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
162+
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
163+
// 这里我就取最新的上传进度用来展示,顺便展示下 id 的用法
164+
165+
if (mLastUploadingingInfo == null) {
166+
mLastUploadingingInfo = progressInfo;
167+
}
168+
169+
//因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新
170+
if (progressInfo.getId() < mLastUploadingingInfo.getId()) {
171+
return;
172+
} else if (progressInfo.getId() > mLastUploadingingInfo.getId()) {
173+
mLastUploadingingInfo = progressInfo;
174+
}
175+
176+
int progress = mLastUploadingingInfo.getPercent();
177+
mUploadProgress.setProgress(progress);
178+
mUploadProgressText.setText(progress + "%");
179+
Log.d(TAG, "--Upload-- " + progress + " % " + mLastUploadingingInfo.getSpeed() + " byte/s " + mLastUploadingingInfo.toString());
180+
if (mLastUploadingingInfo.isFinish()) {
181+
//说明已经上传完成
182+
Log.d(TAG, "--Upload-- finish");
183+
}
184+
}
185+
186+
@Override
187+
public void onError(long id, Exception e) {
188+
mHandler.post(new Runnable() {
189+
@Override
190+
public void run() {
191+
mUploadProgress.setProgress(0);
192+
mUploadProgressText.setText("error");
193+
}
194+
});
195+
}
196+
};
197+
}
198+
199+
@NonNull
200+
private ProgressListener getDownloadListener() {
201+
return new ProgressListener() {
202+
@Override
203+
public void onProgress(ProgressInfo progressInfo) {
204+
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
205+
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
206+
// 这里我就取最新的下载进度用来展示,顺便展示下 id 的用法
207+
208+
if (mLastDownloadingInfo == null) {
209+
mLastDownloadingInfo = progressInfo;
210+
}
211+
212+
//因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新
213+
if (progressInfo.getId() < mLastDownloadingInfo.getId()) {
214+
return;
215+
} else if (progressInfo.getId() > mLastDownloadingInfo.getId()) {
216+
mLastDownloadingInfo = progressInfo;
217+
}
218+
219+
int progress = mLastDownloadingInfo.getPercent();
220+
mDownloadProgress.setProgress(progress);
221+
mDownloadProgressText.setText(progress + "%");
222+
Log.d(TAG, "--Download-- " + progress + " % " + mLastDownloadingInfo.getSpeed() + " byte/s " + mLastDownloadingInfo.toString());
223+
if (mLastDownloadingInfo.isFinish()) {
224+
//说明已经下载完成
225+
Log.d(TAG, "--Download-- finish");
226+
}
227+
}
228+
229+
@Override
230+
public void onError(long id, Exception e) {
231+
mHandler.post(new Runnable() {
232+
@Override
233+
public void run() {
234+
mDownloadProgress.setProgress(0);
235+
mDownloadProgressText.setText("error");
236+
}
237+
});
238+
}
239+
};
240+
}
241+
242+
243+
@Override
244+
public void onClick(View v) {
245+
switch (v.getId()) {
246+
case R.id.glide_start:
247+
glideStart();
248+
break;
249+
case R.id.download_start:
250+
downloadStart();
251+
break;
252+
case R.id.upload_start:
253+
uploadStart();
254+
break;
255+
}
256+
}
257+
258+
/**
259+
* 点击开始上传资源,为了演示,就不做重复点击的处理,即允许用户在还有进度没完成的情况下,使用同一个 url 开始新的上传
260+
*/
261+
private void uploadStart() {
262+
new Thread(new Runnable() {
263+
@Override
264+
public void run() {
265+
try {
266+
//为了方便就不动态申请权限了,直接将文件放到CacheDir()中
267+
File file = new File(getCacheDir(), "a.java");
268+
//读取Assets里面的数据,作为上传源数据
269+
writeToFile(getAssets().open("a.java"), file);
270+
271+
Request request = new Request.Builder()
272+
.url(mNewUploadUrl)
273+
.post(RequestBody.create(MediaType.parse("multipart/form-data"), file))
274+
.build();
275+
276+
Response response = mOkHttpClient.newCall(request).execute();
277+
response.body();
278+
} catch (IOException e) {
279+
e.printStackTrace();
280+
//当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
281+
ProgressManager.getInstance().notifyOnErorr(mNewUploadUrl, e);
282+
}
283+
}
284+
}).start();
285+
}
286+
287+
/**
288+
* 点击开始下载资源,为了演示,就不做重复点击的处理,即允许用户在还有进度没完成的情况下,使用同一个 url 开始新的下载
289+
*/
290+
private void downloadStart() {
291+
new Thread(new Runnable() {
292+
@Override
293+
public void run() {
294+
try {
295+
Request request = new Request.Builder()
296+
.url(mNewDownloadUrl)
297+
.build();
298+
299+
Response response = mOkHttpClient.newCall(request).execute();
300+
301+
InputStream is = response.body().byteStream();
302+
//为了方便就不动态申请权限了,直接将文件放到CacheDir()中
303+
File file = new File(getCacheDir(), "download");
304+
FileOutputStream fos = new FileOutputStream(file);
305+
BufferedInputStream bis = new BufferedInputStream(is);
306+
byte[] buffer = new byte[1024];
307+
int len;
308+
while ((len = bis.read(buffer)) != -1) {
309+
fos.write(buffer, 0, len);
310+
}
311+
fos.flush();
312+
fos.close();
313+
bis.close();
314+
is.close();
315+
316+
317+
} catch (IOException e) {
318+
e.printStackTrace();
319+
//当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
320+
ProgressManager.getInstance().notifyOnErorr(mNewDownloadUrl, e);
321+
}
322+
}
323+
}).start();
324+
}
325+
326+
/**
327+
* 点击开始 Glide 加载图片,为了演示,就不做重复点击的处理,但是 Glide 自己对重复加载做了处理
328+
* 即重复加载同一个 Url 时,停止还在请求当中的进度,再开启新的加载
329+
*/
330+
private void glideStart() {
331+
GlideApp.with(this)
332+
.load(mNewImageUrl)
333+
.centerCrop()
334+
.placeholder(R.color.colorPrimary)
335+
.diskCacheStrategy(DiskCacheStrategy.NONE)
336+
.into(mImageView);
337+
}
338+
339+
@Override
340+
protected void onDestroy() {
341+
super.onDestroy();
342+
//记得释放引用
343+
mNewImageUrl = null;
344+
mNewDownloadUrl = null;
345+
mNewUploadUrl = null;
346+
}
347+
348+
public static File writeToFile(InputStream in, File file) throws IOException {
349+
FileOutputStream out = new FileOutputStream(file);
350+
byte[] buf = new byte[1024];
351+
int num = 0;
352+
while ((num = in.read(buf)) != -1) {
353+
out.write(buf, 0, buf.length);
354+
}
355+
out.close();
356+
return file;
357+
}
358+
}

0 commit comments

Comments
 (0)