-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaFireDownloader.java
278 lines (240 loc) · 8.85 KB
/
MediaFireDownloader.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
©Copyright by DeviceBlack
June 12, 2024
*/
package com.deviceblack;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.File;
/**
* Class for downloading files from MediaFire.
*/
public class MediaFireDownloader implements DownloadListener {
private Context context;
private DownloadListener listener;
private WebView webView;
private DownloadManager downloadManager;
private long downloadId;
private Runnable progressRunnable;
private Handler handler;
private boolean inProcess;
private String downloadUrl;
private File downloadPath;
private MediaFireDownloader downloaderInstance;
private DownloadCompleteReceiver downloadCompleteReceiver;
private final int ERROR_INVALID_LINK = 1;
private final int ERROR_DOWNLOAD_IN_PROGRESS = 2;
private final int ERROR_FAILED_TO_DOWNLOAD = 3;
/**
* Interface for download events.
*/
public interface DownloadListener {
/**
* Called when the download is started.
*
* @param downloader The instance of MediaFireDownloader.
* @param url The URL of the file being downloaded.
* @param path The path where the file will be saved.
*/
void onMediaFireDownloadStart(MediaFireDownloader downloader, String url, File path);
/**
* Called when the download is successfully completed.
*
* @param downloader The instance of MediaFireDownloader.
* @param url The URL of the downloaded file.
* @param path The path where the file was saved.
*/
void onMediaFireDownloadComplete(MediaFireDownloader downloader, String url, File path);
/**
* Called when the download fails.
*
* @param downloader The instance of MediaFireDownloader.
* @param url The URL of the file that failed to download.
* @param path The path where the file should have been saved.
* @param error The error code associated with the download failure.
*/
void onMediaFireDownloadFailed(MediaFireDownloader downloader, String url, File path, int error);
/**
* Called periodically to update the download progress.
*
* @param downloader The instance of MediaFireDownloader.
* @param url The URL of the file being downloaded.
* @param path The path where the file is being saved.
* @param bytesDownloaded The number of bytes downloaded so far.
* @param bytesTotal The total size of the file in bytes.
*/
void onMediaFireDownloadProgressUpdate(MediaFireDownloader downloader, String url, File path, int bytesDownloaded, int bytesTotal);
}
/**
* Non-static methods
*/
/**
* Constructor for the MediaFireDownloader class.
*
* @param context The application context.
* @param listener The listener for download events.
*/
public MediaFireDownloader(Context context, DownloadListener listener) {
this.context = context;
this.listener = listener;
this.downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
this.handler = new Handler();
this.downloadCompleteReceiver = new DownloadCompleteReceiver();
context.registerReceiver(downloadCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
/**
* Starts downloading a file from MediaFire.
*
* @param url The URL of the file to be downloaded.
* @param path The path where the file should be saved.
*/
public void startDownload(String url, File path) {
if (isValidMediaFireUrl(url)) {
if (!inProcess) {
inProcess = true;
downloadUrl = url;
downloadPath = path;
downloaderInstance = this;
webView = new WebView(context);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new CustomWebViewClient());
webView.setDownloadListener(this);
webView.loadUrl(url);
} else {
listener.onMediaFireDownloadFailed(this, url, path, ERROR_DOWNLOAD_IN_PROGRESS);
}
} else {
listener.onMediaFireDownloadFailed(this, url, path, ERROR_INVALID_LINK);
}
}
/**
* Checks if the provided URL is a valid MediaFire URL.
*
* @param url The URL to be checked.
* @return true if the URL is valid, false otherwise.
*/
public boolean isValidMediaFireUrl(String url) {
if (!url.startsWith("https://www.mediafire.com/file/")) return false;
return WebScraper.getContent(url).contains("id=\"downloadButton\"");
}
/**
* Static methods
*/
/**
* Starts downloading a file from MediaFire.
*
* @param context The application context.
* @param listener The listener for download events.
* @param url The URL of the file to be downloaded.
* @param path The path where the file should be saved.
*/
public static void startDownload(Context context, DownloadListener listener, String url, File path) {
new MediaFireDownloader(context, listener).startDownload(url, path);
}
/**
* Checks if the provided URL is a valid MediaFire URL.
*
* @param context The application context.
* @param url The URL to be checked.
* @return true if the URL is valid, false otherwise.
*/
public static boolean isValidMediaFireUrl(Context context, String url) {
return new MediaFireDownloader(context, null).isValidMediaFireUrl(url);
}
/**
* Called when the download is started.
*
* @param url The URL of the file to be downloaded.
* @param userAgent The user agent of the browser.
* @param contentDisposition The content disposition of the file.
* @param mimeType The MIME type of the file.
* @param contentLength The length of the content to be downloaded.
*/
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String cookies = CookieManager.getInstance().getCookie(url);
request.setMimeType(mimeType);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setDestinationUri(Uri.fromFile(downloadPath));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
downloadId = downloadManager.enqueue(request);
progressRunnable = new ProgressRunnable();
handler.post(progressRunnable);
listener.onMediaFireDownloadStart(downloaderInstance, downloadUrl, downloadPath);
}
/**
* Runnable to periodically update the download progress.
*/
private class ProgressRunnable implements Runnable {
@Override
public void run() {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
if (cursor != null && cursor.moveToFirst()) {
int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (bytesTotal > 0) {
listener.onMediaFireDownloadProgressUpdate(downloaderInstance, downloadUrl, downloadPath, bytesDownloaded, bytesTotal);
}
cursor.close();
}
handler.postDelayed(this, 250);
}
}
/**
* Receiver to handle download complete broadcasts.
*/
private class DownloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) == downloadId) {
handler.removeCallbacks(progressRunnable);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
if (cursor != null && cursor.moveToFirst()) {
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (DownloadManager.STATUS_SUCCESSFUL == status) {
listener.onMediaFireDownloadComplete(downloaderInstance, downloadUrl, downloadPath);
} else {
listener.onMediaFireDownloadFailed(downloaderInstance, downloadUrl, downloadPath, ERROR_FAILED_TO_DOWNLOAD);
}
inProcess = false;
cursor.close();
}
context.unregisterReceiver(downloadCompleteReceiver);
}
}
}
/**
* Custom WebViewClient to handle web page loading.
*/
private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (view.equals(webView)) {
view.evaluateJavascript("document.getElementById('downloadButton').click();", null);
}
}
}
}