From 4a62fbb95cfaac60ba6b962b091385dfa97af5c1 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 19 Aug 2019 11:40:40 +0200 Subject: [PATCH] Prevent crash if Uri returned by file picker in chat can't be resolved + improved logs --- .../linphone/chat/ChatMessagesFragment.java | 42 ++++++++++++------- .../java/org/linphone/utils/FileUtils.java | 30 ++++++++----- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java b/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java index 2a7fe4acc..5ab62a940 100644 --- a/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java +++ b/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java @@ -323,7 +323,7 @@ public class ChatMessagesFragment extends Fragment if (getArguments() != null) { String fileSharedUri = getArguments().getString("SharedFiles"); if (fileSharedUri != null) { - Log.i("[ChatMessages] Found shared file(s): " + fileSharedUri); + Log.i("[Chat Messages Fragment] Found shared file(s): " + fileSharedUri); if (fileSharedUri.contains(":")) { String[] files = fileSharedUri.split(":"); for (String file : files) { @@ -337,7 +337,7 @@ public class ChatMessagesFragment extends Fragment if (getArguments().containsKey("SharedText")) { String sharedText = getArguments().getString("SharedText"); mMessageTextToSend.setText(sharedText); - Log.i("[ChatMessages] Found shared text: " + sharedText); + Log.i("[Chat Messages Fragment] Found shared text: " + sharedText); } } @@ -450,19 +450,26 @@ public class ChatMessagesFragment extends Fragment if (fileToUploadPath.startsWith("content://") || fileToUploadPath.startsWith("file://")) { + Uri uriToParse = Uri.parse(fileToUploadPath); fileToUploadPath = FileUtils.getFilePath( - getActivity().getApplicationContext(), - Uri.parse(fileToUploadPath)); + getActivity().getApplicationContext(), uriToParse); + if (fileToUploadPath == null) { + Log.e( + "[Chat Messages Fragment] Failed to get access to file " + + uriToParse.toString()); + } } else if (fileToUploadPath.contains("com.android.contacts/contacts/")) { fileToUploadPath = FileUtils.getCVSPathFromLookupUri(fileToUploadPath).toString(); } - if (FileUtils.isExtensionImage(fileToUploadPath)) { - addImageToPendingList(fileToUploadPath); - } else { - addFileToPendingList(fileToUploadPath); + if (fileToUploadPath != null) { + if (FileUtils.isExtensionImage(fileToUploadPath)) { + addImageToPendingList(fileToUploadPath); + } else { + addFileToPendingList(fileToUploadPath); + } } } else { super.onActivityResult(requestCode, resultCode, data); @@ -991,7 +998,8 @@ public class ChatMessagesFragment extends Fragment private void addFileToPendingList(String path) { if (path == null) { - Log.e("Can't add file to pending list because it's path is null..."); + Log.e( + "[Chat Messages Fragment] Can't add file to pending list because it's path is null..."); return; } @@ -1029,7 +1037,8 @@ public class ChatMessagesFragment extends Fragment private void addImageToPendingList(String path) { if (path == null) { - Log.e("Can't add image to pending list because it's path is null..."); + Log.e( + "[Chat Messages Fragment] Can't add image to pending list because it's path is null..."); return; } @@ -1203,12 +1212,14 @@ public class ChatMessagesFragment extends Fragment ChatMessage msg = event.getChatMessage(); if (msg.getErrorInfo() != null && msg.getErrorInfo().getReason() == Reason.UnsupportedContent) { - Log.w("Message received but content is unsupported, do not display it"); + Log.w( + "[Chat Messages Fragment] Message received but content is unsupported, do not display it"); return; } if (!msg.hasTextContent() && msg.getFileTransferInformation() == null) { - Log.w("Message has no text or file transfer information to display, ignoring it..."); + Log.w( + "[Chat Messages Fragment] Message has no text or file transfer information to display, ignoring it..."); return; } @@ -1374,7 +1385,7 @@ public class ChatMessagesFragment extends Fragment mCurrentInputContentInfo.releasePermission(); } } catch (Exception e) { - Log.e("[TimelineFragment] releasePermission failed : ", e); + Log.e("[Chat Messages Fragment] releasePermission failed : ", e); } finally { mCurrentInputContentInfo = null; } @@ -1398,7 +1409,7 @@ public class ChatMessagesFragment extends Fragment try { inputContentInfo.requestPermission(); } catch (Exception e) { - Log.e("[TimelineFragment] requestPermission failed : ", e); + Log.e("[Chat Messages Fragment] requestPermission failed : ", e); return false; } } @@ -1424,7 +1435,8 @@ public class ChatMessagesFragment extends Fragment try { super.onLayoutChildren(recycler, state); } catch (IndexOutOfBoundsException e) { - Log.e("InvalidIndexOutOfBound Exception, probably while rotating the device"); + Log.e( + "[Chat Messages Fragment] InvalidIndexOutOfBound Exception, probably while rotating the device"); } } } diff --git a/app/src/main/java/org/linphone/utils/FileUtils.java b/app/src/main/java/org/linphone/utils/FileUtils.java index fa9b4f67c..e9cf0963a 100644 --- a/app/src/main/java/org/linphone/utils/FileUtils.java +++ b/app/src/main/java/org/linphone/utils/FileUtils.java @@ -45,6 +45,8 @@ import org.linphone.core.tools.Log; public class FileUtils { public static String getNameFromFilePath(String filePath) { + if (filePath == null) return null; + String name = filePath; int i = filePath.lastIndexOf('/'); if (i > 0) { @@ -54,6 +56,8 @@ public class FileUtils { } public static String getExtensionFromFileName(String fileName) { + if (fileName == null) return null; + String extension = null; int i = fileName.lastIndexOf('.'); if (i > 0) { @@ -84,7 +88,7 @@ public class FileUtils { remoteFile.close(); } catch (IOException e) { - Log.e("[File Utils] Enable to get sharing file", e); + Log.e("[File Utils] Enable to get sharing file ", e); } return result; @@ -92,16 +96,19 @@ public class FileUtils { private static String getNameFromUri(Uri uri, Context context) { String name = null; - if (uri.getScheme().equals("content")) { - Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); - if (returnCursor != null) { - returnCursor.moveToFirst(); - int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); - name = returnCursor.getString(nameIndex); - returnCursor.close(); + if (uri != null) { + if (uri.getScheme().equals("content")) { + Cursor returnCursor = + context.getContentResolver().query(uri, null, null, null, null); + if (returnCursor != null) { + returnCursor.moveToFirst(); + int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); + name = returnCursor.getString(nameIndex); + returnCursor.close(); + } + } else if (uri.getScheme().equals("file")) { + name = uri.getLastPathSegment(); } - } else if (uri.getScheme().equals("file")) { - name = uri.getLastPathSegment(); } return name; } @@ -126,6 +133,7 @@ public class FileUtils { } private static File createFile(Context context, String fileName) { + if (fileName == null) return null; if (TextUtils.isEmpty(fileName)) fileName = getStartDate(); if (!fileName.contains(".")) { @@ -145,6 +153,8 @@ public class FileUtils { } public static Uri getCVSPathFromLookupUri(String content) { + if (content == null) return null; + String contactId = getNameFromFilePath(content); FriendList[] friendList = LinphoneManager.getCore().getFriendsLists(); for (FriendList list : friendList) {