diff --git a/app/src/main/java/com/localtransfer/ServerFile.java b/app/src/main/java/com/localtransfer/ServerFile.java new file mode 100644 index 0000000..c37a809 --- /dev/null +++ b/app/src/main/java/com/localtransfer/ServerFile.java @@ -0,0 +1,217 @@ +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.util.Log; +import android.widget.Button; +import android.widget.LinearLayout; + +import com.google.android.material.snackbar.Snackbar; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +public class ServerFile extends Transfer { + + 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 ServerFile(Integer id) { + this.id = id; + instances.add(this); + } + + public static List getInstances() { + return instances; + } + + public static ServerFile getFileById(int id) { + for (Object obj: instances) { + ServerFile p = (ServerFile) 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; + } + + public InputStream getThumbnail() { + InputStream thumbnail = null; + URL url; + try { + String query = URLEncoder.encode(href, StandardCharsets.UTF_8.toString()); + String serverUrl = root + "/thumbnail.php?file=" + query; + url = new URL(protocol, host, port, serverUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(2000); + + conn.connect(); + + thumbnail = conn.getInputStream(); + } catch (IOException e) { + e.printStackTrace(); + } + return thumbnail; + } + + public void downloadFile() throws IOException { + + URL url = null; + HttpURLConnection conn = null; + int maxBufferSize = 1 * 1024 * 1024; + byte[] buffer = new byte[maxBufferSize]; + int bufferLength; + long loaded = 0; + + 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) { + + OutputStream fileOutput = activity.getContentResolver().openOutputStream(uri); + InputStream in = conn.getInputStream(); + + while ((bufferLength = in.read(buffer)) > 0) { + fileOutput.write(buffer, 0, bufferLength); + loaded+= (long) bufferLength; + progress.loaded = loaded; + progress.percent = ((loaded * 100) / size); + if(Progress.app_started && progress.button != null) + activity.runOnUiThread(() -> progress.button.setText(String.format("Download in progress %d%%", progress.percent))); + } + fileOutput.close(); + + conn.disconnect(); + + progress.stopProgress(); + + if(progress.button != null) { + activity.runOnUiThread(() -> { + final LinearLayout layout = (LinearLayout) progress.button.getParent(); + layout.findViewById(R.id.file_view).setVisibility(LinearLayout.VISIBLE); + layout.findViewById(R.id.file_share).setVisibility(LinearLayout.VISIBLE); + progress.button.setVisibility(LinearLayout.GONE); + progress.button.setEnabled(true); + + progress.button.setText(activity.getString(R.string.file_download)); + progress.button.setCompoundDrawables(progress.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() throws IOException { + + URL url = null; + HttpURLConnection conn = null; + + String message; + + String query = URLEncoder.encode(name, 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 " + name + " successful deleted"; + } + else { + String s = conn.getResponseMessage(); + throw new HttpErrorException("error code: " + responseCode + " " + s); + } + + conn.disconnect(); + + return message; + } +} diff --git a/app/src/main/java/com/localtransfer/Transfer.java b/app/src/main/java/com/localtransfer/Transfer.java index eaa7f7b..cd39481 100644 --- a/app/src/main/java/com/localtransfer/Transfer.java +++ b/app/src/main/java/com/localtransfer/Transfer.java @@ -9,6 +9,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.provider.OpenableColumns; import android.util.Log; +import android.view.Gravity; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.widget.Button; @@ -289,131 +290,6 @@ public class Transfer { return fileList; } - public static InputStream getThumbnail(String href) { - InputStream thumbnail = null; - URL url; - try { - String query = URLEncoder.encode(href, StandardCharsets.UTF_8.toString()); - String serverUrl = root + "/thumbnail.php?file=" + query; - url = new URL(protocol, host, port, serverUrl); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - conn.setConnectTimeout(2000); - - conn.connect(); - - thumbnail = conn.getInputStream(); - } catch (IOException e) { - e.printStackTrace(); - } - return thumbnail; - } - - public static void downloadFile(TransferFile file) throws IOException { - - URL url = null; - HttpURLConnection conn = null; - int maxBufferSize = 1 * 1024 * 1024; - byte[] buffer = new byte[maxBufferSize]; - int bufferLength; - long loaded = 0; - - String[] parts = file.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) { - - 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; - 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(); - - file.progress.stopProgress(); - - if(file.progress.button != null) { - activity.runOnUiThread(() -> { - 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); - file.progress.button.setVisibility(LinearLayout.GONE); - file.progress.button.setEnabled(true); - - 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 " + 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 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("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) { @@ -422,18 +298,18 @@ public class Transfer { if (timeout == 0) { 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(); + activity.runOnUiThread(() -> { + if(layout != null) + layout.removeAllViews(); + if(title != null) { + title.setTextSize(18); + title.setGravity(Gravity.CENTER); + title.setHeight(1000); + layout.addView(title); + title.setText(message); } + Snackbar.make(activity.findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); }); } timeout = 1; diff --git a/app/src/main/java/com/localtransfer/TransferFile.java b/app/src/main/java/com/localtransfer/TransferFile.java deleted file mode 100644 index d72c635..0000000 --- a/app/src/main/java/com/localtransfer/TransferFile.java +++ /dev/null @@ -1,81 +0,0 @@ -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 98a19b5..e95db89 100644 --- a/app/src/main/java/com/localtransfer/fragment/DownloadFragment.java +++ b/app/src/main/java/com/localtransfer/fragment/DownloadFragment.java @@ -30,7 +30,7 @@ import com.google.android.material.snackbar.Snackbar; import com.localtransfer.Progress; import com.localtransfer.R; import com.localtransfer.Transfer; -import com.localtransfer.TransferFile; +import com.localtransfer.ServerFile; import org.json.JSONArray; import org.json.JSONException; @@ -111,10 +111,10 @@ public class DownloadFragment extends Fragment { public boolean onContextItemSelected(MenuItem item) { if (item.getTitle() == "Delete") { LinearLayout fileDesc = (LinearLayout) root.findViewById(item.getItemId()); - final String file = String.valueOf(fileDesc.getTag(R.id.ID_FILE_NAME)); + ServerFile file = ServerFile.getFileById((Integer) fileDesc.getTag(R.id.ID_DOWNLOAD)); new Thread(() -> { try { - String message = Transfer.deleteFile(file); + String message = file.deleteFile(); System.out.println(message); getActivity().runOnUiThread(() -> Snackbar.make(getActivity().findViewById(R.id.view_pager), message, Snackbar.LENGTH_LONG) @@ -164,7 +164,7 @@ public class DownloadFragment extends Fragment { JSONArray array = new JSONArray(data); for (int i = 0; i < array.length(); i++) { Integer id = View.generateViewId(); - TransferFile trFile = new TransferFile(id); + ServerFile trFile = new ServerFile(id); JSONObject row = array.getJSONObject(i); trFile.name = row.getString("name"); @@ -192,9 +192,9 @@ public class DownloadFragment extends Fragment { }).start(); } - private void setThumbnail(String href, ImageView image) { + private void setThumbnail(ServerFile file, ImageView image) { new Thread(() -> { - InputStream thumbnail = Transfer.getThumbnail(href); + InputStream thumbnail = file.getThumbnail(); Bitmap bitmap = BitmapFactory.decodeStream(thumbnail); getActivity().runOnUiThread(() -> { image.setImageBitmap(bitmap); @@ -202,7 +202,7 @@ public class DownloadFragment extends Fragment { }).start(); } - private void showFileList(TransferFile trFile) { + private void showFileList(ServerFile trFile) { final LinearLayout main_layout = root.findViewById(R.id.main_layout); @@ -217,8 +217,11 @@ public class DownloadFragment extends Fragment { fileDesc.setId(View.generateViewId()); + registerForContextMenu(fileDesc); + file_buttons.setId(trFile.getId()); file_buttons.setTag(R.id.ID_DOWNLOAD, trFile.getId()); + fileDesc.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); @@ -228,9 +231,6 @@ public class DownloadFragment extends Fragment { 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)); @@ -238,11 +238,11 @@ public class DownloadFragment extends Fragment { switch (trFile.type) { case "file-image": image.setImageResource(R.drawable.ic_icon_image); - setThumbnail(trFile.href, image); + setThumbnail(trFile, image); break; case "file-video": image.setImageResource(R.drawable.ic_icon_video); - setThumbnail(trFile.href, image); + setThumbnail(trFile, image); break; case "file-audio": image.setImageResource(R.drawable.ic_icon_music); @@ -317,7 +317,7 @@ public class DownloadFragment extends Fragment { anim.setInterpolator(new LinearInterpolator()); anim.start(); - TransferFile file = TransferFile.getDownload(id); + ServerFile file = ServerFile.getFileById(id); file.progress = new Progress(file.name, file.size, Progress.DOWNLOAD); file.progress.button = file.button; @@ -332,7 +332,7 @@ public class DownloadFragment extends Fragment { new Thread(() -> { try { - Transfer.downloadFile(file); + file.downloadFile(); } catch (IOException e) { final String ExceptionName = e.getClass().getSimpleName(); final String ExceptionMess = e.getMessage(); @@ -349,7 +349,7 @@ public class DownloadFragment extends Fragment { LinearLayout layout = (LinearLayout) v.getParent(); final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD); - TransferFile dl = TransferFile.getDownload(id); + ServerFile dl = ServerFile.getFileById(id); if(dl.type.equals("text/plain")) { String text = null; @@ -398,7 +398,7 @@ public class DownloadFragment extends Fragment { LinearLayout layout = (LinearLayout) v.getParent(); final Integer id = (Integer) layout.getTag(R.id.ID_DOWNLOAD); - TransferFile dl = TransferFile.getDownload(id); + ServerFile dl = ServerFile.getFileById(id); try { Intent intent = new Intent(Intent.ACTION_VIEW); diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index b9329d0..feaf92e 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -1,6 +1,5 @@ - \ No newline at end of file