Fixed crash if trying to open a file received by chat and no app can handle it's format + improved MIME type detection + fixed file provider in debug mode
This commit is contained in:
parent
a0c3fe58cb
commit
cc77a5e88a
4 changed files with 41 additions and 11 deletions
|
@ -82,7 +82,8 @@ android {
|
||||||
versionName "${project.version}"
|
versionName "${project.version}"
|
||||||
applicationId getPackageName()
|
applicationId getPackageName()
|
||||||
multiDexEnabled true
|
multiDexEnabled true
|
||||||
manifestPlaceholders = [linphone_address_mime_type:"vnd.android.cursor.item/vnd." + getPackageName() + ".provider.sip_address"]
|
manifestPlaceholders = [linphone_address_mime_type: "vnd.android.cursor.item/vnd." + getPackageName() + ".provider.sip_address",
|
||||||
|
linphone_file_provider: getPackageName() + ".provider"]
|
||||||
}
|
}
|
||||||
|
|
||||||
applicationVariants.all { variant ->
|
applicationVariants.all { variant ->
|
||||||
|
|
|
@ -267,7 +267,7 @@
|
||||||
|
|
||||||
<provider
|
<provider
|
||||||
android:name="androidx.core.content.FileProvider"
|
android:name="androidx.core.content.FileProvider"
|
||||||
android:authorities="${applicationId}.provider"
|
android:authorities="${linphone_file_provider}"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:grantUriPermissions="true">
|
android:grantUriPermissions="true">
|
||||||
<meta-data
|
<meta-data
|
||||||
|
|
|
@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
@ -38,6 +39,7 @@ import android.widget.LinearLayout;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
import androidx.core.content.FileProvider;
|
import androidx.core.content.FileProvider;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
|
@ -294,7 +296,7 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
|
||||||
FileUtils.getStorageDirectory(mContext),
|
FileUtils.getStorageDirectory(mContext),
|
||||||
prefix + "_" + filename);
|
prefix + "_" + filename);
|
||||||
Log.w(
|
Log.w(
|
||||||
"File with that name already exists, renamed to "
|
"[Chat Message View] File with that name already exists, renamed to "
|
||||||
+ prefix
|
+ prefix
|
||||||
+ "_"
|
+ "_"
|
||||||
+ filename);
|
+ filename);
|
||||||
|
@ -323,7 +325,7 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
Log.w(
|
Log.w(
|
||||||
"WRITE_EXTERNAL_STORAGE permission not granted, won't be able to store the downloaded file");
|
"[Chat Message View] WRITE_EXTERNAL_STORAGE permission not granted, won't be able to store the downloaded file");
|
||||||
((ChatActivity) mContext)
|
((ChatActivity) mContext)
|
||||||
.requestPermissionIfNotGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
.requestPermissionIfNotGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
}
|
}
|
||||||
|
@ -353,22 +355,48 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
|
||||||
mContext.getResources().getString(R.string.file_provider),
|
mContext.getResources().getString(R.string.file_provider),
|
||||||
file);
|
file);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
Log.e(
|
||||||
|
"[Chat Message View] Couldn't get URI for file "
|
||||||
|
+ file
|
||||||
|
+ " using file provider "
|
||||||
|
+ mContext.getResources().getString(R.string.file_provider));
|
||||||
contentUri = Uri.parse(path);
|
contentUri = Uri.parse(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String filePath = contentUri.toString();
|
||||||
|
Log.i("[Chat Message View] Trying to open file: " + filePath);
|
||||||
String type = null;
|
String type = null;
|
||||||
String extension = MimeTypeMap.getFileExtensionFromUrl(contentUri.toString());
|
String extension = FileUtils.getExtensionFromFileName(filePath);
|
||||||
if (extension != null) {
|
|
||||||
|
if (extension != null && !extension.isEmpty()) {
|
||||||
|
Log.i("[Chat Message View] Found extension " + extension);
|
||||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||||
}
|
|
||||||
if (type != null) {
|
|
||||||
intent.setDataAndType(contentUri, type);
|
|
||||||
} else {
|
} else {
|
||||||
intent.setDataAndType(contentUri, "*/*");
|
Log.e("[Chat Message View] Couldn't find extension");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type != null) {
|
||||||
|
Log.i("[Chat Message View] Found matching MIME type " + type);
|
||||||
|
} else {
|
||||||
|
type = FileUtils.getMimeFromFile(filePath);
|
||||||
|
Log.e(
|
||||||
|
"[Chat Message View] Can't get MIME type from extension: "
|
||||||
|
+ extension
|
||||||
|
+ ", will use "
|
||||||
|
+ type);
|
||||||
|
}
|
||||||
|
|
||||||
|
intent.setDataAndType(contentUri, type);
|
||||||
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
|
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
mContext.startActivity(intent);
|
|
||||||
|
try {
|
||||||
|
mContext.startActivity(intent);
|
||||||
|
} catch (ActivityNotFoundException anfe) {
|
||||||
|
Log.e("[Chat Message View] Couldn't find an activity to handle MIME type: " + type);
|
||||||
|
Toast.makeText(mContext, R.string.cant_open_file_no_app_found, Toast.LENGTH_LONG)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadBitmap(String path, ImageView imageView) {
|
private void loadBitmap(String path, ImageView imageView) {
|
||||||
|
|
|
@ -254,6 +254,7 @@
|
||||||
<string name="download_file">Download</string>
|
<string name="download_file">Download</string>
|
||||||
<string name="toast_choose_chat_room_for_sharing">Select a conversation or create a new one</string>
|
<string name="toast_choose_chat_room_for_sharing">Select a conversation or create a new one</string>
|
||||||
<string name="trust_denied">Trust has been denied. Make a call to start the authentication process again.</string>
|
<string name="trust_denied">Trust has been denied. Make a call to start the authentication process again.</string>
|
||||||
|
<string name="cant_open_file_no_app_found">Can\'t open file, no application available for this format.</string>
|
||||||
|
|
||||||
<!-- Status Bar -->
|
<!-- Status Bar -->
|
||||||
<string name="status_connected">Connected</string>
|
<string name="status_connected">Connected</string>
|
||||||
|
|
Loading…
Reference in a new issue