Skip to content

Commit 59cd87c

Browse files
committed
update error pager
1 parent daf5f82 commit 59cd87c

5 files changed

Lines changed: 75 additions & 14 deletions

File tree

‎library/src/main/java/com/just/library/DefaultDownLoaderImpl.java‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.concurrent.atomic.AtomicBoolean;
2828
import java.util.concurrent.atomic.AtomicInteger;
2929
import java.util.concurrent.locks.ReentrantLock;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
3032

3133
/**
3234
* Created by cenxiaozhong on 2017/5/13.
@@ -176,7 +178,7 @@ private void preDownload(String url, String contentDisposition, long contentLeng
176178

177179

178180
if (ExecuteTasksMap.getInstance().contains(url)) {
179-
if(mAgentWebUIController.get()!=null){
181+
if (mAgentWebUIController.get() != null) {
180182
mAgentWebUIController.get().showMessage(
181183
mDownLoadMsgConfig.getTaskHasBeenExist(), TAG.concat("|preDownload"));
182184
}
@@ -271,15 +273,50 @@ private File getFile(String contentDisposition, String url) {
271273
if (filename.length() > 64) {
272274
filename = filename.substring(filename.length() - 64, filename.length());
273275
}
274-
LogUtils.i(TAG, "filename:" + filename);
276+
LogUtils.i(TAG, "filename:" + filename+" contentDisposition:"+contentDisposition);
275277
return AgentWebUtils.createFileByName(mContext, filename, false);
278+
279+
280+
/* String fileName = getFileName(contentDisposition);
281+
if (TextUtils.isEmpty(fileName)) {
282+
fileName = url.substring(url.lastIndexOf('/') + 1);
283+
}
284+
if (fileName.startsWith("\"")) {
285+
fileName = fileName.substring(1);
286+
}
287+
if (fileName.endsWith("\"")) {
288+
fileName = fileName.substring(0, fileName.length() - 1);
289+
}
290+
291+
if (TextUtils.isEmpty(fileName)) {
292+
fileName = AgentWebUtils.md5(url);
293+
}
294+
LogUtils.i(TAG, "======> download file name:" + fileName);
295+
if (fileName.length() > 64) {
296+
fileName = fileName.substring(fileName.length() - 64, fileName.length());
297+
}
298+
LogUtils.i(TAG, "filename:" + fileName);
299+
return AgentWebUtils.createFileByName(mContext, fileName, false);
300+
*/
276301
} catch (Exception e) {
277302
e.printStackTrace();
278303
}
279304

280305
return null;
281306
}
282307

308+
private String getFileName(String contentDisposition) {
309+
if (TextUtils.isEmpty(contentDisposition)) {
310+
return "";
311+
}
312+
Matcher m = Pattern.compile(".*filename=(.*)").matcher(contentDisposition.toLowerCase());
313+
if (m.find()) {
314+
return m.group(1);
315+
} else {
316+
return "";
317+
}
318+
}
319+
283320
@Override
284321
public void success(String path) {
285322

@@ -290,7 +327,7 @@ public void success(String path) {
290327
return;
291328
}
292329
for (DownLoadResultListener mDownLoadResultListener : mDownLoadResultListeners) {
293-
if(mDownLoadResultListener!=null){
330+
if (mDownLoadResultListener != null) {
294331
mDownLoadResultListener.success(path);
295332
}
296333
}
@@ -303,7 +340,7 @@ public void error(String path, String resUrl, String cause, Throwable e) {
303340
ExecuteTasksMap.getInstance().removeTask(path);
304341

305342
if (AgentWebUtils.isEmptyCollection(mDownLoadResultListeners)) {
306-
if(mAgentWebUIController.get()!=null){
343+
if (mAgentWebUIController.get() != null) {
307344
mAgentWebUIController.get().showMessage(mDownLoadMsgConfig.getDownLoadFail(), TAG.concat("|error"));
308345
}
309346
return;

‎library/src/main/java/com/just/library/RealDownLoader.java‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public class RealDownLoader extends AsyncTask<Void, Integer, Integer> implements
4343
private Notify mNotify;
4444

4545
private static final int ERROR_LOAD = 406;
46-
47-
private static final String TAG=RealDownLoader.class.getSimpleName();
46+
47+
private static final String TAG = RealDownLoader.class.getSimpleName();
4848

4949

5050
private AtomicBoolean atomic = new AtomicBoolean(false);
@@ -94,7 +94,6 @@ private boolean checknet() {
9494
}
9595

9696

97-
9897
@Override
9998
protected Integer doInBackground(Void... params) {
10099
int result = ERROR_LOAD;
@@ -128,10 +127,11 @@ private int doDownLoad() throws IOException {
128127
}
129128
try {
130129
mHttpURLConnection.connect();
131-
if (mHttpURLConnection.getResponseCode() != 200 && mHttpURLConnection.getResponseCode() != 206) {
130+
boolean isSeek = false;
131+
if (mHttpURLConnection.getResponseCode() != 200 && mHttpURLConnection.getResponseCode() != 206 && (isSeek = true)) {
132132
return DownLoadMsg.NETWORK_ERROR_STATUS_CODE.CODE;
133133
}
134-
return doDownLoad(mHttpURLConnection.getInputStream(), new LoadingRandomAccessFile(mDownLoadTask.getFile()));
134+
return doDownLoad(mHttpURLConnection.getInputStream(), new LoadingRandomAccessFile(mDownLoadTask.getFile()), isSeek);
135135
} finally {
136136
if (mHttpURLConnection != null)
137137
mHttpURLConnection.disconnect();
@@ -217,7 +217,7 @@ protected void onPostExecute(Integer integer) {
217217
private void doCallback(Integer code) {
218218
DownLoadResultListener mDownLoadResultListener = null;
219219
if ((mDownLoadResultListener = mDownLoadTask.getDownLoadResultListener()) == null) {
220-
LogUtils.e(TAG,"DownLoadResultListener has been death");
220+
LogUtils.e(TAG, "DownLoadResultListener has been death");
221221
DefaultDownLoaderImpl.ExecuteTasksMap.getInstance().removeTask(mDownLoadTask.getFile().getPath());
222222
return;
223223
}
@@ -260,13 +260,16 @@ private PendingIntent buildCancelContent(Context context, int id) {
260260
}
261261

262262

263-
private int doDownLoad(InputStream in, RandomAccessFile out) throws IOException {
263+
private int doDownLoad(InputStream in, RandomAccessFile out, boolean isSeek) throws IOException {
264264

265265
byte[] buffer = new byte[10240];
266266
BufferedInputStream bis = new BufferedInputStream(in, 1024 * 10);
267267
try {
268268

269-
out.seek(out.length());
269+
if (isSeek) {
270+
LogUtils.i(TAG, "seek -- >" + isSeek + " length:" + out.length());
271+
out.seek(out.length());
272+
}
270273

271274
int bytes = 0;
272275
long previousBlockTime = -1;
@@ -372,7 +375,6 @@ public static String getMsgByCode(int code) {
372375
}
373376

374377

375-
376378
}
377379

378380

‎sample/src/main/java/com/just/library/agentweb/AgentWebFragment.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.just.library.agentweb;
22

33

4+
import android.app.AlertDialog;
45
import android.content.ClipData;
56
import android.content.ClipboardManager;
67
import android.content.Context;
@@ -57,6 +58,7 @@ public class AgentWebFragment extends Fragment implements FragmentKeyDown {
5758
private ImageView mMoreImageView;
5859
private PopupMenu mPopupMenu;
5960
public static final String TAG = AgentWebFragment.class.getSimpleName();
61+
private AlertDialog mInputWebSiteDialog;
6062

6163
public static AgentWebFragment getInstance(Bundle bundle) {
6264

@@ -147,7 +149,7 @@ public String getUrl() {
147149
if (TextUtils.isEmpty(target = this.getArguments().getString(URL_KEY))) {
148150
target = "http://www.jd.com";
149151
}
150-
return "http://www.jsdadsdad.com";
152+
return target;
151153
}
152154

153155
protected ChromeClientCallbackManager.ReceivedTitleCallback mCallback = new ChromeClientCallbackManager.ReceivedTitleCallback() {
@@ -336,12 +338,21 @@ public boolean onMenuItemClick(MenuItem item) {
336338
case R.id.default_clean:
337339
toCleanWebCache();
338340
return true;
341+
case R.id.error_website:
342+
loadErrorWebSite();
343+
return true;
339344
default:
340345
return false;
341346
}
342347

343348
}
344349
};
350+
//这里用于测试错误页的显示
351+
private void loadErrorWebSite() {
352+
if(mAgentWeb!=null){
353+
mAgentWeb.getLoader().loadUrl("http://unkownwebsite.me");
354+
}
355+
}
345356

346357
//清除 WebView 缓存
347358
private void toCleanWebCache() {

‎sample/src/main/java/com/just/library/agentweb/CommonActivity.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ private void openFragment(int key) {
116116
ft.add(R.id.container_framelayout, mAgentWebFragment = SmartRefreshWebFragment.getInstance(mBundle = new Bundle()), SmartRefreshWebFragment.class.getName());
117117
mBundle.putString(AgentWebFragment.URL_KEY, "http://www.163.com/");
118118
break;
119+
/*地图*/
120+
case 13:
121+
ft.add(R.id.container_framelayout, mAgentWebFragment = AgentWebFragment.getInstance(mBundle = new Bundle()), AgentWebFragment.class.getName());
122+
mBundle.putString(AgentWebFragment.URL_KEY, "http://ditu.amap.com/?amapexchange=%2F");
123+
break;
119124

120125
}
121126
ft.commit();

‎sample/src/main/res/menu/toolbar_menu.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
android:title="清空缓存"
2121
android:visible="false"
2222
app:showAsAction="never"/>
23+
24+
<item
25+
android:id="@+id/error_website"
26+
android:title="显示错误页"
27+
android:visible="true"
28+
app:showAsAction="never"/>
2329
<item
2430
android:id="@+id/default_browser"
2531
android:title="在浏览器打开"

0 commit comments

Comments
 (0)