From bee770bd2e9249dec84a167f87b514883680bf68 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 4 May 2017 18:12:25 +0200 Subject: [PATCH] Support camera on Nougat. --- CHANGELOG.md | 4 +++ MessengerProj/build.gradle | 5 ++-- MessengerProj/src/main/AndroidManifest.xml | 10 ++++++++ .../com/b44t/messenger/AndroidUtilities.java | 8 +++++- .../main/java/com/b44t/ui/ChatActivity.java | 25 ++++++++++++++++--- .../java/com/b44t/ui/WallpapersActivity.java | 11 +++++++- .../src/main/res/xml/provider_paths.xml | 4 +++ 7 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 MessengerProj/src/main/res/xml/provider_paths.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index e85006686..e34cad8f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Delta Chat Changelog +## UNRELEASED + +* Support camera on Android Nougat + ## v0.1.34 2017-05-03 diff --git a/MessengerProj/build.gradle b/MessengerProj/build.gradle index 4ad1df63f..1792d48cb 100644 --- a/MessengerProj/build.gradle +++ b/MessengerProj/build.gradle @@ -76,7 +76,7 @@ android { } } - defaultConfig.versionCode = 34 + defaultConfig.versionCode = 35 sourceSets.main { jniLibs.srcDir 'libs' @@ -117,6 +117,7 @@ android { defaultConfig { minSdkVersion 14 // 14: Android 4.0 Ice Cream Sandwich 2011 (Telegram default), 21: Android 5.0 Lollipop 2014 (recommended for InstantRun) targetSdkVersion 25 // 25: Nougat. CAVE: Do NOT target "Andoid O" without checking the background tasks carefully, see https://developer.android.com/preview/behavior-changes.html#back-all . As long as we target "Nougat", everything works as expected even for "Andoid O" or later - versionName "0.1.34" // do NOT forget to increase defaultConfig.versionCode! + // in general, we should not change the target without reason; eg. after the switch to Nougat, the camera stops working (see https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en ) + versionName "0.1.35" // do NOT forget to increase defaultConfig.versionCode! } } diff --git a/MessengerProj/src/main/AndroidManifest.xml b/MessengerProj/src/main/AndroidManifest.xml index 8f85b60d0..f45cab43a 100644 --- a/MessengerProj/src/main/AndroidManifest.xml +++ b/MessengerProj/src/main/AndroidManifest.xml @@ -222,6 +222,16 @@ + + + + diff --git a/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java b/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java index a29bbb39a..5d2a12ec9 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java @@ -47,6 +47,7 @@ import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; +import android.support.v4.content.FileProvider; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ForegroundColorSpan; @@ -898,7 +899,12 @@ public class AndroidUtilities { if (path.startsWith(MrMailbox.getBlobdir())) { uri = Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".attachments/" + file.getName()); } else { - uri = Uri.fromFile(file); + if (Build.VERSION.SDK_INT >= 24) { + uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", file); + } + else { + uri = Uri.fromFile(file); + } } if( cmd == Intent.ACTION_VIEW ) { diff --git a/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java b/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java index 5002c793a..38edb7921 100644 --- a/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java +++ b/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java @@ -42,6 +42,7 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; +import android.support.v4.content.FileProvider; import android.text.TextUtils; import android.text.style.ClickableSpan; import android.text.style.URLSpan; @@ -59,6 +60,7 @@ import android.widget.TextView; import android.widget.Toast; import com.b44t.messenger.AndroidUtilities; +import com.b44t.messenger.BuildConfig; import com.b44t.messenger.LocaleController; import com.b44t.messenger.MediaController; import com.b44t.messenger.MrChat; @@ -1186,12 +1188,21 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File image = AndroidUtilities.generatePicturePath(); if (image != null) { - takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); + if (Build.VERSION.SDK_INT >= 24) { + // a file:-url (Uri.fromFile()) is no longer allowed in Nougat, see https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image)); + takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + } + else { + // for eg. Kitkat everything but file:-urls crash + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); + } currentPicturePath = image.getAbsolutePath(); } startActivityForResult(takePictureIntent, RC0_CHAT_IMAGE_CAPTURE); } catch (Exception e) { - + e.printStackTrace(); } } else if (which == ChatAttachAlert.ATTACH_BUTTON_IDX_GALLERY ) { if (Build.VERSION.SDK_INT >= 23 && getParentActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { @@ -1243,7 +1254,13 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); File video = AndroidUtilities.generateVideoPath(); if (video != null) { - if (Build.VERSION.SDK_INT >= 18) { + if (Build.VERSION.SDK_INT >= 24) { + // a file:-url is no longer allowed in Nougat, see https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en + takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", video)); + takeVideoIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + } + else if (Build.VERSION.SDK_INT >= 18) { takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(video)); } takeVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, (long) (1024 * 1024 * 1536)); @@ -1251,7 +1268,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } startActivityForResult(takeVideoIntent, RC2_CHAT_VIDEO_CAPTURE); } catch (Exception e) { - + e.printStackTrace(); } } else if (which == ChatAttachAlert.ATTACH_BUTTON_IDX_FILE ) { if (Build.VERSION.SDK_INT >= 23 && getParentActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { diff --git a/MessengerProj/src/main/java/com/b44t/ui/WallpapersActivity.java b/MessengerProj/src/main/java/com/b44t/ui/WallpapersActivity.java index 056b25986..cbfc3f86f 100644 --- a/MessengerProj/src/main/java/com/b44t/ui/WallpapersActivity.java +++ b/MessengerProj/src/main/java/com/b44t/ui/WallpapersActivity.java @@ -41,8 +41,10 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; +import android.support.v4.content.FileProvider; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; @@ -52,6 +54,7 @@ import android.widget.ImageView; import android.widget.ProgressBar; import com.b44t.messenger.AndroidUtilities; +import com.b44t.messenger.BuildConfig; import com.b44t.messenger.ImageLoader; import com.b44t.messenger.LocaleController; import com.b44t.messenger.support.widget.LinearLayoutManager; @@ -222,7 +225,13 @@ public class WallpapersActivity extends BaseFragment implements NotificationCent Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File image = AndroidUtilities.generatePicturePath(); if (image != null) { - takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); + if (Build.VERSION.SDK_INT >= 24) { + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image)); + takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + } else { + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); + } currentPicturePath = image.getAbsolutePath(); } startActivityForResult(takePictureIntent, RC10_WALLPAPER_IMAGE_CAPTURE); diff --git a/MessengerProj/src/main/res/xml/provider_paths.xml b/MessengerProj/src/main/res/xml/provider_paths.xml new file mode 100644 index 000000000..86aed80c3 --- /dev/null +++ b/MessengerProj/src/main/res/xml/provider_paths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file