LeOS-Genesis/app/src/main/java/com/darkweb/genesissearchengine/helperManager/localFileDownloader.java

352 lines
14 KiB
Java
Raw Normal View History

2021-03-06 19:55:24 +01:00
package com.darkweb.genesissearchengine.helperManager;
import android.annotation.SuppressLint;
2021-03-09 07:36:59 +01:00
import android.app.DownloadManager;
2021-03-06 19:55:24 +01:00
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
2021-04-13 10:20:07 +02:00
import android.content.ContentResolver;
import android.content.ContentValues;
2021-03-06 19:55:24 +01:00
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
2021-04-24 23:26:22 +02:00
import android.os.Handler;
2021-03-06 19:55:24 +01:00
import android.os.StrictMode;
2021-04-13 10:20:07 +02:00
import android.provider.MediaStore;
2021-04-24 23:26:22 +02:00
import android.widget.Toast;
2021-03-06 19:55:24 +01:00
import androidx.core.app.NotificationCompat;
2021-03-09 07:36:59 +01:00
import androidx.core.content.FileProvider;
2021-04-21 14:48:24 +02:00
import com.darkweb.genesissearchengine.netcipher.client.StrongHttpsClient;
2021-03-06 19:55:24 +01:00
import com.example.myapplication.R;
import org.mozilla.thirdparty.com.google.android.exoplayer2.util.Log;
2021-04-23 14:13:50 +02:00
import org.torproject.android.proxy.util.Prefs;
2021-03-06 19:55:24 +01:00
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
2021-04-24 23:26:22 +02:00
import java.net.HttpURLConnection;
2021-03-06 19:55:24 +01:00
import java.net.InetSocketAddress;
import java.net.Proxy;
2021-04-18 10:37:16 +02:00
import java.net.URI;
2021-03-06 19:55:24 +01:00
import java.net.URL;
import java.net.URLConnection;
2021-04-24 23:26:22 +02:00
import java.text.DecimalFormat;
2021-04-18 10:37:16 +02:00
2021-04-24 23:26:22 +02:00
import ch.boye.httpclientandroidlib.HttpHost;
2021-04-21 14:48:24 +02:00
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.client.methods.HttpGet;
2021-04-24 23:26:22 +02:00
import ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames;
2021-03-06 19:55:24 +01:00
import static java.lang.Thread.sleep;
public class localFileDownloader extends AsyncTask<String, Integer, String> {
@SuppressLint("StaticFieldLeak")
private Context context;
private NotificationManager mNotifyManager;
private NotificationCompat.Builder build;
private OutputStream output;
private InputStream mStream;
2021-04-24 23:26:22 +02:00
private Boolean mIsCanceled = false;
2021-03-06 19:55:24 +01:00
private String PROXY_ADDRESS = "localhost";
private int PROXY_PORT = 9050;
2021-04-21 14:48:24 +02:00
private int mID;
private String mFileName;
2021-03-06 19:55:24 +01:00
private float mTotalByte;
private float mDownloadByte;
2021-03-09 07:36:59 +01:00
private String mURL;
2021-03-06 19:55:24 +01:00
public localFileDownloader(Context pContext, String pURL, String pFileName, int pID) {
this.context = pContext;
this.mFileName = pFileName;
2021-03-09 07:36:59 +01:00
this.mURL = pURL;
2021-03-06 19:55:24 +01:00
this.mID = pID;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mFileName = pFileName;
}
protected void onPreExecute() {
super.onPreExecute();
Intent snoozeIntent = new Intent(context, downloadNotification.class);
snoozeIntent.setAction("Download_Cancelled");
snoozeIntent.putExtra("N_ID", mID);
snoozeIntent.putExtra("N_COMMAND", 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, mID, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
build = new NotificationCompat.Builder(context);
2021-03-23 12:52:22 +01:00
if(mFileName.length()>30){
mFileName = "..." + mFileName.substring(mFileName.length()-30);
}
2021-03-06 19:55:24 +01:00
build.setContentTitle(mFileName)
.setContentText("starting...")
.setChannelId(mID + "")
.setAutoCancel(false)
.setDefaults(0)
2021-03-14 18:59:37 +01:00
.setColor(Color.parseColor("#84989f"))
2021-03-06 19:55:24 +01:00
.setCategory(Notification.CATEGORY_SERVICE)
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_download, "Cancel",pendingIntent)
2021-03-14 18:59:37 +01:00
.setSmallIcon(android.R.drawable.stat_sys_download);
2021-03-06 19:55:24 +01:00
build.setOngoing(Prefs.persistNotifications());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(mID + "",
"Social Media Downloader",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("no sound");
channel.setSound(null, null);
channel.enableLights(false);
channel.setLightColor(Color.BLUE);
channel.enableVibration(false);
mNotifyManager.createNotificationChannel(channel);
}
build.setProgress(100, 0, false);
mNotifyManager.notify(mID, build.build());
}
2021-04-24 23:26:22 +02:00
public static String getFileSize(long size) {
if (size <= 0)
return "0";
final String[] units = new String[] { "B Downloaded", "KB ⇣", "MB ⇣", "GB ⇣", "TB ⇣" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
2021-03-06 19:55:24 +01:00
@Override
protected String doInBackground(String... f_url) {
int count;
2021-04-24 23:26:22 +02:00
int mRequestCode = 0;
2021-04-21 14:48:24 +02:00
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
try {
URL url = new URL(f_url[0]);
2021-04-24 23:26:22 +02:00
HttpURLConnection conection;
Proxy proxy;
if(helperMethod.getDomainName(f_url[0]).contains(".onion")){
proxy = new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved(PROXY_ADDRESS, 9050));
conection = (HttpURLConnection) url.openConnection(proxy);
}else {
conection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(new URI(f_url[0]));;
}
2021-04-21 14:48:24 +02:00
2021-04-24 23:26:22 +02:00
conection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0");
conection.setRequestProperty("Accept","*/*");
2021-04-21 14:48:24 +02:00
conection.connect();
int lenghtOfFile = conection.getContentLength();
2021-04-24 23:26:22 +02:00
mRequestCode = conection.getResponseCode();
2021-04-21 14:48:24 +02:00
mStream = conection.getInputStream();
// Output stream
output = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()+"/"+mFileName));
byte[] data = new byte[100000];
long total = 0;
mTotalByte = lenghtOfFile;
while ((count = mStream.read(data)) != -1) {
total += count;
int cur = (int) ((total * 100) / lenghtOfFile);
2021-04-24 23:26:22 +02:00
if(lenghtOfFile<0){
cur = (int)total;
mDownloadByte = cur * -1;
}else {
mDownloadByte = cur;
}
2021-04-21 14:48:24 +02:00
publishProgress(Math.min(cur, 100));
if (Math.min(cur, 100) > 98) {
sleep(500);
}
Log.i("currentProgress", "currentProgress: " + Math.min(cur, 100) + "\n " + cur);
output.write(data, 0, count);
2021-03-06 19:55:24 +01:00
}
2021-04-21 14:48:24 +02:00
build.setContentText("saving file");
build.setSmallIcon(android.R.drawable.stat_sys_download);
mNotifyManager.notify(mID, build.build());
2021-03-06 19:55:24 +01:00
2021-04-21 14:48:24 +02:00
output.flush();
output.close();
mStream.close();
} catch (Exception ex) {
2021-04-24 23:26:22 +02:00
Log.i("FIZZAHFUCK", ex.getMessage());
if(mRequestCode>300){
//Toast.makeText(context,"Request Forbidden Error Code : ",mRequestCode).show();
}
2021-04-21 14:48:24 +02:00
onCancel();
2021-03-06 19:55:24 +01:00
}
2021-04-21 14:48:24 +02:00
}else {
try {
String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
String urlEncoded = Uri.encode(f_url[0], ALLOWED_URI_CHARS);
2021-03-06 19:55:24 +01:00
2021-04-21 14:48:24 +02:00
StrongHttpsClient httpclient = new StrongHttpsClient(context);
2021-03-14 18:59:37 +01:00
2021-04-24 23:26:22 +02:00
if(helperMethod.getDomainName(f_url[0]).contains(".onion")){
httpclient.useProxy(true, "SOCKS", "127.0.0.1", 9050);
}else {
httpclient.useProxy(true, "SOCKS", "127.0.0.1", 9050);
}
2021-03-06 19:55:24 +01:00
2021-04-21 14:48:24 +02:00
HttpGet httpget = new HttpGet(urlEncoded);
HttpResponse response = httpclient.execute(httpget);
StringBuffer sb = new StringBuffer();
sb.append(response.getStatusLine()).append("\n\n");
InputStream mStream = response.getEntity().getContent();
2021-03-06 19:55:24 +01:00
2021-04-24 23:26:22 +02:00
mRequestCode = response.getStatusLine().getStatusCode();
2021-04-21 14:48:24 +02:00
output = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()+"/"+mFileName));
byte[] data = new byte[100000];
long total = 0;
2021-04-24 23:26:22 +02:00
float lenghtOfFile = response.getEntity().getContentLength();
mTotalByte = lenghtOfFile;
2021-04-21 14:48:24 +02:00
int read;
while ((read = mStream.read(data)) != -1) {
total += read;
int cur = (int) ((total * 100) / response.getEntity().getContentLength());
mDownloadByte = cur;
2021-04-24 23:26:22 +02:00
if(lenghtOfFile<0){
cur = (int)total;
mDownloadByte = total * -1;
}else {
mDownloadByte = cur;
}
2021-04-21 14:48:24 +02:00
publishProgress(Math.min(cur, 100));
if (Math.min(cur, 100) > 98) {
sleep(500);
}
Log.i("currentProgress", "currentProgress: " + Math.min(cur, 100) + "\n " + cur);
output.write(data, 0, read);
}
build.setContentText("saving file");
build.setSmallIcon(android.R.drawable.stat_sys_download);
mNotifyManager.notify(mID, build.build());
output.flush();
output.close();
mStream.close();
}catch (Exception ex){
2021-04-24 23:26:22 +02:00
if(mRequestCode>300){
//Toast.makeText(context,"Request Forbidden Error Code : ",mRequestCode).show();
}
onCancel();
2021-04-21 14:48:24 +02:00
}
}
2021-03-06 19:55:24 +01:00
return null;
}
protected void onProgressUpdate(Integer... progress) {
int mPercentage = (int)(mDownloadByte);
if(mPercentage<0){
2021-04-24 23:26:22 +02:00
build.setProgress(100, progress[0], true);
build.setContentText(getFileSize(mPercentage * -1));
}else {
build.setProgress(100, progress[0], false);
build.setContentText(mPercentage+"%");
2021-03-06 19:55:24 +01:00
}
mNotifyManager.notify(mID, build.build());
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String file_url) {
Intent snoozeIntentPost = new Intent(context, downloadNotification.class);
2021-04-24 23:26:22 +02:00
snoozeIntentPost.setAction("Download_Open");
2021-03-06 19:55:24 +01:00
snoozeIntentPost.putExtra("N_ID", mID);
snoozeIntentPost.putExtra("N_COMMAND", 1);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, mID, snoozeIntentPost, PendingIntent.FLAG_UPDATE_CURRENT);
2021-04-24 23:26:22 +02:00
Intent snoozeIntentPost1 = new Intent(context, downloadNotification.class);
snoozeIntentPost1.setAction("Download_Cancelled");
snoozeIntentPost1.putExtra("N_ID", mID);
snoozeIntentPost1.putExtra("N_COMMAND", 2);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, mID, snoozeIntentPost1, PendingIntent.FLAG_UPDATE_CURRENT);
build.addAction(0, null, null);
2021-03-06 19:55:24 +01:00
build.setContentIntent(pendingIntent);
build.setContentText("Download complete");
2021-03-14 18:59:37 +01:00
build.setSmallIcon(R.xml.ic_check);
2021-04-24 23:26:22 +02:00
build.setColor(Color.parseColor("#84989f"));
2021-03-06 19:55:24 +01:00
build.setProgress(0, 0, false);
build.setAutoCancel(true);
2021-03-09 07:36:59 +01:00
build.setOngoing(false);
2021-04-24 23:26:22 +02:00
build.addAction(android.R.drawable.stat_sys_download, "Open",pendingIntent);
build.addAction(R.drawable.ic_download, "Cancel",pendingIntent1);
2021-03-06 19:55:24 +01:00
build.setPriority(Notification.PRIORITY_LOW);
mNotifyManager.notify(mID, build.build());
2021-03-09 07:36:59 +01:00
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
String mPath = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator + mFileName).replace("File//","content://");
File mFile = new File(mPath);
2021-04-13 10:20:07 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", mFile);
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.TITLE, mFileName);
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, mFileName);
contentValues.put(MediaStore.Downloads.SIZE, mDownloadByte);
2021-04-24 23:26:22 +02:00
contentValues.put(MediaStore.Downloads.MIME_TYPE, helperMethod.getMimeType(uri.toString(), context));
2021-04-13 10:20:07 +02:00
contentValues.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + File.separator + "Temp");
ContentResolver database = context.getContentResolver();
database.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
} else {
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", mFile);
2021-04-24 23:26:22 +02:00
String mime = helperMethod.getMimeType(uri.toString(), context);
if(mime!=null){
dm.addCompletedDownload(mFileName, mURL, false, mime, mFile.getAbsolutePath(), mFile.length(), false);
}
2021-04-13 10:20:07 +02:00
}
2021-03-09 07:36:59 +01:00
2021-03-06 19:55:24 +01:00
}
public void onCancel(){
2021-04-24 23:26:22 +02:00
mIsCanceled = true;
2021-03-06 19:55:24 +01:00
mNotifyManager.cancel(mID);
cancel(true);
}
public void onTrigger(){
2021-04-24 23:26:22 +02:00
if(!mIsCanceled){
mNotifyManager.cancel(mID);
String mPath = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator + mFileName).replace("File//","content://");
File mFile = new File(mPath);
new Handler().postDelayed(() -> helperMethod.openFile(mFile, context), 500);
}
2021-03-06 19:55:24 +01:00
}
}