TransferFile
- unification class TransferFile - MediaStore Android 11 Q download support
This commit is contained in:
parent
3df644e6a8
commit
92e9c35584
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -7,7 +7,6 @@
|
|||||||
<option name="testRunner" value="GRADLE" />
|
<option name="testRunner" value="GRADLE" />
|
||||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
<option name="gradleJvm" value="Android Studio default JDK" />
|
|
||||||
<option name="modules">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
package="com.localtransfer">
|
package="com.localtransfer">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
|
android:maxSdkVersion="28" />
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
@ -12,7 +13,8 @@
|
|||||||
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"
|
||||||
android:networkSecurityConfig="@xml/network_security_config">
|
android:networkSecurityConfig="@xml/network_security_config"
|
||||||
|
android:requestLegacyExternalStorage="true">
|
||||||
<!--android:usesCleartextTraffic="true"-->
|
<!--android:usesCleartextTraffic="true"-->
|
||||||
|
|
||||||
<provider
|
<provider
|
||||||
|
@ -59,7 +59,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
|
||||||
checkAndRequestPermissions();
|
checkAndRequestPermissions();
|
||||||
|
}
|
||||||
|
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.localtransfer;
|
package com.localtransfer;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
@ -63,16 +65,8 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
case REQUEST_DIRECTORY_PICKER:
|
case REQUEST_DIRECTORY_PICKER:
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
String currentPath = data.getExtras().getString("data");
|
String currentPath = data.getExtras().getString("data");
|
||||||
|
if(currentPath.contains(Environment.getExternalStorageDirectory() + "/")) {
|
||||||
Uri uri;
|
String save_location = currentPath.replace(Environment.getExternalStorageDirectory() + "/", "");
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
|
|
||||||
uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
uri = Uri.fromFile(Environment.getExternalStorageDirectory());
|
|
||||||
}
|
|
||||||
if(currentPath.contains(uri + "/")) {
|
|
||||||
String save_location = currentPath.replace(uri + "/", "");
|
|
||||||
Log.d("Path", save_location);
|
Log.d("Path", save_location);
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||||
prefs.edit().putString("local_storage", save_location).apply();
|
prefs.edit().putString("local_storage", save_location).apply();
|
||||||
|
@ -73,7 +73,7 @@ public class Transfer {
|
|||||||
port = valueOf(portValue);
|
port = valueOf(portValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private URL checkRedirection(URL url) throws IOException {
|
private static URL checkRedirection(URL url) throws IOException {
|
||||||
String location = null;
|
String location = null;
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
int responseCode;
|
int responseCode;
|
||||||
@ -114,11 +114,11 @@ public class Transfer {
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleSendFile(Uri uri) {
|
public static void handleSendFile(Uri uri) {
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
uploadFile(uri);
|
Transfer.uploadFile(uri);
|
||||||
} 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();
|
||||||
@ -132,7 +132,7 @@ public class Transfer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleSendText(String sharedText) {
|
public static void handleSendText(String sharedText) {
|
||||||
if (sharedText != null) {
|
if (sharedText != null) {
|
||||||
SimpleDateFormat sdfDate = new SimpleDateFormat("text_yyyyMMdd_HHmmss");
|
SimpleDateFormat sdfDate = new SimpleDateFormat("text_yyyyMMdd_HHmmss");
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
@ -150,7 +150,7 @@ public class Transfer {
|
|||||||
|
|
||||||
Uri uri = FileProvider.getUriForFile(activity, activity.getPackageName(), outputFile);
|
Uri uri = FileProvider.getUriForFile(activity, activity.getPackageName(), outputFile);
|
||||||
|
|
||||||
uploadFile(uri);
|
Transfer.uploadFile(uri);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ public class Transfer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String uploadFile(Uri uri) throws IOException {
|
public static String uploadFile(Uri uri) throws IOException {
|
||||||
String message = null;
|
String message = null;
|
||||||
URL url = null;
|
URL url = null;
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
@ -257,7 +257,7 @@ public class Transfer {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFileList() throws IOException {
|
public static String getFileList() throws IOException {
|
||||||
URL url = null;
|
URL url = null;
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
|
|
||||||
@ -289,7 +289,7 @@ public class Transfer {
|
|||||||
return fileList;
|
return fileList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getThumbnail(String href) {
|
public static InputStream getThumbnail(String href) {
|
||||||
InputStream thumbnail = null;
|
InputStream thumbnail = null;
|
||||||
URL url;
|
URL url;
|
||||||
try {
|
try {
|
||||||
@ -309,14 +309,7 @@ public class Transfer {
|
|||||||
return thumbnail;
|
return thumbnail;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void downloadFile(final Uri uri, String name, long fileSize, String href, Button button) throws IOException {
|
public static void downloadFile(TransferFile file) throws IOException {
|
||||||
|
|
||||||
/*final LinearLayout layout = activity.findViewById(id);
|
|
||||||
final Button button;
|
|
||||||
if(layout != null)
|
|
||||||
button = layout.findViewById(R.id.file_download);
|
|
||||||
else
|
|
||||||
button = null;*/
|
|
||||||
|
|
||||||
URL url = null;
|
URL url = null;
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
@ -325,27 +318,7 @@ public class Transfer {
|
|||||||
int bufferLength;
|
int bufferLength;
|
||||||
long loaded = 0;
|
long loaded = 0;
|
||||||
|
|
||||||
final Drawable image = activity.getDrawable(R.drawable.ic_spinner_rotate);
|
String[] parts = file.href.split("/");
|
||||||
|
|
||||||
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 = "";
|
String path = "";
|
||||||
for(int i=0; i< parts.length - 1; i++) {
|
for(int i=0; i< parts.length - 1; i++) {
|
||||||
path = path + parts[i] + "/";
|
path = path + parts[i] + "/";
|
||||||
@ -365,37 +338,37 @@ public class Transfer {
|
|||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
|
||||||
OutputStream fileOutput = activity.getContentResolver().openOutputStream(uri);
|
OutputStream fileOutput = activity.getContentResolver().openOutputStream(file.uri);
|
||||||
InputStream in = conn.getInputStream();
|
InputStream in = conn.getInputStream();
|
||||||
|
|
||||||
while ((bufferLength = in.read(buffer)) > 0) {
|
while ((bufferLength = in.read(buffer)) > 0) {
|
||||||
fileOutput.write(buffer, 0, bufferLength);
|
fileOutput.write(buffer, 0, bufferLength);
|
||||||
loaded+= (long) bufferLength;
|
loaded+= (long) bufferLength;
|
||||||
p.loaded = loaded;
|
file.progress.loaded = loaded;
|
||||||
p.percent = ((loaded * 100) / fileSize);
|
file.progress.percent = ((loaded * 100) / file.size);
|
||||||
if(Progress.app_started && p.button != null)
|
if(Progress.app_started && file.progress.button != null)
|
||||||
activity.runOnUiThread(() -> p.button.setText(String.format("Download in progress %d%%", p.percent)));
|
activity.runOnUiThread(() -> file.progress.button.setText(String.format("Download in progress %d%%", file.progress.percent)));
|
||||||
}
|
}
|
||||||
fileOutput.close();
|
fileOutput.close();
|
||||||
|
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
|
|
||||||
p.stopProgress();
|
file.progress.stopProgress();
|
||||||
|
|
||||||
if(p.button != null) {
|
if(file.progress.button != null) {
|
||||||
activity.runOnUiThread(() -> {
|
activity.runOnUiThread(() -> {
|
||||||
final LinearLayout layout = (LinearLayout) p.button.getParent();
|
final LinearLayout layout = (LinearLayout) file.progress.button.getParent();
|
||||||
layout.findViewById(R.id.file_view).setVisibility(LinearLayout.VISIBLE);
|
layout.findViewById(R.id.file_view).setVisibility(LinearLayout.VISIBLE);
|
||||||
layout.findViewById(R.id.file_share).setVisibility(LinearLayout.VISIBLE);
|
layout.findViewById(R.id.file_share).setVisibility(LinearLayout.VISIBLE);
|
||||||
p.button.setVisibility(LinearLayout.GONE);
|
file.progress.button.setVisibility(LinearLayout.GONE);
|
||||||
p.button.setEnabled(true);
|
file.progress.button.setEnabled(true);
|
||||||
|
|
||||||
p.button.setText(activity.getString(R.string.file_download));
|
file.progress.button.setText(activity.getString(R.string.file_download));
|
||||||
p.button.setCompoundDrawables(p.button.getCompoundDrawables()[0], null, null, null);
|
file.progress.button.setCompoundDrawables(file.progress.button.getCompoundDrawables()[0], null, null, null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
activity.runOnUiThread(() -> {
|
activity.runOnUiThread(() -> {
|
||||||
String message = "File " + name + " successful download";
|
String message = "File " + file.name + " successful download";
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG)
|
||||||
.setAction("Action", null).show();
|
.setAction("Action", null).show();
|
||||||
@ -409,7 +382,7 @@ public class Transfer {
|
|||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String deleteFile(String file) throws IOException {
|
public static String deleteFile(String file) throws IOException {
|
||||||
|
|
||||||
URL url = null;
|
URL url = null;
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
|
81
app/src/main/java/com/localtransfer/TransferFile.java
Normal file
81
app/src/main/java/com/localtransfer/TransferFile.java
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package com.localtransfer;
|
||||||
|
|
||||||
|
import android.content.ContentUris;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.provider.MediaStore;
|
||||||
|
import android.widget.Button;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TransferFile {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public String href;
|
||||||
|
|
||||||
|
public String mime;
|
||||||
|
|
||||||
|
public String type;
|
||||||
|
|
||||||
|
public long size;
|
||||||
|
|
||||||
|
public String save_location;
|
||||||
|
|
||||||
|
public Uri uri;
|
||||||
|
|
||||||
|
public Button button;
|
||||||
|
|
||||||
|
public Progress progress;
|
||||||
|
|
||||||
|
private static List<TransferFile> instances = new ArrayList<>();
|
||||||
|
|
||||||
|
public TransferFile(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
instances.add(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List getInstances() {
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TransferFile getDownload(int id) {
|
||||||
|
for (Object obj: instances) {
|
||||||
|
TransferFile p = (TransferFile) obj;
|
||||||
|
if (p.id == id) return p;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exist(Context context) {
|
||||||
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
|
||||||
|
Cursor cursor = context.getContentResolver().query(MediaStore.Downloads.EXTERNAL_CONTENT_URI, null, null, null, null);
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
String MediaStore_File_name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DISPLAY_NAME));
|
||||||
|
long MediaStore_File_size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Downloads.SIZE));
|
||||||
|
if(MediaStore_File_name.equals(name) && MediaStore_File_size == size) {
|
||||||
|
int cursor_id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID));
|
||||||
|
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor_id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
File file = new File(save_location, name);
|
||||||
|
new File(save_location).mkdirs();
|
||||||
|
uri = Uri.fromFile(file);
|
||||||
|
if (file.exists() && file.length() == size)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -7,11 +7,7 @@ import android.content.Intent;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
||||||
|
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
@ -27,17 +23,20 @@ import android.widget.ImageView;
|
|||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.localtransfer.Progress;
|
import com.localtransfer.Progress;
|
||||||
import com.localtransfer.R;
|
import com.localtransfer.R;
|
||||||
import com.localtransfer.Transfer;
|
import com.localtransfer.Transfer;
|
||||||
|
import com.localtransfer.TransferFile;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@ -53,6 +52,7 @@ public class DownloadFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private View root;
|
private View root;
|
||||||
|
private LayoutInflater inflater;
|
||||||
|
|
||||||
public static DownloadFragment newInstance() {
|
public static DownloadFragment newInstance() {
|
||||||
DownloadFragment fragment = new DownloadFragment();
|
DownloadFragment fragment = new DownloadFragment();
|
||||||
@ -69,17 +69,20 @@ public class DownloadFragment extends Fragment {
|
|||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
root = inflater.inflate(R.layout.fragment_download, container, false);
|
root = inflater.inflate(R.layout.fragment_download, container, false);
|
||||||
|
|
||||||
|
this.inflater = inflater;
|
||||||
|
|
||||||
new Timer().schedule(new TimerTask() {
|
new Timer().schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
showFileList();
|
listFile();
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
SwipeRefreshLayout refresh = root.findViewById(R.id.refresh);
|
SwipeRefreshLayout refresh = root.findViewById(R.id.refresh);
|
||||||
|
|
||||||
|
|
||||||
refresh.setOnRefreshListener(() -> {
|
refresh.setOnRefreshListener(() -> {
|
||||||
new Thread(() -> showFileList()).start();
|
listFile();
|
||||||
refresh.setRefreshing(false);
|
refresh.setRefreshing(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -111,13 +114,12 @@ 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 {
|
||||||
Transfer tr = new Transfer();
|
String message = Transfer.deleteFile(file);
|
||||||
String message = tr.deleteFile(file);
|
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
Transfer.activity.runOnUiThread(() ->
|
getActivity().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)
|
||||||
.setAction("Action", null).show());
|
.setAction("Action", null).show());
|
||||||
showFileList();
|
listFile();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
String ExceptionName = e.getClass().getSimpleName();
|
String ExceptionName = e.getClass().getSimpleName();
|
||||||
String ExceptionMess = e.getMessage();
|
String ExceptionMess = e.getMessage();
|
||||||
@ -135,20 +137,49 @@ public class DownloadFragment extends Fragment {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showFileList() {
|
public void listFile() {
|
||||||
|
new Thread(() -> {
|
||||||
final TextView title = new TextView(Transfer.activity);
|
final TextView title = new TextView(Transfer.activity);
|
||||||
|
final LinearLayout main_layout = root.findViewById(R.id.main_layout);
|
||||||
|
try {
|
||||||
|
String data = Transfer.getFileList();
|
||||||
|
|
||||||
|
if (data.equals("[]")) {
|
||||||
title.setTextSize(18);
|
title.setTextSize(18);
|
||||||
title.setGravity(Gravity.CENTER);
|
title.setGravity(Gravity.CENTER);
|
||||||
final LinearLayout main_layout = root.findViewById(R.id.main_layout);
|
main_layout.removeAllViews();
|
||||||
final LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
|
main_layout.addView(title);
|
||||||
|
title.setHeight(1000);
|
||||||
|
title.setText("No file to download");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
Transfer tr = new Transfer();
|
getActivity().runOnUiThread(() -> {
|
||||||
|
main_layout.removeAllViews();
|
||||||
String data = "";
|
final View line = inflater.inflate(R.layout.horizontal_line, null);
|
||||||
|
main_layout.addView(line);
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
data = tr.getFileList();
|
JSONArray array = new JSONArray(data);
|
||||||
|
for (int i = 0; i < array.length(); i++) {
|
||||||
|
Integer id = View.generateViewId();
|
||||||
|
TransferFile trFile = new TransferFile(id);
|
||||||
|
|
||||||
|
JSONObject row = array.getJSONObject(i);
|
||||||
|
trFile.name = row.getString("name");
|
||||||
|
trFile.size = row.getLong("size");
|
||||||
|
trFile.href = row.getString("href");
|
||||||
|
trFile.mime = row.getString("mime");
|
||||||
|
trFile.type = row.getString("type");
|
||||||
|
|
||||||
|
getActivity().runOnUiThread(() -> showFileList(trFile));
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
} 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();
|
||||||
@ -158,36 +189,23 @@ public class DownloadFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (data.equals("[]")) {
|
}).start();
|
||||||
Transfer.activity.runOnUiThread(() -> {
|
|
||||||
main_layout.removeAllViews();
|
|
||||||
main_layout.addView(title);
|
|
||||||
title.setHeight(1000);
|
|
||||||
title.setText("No file to download");
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
private void setThumbnail(String href, ImageView image) {
|
||||||
|
new Thread(() -> {
|
||||||
JSONArray array;
|
InputStream thumbnail = Transfer.getThumbnail(href);
|
||||||
array = new JSONArray(data);
|
Bitmap bitmap = BitmapFactory.decodeStream(thumbnail);
|
||||||
|
getActivity().runOnUiThread(() -> {
|
||||||
Transfer.activity.runOnUiThread(() -> {
|
image.setImageBitmap(bitmap);
|
||||||
main_layout.removeAllViews();
|
|
||||||
final View line = inflater.inflate(R.layout.horizontal_line, null);
|
|
||||||
main_layout.addView(line);
|
|
||||||
});
|
});
|
||||||
for (int i = 0; i < array.length(); i++) {
|
}).start();
|
||||||
JSONObject row = array.getJSONObject(i);
|
}
|
||||||
final String name = row.getString("name");
|
|
||||||
final String href = row.getString("href");
|
private void showFileList(TransferFile trFile) {
|
||||||
final long size = row.getLong("size");
|
|
||||||
final String mime = row.getString("mime");
|
final LinearLayout main_layout = root.findViewById(R.id.main_layout);
|
||||||
final String type = row.getString("type");
|
|
||||||
|
|
||||||
Transfer.activity.runOnUiThread(() -> {
|
|
||||||
title.setText("Choose file to download");
|
|
||||||
View file_info = inflater.inflate(R.layout.file_info, null);
|
View file_info = inflater.inflate(R.layout.file_info, null);
|
||||||
main_layout.addView(file_info);
|
main_layout.addView(file_info);
|
||||||
ImageView image = file_info.findViewById(R.id.file_image);
|
ImageView image = file_info.findViewById(R.id.file_image);
|
||||||
@ -198,27 +216,33 @@ public class DownloadFragment extends Fragment {
|
|||||||
LinearLayout file_buttons = file_info.findViewById(R.id.file_buttons);
|
LinearLayout file_buttons = file_info.findViewById(R.id.file_buttons);
|
||||||
|
|
||||||
fileDesc.setId(View.generateViewId());
|
fileDesc.setId(View.generateViewId());
|
||||||
file_buttons.setId(View.generateViewId());
|
|
||||||
|
file_buttons.setId(trFile.getId());
|
||||||
|
file_buttons.setTag(R.id.ID_DOWNLOAD, trFile.getId());
|
||||||
file_buttons.setTag(R.id.ID_FILE_BUTTONS, "FOR VISIBILITY");
|
file_buttons.setTag(R.id.ID_FILE_BUTTONS, "FOR VISIBILITY");
|
||||||
|
|
||||||
|
trFile.button = file_buttons.findViewById(R.id.file_download);
|
||||||
|
|
||||||
|
if (trFile.mime.equals("text/plain"))
|
||||||
|
trFile.save_location = getContext().getCacheDir().toString();
|
||||||
|
else
|
||||||
|
trFile.save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.local_storage;
|
||||||
|
|
||||||
registerForContextMenu(fileDesc);
|
registerForContextMenu(fileDesc);
|
||||||
fileDesc.setTag(R.id.ID_FILE_NAME, name);
|
fileDesc.setTag(R.id.ID_FILE_NAME, trFile.name);
|
||||||
|
|
||||||
viewName.setText(name);
|
viewName.setText(trFile.name);
|
||||||
viewType.setText(mime);
|
viewType.setText(trFile.mime);
|
||||||
viewSize.setText(Transfer.humanReadableByteCountBin(size));
|
viewSize.setText(Transfer.humanReadableByteCountBin(trFile.size));
|
||||||
|
|
||||||
switch (type) {
|
switch (trFile.type) {
|
||||||
case "file-image":
|
case "file-image":
|
||||||
|
image.setImageResource(R.drawable.ic_icon_image);
|
||||||
|
setThumbnail(trFile.href, image);
|
||||||
|
break;
|
||||||
case "file-video":
|
case "file-video":
|
||||||
//image.setImageResource(R.drawable.ic_icon_image);
|
image.setImageResource(R.drawable.ic_icon_video);
|
||||||
new Thread(() -> {
|
setThumbnail(trFile.href, image);
|
||||||
InputStream thumbnail = tr.getThumbnail(href);
|
|
||||||
Bitmap bitmap = BitmapFactory.decodeStream(thumbnail);
|
|
||||||
getActivity().runOnUiThread(() -> {
|
|
||||||
image.setImageBitmap(bitmap);
|
|
||||||
});
|
|
||||||
}).start();
|
|
||||||
break;
|
break;
|
||||||
case "file-audio":
|
case "file-audio":
|
||||||
image.setImageResource(R.drawable.ic_icon_music);
|
image.setImageResource(R.drawable.ic_icon_music);
|
||||||
@ -228,54 +252,33 @@ public class DownloadFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fileDesc.setOnClickListener(view -> {
|
fileDesc.setOnClickListener(view -> {
|
||||||
String save_location;
|
|
||||||
|
|
||||||
if (mime.equals("text/plain"))
|
|
||||||
save_location = getContext().getCacheDir().toString();
|
|
||||||
else
|
|
||||||
save_location = Environment.getExternalStorageDirectory() + "/" + Transfer.local_storage;
|
|
||||||
|
|
||||||
File file = new File(save_location, name);
|
|
||||||
ContentValues values = new ContentValues();
|
|
||||||
values.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
|
|
||||||
values.put(MediaStore.MediaColumns.MIME_TYPE, mime);
|
|
||||||
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
|
|
||||||
|
|
||||||
Uri uri;
|
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
|
|
||||||
uri = getContext().getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
uri = null;
|
|
||||||
|
|
||||||
file_buttons.setId(View.generateViewId());
|
|
||||||
|
|
||||||
Button Bview = file_buttons.findViewById(R.id.file_view);
|
Button Bview = file_buttons.findViewById(R.id.file_view);
|
||||||
Button Bshare = file_buttons.findViewById(R.id.file_share);
|
Button Bshare = file_buttons.findViewById(R.id.file_share);
|
||||||
Button dl = file_buttons.findViewById(R.id.file_download);
|
Button Bdownload = file_buttons.findViewById(R.id.file_download);
|
||||||
|
|
||||||
dl.setOnClickListener(ListenerDL);
|
Bdownload.setOnClickListener(ListenerDL);
|
||||||
Bview.setOnClickListener(ListenerView);
|
Bview.setOnClickListener(ListenerView);
|
||||||
Bshare.setOnClickListener(ListenerShare);
|
Bshare.setOnClickListener(ListenerShare);
|
||||||
|
|
||||||
if (file.exists() && file.length() == size) {
|
if (trFile.exist(getContext())) {
|
||||||
Bview.setVisibility(LinearLayout.VISIBLE);
|
Bview.setVisibility(LinearLayout.VISIBLE);
|
||||||
Bshare.setVisibility(LinearLayout.VISIBLE);
|
Bshare.setVisibility(LinearLayout.VISIBLE);
|
||||||
dl.setVisibility(LinearLayout.GONE);
|
Bdownload.setVisibility(LinearLayout.GONE);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
dl.setVisibility(LinearLayout.VISIBLE);
|
Bdownload.setVisibility(LinearLayout.VISIBLE);
|
||||||
Bview.setVisibility(LinearLayout.GONE);
|
Bview.setVisibility(LinearLayout.GONE);
|
||||||
Bshare.setVisibility(LinearLayout.GONE);
|
Bshare.setVisibility(LinearLayout.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Progress.setButton(name, dl)) {
|
if (Progress.setButton(trFile.name, Bdownload)) {
|
||||||
dl.setEnabled(false);
|
Bdownload.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();
|
||||||
int w = spinner.getIntrinsicWidth();
|
int w = spinner.getIntrinsicWidth();
|
||||||
spinner.setBounds(0, 0, w, h);
|
spinner.setBounds(0, 0, w, h);
|
||||||
dl.setCompoundDrawables(dl.getCompoundDrawables()[0], null, spinner, null);
|
Bdownload.setCompoundDrawables(Bdownload.getCompoundDrawables()[0], null, spinner, null);
|
||||||
ObjectAnimator anim = ObjectAnimator.ofInt(spinner, "level", 0, 10000);
|
ObjectAnimator anim = ObjectAnimator.ofInt(spinner, "level", 0, 10000);
|
||||||
anim.setDuration(1000);
|
anim.setDuration(1000);
|
||||||
anim.setRepeatCount(Animation.INFINITE);
|
anim.setRepeatCount(Animation.INFINITE);
|
||||||
@ -283,12 +286,6 @@ public class DownloadFragment extends Fragment {
|
|||||||
anim.start();
|
anim.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
file_buttons.setTag(R.id.ID_FILE_NAME, name);
|
|
||||||
file_buttons.setTag(R.id.ID_FILE_SIZE, size);
|
|
||||||
file_buttons.setTag(R.id.ID_FILE_HREF, href);
|
|
||||||
file_buttons.setTag(R.id.ID_FILE_MIME, mime);
|
|
||||||
file_buttons.setTag(R.id.ID_FILE_URI, uri);
|
|
||||||
file_buttons.setTag(R.id.ID_SAVE_LOCATION, save_location);
|
|
||||||
int state = file_buttons.getVisibility();
|
int state = file_buttons.getVisibility();
|
||||||
if(state == LinearLayout.GONE) {
|
if(state == LinearLayout.GONE) {
|
||||||
for(View child : getAllChildren(main_layout)) {
|
for(View child : getAllChildren(main_layout)) {
|
||||||
@ -300,12 +297,6 @@ public class DownloadFragment extends Fragment {
|
|||||||
else if(state == LinearLayout.VISIBLE)
|
else if(state == LinearLayout.VISIBLE)
|
||||||
file_buttons.setVisibility(LinearLayout.GONE);
|
file_buttons.setVisibility(LinearLayout.GONE);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private View.OnClickListener ListenerDL = v -> {
|
private View.OnClickListener ListenerDL = v -> {
|
||||||
@ -313,18 +304,35 @@ public class DownloadFragment extends Fragment {
|
|||||||
final LinearLayout layout = (LinearLayout) v.getParent();
|
final LinearLayout layout = (LinearLayout) v.getParent();
|
||||||
final Button button = layout.findViewById(R.id.file_download);
|
final Button button = layout.findViewById(R.id.file_download);
|
||||||
|
|
||||||
final String name = (String) layout.getTag(R.id.ID_FILE_NAME);
|
final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD);
|
||||||
final String save_location = String.valueOf(layout.getTag(R.id.ID_SAVE_LOCATION));
|
|
||||||
final long fileSize = (long) layout.getTag(R.id.ID_FILE_SIZE);
|
|
||||||
final String href = (String) layout.getTag(R.id.ID_FILE_HREF);
|
|
||||||
final String mime = (String) layout.getTag(R.id.ID_FILE_MIME);
|
|
||||||
final Uri uri = (Uri) layout.getTag(R.id.ID_FILE_URI);
|
|
||||||
|
|
||||||
Transfer tr = new Transfer();
|
final Drawable image = getActivity().getDrawable(R.drawable.ic_spinner_rotate);
|
||||||
|
int h = image.getIntrinsicHeight();
|
||||||
|
int w = image.getIntrinsicWidth();
|
||||||
|
image.setBounds(0, 0, w, h);
|
||||||
|
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();
|
||||||
|
|
||||||
|
TransferFile file = TransferFile.getDownload(id);
|
||||||
|
|
||||||
|
file.progress = new Progress(file.name, file.size, Progress.DOWNLOAD);
|
||||||
|
file.progress.button = file.button;
|
||||||
|
|
||||||
|
if (file.uri == null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(MediaStore.MediaColumns.DISPLAY_NAME, file.name);
|
||||||
|
values.put(MediaStore.MediaColumns.MIME_TYPE, file.mime);
|
||||||
|
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Transfer.local_storage);
|
||||||
|
file.uri = getContext().getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
|
||||||
|
}
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
tr.downloadFile(uri, name, fileSize, href, button);
|
Transfer.downloadFile(file);
|
||||||
} 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();
|
||||||
@ -339,14 +347,14 @@ public class DownloadFragment extends Fragment {
|
|||||||
|
|
||||||
private View.OnClickListener ListenerShare = v -> {
|
private View.OnClickListener ListenerShare = v -> {
|
||||||
LinearLayout layout = (LinearLayout) v.getParent();
|
LinearLayout layout = (LinearLayout) v.getParent();
|
||||||
final String name = (String) layout.getTag(R.id.ID_FILE_NAME);
|
final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD);
|
||||||
final String type = (String) layout.getTag(R.id.ID_FILE_MIME);
|
|
||||||
final Uri uri = (Uri) layout.getTag(R.id.ID_FILE_URI);
|
|
||||||
|
|
||||||
if(type.equals("text/plain")) {
|
TransferFile dl = TransferFile.getDownload(id);
|
||||||
|
|
||||||
|
if(dl.type.equals("text/plain")) {
|
||||||
String text = null;
|
String text = null;
|
||||||
try {
|
try {
|
||||||
InputStream is = getActivity().getContentResolver().openInputStream(uri);
|
InputStream is = getActivity().getContentResolver().openInputStream(dl.uri);
|
||||||
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
|
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
@ -364,7 +372,7 @@ public class DownloadFragment extends Fragment {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||||
intent.setType(type);
|
intent.setType(dl.type);
|
||||||
intent.putExtra(Intent.EXTRA_TEXT, text);
|
intent.putExtra(Intent.EXTRA_TEXT, text);
|
||||||
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
|
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
@ -374,8 +382,8 @@ public class DownloadFragment extends Fragment {
|
|||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||||
intent.putExtra(Intent.EXTRA_STREAM, uri);
|
intent.putExtra(Intent.EXTRA_STREAM, dl.uri);
|
||||||
intent.setType(type);
|
intent.setType(dl.mime);
|
||||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
|
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
@ -388,13 +396,13 @@ public class DownloadFragment extends Fragment {
|
|||||||
|
|
||||||
private View.OnClickListener ListenerView = v -> {
|
private View.OnClickListener ListenerView = v -> {
|
||||||
LinearLayout layout = (LinearLayout) v.getParent();
|
LinearLayout layout = (LinearLayout) v.getParent();
|
||||||
final String name = (String) layout.getTag(R.id.ID_FILE_NAME);
|
final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD);
|
||||||
final String type = (String) layout.getTag(R.id.ID_FILE_MIME);
|
|
||||||
final Uri uri = (Uri) layout.getTag(R.id.ID_FILE_URI);
|
TransferFile dl = TransferFile.getDownload(id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||||
intent.setDataAndType(uri, type);
|
intent.setDataAndType(dl.uri, dl.mime);
|
||||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
|
<item name="ID_DOWNLOAD" type="id" />
|
||||||
<item name="ID_FILE_NAME" type="id" />
|
<item name="ID_FILE_NAME" type="id" />
|
||||||
<item name="ID_FILE_SIZE" type="id" />
|
|
||||||
<item name="ID_FILE_HREF" type="id" />
|
|
||||||
<item name="ID_FILE_MIME" type="id" />
|
|
||||||
<item name="ID_FILE_URI" type="id" />
|
|
||||||
<item name="ID_SAVE_LOCATION" type="id" />
|
|
||||||
<item name="ID_FILE_BUTTONS" type="id" />
|
<item name="ID_FILE_BUTTONS" type="id" />
|
||||||
</resources>
|
</resources>
|
Loading…
x
Reference in New Issue
Block a user