This commit is contained in:
tom79 2019-12-07 12:30:26 +01:00
parent 6adedf2376
commit f0634c067e
3 changed files with 111 additions and 13 deletions

View File

@ -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) {

View File

@ -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;
}
});

View File

@ -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<File>() {
@Override
public void onResourceReady(@NotNull File file, Transition<? super File> 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);