Skip to content

Commit 083dcb4

Browse files
committed
first commit
1 parent 70ef503 commit 083dcb4

File tree

11 files changed

+427
-1
lines changed

11 files changed

+427
-1
lines changed

FileScrapper/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

FileScrapper/build.gradle

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id 'com.android.library'
3+
}
4+
5+
android {
6+
compileSdkVersion 30
7+
buildToolsVersion "30.0.3"
8+
9+
defaultConfig {
10+
minSdkVersion 23
11+
targetSdkVersion 30
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
consumerProguardFiles "consumer-rules.pro"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation 'androidx.appcompat:appcompat:1.3.0'
34+
implementation 'com.google.android.material:material:1.3.0'
35+
implementation 'org.jsoup:jsoup:1.13.1'
36+
implementation 'org.jetbrains:annotations:15.0'
37+
testImplementation 'junit:junit:4.13.2'
38+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
39+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
40+
}

FileScrapper/consumer-rules.pro

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.jsoup.filescrapper;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.jsoup.filescrapper.test", appContext.getPackageName());
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.jsoup.filescrapper">
4+
5+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
</manifest>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.jsoup.filescrapper;
2+
3+
import android.app.ProgressDialog;
4+
import android.content.Context;
5+
import android.os.AsyncTask;
6+
import android.widget.Toast;
7+
8+
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.lang.ref.WeakReference;
13+
import java.net.URL;
14+
import java.text.NumberFormat;
15+
import java.text.SimpleDateFormat;
16+
import java.util.Date;
17+
import java.util.Locale;
18+
19+
import static com.jsoup.filescrapper.FileScrapper.getDownloadLink;
20+
21+
public class Downloader extends AsyncTask<String, Integer,Boolean> {
22+
23+
private final WeakReference<Context> context;
24+
private final Provider provider;
25+
private ProgressDialog dialog;
26+
27+
protected Downloader(Context context, Provider provider){
28+
this.context = new WeakReference<>(context);
29+
this.provider = provider;
30+
}
31+
32+
@Override
33+
protected void onPreExecute() {
34+
dialog = new ProgressDialog(context.get());
35+
dialog.setTitle("Downloading");
36+
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
37+
dialog.setProgressPercentFormat(NumberFormat.getPercentInstance());
38+
dialog.show();
39+
}
40+
41+
@Override
42+
protected Boolean doInBackground(String... string) {
43+
try {
44+
String link = string[0];
45+
String filepath = string[1];
46+
URL url = new URL(getDownloadLink(provider,link));
47+
int size = url.openConnection().getContentLength();
48+
InputStream in = url.openStream();
49+
String filename = url.openConnection().getHeaderField("Content-Disposition");
50+
if (filename != null && filename.contains("filename=\"")){
51+
filename = filename.substring(filename.indexOf("filename=\"") + 10, filename.length() - 1);
52+
} else {
53+
String date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(new Date());
54+
filename = "filescrapper_" + date;
55+
}
56+
File file = new File(filename,filename);
57+
if (!file.createNewFile()){
58+
int count = 0;
59+
for (String name : file.getParentFile().list()){
60+
if (name.startsWith(filename))
61+
count ++;
62+
}
63+
file = new File(file.getPath() + String.format(Locale.ENGLISH,"(%d)",count));
64+
}
65+
FileOutputStream out = new FileOutputStream(file);
66+
int read;
67+
byte[] bytes = new byte[1024];
68+
int progress = 0;
69+
while ((read = in.read(bytes)) != -1){
70+
out.write(bytes,0,read);
71+
progress += read;
72+
publishProgress(progress * 100 / size);
73+
}
74+
out.flush();
75+
in.close();
76+
out.close();
77+
dialog.dismiss();
78+
return true;
79+
} catch (IOException | DriveException e) {
80+
e.printStackTrace();
81+
return false;
82+
}
83+
}
84+
85+
@Override
86+
protected void onProgressUpdate(Integer... values) {
87+
int progress = values[0];
88+
dialog.setProgress(progress);
89+
}
90+
91+
@Override
92+
protected void onPostExecute(Boolean aBoolean) {
93+
dialog.dismiss();
94+
}
95+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.jsoup.filescrapper;
2+
3+
public class DriveException extends Throwable {
4+
protected DriveException(String msg){
5+
super(msg);
6+
}
7+
}

0 commit comments

Comments
 (0)