From f0634c067e084595e4ccba124eb1dbbcd0dc6c36 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sat, 7 Dec 2019 12:30:26 +0100 Subject: [PATCH] Fix issue #371 --- .../activities/SlideMediaActivity.java | 22 +++-- .../android/drawers/StatusListAdapter.java | 7 +- .../app/fedilab/android/helper/Helper.java | 95 ++++++++++++++++++- 3 files changed, 111 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/SlideMediaActivity.java b/app/src/main/java/app/fedilab/android/activities/SlideMediaActivity.java index 26d95ede7..2d8a78d55 100644 --- a/app/src/main/java/app/fedilab/android/activities/SlideMediaActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/SlideMediaActivity.java @@ -96,7 +96,6 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf Uri uri = manager.getUriForDownloadedFile(downloadID); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); - shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_with)); ContentResolver cR = context.getContentResolver(); shareIntent.setType(cR.getType(uri)); try { @@ -155,17 +154,22 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf public void onClick(View view) { int position = mPager.getCurrentItem(); Attachment attachment = attachments.get(position); - if (Build.VERSION.SDK_INT >= 23) { - if (ContextCompat.checkSelfPermission(SlideMediaActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(SlideMediaActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(SlideMediaActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Helper.EXTERNAL_STORAGE_REQUEST_CODE); + if( attachment.getType().compareTo("image") == 0 ){ + Helper.manageMove(SlideMediaActivity.this, attachment.getUrl(), false); + }else { + if (Build.VERSION.SDK_INT >= 23) { + if (ContextCompat.checkSelfPermission(SlideMediaActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(SlideMediaActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(SlideMediaActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Helper.EXTERNAL_STORAGE_REQUEST_CODE); + } else { + Helper.manageDownloadsNoPopup(SlideMediaActivity.this, attachment.getUrl()); + downloadID = -1; + } } else { Helper.manageDownloadsNoPopup(SlideMediaActivity.this, attachment.getUrl()); downloadID = -1; } - } else { - Helper.manageDownloadsNoPopup(SlideMediaActivity.this, attachment.getUrl()); - downloadID = -1; } + } }); media_share.setOnClickListener(new View.OnClickListener() { @@ -173,7 +177,9 @@ public class SlideMediaActivity extends BaseActivity implements OnDownloadInterf public void onClick(View view) { int position = mPager.getCurrentItem(); Attachment attachment = attachments.get(position); - if (attachment.getType().toLowerCase().equals("video") || attachment.getType().toLowerCase().equals("audio") || attachment.getType().toLowerCase().equals("gifv")) { + if( attachment.getType().compareTo("image") == 0 ){ + Helper.manageMove(SlideMediaActivity.this, attachment.getUrl(), true); + }else if (attachment.getType().toLowerCase().equals("video") || attachment.getType().toLowerCase().equals("audio") || attachment.getType().toLowerCase().equals("gifv")) { downloadID = Helper.manageDownloadsNoPopup(SlideMediaActivity.this, attachment.getUrl()); } else { if (Build.VERSION.SDK_INT >= 23) { diff --git a/app/src/main/java/app/fedilab/android/drawers/StatusListAdapter.java b/app/src/main/java/app/fedilab/android/drawers/StatusListAdapter.java index 3182364a1..c5e974427 100644 --- a/app/src/main/java/app/fedilab/android/drawers/StatusListAdapter.java +++ b/app/src/main/java/app/fedilab/android/drawers/StatusListAdapter.java @@ -33,6 +33,7 @@ import android.os.Bundle; import android.os.CountDownTimer; import android.os.Environment; import android.os.Handler; +import android.os.Looper; import android.text.Html; import android.text.InputType; import android.text.Spannable; @@ -110,6 +111,7 @@ import java.util.Iterator; import java.util.List; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.ExecutionException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -3524,12 +3526,11 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct }); boolean long_press_media = sharedpreferences.getBoolean(Helper.SET_LONG_PRESS_MEDIA, true); if (long_press_media) { + String finalUrl = url; imageView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - String myDir = sharedpreferences.getString(Helper.SET_FOLDER_RECORD, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()); - String fileName = URLUtil.guessFileName(attachment.getUrl(), null, null); - Helper.download(context, myDir + "/" + fileName, attachment.getUrl()); + Helper.manageMove(context, finalUrl, false); return true; } }); diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 1664a8cac..bbd64c0d0 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -58,6 +58,7 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Environment; +import android.os.Looper; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.provider.OpenableColumns; @@ -161,6 +162,7 @@ import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Proxy; import java.net.URISyntaxException; +import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.Security; @@ -182,6 +184,7 @@ import java.util.Map; import java.util.Objects; import java.util.Random; import java.util.TimeZone; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -4232,11 +4235,99 @@ public class Helper { //Request was successfully enqueued for download. }, error -> { }); - - } + /** + * Download from Glid cache + * @param context + * @param url + */ + public static void manageMove(Context context, String url, boolean share){ + Glide.with(context) + .asFile() + .load(url) + .into(new SimpleTarget() { + @Override + public void onResourceReady(@NotNull File file, Transition transition) { + Helper.notifyDownload(context, url, file, share); + } + }); + } + + /** + * Notify after moving a file from Glide cache + * @param context + * @param url + * @param sourceFile + */ + private static void notifyDownload(Context context, String url, File sourceFile, boolean share){ + + final String fileName = URLUtil.guessFileName(url, null, null); + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + final String targeted_folder = sharedpreferences.getString(Helper.SET_FOLDER_RECORD, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()); + + FileInputStream fis = null; + FileOutputStream fos = null; + FileChannel in = null; + FileChannel out = null; + try + { + File backupFile = new File(targeted_folder+"/"+fileName); + backupFile.createNewFile(); + fis = new FileInputStream(sourceFile); + fos = new FileOutputStream(backupFile); + in = fis.getChannel(); + out = fos.getChannel(); + long size = in.size(); + in.transferTo(0, size, out); + String mime = Helper.getMimeType(url); + final Intent intent = new Intent(); + intent.setAction(Intent.ACTION_VIEW); + Uri uri = Uri.fromFile(backupFile); + intent.setDataAndType(uri, mime); + SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context)); + Account account = new AccountDAO(context, db).getUniqAccount(userId, instance); + if (!share) { + Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(), + R.mipmap.ic_launcher_bubbles), NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName)); + Toasty.success(context, context.getString(R.string.save_over), Toasty.LENGTH_LONG).show(); + } else { + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_STREAM, uri); + shareIntent.setType(mime); + try { + context.startActivity(shareIntent); + } catch (Exception ignored) { + ignored.printStackTrace(); + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + finally { + try { + if (fis != null) + fis.close(); + } catch (Throwable ignore) {} + try { + if (fos != null) + fos.close(); + } catch (Throwable ignore) {} + try { + if (in != null && in.isOpen()) + in.close(); + } catch (Throwable ignore) {} + + try { + if (out != null && out.isOpen()) + out.close(); + } catch (Throwable ignore) {} + } + } + public static Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);