From 35d9141ae31020ff1c16d6c900e614e25411833d Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 19 Aug 2019 13:34:16 +0200 Subject: [PATCH] Added more logs to help debug file picking in chat --- .../linphone/chat/ChatMessagesFragment.java | 29 ++++++++++++-- .../java/org/linphone/utils/FileUtils.java | 40 ++++++++++++------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java b/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java index 5ab62a940..185f7789c 100644 --- a/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java +++ b/app/src/main/java/org/linphone/chat/ChatMessagesFragment.java @@ -428,11 +428,14 @@ public class ChatMessagesFragment extends Fragment if (requestCode == ADD_PHOTO && resultCode == Activity.RESULT_OK) { String fileToUploadPath = null; if (data.getData() != null) { + Log.i( + "[Chat Messages Fragment] Intent data after picking file is " + + data.getData().toString()); if (data.getData().toString().contains("com.android.contacts/contacts/")) { - if (FileUtils.getCVSPathFromLookupUri(data.getData().toString()) != null) { - fileToUploadPath = - FileUtils.getCVSPathFromLookupUri(data.getData().toString()) - .toString(); + Uri cvsPath = FileUtils.getCVSPathFromLookupUri(data.getData().toString()); + if (cvsPath != null) { + fileToUploadPath = cvsPath.toString(); + Log.i("[Chat Messages Fragment] Found CVS path: " + fileToUploadPath); } else { // TODO Error return; @@ -440,12 +443,21 @@ public class ChatMessagesFragment extends Fragment } else { fileToUploadPath = FileUtils.getRealPathFromURI(getActivity(), data.getData()); + Log.i( + "[Chat Messages Fragment] Resolved path for data is: " + + fileToUploadPath); } if (fileToUploadPath == null) { fileToUploadPath = data.getData().toString(); + Log.i( + "[Chat Messages Fragment] Couldn't resolve path, using as-is: " + + fileToUploadPath); } } else if (mImageToUploadUri != null) { fileToUploadPath = mImageToUploadUri.getPath(); + Log.i( + "[Chat Messages Fragment] Using pre-created path for dynamic capture " + + fileToUploadPath); } if (fileToUploadPath.startsWith("content://") @@ -454,6 +466,9 @@ public class ChatMessagesFragment extends Fragment fileToUploadPath = FileUtils.getFilePath( getActivity().getApplicationContext(), uriToParse); + Log.i( + "[Chat Messages Fragment] Path was using a content or file scheme, real path is: " + + fileToUploadPath); if (fileToUploadPath == null) { Log.e( "[Chat Messages Fragment] Failed to get access to file " @@ -462,6 +477,9 @@ public class ChatMessagesFragment extends Fragment } else if (fileToUploadPath.contains("com.android.contacts/contacts/")) { fileToUploadPath = FileUtils.getCVSPathFromLookupUri(fileToUploadPath).toString(); + Log.i( + "[Chat Messages Fragment] Path was using a contact scheme, real path is: " + + fileToUploadPath); } if (fileToUploadPath != null) { @@ -470,6 +488,9 @@ public class ChatMessagesFragment extends Fragment } else { addFileToPendingList(fileToUploadPath); } + } else { + Log.e( + "[Chat Messages Fragment] Failed to get a path that we could use, aborting attachment"); } } else { super.onActivityResult(requestCode, resultCode, data); diff --git a/app/src/main/java/org/linphone/utils/FileUtils.java b/app/src/main/java/org/linphone/utils/FileUtils.java index e9cf0963a..5b3165369 100644 --- a/app/src/main/java/org/linphone/utils/FileUtils.java +++ b/app/src/main/java/org/linphone/utils/FileUtils.java @@ -38,6 +38,7 @@ 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; @@ -81,14 +82,22 @@ public class FileUtils { try { File localFile = createFile(context, name); InputStream remoteFile = context.getContentResolver().openInputStream(uri); + Log.i( + "[File Utils] Trying to copy file from " + + uri.toString() + + " to local file " + + localFile.getAbsolutePath()); if (copyToFile(remoteFile, localFile)) { + Log.i("[File Utils] Copy successful"); result = localFile.getAbsolutePath(); + } else { + Log.e("[File Utils] Copy failed"); } remoteFile.close(); } catch (IOException e) { - Log.e("[File Utils] Enable to get sharing file ", e); + Log.e("[File Utils] getFilePath exception: ", e); } return result; @@ -128,8 +137,9 @@ public class FileUtils { } return true; } catch (IOException e) { - return false; + Log.e("[File Utils] copyToFile exception: " + e); } + return false; } private static File createFile(Context context, String fileName) { @@ -182,12 +192,9 @@ public class FileUtils { public static String getStorageDirectory(Context mContext) { String storageDir = - Environment.getExternalStorageDirectory() + Environment.getExternalStorageDirectory().getAbsolutePath() + "/" - + mContext.getString( - mContext.getResources() - .getIdentifier( - "app_name", "string", mContext.getPackageName())); + + mContext.getString(R.string.app_name); File file = new File(storageDir); if (!file.isDirectory() || !file.exists()) { Log.w( @@ -196,7 +203,11 @@ public class FileUtils { + " doesn't seem to exists yet, let's create it"); boolean result = file.mkdirs(); if (!result) { - Log.e("[File Utils] Couldn't create directory " + file.getAbsolutePath()); + Log.e( + "[File Utils] Couldn't create media directory " + + file.getAbsolutePath() + + ", using external storage dir instead"); + return Environment.getExternalStorageDirectory().getAbsolutePath(); } LinphoneManager.getInstance().getMediaScanner().scanFile(file, null); } @@ -207,10 +218,7 @@ public class FileUtils { String recordingsDir = Environment.getExternalStorageDirectory() + "/" - + mContext.getString( - mContext.getResources() - .getIdentifier( - "app_name", "string", mContext.getPackageName())) + + mContext.getString(R.string.app_name) + "/recordings"; File file = new File(recordingsDir); if (!file.isDirectory() || !file.exists()) { @@ -220,7 +228,11 @@ public class FileUtils { + " doesn't seem to exists yet, let's create it"); boolean result = file.mkdirs(); if (!result) { - Log.e("[File Utils] Couldn't create directory " + file.getAbsolutePath()); + 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); } @@ -250,7 +262,7 @@ public class FileUtils { fw.close(); return Uri.fromFile(vcfFile); } catch (IOException e) { - e.printStackTrace(); + Log.e("[File Utils] createCVSFromString exception: " + e); } return null; }