Prevent crash if Uri returned by file picker in chat can't be resolved + improved logs

This commit is contained in:
Sylvain Berfini 2019-08-19 11:40:40 +02:00
parent 6fe1c4d5f4
commit 4a62fbb95c
2 changed files with 47 additions and 25 deletions

View file

@ -323,7 +323,7 @@ public class ChatMessagesFragment extends Fragment
if (getArguments() != null) { if (getArguments() != null) {
String fileSharedUri = getArguments().getString("SharedFiles"); String fileSharedUri = getArguments().getString("SharedFiles");
if (fileSharedUri != null) { if (fileSharedUri != null) {
Log.i("[ChatMessages] Found shared file(s): " + fileSharedUri); Log.i("[Chat Messages Fragment] Found shared file(s): " + fileSharedUri);
if (fileSharedUri.contains(":")) { if (fileSharedUri.contains(":")) {
String[] files = fileSharedUri.split(":"); String[] files = fileSharedUri.split(":");
for (String file : files) { for (String file : files) {
@ -337,7 +337,7 @@ public class ChatMessagesFragment extends Fragment
if (getArguments().containsKey("SharedText")) { if (getArguments().containsKey("SharedText")) {
String sharedText = getArguments().getString("SharedText"); String sharedText = getArguments().getString("SharedText");
mMessageTextToSend.setText(sharedText); mMessageTextToSend.setText(sharedText);
Log.i("[ChatMessages] Found shared text: " + sharedText); Log.i("[Chat Messages Fragment] Found shared text: " + sharedText);
} }
} }
@ -450,20 +450,27 @@ public class ChatMessagesFragment extends Fragment
if (fileToUploadPath.startsWith("content://") if (fileToUploadPath.startsWith("content://")
|| fileToUploadPath.startsWith("file://")) { || fileToUploadPath.startsWith("file://")) {
Uri uriToParse = Uri.parse(fileToUploadPath);
fileToUploadPath = fileToUploadPath =
FileUtils.getFilePath( FileUtils.getFilePath(
getActivity().getApplicationContext(), getActivity().getApplicationContext(), uriToParse);
Uri.parse(fileToUploadPath)); if (fileToUploadPath == null) {
Log.e(
"[Chat Messages Fragment] Failed to get access to file "
+ uriToParse.toString());
}
} else if (fileToUploadPath.contains("com.android.contacts/contacts/")) { } else if (fileToUploadPath.contains("com.android.contacts/contacts/")) {
fileToUploadPath = fileToUploadPath =
FileUtils.getCVSPathFromLookupUri(fileToUploadPath).toString(); FileUtils.getCVSPathFromLookupUri(fileToUploadPath).toString();
} }
if (fileToUploadPath != null) {
if (FileUtils.isExtensionImage(fileToUploadPath)) { if (FileUtils.isExtensionImage(fileToUploadPath)) {
addImageToPendingList(fileToUploadPath); addImageToPendingList(fileToUploadPath);
} else { } else {
addFileToPendingList(fileToUploadPath); addFileToPendingList(fileToUploadPath);
} }
}
} else { } else {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
@ -991,7 +998,8 @@ public class ChatMessagesFragment extends Fragment
private void addFileToPendingList(String path) { private void addFileToPendingList(String path) {
if (path == null) { 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; return;
} }
@ -1029,7 +1037,8 @@ public class ChatMessagesFragment extends Fragment
private void addImageToPendingList(String path) { private void addImageToPendingList(String path) {
if (path == null) { 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; return;
} }
@ -1203,12 +1212,14 @@ public class ChatMessagesFragment extends Fragment
ChatMessage msg = event.getChatMessage(); ChatMessage msg = event.getChatMessage();
if (msg.getErrorInfo() != null if (msg.getErrorInfo() != null
&& msg.getErrorInfo().getReason() == Reason.UnsupportedContent) { && 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; return;
} }
if (!msg.hasTextContent() && msg.getFileTransferInformation() == null) { 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; return;
} }
@ -1374,7 +1385,7 @@ public class ChatMessagesFragment extends Fragment
mCurrentInputContentInfo.releasePermission(); mCurrentInputContentInfo.releasePermission();
} }
} catch (Exception e) { } catch (Exception e) {
Log.e("[TimelineFragment] releasePermission failed : ", e); Log.e("[Chat Messages Fragment] releasePermission failed : ", e);
} finally { } finally {
mCurrentInputContentInfo = null; mCurrentInputContentInfo = null;
} }
@ -1398,7 +1409,7 @@ public class ChatMessagesFragment extends Fragment
try { try {
inputContentInfo.requestPermission(); inputContentInfo.requestPermission();
} catch (Exception e) { } catch (Exception e) {
Log.e("[TimelineFragment] requestPermission failed : ", e); Log.e("[Chat Messages Fragment] requestPermission failed : ", e);
return false; return false;
} }
} }
@ -1424,7 +1435,8 @@ public class ChatMessagesFragment extends Fragment
try { try {
super.onLayoutChildren(recycler, state); super.onLayoutChildren(recycler, state);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
Log.e("InvalidIndexOutOfBound Exception, probably while rotating the device"); Log.e(
"[Chat Messages Fragment] InvalidIndexOutOfBound Exception, probably while rotating the device");
} }
} }
} }

View file

@ -45,6 +45,8 @@ import org.linphone.core.tools.Log;
public class FileUtils { public class FileUtils {
public static String getNameFromFilePath(String filePath) { public static String getNameFromFilePath(String filePath) {
if (filePath == null) return null;
String name = filePath; String name = filePath;
int i = filePath.lastIndexOf('/'); int i = filePath.lastIndexOf('/');
if (i > 0) { if (i > 0) {
@ -54,6 +56,8 @@ public class FileUtils {
} }
public static String getExtensionFromFileName(String fileName) { public static String getExtensionFromFileName(String fileName) {
if (fileName == null) return null;
String extension = null; String extension = null;
int i = fileName.lastIndexOf('.'); int i = fileName.lastIndexOf('.');
if (i > 0) { if (i > 0) {
@ -92,8 +96,10 @@ public class FileUtils {
private static String getNameFromUri(Uri uri, Context context) { private static String getNameFromUri(Uri uri, Context context) {
String name = null; String name = null;
if (uri != null) {
if (uri.getScheme().equals("content")) { if (uri.getScheme().equals("content")) {
Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
if (returnCursor != null) { if (returnCursor != null) {
returnCursor.moveToFirst(); returnCursor.moveToFirst();
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
@ -103,6 +109,7 @@ public class FileUtils {
} else if (uri.getScheme().equals("file")) { } else if (uri.getScheme().equals("file")) {
name = uri.getLastPathSegment(); name = uri.getLastPathSegment();
} }
}
return name; return name;
} }
@ -126,6 +133,7 @@ public class FileUtils {
} }
private static File createFile(Context context, String fileName) { private static File createFile(Context context, String fileName) {
if (fileName == null) return null;
if (TextUtils.isEmpty(fileName)) fileName = getStartDate(); if (TextUtils.isEmpty(fileName)) fileName = getStartDate();
if (!fileName.contains(".")) { if (!fileName.contains(".")) {
@ -145,6 +153,8 @@ public class FileUtils {
} }
public static Uri getCVSPathFromLookupUri(String content) { public static Uri getCVSPathFromLookupUri(String content) {
if (content == null) return null;
String contactId = getNameFromFilePath(content); String contactId = getNameFromFilePath(content);
FriendList[] friendList = LinphoneManager.getCore().getFriendsLists(); FriendList[] friendList = LinphoneManager.getCore().getFriendsLists();
for (FriendList list : friendList) { for (FriendList list : friendList) {