-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathMainFragment.java
More file actions
215 lines (185 loc) · 8.23 KB
/
MainFragment.java
File metadata and controls
215 lines (185 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* 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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
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
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
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 MainFragment newInstance(String imageUrl, String downloadUrl, String uploadUrl) {
MainFragment fragment = new MainFragment();
Bundle bundle = new Bundle();
ArrayList<String> 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<String> 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");
}
});
}
};
}
}