diff --git a/.idea/gradle.xml b/.idea/gradle.xml index bb80ebf..526b4c2 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -7,7 +7,6 @@ - diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a57f5bc..bfa73f6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,7 +3,8 @@ package="com.localtransfer"> - + + android:networkSecurityConfig="@xml/network_security_config" + android:requestLegacyExternalStorage="true"> = 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 + "/", ""); + if(currentPath.contains(Environment.getExternalStorageDirectory() + "/")) { + String save_location = currentPath.replace(Environment.getExternalStorageDirectory() + "/", ""); Log.d("Path", save_location); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); prefs.edit().putString("local_storage", save_location).apply(); diff --git a/app/src/main/java/com/localtransfer/Transfer.java b/app/src/main/java/com/localtransfer/Transfer.java index ca08bdc..eaa7f7b 100644 --- a/app/src/main/java/com/localtransfer/Transfer.java +++ b/app/src/main/java/com/localtransfer/Transfer.java @@ -73,7 +73,7 @@ public class Transfer { port = valueOf(portValue); } - private URL checkRedirection(URL url) throws IOException { + private static URL checkRedirection(URL url) throws IOException { String location = null; HttpURLConnection conn = null; int responseCode; @@ -114,11 +114,11 @@ public class Transfer { return url; } - public void handleSendFile(Uri uri) { + public static void handleSendFile(Uri uri) { if (uri != null) { new Thread(() -> { try { - uploadFile(uri); + Transfer.uploadFile(uri); } catch (IOException e) { final String ExceptionName = e.getClass().getSimpleName(); 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) { SimpleDateFormat sdfDate = new SimpleDateFormat("text_yyyyMMdd_HHmmss"); Date now = new Date(); @@ -150,7 +150,7 @@ public class Transfer { Uri uri = FileProvider.getUriForFile(activity, activity.getPackageName(), outputFile); - uploadFile(uri); + Transfer.uploadFile(uri); } catch (IOException e) { 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; URL url = null; HttpURLConnection conn = null; @@ -257,7 +257,7 @@ public class Transfer { return message; } - public String getFileList() throws IOException { + public static String getFileList() throws IOException { URL url = null; HttpURLConnection conn = null; @@ -289,7 +289,7 @@ public class Transfer { return fileList; } - public InputStream getThumbnail(String href) { + public static InputStream getThumbnail(String href) { InputStream thumbnail = null; URL url; try { @@ -309,14 +309,7 @@ public class Transfer { return thumbnail; } - 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;*/ + public static void downloadFile(TransferFile file) throws IOException { URL url = null; HttpURLConnection conn = null; @@ -325,27 +318,7 @@ public class Transfer { 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[] parts = file.href.split("/"); String path = ""; for(int i=0; i< parts.length - 1; i++) { path = path + parts[i] + "/"; @@ -365,37 +338,37 @@ public class Transfer { if (responseCode == HttpURLConnection.HTTP_OK) { - OutputStream fileOutput = activity.getContentResolver().openOutputStream(uri); + OutputStream fileOutput = activity.getContentResolver().openOutputStream(file.uri); 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))); + file.progress.loaded = loaded; + file.progress.percent = ((loaded * 100) / file.size); + if(Progress.app_started && file.progress.button != null) + activity.runOnUiThread(() -> file.progress.button.setText(String.format("Download in progress %d%%", file.progress.percent))); } fileOutput.close(); conn.disconnect(); - p.stopProgress(); + file.progress.stopProgress(); - if(p.button != null) { + if(file.progress.button != null) { 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_share).setVisibility(LinearLayout.VISIBLE); - p.button.setVisibility(LinearLayout.GONE); - p.button.setEnabled(true); + file.progress.button.setVisibility(LinearLayout.GONE); + file.progress.button.setEnabled(true); - p.button.setText(activity.getString(R.string.file_download)); - p.button.setCompoundDrawables(p.button.getCompoundDrawables()[0], null, null, null); + file.progress.button.setText(activity.getString(R.string.file_download)); + file.progress.button.setCompoundDrawables(file.progress.button.getCompoundDrawables()[0], null, null, null); }); } activity.runOnUiThread(() -> { - String message = "File " + name + " successful download"; + String message = "File " + file.name + " successful download"; System.out.println(message); Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG) .setAction("Action", null).show(); @@ -409,7 +382,7 @@ public class Transfer { conn.disconnect(); } - public String deleteFile(String file) throws IOException { + public static String deleteFile(String file) throws IOException { URL url = null; HttpURLConnection conn = null; diff --git a/app/src/main/java/com/localtransfer/TransferFile.java b/app/src/main/java/com/localtransfer/TransferFile.java new file mode 100644 index 0000000..d72c635 --- /dev/null +++ b/app/src/main/java/com/localtransfer/TransferFile.java @@ -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 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; + } +} diff --git a/app/src/main/java/com/localtransfer/fragment/DownloadFragment.java b/app/src/main/java/com/localtransfer/fragment/DownloadFragment.java index 4681a0e..98a19b5 100644 --- a/app/src/main/java/com/localtransfer/fragment/DownloadFragment.java +++ b/app/src/main/java/com/localtransfer/fragment/DownloadFragment.java @@ -7,11 +7,7 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; -import android.net.Uri; import android.os.Bundle; -import androidx.fragment.app.Fragment; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; - import android.os.Environment; import android.provider.MediaStore; import android.view.ContextMenu; @@ -27,17 +23,20 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.fragment.app.Fragment; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + import com.google.android.material.snackbar.Snackbar; import com.localtransfer.Progress; import com.localtransfer.R; import com.localtransfer.Transfer; +import com.localtransfer.TransferFile; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -53,6 +52,7 @@ public class DownloadFragment extends Fragment { } private View root; + private LayoutInflater inflater; public static DownloadFragment newInstance() { DownloadFragment fragment = new DownloadFragment(); @@ -69,17 +69,20 @@ public class DownloadFragment extends Fragment { Bundle savedInstanceState) { root = inflater.inflate(R.layout.fragment_download, container, false); + this.inflater = inflater; + new Timer().schedule(new TimerTask() { @Override public void run() { - showFileList(); + listFile(); } }, 100); SwipeRefreshLayout refresh = root.findViewById(R.id.refresh); + refresh.setOnRefreshListener(() -> { - new Thread(() -> showFileList()).start(); + listFile(); refresh.setRefreshing(false); }); @@ -111,13 +114,12 @@ public class DownloadFragment extends Fragment { final String file = String.valueOf(fileDesc.getTag(R.id.ID_FILE_NAME)); new Thread(() -> { try { - Transfer tr = new Transfer(); - String message = tr.deleteFile(file); + String message = Transfer.deleteFile(file); System.out.println(message); - Transfer.activity.runOnUiThread(() -> + getActivity().runOnUiThread(() -> Snackbar.make(getActivity().findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG) .setAction("Action", null).show()); - showFileList(); + listFile(); } catch (IOException e) { String ExceptionName = e.getClass().getSimpleName(); String ExceptionMess = e.getMessage(); @@ -135,177 +137,166 @@ public class DownloadFragment extends Fragment { return true; } - public void showFileList() { + public void listFile() { + new Thread(() -> { + final TextView title = new TextView(Transfer.activity); + final LinearLayout main_layout = root.findViewById(R.id.main_layout); + try { + String data = Transfer.getFileList(); - final TextView title = new TextView(Transfer.activity); - title.setTextSize(18); - title.setGravity(Gravity.CENTER); - final LinearLayout main_layout = root.findViewById(R.id.main_layout); - final LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(getContext().LAYOUT_INFLATER_SERVICE); + if (data.equals("[]")) { + title.setTextSize(18); + title.setGravity(Gravity.CENTER); + main_layout.removeAllViews(); + main_layout.addView(title); + title.setHeight(1000); + title.setText("No file to download"); + } + else { - Transfer tr = new Transfer(); - - String data = ""; - - try { - data = tr.getFileList(); - } catch (IOException e) { - final String ExceptionName = e.getClass().getSimpleName(); - final String ExceptionMess = e.getMessage(); - - if(ExceptionName != null && ExceptionMess != null) { - Transfer.error(ExceptionName + ": " + ExceptionMess, title, main_layout); - } - - } - if (data.equals("[]")) { - Transfer.activity.runOnUiThread(() -> { - main_layout.removeAllViews(); - main_layout.addView(title); - title.setHeight(1000); - title.setText("No file to download"); - }); - return; - } - - try { - - JSONArray array; - array = new JSONArray(data); - - Transfer.activity.runOnUiThread(() -> { - 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++) { - JSONObject row = array.getJSONObject(i); - final String name = row.getString("name"); - final String href = row.getString("href"); - final long size = row.getLong("size"); - final String mime = row.getString("mime"); - 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); - main_layout.addView(file_info); - ImageView image = file_info.findViewById(R.id.file_image); - TextView viewName = file_info.findViewById(R.id.file_name); - TextView viewType = file_info.findViewById(R.id.file_type); - TextView viewSize = file_info.findViewById(R.id.file_size); - LinearLayout fileDesc = file_info.findViewById(R.id.file_desc); - LinearLayout file_buttons = file_info.findViewById(R.id.file_buttons); - - fileDesc.setId(View.generateViewId()); - file_buttons.setId(View.generateViewId()); - file_buttons.setTag(R.id.ID_FILE_BUTTONS, "FOR VISIBILITY"); - - registerForContextMenu(fileDesc); - fileDesc.setTag(R.id.ID_FILE_NAME, name); - - viewName.setText(name); - viewType.setText(mime); - viewSize.setText(Transfer.humanReadableByteCountBin(size)); - - switch (type) { - case "file-image": - case "file-video": - //image.setImageResource(R.drawable.ic_icon_image); - new Thread(() -> { - InputStream thumbnail = tr.getThumbnail(href); - Bitmap bitmap = BitmapFactory.decodeStream(thumbnail); - getActivity().runOnUiThread(() -> { - image.setImageBitmap(bitmap); - }); - }).start(); - break; - case "file-audio": - image.setImageResource(R.drawable.ic_icon_music); - break; - default: - image.setImageResource(R.drawable.ic_icon_file); - } - - 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 Bshare = file_buttons.findViewById(R.id.file_share); - Button dl = file_buttons.findViewById(R.id.file_download); - - dl.setOnClickListener(ListenerDL); - Bview.setOnClickListener(ListenerView); - Bshare.setOnClickListener(ListenerShare); - - if (file.exists() && file.length() == size) { - Bview.setVisibility(LinearLayout.VISIBLE); - Bshare.setVisibility(LinearLayout.VISIBLE); - dl.setVisibility(LinearLayout.GONE); - } - else { - dl.setVisibility(LinearLayout.VISIBLE); - Bview.setVisibility(LinearLayout.GONE); - Bshare.setVisibility(LinearLayout.GONE); - } - - if (Progress.setButton(name, dl)) { - dl.setEnabled(false); - final Drawable spinner = Transfer.activity.getDrawable(R.drawable.ic_spinner_rotate); - int h = spinner.getIntrinsicHeight(); - int w = spinner.getIntrinsicWidth(); - spinner.setBounds(0, 0, w, h); - dl.setCompoundDrawables(dl.getCompoundDrawables()[0], null, spinner, null); - ObjectAnimator anim = ObjectAnimator.ofInt(spinner, "level", 0, 10000); - anim.setDuration(1000); - anim.setRepeatCount(Animation.INFINITE); - anim.setInterpolator(new LinearInterpolator()); - 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(); - if(state == LinearLayout.GONE) { - for(View child : getAllChildren(main_layout)) { - if(child.getTag(R.id.ID_FILE_BUTTONS) != null) - child.setVisibility(LinearLayout.GONE); - } - file_buttons.setVisibility(LinearLayout.VISIBLE); - } - else if(state == LinearLayout.VISIBLE) - file_buttons.setVisibility(LinearLayout.GONE); + getActivity().runOnUiThread(() -> { + main_layout.removeAllViews(); + final View line = inflater.inflate(R.layout.horizontal_line, null); + main_layout.addView(line); }); - }); + try { + 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) { + final String ExceptionName = e.getClass().getSimpleName(); + final String ExceptionMess = e.getMessage(); + + if(ExceptionName != null && ExceptionMess != null) { + Transfer.error(ExceptionName + ": " + ExceptionMess, title, main_layout); + } + } - } catch (JSONException e) { - e.printStackTrace(); + }).start(); + } + + private void setThumbnail(String href, ImageView image) { + new Thread(() -> { + InputStream thumbnail = Transfer.getThumbnail(href); + Bitmap bitmap = BitmapFactory.decodeStream(thumbnail); + getActivity().runOnUiThread(() -> { + image.setImageBitmap(bitmap); + }); + }).start(); + } + + private void showFileList(TransferFile trFile) { + + final LinearLayout main_layout = root.findViewById(R.id.main_layout); + + View file_info = inflater.inflate(R.layout.file_info, null); + main_layout.addView(file_info); + ImageView image = file_info.findViewById(R.id.file_image); + TextView viewName = file_info.findViewById(R.id.file_name); + TextView viewType = file_info.findViewById(R.id.file_type); + TextView viewSize = file_info.findViewById(R.id.file_size); + LinearLayout fileDesc = file_info.findViewById(R.id.file_desc); + LinearLayout file_buttons = file_info.findViewById(R.id.file_buttons); + + fileDesc.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"); + + 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); + fileDesc.setTag(R.id.ID_FILE_NAME, trFile.name); + + viewName.setText(trFile.name); + viewType.setText(trFile.mime); + viewSize.setText(Transfer.humanReadableByteCountBin(trFile.size)); + + switch (trFile.type) { + case "file-image": + image.setImageResource(R.drawable.ic_icon_image); + setThumbnail(trFile.href, image); + break; + case "file-video": + image.setImageResource(R.drawable.ic_icon_video); + setThumbnail(trFile.href, image); + break; + case "file-audio": + image.setImageResource(R.drawable.ic_icon_music); + break; + default: + image.setImageResource(R.drawable.ic_icon_file); } + + fileDesc.setOnClickListener(view -> { + + Button Bview = file_buttons.findViewById(R.id.file_view); + Button Bshare = file_buttons.findViewById(R.id.file_share); + Button Bdownload = file_buttons.findViewById(R.id.file_download); + + Bdownload.setOnClickListener(ListenerDL); + Bview.setOnClickListener(ListenerView); + Bshare.setOnClickListener(ListenerShare); + + if (trFile.exist(getContext())) { + Bview.setVisibility(LinearLayout.VISIBLE); + Bshare.setVisibility(LinearLayout.VISIBLE); + Bdownload.setVisibility(LinearLayout.GONE); + } + else { + Bdownload.setVisibility(LinearLayout.VISIBLE); + Bview.setVisibility(LinearLayout.GONE); + Bshare.setVisibility(LinearLayout.GONE); + } + + if (Progress.setButton(trFile.name, Bdownload)) { + Bdownload.setEnabled(false); + final Drawable spinner = Transfer.activity.getDrawable(R.drawable.ic_spinner_rotate); + int h = spinner.getIntrinsicHeight(); + int w = spinner.getIntrinsicWidth(); + spinner.setBounds(0, 0, w, h); + Bdownload.setCompoundDrawables(Bdownload.getCompoundDrawables()[0], null, spinner, null); + ObjectAnimator anim = ObjectAnimator.ofInt(spinner, "level", 0, 10000); + anim.setDuration(1000); + anim.setRepeatCount(Animation.INFINITE); + anim.setInterpolator(new LinearInterpolator()); + anim.start(); + } + + int state = file_buttons.getVisibility(); + if(state == LinearLayout.GONE) { + for(View child : getAllChildren(main_layout)) { + if(child.getTag(R.id.ID_FILE_BUTTONS) != null) + child.setVisibility(LinearLayout.GONE); + } + file_buttons.setVisibility(LinearLayout.VISIBLE); + } + else if(state == LinearLayout.VISIBLE) + file_buttons.setVisibility(LinearLayout.GONE); + }); } private View.OnClickListener ListenerDL = v -> { @@ -313,18 +304,35 @@ public class DownloadFragment extends Fragment { final LinearLayout layout = (LinearLayout) v.getParent(); final Button button = layout.findViewById(R.id.file_download); - final String name = (String) layout.getTag(R.id.ID_FILE_NAME); - 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); + final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD); - 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(() -> { try { - tr.downloadFile(uri, name, fileSize, href, button); + Transfer.downloadFile(file); } catch (IOException e) { final String ExceptionName = e.getClass().getSimpleName(); final String ExceptionMess = e.getMessage(); @@ -339,14 +347,14 @@ public class DownloadFragment extends Fragment { private View.OnClickListener ListenerShare = v -> { LinearLayout layout = (LinearLayout) v.getParent(); - final String name = (String) layout.getTag(R.id.ID_FILE_NAME); - final String type = (String) layout.getTag(R.id.ID_FILE_MIME); - final Uri uri = (Uri) layout.getTag(R.id.ID_FILE_URI); + final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD); - if(type.equals("text/plain")) { + TransferFile dl = TransferFile.getDownload(id); + + if(dl.type.equals("text/plain")) { String text = null; try { - InputStream is = getActivity().getContentResolver().openInputStream(uri); + InputStream is = getActivity().getContentResolver().openInputStream(dl.uri); BufferedReader buf = new BufferedReader(new InputStreamReader(is)); String line; @@ -364,7 +372,7 @@ public class DownloadFragment extends Fragment { try { Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType(type); + intent.setType(dl.type); intent.putExtra(Intent.EXTRA_TEXT, text); startActivity(Intent.createChooser(intent, getString(R.string.share_title))); } catch (ActivityNotFoundException e) { @@ -374,8 +382,8 @@ public class DownloadFragment extends Fragment { else { try { Intent intent = new Intent(Intent.ACTION_SEND); - intent.putExtra(Intent.EXTRA_STREAM, uri); - intent.setType(type); + intent.putExtra(Intent.EXTRA_STREAM, dl.uri); + intent.setType(dl.mime); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(Intent.createChooser(intent, getString(R.string.share_title))); } catch (ActivityNotFoundException e) { @@ -388,13 +396,13 @@ public class DownloadFragment extends Fragment { private View.OnClickListener ListenerView = v -> { LinearLayout layout = (LinearLayout) v.getParent(); - final String name = (String) layout.getTag(R.id.ID_FILE_NAME); - final String type = (String) layout.getTag(R.id.ID_FILE_MIME); - final Uri uri = (Uri) layout.getTag(R.id.ID_FILE_URI); + final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD); + + TransferFile dl = TransferFile.getDownload(id); try { 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); startActivity(intent); } catch (ActivityNotFoundException e) { diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 77b29e4..b9329d0 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -1,10 +1,6 @@ + - - - - - \ No newline at end of file