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) {
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,20 +450,27 @@ 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 (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");
}
}
}

View file

@ -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) {
@ -92,8 +96,10 @@ public class FileUtils {
private static String getNameFromUri(Uri uri, Context context) {
String name = null;
if (uri != null) {
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) {
returnCursor.moveToFirst();
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
@ -103,6 +109,7 @@ public class FileUtils {
} 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) {