Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
99fd285d41 |
7
.idea/dictionaries/lionel.xml
generated
7
.idea/dictionaries/lionel.xml
generated
@ -1,7 +0,0 @@
|
|||||||
<component name="ProjectDictionaryState">
|
|
||||||
<dictionary name="lionel">
|
|
||||||
<words>
|
|
||||||
<w>netdldata</w>
|
|
||||||
</words>
|
|
||||||
</dictionary>
|
|
||||||
</component>
|
|
@ -9,7 +9,7 @@ android {
|
|||||||
minSdkVersion 24
|
minSdkVersion 24
|
||||||
targetSdkVersion 30
|
targetSdkVersion 30
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "2.0"
|
versionName "1.0"
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,6 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
<service
|
|
||||||
android:name=".TransferService"
|
|
||||||
android:enabled="true"
|
|
||||||
android:exported="true"></service>
|
|
||||||
|
|
||||||
<provider
|
<provider
|
||||||
android:name="androidx.core.content.FileProvider"
|
android:name="androidx.core.content.FileProvider"
|
||||||
@ -30,30 +26,31 @@
|
|||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".SettingsActivity"
|
android:name=".SettingsActivity"
|
||||||
android:label="@string/title_activity_settings"></activity>
|
android:label="@string/title_activity_settings">
|
||||||
|
</activity>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:theme="@style/AppTheme.NoActionBar">
|
android:theme="@style/AppTheme.NoActionBar">
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.SEND" />
|
<action android:name="android.intent.action.SEND" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.DEFAULT" />
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
|
||||||
<data android:mimeType="*/*" />
|
<data android:mimeType="*/*" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.SEND_MULTIPLE" />
|
<action android:name="android.intent.action.SEND_MULTIPLE" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.DEFAULT" />
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
|
||||||
<data android:mimeType="*/*" />
|
<data android:mimeType="*/*" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
@ -78,14 +78,16 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
String type = intent.getType();
|
String type = intent.getType();
|
||||||
|
|
||||||
|
Transfer tr = new Transfer();
|
||||||
|
|
||||||
if (Intent.ACTION_SEND.equals(action) && type != null) {
|
if (Intent.ACTION_SEND.equals(action) && type != null) {
|
||||||
viewPager.setCurrentItem(2);
|
viewPager.setCurrentItem(2);
|
||||||
if ("text/plain".equals(type)) {
|
if ("text/plain".equals(type)) {
|
||||||
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
|
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
|
||||||
Transfer.handleSendText(sharedText);
|
tr.handleSendText(sharedText);
|
||||||
} else {
|
} else {
|
||||||
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||||
Transfer.handleSendFile(uri);
|
new Thread(() -> tr.handleSendFile(uri)).start();
|
||||||
}
|
}
|
||||||
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
|
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
|
||||||
viewPager.setCurrentItem(2);
|
viewPager.setCurrentItem(2);
|
||||||
@ -93,7 +95,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
int nbItem = fileUris.size();
|
int nbItem = fileUris.size();
|
||||||
Toast.makeText(this, "You select " + nbItem + " files", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "You select " + nbItem + " files", Toast.LENGTH_SHORT).show();
|
||||||
if (fileUris != null)
|
if (fileUris != null)
|
||||||
for(Uri uri : fileUris) Transfer.handleSendFile(uri);
|
for(Uri uri : fileUris) tr.handleSendFile(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -108,10 +110,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
ViewPager viewPager = this.findViewById(R.id.view_pager);
|
ViewPager viewPager = this.findViewById(R.id.view_pager);
|
||||||
viewPager.setCurrentItem(2);
|
viewPager.setCurrentItem(2);
|
||||||
|
|
||||||
|
Transfer tr = new Transfer();
|
||||||
|
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
String sharedText = data.getStringExtra(Intent.EXTRA_TEXT);
|
String sharedText = data.getStringExtra(Intent.EXTRA_TEXT);
|
||||||
Transfer.handleSendText(sharedText);
|
tr.handleSendText(sharedText);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ArrayList<Uri> fileUris = new ArrayList<>();
|
ArrayList<Uri> fileUris = new ArrayList<>();
|
||||||
@ -127,7 +130,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
fileUris.add(uri);
|
fileUris.add(uri);
|
||||||
}
|
}
|
||||||
for (Uri uri : fileUris) {
|
for (Uri uri : fileUris) {
|
||||||
Transfer.handleSendFile(uri);
|
tr.handleSendFile(uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,161 +1,188 @@
|
|||||||
package com.localtransfer;
|
package com.localtransfer;
|
||||||
|
|
||||||
import android.animation.ObjectAnimator;
|
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.animation.Animation;
|
import android.widget.Button;
|
||||||
import android.view.animation.LinearInterpolator;
|
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.app.NotificationManagerCompat;
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import java.util.ConcurrentModificationException;
|
import java.util.ConcurrentModificationException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class Progress {
|
public class Progress {
|
||||||
|
|
||||||
private static Notification.Builder bl = new Notification.Builder(Transfer.activity);
|
public static final int UPLOAD = 1000;
|
||||||
|
public static final int DOWNLOAD = 1001;
|
||||||
|
|
||||||
|
|
||||||
|
private static NotificationCompat.Builder bl = new NotificationCompat.Builder(Transfer.activity, null);
|
||||||
private static NotificationManagerCompat notifiManager = NotificationManagerCompat.from(Transfer.activity);
|
private static NotificationManagerCompat notifiManager = NotificationManagerCompat.from(Transfer.activity);
|
||||||
private static final LayoutInflater inflater = (LayoutInflater) Transfer.activity.getSystemService(Transfer.activity.LAYOUT_INFLATER_SERVICE);
|
private static final LayoutInflater inflater = (LayoutInflater) Transfer.activity.getSystemService(Transfer.activity.LAYOUT_INFLATER_SERVICE);
|
||||||
|
|
||||||
private static Intent notificationIntent = new Intent(Transfer.activity, MainActivity.class);
|
private static List instances = new ArrayList();
|
||||||
private static PendingIntent pendingIntent = PendingIntent.getActivity(Transfer.activity,0, notificationIntent, 0);
|
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private long size;
|
||||||
|
|
||||||
|
public long loaded;
|
||||||
|
public long percent;
|
||||||
|
|
||||||
public static View root;
|
public static View root;
|
||||||
public static boolean fragment_on = false;
|
public static boolean fragment_on = false;
|
||||||
public static boolean app_started = false;
|
public static boolean app_started = false;
|
||||||
|
|
||||||
private static long savedTimeMillis;
|
private Timer timer = new Timer();
|
||||||
|
private int drawable;
|
||||||
|
private String info;
|
||||||
|
|
||||||
public static void PreProgress(Transfer tr) {
|
public Button button;
|
||||||
Transfer.activity.runOnUiThread(new Runnable() {
|
private boolean run = false;
|
||||||
|
|
||||||
|
public Progress(String name, long size, int type) {
|
||||||
|
|
||||||
|
instances.add(this);
|
||||||
|
|
||||||
|
run = true;
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
this.size = size;
|
||||||
|
|
||||||
|
id = View.generateViewId();
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case Progress.DOWNLOAD:
|
||||||
|
drawable = R.drawable.ic_download_24;
|
||||||
|
info = "Download complete";
|
||||||
|
break;
|
||||||
|
case Progress.UPLOAD:
|
||||||
|
drawable = R.drawable.ic_upload_24;
|
||||||
|
info = "Upload complete";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException("Unexpected value: " + type);
|
||||||
|
}
|
||||||
|
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
showProgress();
|
||||||
final Drawable image = Transfer.activity.getDrawable(R.drawable.ic_spinner_rotate);
|
|
||||||
|
|
||||||
if(tr.button != null) {
|
|
||||||
int h = image.getIntrinsicHeight();
|
|
||||||
int w = image.getIntrinsicWidth();
|
|
||||||
image.setBounds(0, 0, w, h);
|
|
||||||
tr.button.setCompoundDrawables(tr.button.getCompoundDrawables()[0], null, image, null);
|
|
||||||
ObjectAnimator anim = ObjectAnimator.ofInt(image, "level", 0, 10000);
|
|
||||||
anim.setDuration(1000);
|
|
||||||
anim.setRepeatCount(Animation.INFINITE);
|
|
||||||
anim.setInterpolator(new LinearInterpolator());
|
|
||||||
anim.start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}, 0, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ProgressUpdateDelay(Transfer tr) {
|
public static List getInstances() {
|
||||||
// Run only every second to reduce CPU usage
|
return instances;
|
||||||
if (System.currentTimeMillis() > (savedTimeMillis + 1000)) {
|
}
|
||||||
savedTimeMillis = System.currentTimeMillis();
|
|
||||||
ProgressUpdate(tr);
|
public void stopProgress() {
|
||||||
|
timer.cancel();
|
||||||
|
showProgress();
|
||||||
|
|
||||||
|
run = false;
|
||||||
|
|
||||||
|
if(! app_started) {
|
||||||
|
bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud)
|
||||||
|
.setContentTitle(name)
|
||||||
|
.setContentText(info)
|
||||||
|
.setProgress(0, 0, false)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setOngoing(false);
|
||||||
|
notifiManager.notify(id, bl.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProgressUpdate(Transfer tr){
|
public void delete() {
|
||||||
Transfer.activity.runOnUiThread(new Runnable() {
|
instances.remove(this);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
if (Progress.app_started && tr.button != null)
|
|
||||||
tr.button.setText(String.format("Download in progress %d%%", tr.percent));
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud)
|
|
||||||
.setContentTitle(tr.fileName)
|
|
||||||
.setContentText(String.format("%d%% %s/%s", tr.percent, tr.loadedHuman, tr.fileSizeHuman))
|
|
||||||
.setProgress(100, (int) tr.percent, false)
|
|
||||||
.setContentIntent(pendingIntent);
|
|
||||||
notifiManager.notify(Transfer.NOTIF_SERVICE, bl.build());
|
|
||||||
|
|
||||||
if (app_started && fragment_on)
|
|
||||||
showProgressFragment(tr);
|
|
||||||
|
|
||||||
} catch (ConcurrentModificationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PostProgress(Transfer tr) {
|
public static Progress getProgress(int id) {
|
||||||
ProgressUpdate(tr);
|
|
||||||
Transfer.activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
notifiManager.cancel(Transfer.NOTIF_SERVICE);
|
for (Object obj: instances) {
|
||||||
|
Progress p = (Progress) obj;
|
||||||
if (!app_started) {
|
if (p.id == id) return p;
|
||||||
bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud)
|
}
|
||||||
.setContentTitle(tr.fileName)
|
return null;
|
||||||
.setContentText(tr.info)
|
|
||||||
.setProgress(0, 0, false)
|
|
||||||
.setContentIntent(pendingIntent);
|
|
||||||
notifiManager.notify(tr.id, bl.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tr.button != null) {
|
|
||||||
final LinearLayout layout = (LinearLayout) tr.button.getParent();
|
|
||||||
layout.findViewById(R.id.file_view).setVisibility(LinearLayout.VISIBLE);
|
|
||||||
layout.findViewById(R.id.file_share).setVisibility(LinearLayout.VISIBLE);
|
|
||||||
tr.button.setVisibility(LinearLayout.GONE);
|
|
||||||
tr.button.setEnabled(true);
|
|
||||||
|
|
||||||
tr.button.setText(Transfer.activity.getString(R.string.file_download));
|
|
||||||
tr.button.setCompoundDrawables(tr.button.getCompoundDrawables()[0], null, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println(tr.message);
|
|
||||||
/*Snackbar.make(Transfer.activity.findViewById(R.id.view_pager), tr.message, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();*/
|
|
||||||
Toast.makeText(Transfer.activity, tr.message, Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void showProgressFragment(Transfer tr){
|
public static boolean setButton(String name, Button button) {
|
||||||
|
|
||||||
LinearLayout groot = root.findViewById(R.id.groot);
|
for (Object obj: instances) {
|
||||||
|
Progress p = (Progress) obj;
|
||||||
|
if (name.equals(p.name) && p.run) {
|
||||||
|
p.button = button;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
final View progress;
|
|
||||||
|
|
||||||
if (groot.findViewById(tr.id) != null) {
|
public void showProgress(){
|
||||||
progress = groot.findViewById(tr.id);
|
|
||||||
} else {
|
try {
|
||||||
progress = inflater.inflate(R.layout.progress, null);
|
|
||||||
progress.setId(tr.id);
|
String HumanLoaded = Transfer.humanReadableByteCountBin(loaded);
|
||||||
Transfer.activity.runOnUiThread(() -> {
|
String HumanSize = Transfer.humanReadableByteCountBin(size);
|
||||||
groot.addView(progress, 0);
|
|
||||||
});
|
if(app_started) {
|
||||||
|
notifiManager.cancel(id);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
Intent intent = new Intent(Transfer.activity, MainActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(Transfer.activity, 0, intent, 0);
|
||||||
|
|
||||||
|
bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud)
|
||||||
|
.setContentTitle(name)
|
||||||
|
.setContentText(String.format("%d%% %s/%s", percent, HumanLoaded, HumanSize))
|
||||||
|
.setProgress(100, (int) percent, false)
|
||||||
|
.setContentIntent(pendingIntent)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setOngoing(true);
|
||||||
|
notifiManager.notify(id, bl.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(app_started && fragment_on) {
|
||||||
|
LinearLayout groot = root.findViewById(R.id.groot);
|
||||||
|
|
||||||
|
final View progress;
|
||||||
|
|
||||||
|
if (groot.findViewById(id) != null) {
|
||||||
|
progress = groot.findViewById(id);
|
||||||
|
} else {
|
||||||
|
progress = inflater.inflate(R.layout.progress, null);
|
||||||
|
progress.setId(id);
|
||||||
|
Transfer.activity.runOnUiThread(() -> {
|
||||||
|
groot.addView(progress, 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Transfer.activity.runOnUiThread(() -> {
|
||||||
|
((ProgressBar) progress.findViewById(R.id.progressBar)).setProgress((int) percent);
|
||||||
|
((ImageView) progress.findViewById(R.id.transferType)).setImageResource(drawable);
|
||||||
|
((TextView) progress.findViewById(R.id.progressText)).setText(percent + " %");
|
||||||
|
((TextView) progress.findViewById(R.id.fileName)).setText(name);
|
||||||
|
((TextView) progress.findViewById(R.id.fileSize)).setText(String.format("%s/%s", HumanLoaded, HumanSize));
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (ConcurrentModificationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
((ProgressBar) progress.findViewById(R.id.progressBar)).setProgress((int) tr.percent);
|
|
||||||
((ImageView) progress.findViewById(R.id.transferType)).setImageResource(tr.drawable);
|
|
||||||
((TextView) progress.findViewById(R.id.progressText)).setText(tr.percent + " %");
|
|
||||||
((TextView) progress.findViewById(R.id.fileName)).setText(tr.fileName);
|
|
||||||
((TextView) progress.findViewById(R.id.fileSize)).setText(String.format("%s/%s", tr.loadedHuman, tr.fileSizeHuman));
|
|
||||||
|
|
||||||
if (tr.state == Transfer.STATE_SUCCESS)
|
|
||||||
((TextView) progress.findViewById(R.id.fileName)).setTextColor(Color.GREEN);
|
|
||||||
else if (tr.state == Transfer.STATE_FAILED)
|
|
||||||
((TextView) progress.findViewById(R.id.fileName)).setTextColor(Color.RED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
|||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
|
||||||
public SectionsPagerAdapter(Context context, FragmentManager fm) {
|
public SectionsPagerAdapter(Context context, FragmentManager fm) {
|
||||||
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
super(fm);
|
||||||
mContext = context;
|
mContext = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +1,25 @@
|
|||||||
package com.localtransfer;
|
package com.localtransfer;
|
||||||
|
|
||||||
|
import android.animation.ObjectAnimator;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
import android.provider.OpenableColumns;
|
import android.provider.OpenableColumns;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.animation.Animation;
|
||||||
|
import android.view.animation.LinearInterpolator;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
import androidx.core.content.FileProvider;
|
import androidx.core.content.FileProvider;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
|
|
||||||
@ -26,6 +32,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.ProtocolException;
|
import java.net.ProtocolException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -34,7 +41,6 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.text.CharacterIterator;
|
import java.text.CharacterIterator;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.text.StringCharacterIterator;
|
import java.text.StringCharacterIterator;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.ConcurrentModificationException;
|
import java.util.ConcurrentModificationException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -53,127 +59,6 @@ public class Transfer {
|
|||||||
|
|
||||||
public static Activity activity;
|
public static Activity activity;
|
||||||
|
|
||||||
public static final int STATE_STANDBY = 100;
|
|
||||||
public static final int STATE_RUNNING = 101;
|
|
||||||
public static final int STATE_SUCCESS = 102;
|
|
||||||
public static final int STATE_FAILED = 103;
|
|
||||||
|
|
||||||
public static final int TYPE_DOWNLOAD = 200;
|
|
||||||
public static final int TYPE_UPLOAD = 201;
|
|
||||||
|
|
||||||
public static final int NOTIF_SERVICE = 1000;
|
|
||||||
public static final int NOTIF_GROUP = 1001;
|
|
||||||
|
|
||||||
public static boolean runningTransfer = false;
|
|
||||||
|
|
||||||
private static List<Transfer> instances = new ArrayList<>();
|
|
||||||
|
|
||||||
public static List getInstances() {
|
|
||||||
return instances;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int state;
|
|
||||||
public String error;
|
|
||||||
|
|
||||||
public int type;
|
|
||||||
|
|
||||||
public Integer id;
|
|
||||||
public int drawable;
|
|
||||||
public String info;
|
|
||||||
public String message;
|
|
||||||
public String fileName;
|
|
||||||
|
|
||||||
public long fileSize;
|
|
||||||
public String fileSizeHuman;
|
|
||||||
|
|
||||||
public long loaded;
|
|
||||||
public String loadedHuman = "0";
|
|
||||||
public long percent;
|
|
||||||
|
|
||||||
public Transfer(int type) {
|
|
||||||
|
|
||||||
id = View.generateViewId();
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case TYPE_DOWNLOAD:
|
|
||||||
drawable = R.drawable.ic_download_24;
|
|
||||||
info = "Download complete";
|
|
||||||
break;
|
|
||||||
case TYPE_UPLOAD:
|
|
||||||
drawable = R.drawable.ic_upload_24;
|
|
||||||
info = "Upload complete";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalStateException("Unexpected value: " + type);
|
|
||||||
}
|
|
||||||
|
|
||||||
state = STATE_STANDBY;
|
|
||||||
this.type = type;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddTransfer() {
|
|
||||||
instances.add(this);
|
|
||||||
|
|
||||||
if(!runningTransfer) {
|
|
||||||
Intent serviceIntent = new Intent(activity, TransferService.class);
|
|
||||||
ContextCompat.startForegroundService(activity, serviceIntent);
|
|
||||||
runningTransfer = true;
|
|
||||||
new Thread(() -> StartTransfer(this)).start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void StartTransfer(Transfer tr) {
|
|
||||||
try {
|
|
||||||
switch (tr.type) {
|
|
||||||
case Transfer.TYPE_DOWNLOAD:
|
|
||||||
tr.downloadFile();
|
|
||||||
break;
|
|
||||||
case Transfer.TYPE_UPLOAD:
|
|
||||||
tr.uploadFile();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalStateException("Unexpected value: " + tr.type);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
final String ExceptionName = e.getClass().getSimpleName();
|
|
||||||
final String ExceptionMess = e.getMessage();
|
|
||||||
|
|
||||||
tr.state = Transfer.STATE_FAILED;
|
|
||||||
tr.error = ExceptionName + ": " + ExceptionMess;
|
|
||||||
Progress.PostProgress(tr);
|
|
||||||
|
|
||||||
if(ExceptionName != null && ExceptionMess != null) {
|
|
||||||
Transfer.error(ExceptionMess);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
for (Object obj : Transfer.getInstances()) {
|
|
||||||
Transfer transfer = (Transfer) obj;
|
|
||||||
if (transfer.state == Transfer.STATE_STANDBY)
|
|
||||||
StartTransfer(transfer);
|
|
||||||
}
|
|
||||||
} catch (ConcurrentModificationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
try {
|
|
||||||
Thread.sleep(10);
|
|
||||||
} catch (InterruptedException InterrupEx) {
|
|
||||||
InterrupEx.printStackTrace();
|
|
||||||
}
|
|
||||||
for (Object obj : Transfer.getInstances()) {
|
|
||||||
Transfer transfer = (Transfer) obj;
|
|
||||||
if (transfer.state == Transfer.STATE_STANDBY)
|
|
||||||
StartTransfer(transfer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Intent serviceIntent = new Intent(Transfer.activity, TransferService.class);
|
|
||||||
Transfer.activity.stopService(serviceIntent);
|
|
||||||
Transfer.runningTransfer = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void parameter(Context context) {
|
public static void parameter(Context context) {
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
|
||||||
@ -187,267 +72,7 @@ public class Transfer {
|
|||||||
protocol = "http";
|
protocol = "http";
|
||||||
}
|
}
|
||||||
|
|
||||||
public Uri uri;
|
private URL checkRedirection(URL url) throws IOException {
|
||||||
|
|
||||||
public static void handleSendFile(Uri uri) {
|
|
||||||
if (uri != null) {
|
|
||||||
Transfer tr = new Transfer(Transfer.TYPE_UPLOAD);
|
|
||||||
tr.uri = uri;
|
|
||||||
|
|
||||||
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
|
||||||
cursor.moveToFirst();
|
|
||||||
|
|
||||||
tr.fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
|
|
||||||
tr.fileSize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
|
|
||||||
tr.fileSizeHuman = Transfer.humanReadableByteCountBin(tr.fileSize);
|
|
||||||
|
|
||||||
tr.AddTransfer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void handleSendText(String sharedText) {
|
|
||||||
if (sharedText != null) {
|
|
||||||
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
|
||||||
Date now = new Date();
|
|
||||||
String strDate = sdfDate.format(now);
|
|
||||||
Uri uri = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
File outputFile = new File(activity.getCacheDir(), strDate + ".txt");
|
|
||||||
FileOutputStream fileOut = new FileOutputStream(outputFile);
|
|
||||||
OutputStreamWriter OutWriter = new OutputStreamWriter(fileOut);
|
|
||||||
OutWriter.append(sharedText);
|
|
||||||
OutWriter.close();
|
|
||||||
|
|
||||||
fileOut.flush();
|
|
||||||
fileOut.close();
|
|
||||||
|
|
||||||
uri = FileProvider.getUriForFile(activity, activity.getPackageName(), outputFile);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
handleSendFile(uri);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String uploadFile() throws IOException {
|
|
||||||
URL url = null;
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
DataOutputStream request = null;
|
|
||||||
String boundary = "*****";
|
|
||||||
int maxBufferSize = 1 * 1024 * 1024;
|
|
||||||
byte[] buffer = new byte[maxBufferSize];
|
|
||||||
int bufferLength;
|
|
||||||
loaded = 0;
|
|
||||||
|
|
||||||
String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
|
|
||||||
|
|
||||||
String type = activity.getContentResolver().getType(uri);
|
|
||||||
|
|
||||||
url = new URL(protocol, host, port, root + "/upload.php");
|
|
||||||
|
|
||||||
url = checkRedirection(url);
|
|
||||||
|
|
||||||
Progress.PreProgress(this);
|
|
||||||
|
|
||||||
if(url != null) {
|
|
||||||
|
|
||||||
Log.d("URL", url.toString());
|
|
||||||
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setDoInput(true); // Allow Inputs
|
|
||||||
conn.setDoOutput(true); // Allow Outputs
|
|
||||||
conn.setUseCaches(false); // Don't use a Cached Copy
|
|
||||||
conn.setChunkedStreamingMode(maxBufferSize);
|
|
||||||
conn.setRequestMethod("POST");
|
|
||||||
conn.setRequestProperty("Connection", "keep-alive");
|
|
||||||
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
|
||||||
|
|
||||||
conn.connect();
|
|
||||||
|
|
||||||
request = new DataOutputStream(conn.getOutputStream());
|
|
||||||
|
|
||||||
request.writeBytes("--" + boundary + "\r\n");
|
|
||||||
request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileNameUTF8 + "\"\r\n");
|
|
||||||
request.writeBytes("Content-Type: " + type + "\r\n\r\n");
|
|
||||||
|
|
||||||
InputStream in = activity.getContentResolver().openInputStream(uri);
|
|
||||||
|
|
||||||
while ((bufferLength = in.read(buffer)) > 0) {
|
|
||||||
request.write(buffer, 0, bufferLength);
|
|
||||||
loaded+= (long) bufferLength;
|
|
||||||
loadedHuman = Transfer.humanReadableByteCountBin(loaded);
|
|
||||||
percent = ((loaded * 100) / fileSize);
|
|
||||||
Progress.ProgressUpdateDelay(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
request.writeBytes("\r\n");
|
|
||||||
request.writeBytes("--" + boundary + "--\r\n");
|
|
||||||
|
|
||||||
in.close();
|
|
||||||
|
|
||||||
int responseCode = conn.getResponseCode();
|
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
||||||
message = "File " + fileName + " successful upload";
|
|
||||||
state = Transfer.STATE_SUCCESS;
|
|
||||||
Progress.PostProgress(this);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
String s = conn.getResponseMessage();
|
|
||||||
throw new HttpErrorException("HTTP Error code " + responseCode + " " + s);
|
|
||||||
}
|
|
||||||
|
|
||||||
request.flush();
|
|
||||||
|
|
||||||
request.close();
|
|
||||||
|
|
||||||
conn.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public File file;
|
|
||||||
public String href;
|
|
||||||
public Button button;
|
|
||||||
|
|
||||||
public void downloadFile() throws IOException {
|
|
||||||
URL url = null;
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
int maxBufferSize = 1 * 1024 * 1024;
|
|
||||||
byte[] buffer = new byte[maxBufferSize];
|
|
||||||
int bufferLength;
|
|
||||||
loaded = 0;
|
|
||||||
|
|
||||||
Progress.PreProgress(this);
|
|
||||||
|
|
||||||
String[] parts = href.split("/");
|
|
||||||
String path = "";
|
|
||||||
for(int i=0; i< parts.length - 1; i++) {
|
|
||||||
path = path + parts[i] + "/";
|
|
||||||
}
|
|
||||||
String query = URLEncoder.encode(parts[parts.length - 1], StandardCharsets.UTF_8.toString());
|
|
||||||
url = new URL(protocol, host, port, path + query.replace("+", "%20"));
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setDoOutput(true);
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
conn.setConnectTimeout(2000);
|
|
||||||
|
|
||||||
Log.d("URL", url.toString());
|
|
||||||
|
|
||||||
conn.connect();
|
|
||||||
|
|
||||||
int responseCode = conn.getResponseCode();
|
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
||||||
|
|
||||||
FileOutputStream fileOutput = new FileOutputStream(file);
|
|
||||||
InputStream in = conn.getInputStream();
|
|
||||||
|
|
||||||
while ((bufferLength = in.read(buffer)) > 0) {
|
|
||||||
fileOutput.write(buffer, 0, bufferLength);
|
|
||||||
loaded+= (long) bufferLength;
|
|
||||||
loadedHuman = Transfer.humanReadableByteCountBin(loaded);
|
|
||||||
percent = ((loaded * 100) / fileSize);
|
|
||||||
Progress.ProgressUpdateDelay(this);
|
|
||||||
}
|
|
||||||
fileOutput.close();
|
|
||||||
|
|
||||||
conn.disconnect();
|
|
||||||
|
|
||||||
message = "File " + fileName + " successful download";
|
|
||||||
state = Transfer.STATE_SUCCESS;
|
|
||||||
Progress.PostProgress(this);
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
String s = conn.getResponseMessage();
|
|
||||||
throw new HttpErrorException("HTTP Error code " + responseCode + " " + s);
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean setButton(String name, Button button) {
|
|
||||||
|
|
||||||
for (Object obj: instances) {
|
|
||||||
Transfer tr = (Transfer) obj;
|
|
||||||
if (name.equals(tr.fileName) && tr.state == STATE_RUNNING) {
|
|
||||||
tr.button = button;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFileList() throws IOException {
|
|
||||||
URL url = null;
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
|
|
||||||
String fileList = "";
|
|
||||||
int buf;
|
|
||||||
|
|
||||||
url = new URL(protocol, host, port, root + "/list.php");
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setConnectTimeout(2000);
|
|
||||||
|
|
||||||
conn.connect();
|
|
||||||
|
|
||||||
int responseCode = conn.getResponseCode();
|
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
||||||
InputStreamReader isw = new InputStreamReader(conn.getInputStream());
|
|
||||||
|
|
||||||
while ((buf = isw.read()) > 0) {
|
|
||||||
fileList = fileList + (char) buf;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
String s = conn.getResponseMessage();
|
|
||||||
throw new HttpErrorException("HTTP Error code " + responseCode + " " + s);
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.disconnect();
|
|
||||||
|
|
||||||
return fileList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String deleteFile(String file) throws IOException {
|
|
||||||
|
|
||||||
URL url = null;
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
|
|
||||||
String message;
|
|
||||||
|
|
||||||
String query = URLEncoder.encode(file, StandardCharsets.UTF_8.toString());
|
|
||||||
String serverUrl = root + "/delete.php?file=" + query;
|
|
||||||
url = new URL(protocol, host, port, serverUrl);
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
conn.setConnectTimeout(2000);
|
|
||||||
|
|
||||||
conn.connect();
|
|
||||||
|
|
||||||
int responseCode = conn.getResponseCode();
|
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
||||||
|
|
||||||
message = "File " + file + " successful deleted";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
String s = conn.getResponseMessage();
|
|
||||||
throw new HttpErrorException("HTTP Error code " + responseCode + " " + s);
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.disconnect();
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static URL checkRedirection(URL url) throws IOException {
|
|
||||||
String location = null;
|
String location = null;
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
int responseCode;
|
int responseCode;
|
||||||
@ -488,18 +113,333 @@ public class Transfer {
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void error(final String message) {
|
public void handleSendFile(Uri uri) {
|
||||||
|
if (uri != null) {
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
uploadFile(uri);
|
||||||
|
} catch (IOException e) {
|
||||||
|
final String ExceptionName = e.getClass().getSimpleName();
|
||||||
|
final String ExceptionMess = e.getMessage();
|
||||||
|
|
||||||
|
if(ExceptionName != null && ExceptionMess != null) {
|
||||||
|
Transfer.error(ExceptionName + ": " + ExceptionMess, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleSendText(String sharedText) {
|
||||||
|
if (sharedText != null) {
|
||||||
|
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
||||||
|
Date now = new Date();
|
||||||
|
String strDate = sdfDate.format(now);
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
File outputFile = new File(activity.getCacheDir(), strDate + ".txt");
|
||||||
|
FileOutputStream fileOut = new FileOutputStream(outputFile);
|
||||||
|
OutputStreamWriter OutWriter = new OutputStreamWriter(fileOut);
|
||||||
|
OutWriter.append(sharedText);
|
||||||
|
OutWriter.close();
|
||||||
|
|
||||||
|
fileOut.flush();
|
||||||
|
fileOut.close();
|
||||||
|
|
||||||
|
Uri uri = FileProvider.getUriForFile(activity, activity.getPackageName(), outputFile);
|
||||||
|
|
||||||
|
uploadFile(uri);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String uploadFile(Uri uri) throws IOException {
|
||||||
|
String message = null;
|
||||||
|
URL url = null;
|
||||||
|
HttpURLConnection conn = null;
|
||||||
|
DataOutputStream request = null;
|
||||||
|
String boundary = "*****";
|
||||||
|
int maxBufferSize = 1 * 1024 * 1024;
|
||||||
|
byte[] buffer = new byte[maxBufferSize];
|
||||||
|
int bufferLength;
|
||||||
|
long loaded = 0;
|
||||||
|
|
||||||
|
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
||||||
|
cursor.moveToFirst();
|
||||||
|
String fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
|
||||||
|
String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
|
||||||
|
long fileSize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
|
||||||
|
String type = activity.getContentResolver().getType(uri);
|
||||||
|
|
||||||
|
url = new URL(protocol, host, port, root + "/upload.php");
|
||||||
|
|
||||||
|
url = checkRedirection(url);
|
||||||
|
|
||||||
|
Progress p = new Progress(fileName, fileSize, Progress.UPLOAD);
|
||||||
|
|
||||||
|
if(url != null) {
|
||||||
|
|
||||||
|
Log.d("URL", url.toString());
|
||||||
|
|
||||||
|
conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setDoInput(true); // Allow Inputs
|
||||||
|
conn.setDoOutput(true); // Allow Outputs
|
||||||
|
conn.setUseCaches(false); // Don't use a Cached Copy
|
||||||
|
conn.setChunkedStreamingMode(maxBufferSize);
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setRequestProperty("Connection", "keep-alive");
|
||||||
|
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
||||||
|
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
request = new DataOutputStream(conn.getOutputStream());
|
||||||
|
|
||||||
|
request.writeBytes("--" + boundary + "\r\n");
|
||||||
|
request.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileNameUTF8 + "\"\r\n");
|
||||||
|
request.writeBytes("Content-Type: " + type + "\r\n\r\n");
|
||||||
|
|
||||||
|
InputStream in = activity.getContentResolver().openInputStream(uri);
|
||||||
|
|
||||||
|
while ((bufferLength = in.read(buffer)) > 0) {
|
||||||
|
request.write(buffer, 0, bufferLength);
|
||||||
|
loaded+= (long) bufferLength;
|
||||||
|
p.loaded = loaded;
|
||||||
|
p.percent = ((loaded * 100) / fileSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
request.writeBytes("\r\n");
|
||||||
|
request.writeBytes("--" + boundary + "--\r\n");
|
||||||
|
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
message = "File " + fileName + " successful upload";
|
||||||
|
p.stopProgress();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String s = conn.getResponseMessage();
|
||||||
|
throw new HttpErrorException("error code: " + responseCode + " " + s);
|
||||||
|
}
|
||||||
|
|
||||||
|
request.flush();
|
||||||
|
|
||||||
|
request.close();
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
System.out.println(message);
|
||||||
|
if (activity != null) {
|
||||||
|
String finalMessage = message;
|
||||||
|
activity.runOnUiThread(() ->
|
||||||
|
Snackbar.make(activity.findViewById(R.id.view_pager), finalMessage, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFileList() throws IOException {
|
||||||
|
URL url = null;
|
||||||
|
HttpURLConnection conn = null;
|
||||||
|
|
||||||
|
String fileList = "";
|
||||||
|
int buf;
|
||||||
|
|
||||||
|
url = new URL(protocol, host, port, root + "/list.php");
|
||||||
|
conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setConnectTimeout(2000);
|
||||||
|
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
InputStreamReader isw = new InputStreamReader(conn.getInputStream());
|
||||||
|
|
||||||
|
while ((buf = isw.read()) > 0) {
|
||||||
|
fileList = fileList + (char) buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String s = conn.getResponseMessage();
|
||||||
|
throw new HttpErrorException("error code: " + responseCode + " " + s);
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void downloadFile(final Uri uri, String name, long fileSize, String href, Button button) throws IOException {
|
||||||
|
|
||||||
|
/*final LinearLayout layout = activity.findViewById(id);
|
||||||
|
final Button button;
|
||||||
|
if(layout != null)
|
||||||
|
button = layout.findViewById(R.id.file_download);
|
||||||
|
else
|
||||||
|
button = null;*/
|
||||||
|
|
||||||
|
final File file = new File(uri.getPath());
|
||||||
|
|
||||||
|
URL url = null;
|
||||||
|
HttpURLConnection conn = null;
|
||||||
|
int maxBufferSize = 1 * 1024 * 1024;
|
||||||
|
byte[] buffer = new byte[maxBufferSize];
|
||||||
|
int bufferLength;
|
||||||
|
long loaded = 0;
|
||||||
|
|
||||||
|
final Drawable image = activity.getDrawable(R.drawable.ic_spinner_rotate);
|
||||||
|
|
||||||
|
Progress p = new Progress(name, fileSize, Progress.DOWNLOAD);
|
||||||
|
if(button != null)
|
||||||
|
p.button = button;
|
||||||
|
|
||||||
|
if(p.button != null) {
|
||||||
|
activity.runOnUiThread(() -> {
|
||||||
|
int h = image.getIntrinsicHeight();
|
||||||
|
int w = image.getIntrinsicWidth();
|
||||||
|
image.setBounds(0, 0, w, h);
|
||||||
|
p.button.setCompoundDrawables(button.getCompoundDrawables()[0], null, image, null);
|
||||||
|
ObjectAnimator anim = ObjectAnimator.ofInt(image, "level", 0, 10000);
|
||||||
|
anim.setDuration(1000);
|
||||||
|
anim.setRepeatCount(Animation.INFINITE);
|
||||||
|
anim.setInterpolator(new LinearInterpolator());
|
||||||
|
anim.start();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] parts = href.split("/");
|
||||||
|
String path = "";
|
||||||
|
for(int i=0; i< parts.length - 1; i++) {
|
||||||
|
path = path + parts[i] + "/";
|
||||||
|
}
|
||||||
|
String query = URLEncoder.encode(parts[parts.length - 1], StandardCharsets.UTF_8.toString());
|
||||||
|
url = new URL(protocol, host, port, path + query.replace("+", "%20"));
|
||||||
|
conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.setConnectTimeout(2000);
|
||||||
|
|
||||||
|
Log.d("URL", url.toString());
|
||||||
|
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
|
||||||
|
FileOutputStream fileOutput = new FileOutputStream(file);
|
||||||
|
InputStream in = conn.getInputStream();
|
||||||
|
|
||||||
|
while ((bufferLength = in.read(buffer)) > 0) {
|
||||||
|
fileOutput.write(buffer, 0, bufferLength);
|
||||||
|
loaded+= (long) bufferLength;
|
||||||
|
p.loaded = loaded;
|
||||||
|
p.percent = ((loaded * 100) / fileSize);
|
||||||
|
if(Progress.app_started && p.button != null)
|
||||||
|
activity.runOnUiThread(() -> p.button.setText(String.format("Download in progress %d%%", p.percent)));
|
||||||
|
}
|
||||||
|
fileOutput.close();
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
p.stopProgress();
|
||||||
|
|
||||||
|
if(p.button != null) {
|
||||||
|
activity.runOnUiThread(() -> {
|
||||||
|
final LinearLayout layout = (LinearLayout) p.button.getParent();
|
||||||
|
layout.findViewById(R.id.file_view).setVisibility(LinearLayout.VISIBLE);
|
||||||
|
layout.findViewById(R.id.file_share).setVisibility(LinearLayout.VISIBLE);
|
||||||
|
p.button.setVisibility(LinearLayout.GONE);
|
||||||
|
p.button.setEnabled(true);
|
||||||
|
|
||||||
|
p.button.setText(activity.getString(R.string.file_download));
|
||||||
|
p.button.setCompoundDrawables(p.button.getCompoundDrawables()[0], null, null, null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
activity.runOnUiThread(() -> {
|
||||||
|
String message = "File " + name + " successful download";
|
||||||
|
System.out.println(message);
|
||||||
|
Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String s = conn.getResponseMessage();
|
||||||
|
throw new HttpErrorException("error code: " + responseCode + " " + s);
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String deleteFile(String file) throws IOException {
|
||||||
|
|
||||||
|
URL url = null;
|
||||||
|
HttpURLConnection conn = null;
|
||||||
|
|
||||||
|
String message;
|
||||||
|
|
||||||
|
String query = URLEncoder.encode(file, StandardCharsets.UTF_8.toString());
|
||||||
|
String serverUrl = root + "/delete.php?file=" + query;
|
||||||
|
url = new URL(protocol, host, port, serverUrl);
|
||||||
|
conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.setConnectTimeout(2000);
|
||||||
|
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
|
||||||
|
message = "File " + file + " successful deleted";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String s = conn.getResponseMessage();
|
||||||
|
throw new HttpErrorException("error code: " + responseCode + " " + s);
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int timeout;
|
||||||
|
|
||||||
|
public static void error(final String message, final TextView title, final LinearLayout layout) {
|
||||||
|
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
|
|
||||||
if (activity != null) {
|
if (timeout == 0) {
|
||||||
activity.runOnUiThread(new Runnable() {
|
if (activity != null) {
|
||||||
|
activity.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if(layout != null)
|
||||||
|
layout.removeAllViews();
|
||||||
|
if(title != null) {
|
||||||
|
layout.addView(title);
|
||||||
|
title.setText(message);
|
||||||
|
}
|
||||||
|
Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
timeout = 1;
|
||||||
|
new Timer().schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
timeout = 0;
|
||||||
.setAction("Action", null).show();
|
|
||||||
}
|
}
|
||||||
});
|
}, 3000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package com.localtransfer;
|
|
||||||
|
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.PendingIntent;
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.IBinder;
|
|
||||||
|
|
||||||
import androidx.core.app.NotificationCompat;
|
|
||||||
|
|
||||||
public class TransferService extends Service {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate() {
|
|
||||||
super.onCreate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
||||||
|
|
||||||
Intent notificationIntent = new Intent(this, MainActivity.class);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(this,
|
|
||||||
0, notificationIntent, 0);
|
|
||||||
|
|
||||||
Notification notification = new NotificationCompat.Builder(this, "CHANNEL_ID")
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
startForeground(Transfer.NOTIF_SERVICE, notification);
|
|
||||||
|
|
||||||
//do heavy work on a background thread
|
|
||||||
//stopSelf();
|
|
||||||
|
|
||||||
return START_NOT_STICKY;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
// TODO: Return the communication channel to the service.
|
|
||||||
throw new UnsupportedOperationException("Not yet implemented");
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,6 +27,7 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.localtransfer.BuildConfig;
|
import com.localtransfer.BuildConfig;
|
||||||
|
import com.localtransfer.Progress;
|
||||||
import com.localtransfer.R;
|
import com.localtransfer.R;
|
||||||
import com.localtransfer.Transfer;
|
import com.localtransfer.Transfer;
|
||||||
|
|
||||||
@ -110,7 +111,8 @@ public class DownloadFragment extends Fragment {
|
|||||||
final String file = String.valueOf(fileDesc.getTag(R.id.ID_FILE_NAME));
|
final String file = String.valueOf(fileDesc.getTag(R.id.ID_FILE_NAME));
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
String message = Transfer.deleteFile(file);
|
Transfer tr = new Transfer();
|
||||||
|
String message = tr.deleteFile(file);
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
Transfer.activity.runOnUiThread(() ->
|
Transfer.activity.runOnUiThread(() ->
|
||||||
Snackbar.make(getActivity().findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
Snackbar.make(getActivity().findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
||||||
@ -121,7 +123,7 @@ public class DownloadFragment extends Fragment {
|
|||||||
String ExceptionMess = e.getMessage();
|
String ExceptionMess = e.getMessage();
|
||||||
|
|
||||||
if(ExceptionName != null && ExceptionMess != null) {
|
if(ExceptionName != null && ExceptionMess != null) {
|
||||||
Transfer.error(ExceptionMess);
|
Transfer.error(ExceptionName + ": " + ExceptionMess, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -141,24 +143,18 @@ public class DownloadFragment extends Fragment {
|
|||||||
final LinearLayout main_layout = root.findViewById(R.id.main_layout);
|
final LinearLayout main_layout = root.findViewById(R.id.main_layout);
|
||||||
final LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
|
final LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
|
||||||
|
|
||||||
|
Transfer tr = new Transfer();
|
||||||
|
|
||||||
String data = "";
|
String data = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
data = Transfer.getFileList();
|
data = tr.getFileList();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
final String ExceptionName = e.getClass().getSimpleName();
|
final String ExceptionName = e.getClass().getSimpleName();
|
||||||
final String ExceptionMess = e.getMessage();
|
final String ExceptionMess = e.getMessage();
|
||||||
|
|
||||||
if(ExceptionName != null && ExceptionMess != null) {
|
if(ExceptionName != null && ExceptionMess != null) {
|
||||||
Transfer.error(ExceptionMess);
|
Transfer.error(ExceptionName + ": " + ExceptionMess, title, main_layout);
|
||||||
Transfer.activity.runOnUiThread(() -> {
|
|
||||||
if(main_layout != null)
|
|
||||||
main_layout.removeAllViews();
|
|
||||||
if(title != null) {
|
|
||||||
main_layout.addView(title);
|
|
||||||
title.setText(ExceptionMess);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -257,7 +253,7 @@ public class DownloadFragment extends Fragment {
|
|||||||
Bshare.setVisibility(LinearLayout.GONE);
|
Bshare.setVisibility(LinearLayout.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Transfer.setButton(name, dl)) {
|
if (Progress.setButton(name, dl)) {
|
||||||
dl.setEnabled(false);
|
dl.setEnabled(false);
|
||||||
final Drawable spinner = Transfer.activity.getDrawable(R.drawable.ic_spinner_rotate);
|
final Drawable spinner = Transfer.activity.getDrawable(R.drawable.ic_spinner_rotate);
|
||||||
int h = spinner.getIntrinsicHeight();
|
int h = spinner.getIntrinsicHeight();
|
||||||
@ -306,16 +302,23 @@ public class DownloadFragment extends Fragment {
|
|||||||
final String href = (String) layout.getTag(R.id.ID_FILE_HREF);
|
final String href = (String) layout.getTag(R.id.ID_FILE_HREF);
|
||||||
|
|
||||||
new File(save_location).mkdirs();
|
new File(save_location).mkdirs();
|
||||||
File file = new File(save_location, name);
|
Uri uri = Uri.fromFile(new File(save_location, name));
|
||||||
|
|
||||||
Transfer tr = new Transfer(Transfer.TYPE_DOWNLOAD);
|
Transfer tr = new Transfer();
|
||||||
tr.file = file;
|
|
||||||
tr.fileName = name;
|
new Thread(() -> {
|
||||||
tr.fileSize = fileSize;
|
try {
|
||||||
tr.fileSizeHuman = Transfer.humanReadableByteCountBin(fileSize);
|
tr.downloadFile(uri, name, fileSize, href, button);
|
||||||
tr.href = href;
|
} catch (IOException e) {
|
||||||
tr.button = button;
|
final String ExceptionName = e.getClass().getSimpleName();
|
||||||
tr.AddTransfer();
|
final String ExceptionMess = e.getMessage();
|
||||||
|
|
||||||
|
if(ExceptionName != null && ExceptionMess != null) {
|
||||||
|
Transfer.error(ExceptionName + ": " + ExceptionMess, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
};
|
};
|
||||||
|
|
||||||
private View.OnClickListener ListenerShare = v -> {
|
private View.OnClickListener ListenerShare = v -> {
|
||||||
|
@ -54,10 +54,10 @@ public class ProgressFragment extends Fragment {
|
|||||||
|
|
||||||
Progress.fragment_on = true;
|
Progress.fragment_on = true;
|
||||||
|
|
||||||
List instances = Transfer.getInstances();
|
List instances = Progress.getInstances();
|
||||||
for (Object obj: instances) {
|
for (Object obj: instances) {
|
||||||
Transfer tr = (Transfer) obj;
|
Progress p = (Progress) obj;
|
||||||
Progress.showProgressFragment(tr);
|
p.showProgress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ buildscript {
|
|||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:4.1.2'
|
classpath 'com.android.tools.build:gradle:4.1.1'
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
@ -17,11 +17,6 @@ allprojects {
|
|||||||
google()
|
google()
|
||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
gradle.projectsEvaluated {
|
|
||||||
tasks.withType(JavaCompile) {
|
|
||||||
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
task clean(type: Delete) {
|
task clean(type: Delete) {
|
||||||
|
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
|||||||
#Thu Feb 18 20:04:28 CET 2021
|
#Mon Jan 11 18:53:24 CET 2021
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
|
||||||
|
Reference in New Issue
Block a user