add option to save files in global Pictures/Movies/Documents/Music

This commit is contained in:
Christian Schneppe 2021-02-28 19:14:55 +01:00
parent dda0a3e114
commit ee32c427af
5 changed files with 102 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.persistance;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
@ -33,6 +34,7 @@ import android.util.Base64OutputStream;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.LruCache;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
@ -52,6 +54,7 @@ import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -80,6 +83,7 @@ import eu.siacs.conversations.utils.MimeUtils;
import eu.siacs.conversations.xmpp.pep.Avatar;
import ezvcard.Ezvcard;
import ezvcard.VCard;
import me.drakeet.support.toast.ToastCompat;
public class FileBackend {
@ -729,6 +733,33 @@ public class FileBackend {
updateFileParams(message);
}
public void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
if (!destFile.exists()) {
destFile.createNewFile();
}
Log.d(Config.LOGTAG, "Copy " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath());
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
updateMediaScanner(destFile);
}
}
public boolean unusualBounds(final Uri image) {
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
@ -1847,4 +1878,59 @@ public class FileBackend {
return bitmap;
}
}
public static String getGlobalPicturesPath() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/blabber.im/";
}
public static String getGlobalVideosPath() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/blabber.im/";
}
public static String getGlobalDocumentsPath() {
if (Compatibility.runsNineTeen()) {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/blabber.im/";
} else {
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Documents";
}
}
public static String getGlobalAudiosPath() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/blabber.im/";
}
public void saveFile(final Message message, final Activity activity) {
final DownloadableFile source = getFile(message);
final File destination = new File(getDestinationToSaveFile(message));
try {
copyFile(source, destination);
ToastCompat.makeText(activity, activity.getString(R.string.file_copied_to, destination), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getDestinationToSaveFile(Message message) {
final DownloadableFile file = getFile(message);
final String mime = file.getMimeType();
String extension = MimeUtils.guessExtensionFromMimeType(mime);
if (extension == null) {
Log.d(Config.LOGTAG, "extension from mime type was null");
extension = "null";
}
if ("ogg".equals(extension) && mime.startsWith("audio/")) {
extension = "oga";
}
String filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension;
if (mime != null && mime.startsWith("image")) {
return getGlobalPicturesPath() + File.separator + filename;
} else if (mime != null && mime.startsWith("video")) {
return getGlobalVideosPath() + File.separator + filename;
} else if (mime != null && mime.startsWith("audio")) {
return getGlobalAudiosPath() + File.separator + filename;
} else {
return getGlobalDocumentsPath() + File.separator + filename;
}
}
}

View file

@ -1306,6 +1306,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
MenuItem downloadFile = menu.findItem(R.id.download_file);
MenuItem deleteFile = menu.findItem(R.id.delete_file);
MenuItem showErrorMessage = menu.findItem(R.id.show_error_message);
MenuItem saveFile = menu.findItem(R.id.save_file);
final boolean unInitiatedButKnownSize = MessageUtils.unInitiatedButKnownSize(m);
final boolean showError = m.getStatus() == Message.STATUS_SEND_FAILED && m.getErrorMessage() != null && !Message.ERROR_MESSAGE_CANCELLED.equals(m.getErrorMessage());
deleteMessage.setVisible(true);
@ -1366,6 +1367,8 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
deleteFile.setVisible(true);
deleteFile.setTitle(activity.getString(R.string.delete_x_file, UIHelper.getFileDescriptionString(activity, m)));
}
saveFile.setVisible(true);
saveFile.setTitle(activity.getString(R.string.save_x_file, UIHelper.getFileDescriptionString(activity, m)));
}
showErrorMessage.setVisible(showError);
final String mime = m.isFileOrImage() ? m.getMimeType() : null;
@ -1442,6 +1445,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
case R.id.open_with:
openWith(selectedMessage);
return true;
case R.id.save_file:
activity.xmppConnectionService.getFileBackend().saveFile(selectedMessage, activity);
return true;
default:
return super.onContextItemSelected(item);
}

View file

@ -50,6 +50,10 @@ public class Compatibility {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
public static boolean runsNineTeen() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}
public static boolean runsTwentySix() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}

View file

@ -53,6 +53,10 @@
android:id="@+id/cancel_transmission"
android:title="@string/cancel_transmission"
android:visible="false" />
<item
android:id="@+id/save_file"
android:title="@string/save_x_file"
android:visible="false" />
<item
android:id="@+id/delete_file"
android:title="@string/delete_x_file"

View file

@ -1121,4 +1121,6 @@
<string name="open_settings">Open settings</string>
<string name="oldAndroidVersion">Old Android version</string>
<string name="oldAndroidVersionMessage">You are using an old Android version that will no longer be supported in future updates. From blabber.im version 3.1.0 only devices with Android 5 or higher are supported. Please update your device firmware.</string>
<string name="save_x_file">Save %s</string>
<string name="file_copied_to">File copied to %s</string>
</resources>