-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadTask.java
119 lines (95 loc) · 4.03 KB
/
DownloadTask.java
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
package com.myPack.networkCalls;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import com.myPack.UmahroConstants;
import com.myPack.utils.AppCallBackListner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadTask {
private static final String TAG = "Download Task";
private Context context;
private String downloadUrl = "", downloadFileName = "";
private ProgressDialog progressDialog;
AppCallBackListner.ResultListener listener;
public DownloadTask(Context context, String downloadUrl, AppCallBackListner.ResultListener listener) {
this.context = context;
this.listener = listener;
this.downloadUrl = downloadUrl;
downloadFileName = downloadUrl.substring(downloadUrl.lastIndexOf('/')+1, downloadUrl.lastIndexOf('.')-1);//Create file name by picking download file name from URL
Log.e(TAG, downloadFileName);
//Start Downloading Task
new DownloadingTask().execute();
}
private class DownloadingTask extends AsyncTask<Void, Void, Void> {
File apkStorage = null;
File outputFile = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Downloading...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected void onPostExecute(Void result) {
try {
if (apkStorage != null) {
listener.onClick(apkStorage);
Toast.makeText(context, "Document Downloaded Successfully", Toast.LENGTH_SHORT).show();
} else {
Log.e(TAG, "Download Failed");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
progressDialog.dismiss();
}
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(downloadUrl);//Create Download URl
HttpURLConnection c = (HttpURLConnection) url.openConnection();//Open Url Connection
c.setRequestMethod("GET");//Set Request Method to "GET" since we are grtting data
c.connect();//connect the URL Connection
//If Connection response is not OK then show Logs
if (c.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
+ " " + c.getResponseMessage());
}
File myDir = new File(UmahroConstants.FILE_PATH);
myDir.mkdirs();
final String name = UmahroConstants.FILE_PATH + "/" +downloadFileName + ".pdf";
apkStorage = new File(name);
FileOutputStream fos = new FileOutputStream(apkStorage);//Get OutputStream for NewFile Location
InputStream is = c.getInputStream();//Get InputStream for connection
byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);//Write new file
}
//Close all connection after doing task
fos.close();
is.close();
} catch (Exception e) {
progressDialog.dismiss();
//Read exception if something went wrong
e.printStackTrace();
apkStorage = null;
Log.e(TAG, "Download Error Exception " + e.getMessage());
}
return null;
}
}
}