Store files in private app folder, delete files when message is deleted

This commit is contained in:
Sylvain Berfini 2020-01-17 14:45:58 +01:00
parent 431881cf50
commit c9390f87fe
6 changed files with 69 additions and 46 deletions

View file

@ -35,6 +35,7 @@ import org.linphone.core.Address;
import org.linphone.core.ChatMessage;
import org.linphone.core.ChatMessageListenerStub;
import org.linphone.core.EventLog;
import org.linphone.core.tools.Log;
import org.linphone.utils.LinphoneUtils;
import org.linphone.utils.SelectableAdapter;
import org.linphone.utils.SelectableHelper;
@ -91,6 +92,9 @@ public class ChatMessagesAdapter extends SelectableAdapter<ChatMessageViewHolder
}
if (state == ChatMessage.State.Displayed) {
mTransientMessages.remove(message);
} else if (state == ChatMessage.State.FileTransferDone) {
Log.i("[Chat Message] File transfer done");
// TODO: make picture public
}
}
};

View file

@ -468,6 +468,7 @@ public class ChatMessagesFragment extends Fragment
public void onDeleteSelection(Object[] objectsToDelete) {
for (Object obj : objectsToDelete) {
EventLog eventLog = (EventLog) obj;
LinphoneUtils.deleteFileContentIfExists(eventLog);
eventLog.deleteFromDatabase();
}
((ChatMessagesGenericAdapter) mChatEventsList.getAdapter())
@ -559,6 +560,7 @@ public class ChatMessagesFragment extends Fragment
return true;
}
if (item.getItemId() == R.id.delete_message) {
LinphoneUtils.deleteFileContentIfExists(event);
mChatRoom.deleteMessage(message);
((ChatMessagesGenericAdapter) mChatEventsList.getAdapter())
.removeItem(mContextMenuMessagePosition);
@ -1385,6 +1387,8 @@ public class ChatMessagesFragment extends Fragment
@Override
public void onEphemeralMessageDeleted(ChatRoom chatRoom, EventLog eventLog) {
Log.i("[Chat Room] Ephemeral message expired");
LinphoneUtils.deleteFileContentIfExists(eventLog);
if (!((ChatMessagesGenericAdapter) mChatEventsList.getAdapter())
.removeFromHistory(eventLog)) {
Log.w("[Chat Room] Ephemeral message not found, refresh list");

View file

@ -43,7 +43,9 @@ import org.linphone.core.ChatRoom;
import org.linphone.core.ChatRoomListenerStub;
import org.linphone.core.Core;
import org.linphone.core.CoreListenerStub;
import org.linphone.core.EventLog;
import org.linphone.core.ProxyConfig;
import org.linphone.utils.LinphoneUtils;
import org.linphone.utils.SelectableHelper;
public class ChatRoomsFragment extends Fragment
@ -257,6 +259,11 @@ public class ChatRoomsFragment extends Fragment
for (Object obj : objectsToDelete) {
ChatRoom room = (ChatRoom) obj;
room.addListener(mChatRoomListener);
for (EventLog eventLog : room.getHistoryMessageEvents(0)) {
LinphoneUtils.deleteFileContentIfExists(eventLog);
}
core.deleteChatRoom(room);
}
if (mChatRoomDeletionPendingCount > 0) {

View file

@ -38,7 +38,6 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.linphone.LinphoneManager;
import org.linphone.R;
import org.linphone.core.Address;
import org.linphone.core.Friend;
import org.linphone.core.FriendList;
@ -190,53 +189,42 @@ public class FileUtils {
return null;
}
public static String getStorageDirectory(Context mContext) {
String storageDir =
Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/"
+ mContext.getString(R.string.app_name);
File file = new File(storageDir);
if (!file.isDirectory() || !file.exists()) {
Log.w(
"[File Utils] Directory "
+ file
+ " doesn't seem to exists yet, let's create it");
boolean result = file.mkdirs();
if (!result) {
Log.e(
"[File Utils] Couldn't create media directory "
+ file.getAbsolutePath()
+ ", using external storage dir instead");
return Environment.getExternalStorageDirectory().getAbsolutePath();
public static void deleteFile(String filePath) {
if (filePath == null || filePath.isEmpty()) return;
File file = new File(filePath);
if (file.exists()) {
try {
if (file.delete()) {
Log.i("[File Utils] File deleted: ", filePath);
} else {
Log.e("[File Utils] Can't delete ", filePath);
}
} catch (Exception e) {
Log.e("[File Utils] Can't delete ", filePath, ", exception: ", e);
}
LinphoneManager.getInstance().getMediaScanner().scanFile(file, null);
} else {
Log.e("[File Utils] File ", filePath, " doesn't exists");
}
return storageDir;
}
public static String getStorageDirectory(Context mContext) {
File path = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.w("[File Utils] External storage is mounted");
String directory = Environment.DIRECTORY_DOWNLOADS;
path = mContext.getExternalFilesDir(directory);
}
if (path == null) {
Log.w("[File Utils] Couldn't get external storage path, using internal");
path = mContext.getFilesDir();
}
return path.getAbsolutePath();
}
public static String getRecordingsDirectory(Context mContext) {
String recordingsDir =
Environment.getExternalStorageDirectory()
+ "/"
+ mContext.getString(R.string.app_name)
+ "/recordings";
File file = new File(recordingsDir);
if (!file.isDirectory() || !file.exists()) {
Log.w(
"[File Utils] Directory "
+ file
+ " doesn't seem to exists yet, let's create it");
boolean result = file.mkdirs();
if (!result) {
Log.e(
"[File Utils] Couldn't create recordings directory "
+ file.getAbsolutePath()
+ ", using external storage dir instead");
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
LinphoneManager.getInstance().getMediaScanner().scanFile(file, null);
}
return recordingsDir;
return getStorageDirectory(mContext);
}
@SuppressLint("SimpleDateFormat")

View file

@ -48,7 +48,10 @@ import org.linphone.R;
import org.linphone.core.Address;
import org.linphone.core.Call;
import org.linphone.core.CallLog;
import org.linphone.core.ChatMessage;
import org.linphone.core.Content;
import org.linphone.core.Core;
import org.linphone.core.EventLog;
import org.linphone.core.Factory;
import org.linphone.core.LogCollectionState;
import org.linphone.core.ProxyConfig;
@ -410,4 +413,21 @@ public final class LinphoneUtils {
customText.setText(text);
return dialog;
}
public static void deleteFileContentIfExists(EventLog eventLog) {
if (eventLog.getType() == EventLog.Type.ConferenceChatMessage) {
ChatMessage message = eventLog.getChatMessage();
if (message != null) {
for (Content content : message.getContents()) {
if (content.isFile() && content.getFilePath() != null) {
Log.w(
"[Linphone Utils] Chat message is being deleted, file ",
content.getFilePath(),
" will also be deleted");
FileUtils.deleteFile(content.getFilePath());
}
}
}
}
}
}

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
<files-path name="internal_files" path="." />
<external-files-path name="downloads" path="Download/" />
<external-files-path name="files" path="." />
</paths>