diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-10-31 13:28:44 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-10-31 13:28:44 +0300 |
commit | 78f476eab855f2ac63758720d1889266ecd20da6 (patch) | |
tree | e9119fb53691dadd54b9c333d8ecd53a14c8c463 /app/src | |
parent | 96ffc2b2d03dab57790d6f4373a01d11e89af838 (diff) |
Use CameraX
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src')
6 files changed, 63 insertions, 29 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 73a1bcbe6..c852bf5e4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,11 +8,7 @@ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> - <queries> - <intent> - <action android:name="android.media.action.IMAGE_CAPTURE" /> - </intent> - </queries> + <uses-sdk android:minSdkVersion="19" tools:overrideLibrary="androidx.camera.camera2, androidx.camera.lifecycle, androidx.camera.core"/> <application android:name="it.niedermann.nextcloud.deck.DeckApplication" diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java index 9c6cd3f99..141cfb247 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java @@ -7,8 +7,8 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; -import android.provider.MediaStore; import android.util.DisplayMetrics; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -16,8 +16,10 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.camera.core.ImageCapture; +import androidx.camera.core.ImageCaptureException; import androidx.core.app.SharedElementCallback; -import androidx.core.content.FileProvider; +import androidx.core.content.ContextCompat; import androidx.core.content.PermissionChecker; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; @@ -35,7 +37,6 @@ import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; -import it.niedermann.nextcloud.deck.BuildConfig; import it.niedermann.nextcloud.deck.DeckLog; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.FragmentCardEditTabAttachmentsBinding; @@ -50,6 +51,7 @@ import it.niedermann.nextcloud.deck.ui.card.EditCardViewModel; import it.niedermann.nextcloud.deck.ui.card.attachments.picker.CardAttachmentPicker; import it.niedermann.nextcloud.deck.ui.card.attachments.picker.CardAttachmentPickerListener; import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment; +import it.niedermann.nextcloud.deck.util.AttachmentUtil; import it.niedermann.nextcloud.deck.util.VCardUtil; import static android.Manifest.permission.READ_EXTERNAL_STORAGE; @@ -161,18 +163,40 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme @Override public void pickCamera() { - final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - if (takePictureIntent.resolveActivity(requireContext().getPackageManager()) != null) { - Long localId = viewModel.getFullCard().getLocalId(); - String imageFileName = Instant.now().atZone(ZoneId.systemDefault()).format(fileNameFromCameraFormatter); - File tempFile = new File(requireContext().getApplicationContext().getFilesDir().getAbsolutePath() + "/attachments/account-" + viewModel.getFullCard().getAccountId() + "/card-" + (localId == null ? "pending-creation" : localId) + '/' + imageFileName); - - Uri photoURI = FileProvider.getUriForFile(requireContext(), - BuildConfig.APPLICATION_ID + ".fileprovider", - tempFile); - takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); - startActivityForResult(takePictureIntent, REQUEST_CODE_ADD_FILE); - } + + final String photoFileName = Instant.now().atZone(ZoneId.systemDefault()).format(fileNameFromCameraFormatter); + final File photoFile = AttachmentUtil.getTempCacheFile(requireContext(), viewModel.getAccount().getId(), viewModel.getFullCard().getLocalId(), photoFileName); + final ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(photoFile).build(); + ImageCapture imageCapture = new ImageCapture.Builder().build(); + imageCapture.takePicture(options, ContextCompat.getMainExecutor(requireContext()), new ImageCapture.OnImageSavedCallback() { + @Override + public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { + Uri savedUri = Uri.fromFile(photoFile); + Log.v("TAG", savedUri.toString()); + } + + @Override + public void onError(@NonNull ImageCaptureException exception) { + Log.v("TAG", exception.getMessage()); + } + }); + + + + + + +// final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); +// if (takePictureIntent.resolveActivity(requireContext().getPackageManager()) != null) { +// Long localId = viewModel.getFullCard().getLocalId(); +// File tempFile = new File(requireContext().getApplicationContext().getFilesDir().getAbsolutePath() + "/attachments/account-" + viewModel.getFullCard().getAccountId() + "/card-" + (localId == null ? "pending-creation" : localId) + '/' + photoFileName); +// +// Uri photoURI = FileProvider.getUriForFile(requireContext(), +// BuildConfig.APPLICATION_ID + ".fileprovider", +// tempFile); +// takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); +// startActivityForResult(takePictureIntent, REQUEST_CODE_ADD_FILE); +// } } @Override @@ -215,7 +239,6 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme } break; } - case REQUEST_CODE_CAPTURE_IMAGE: case REQUEST_CODE_ADD_FILE: { if (resultCode == RESULT_OK) { try { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPicker.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPicker.java index f8626d5ff..e0cfeabb2 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPicker.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPicker.java @@ -14,6 +14,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -24,6 +25,7 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import java.util.stream.Stream; +import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.DialogAttachmentPickerBinding; import it.niedermann.nextcloud.deck.ui.branding.Branded; import it.niedermann.nextcloud.deck.util.DeckColorUtil; @@ -86,8 +88,12 @@ public class CardAttachmentPicker extends BottomSheetDialogFragment implements B public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); binding.pickCamera.setOnClickListener((v) -> { - listener.pickCamera(); - dismiss(); + if (SDK_INT >= LOLLIPOP) { + listener.pickCamera(); + dismiss(); + } else { + Toast.makeText(requireContext(), R.string.min_api_21, Toast.LENGTH_SHORT).show(); + } }); binding.pickContact.setOnClickListener((v) -> { listener.pickContact(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPickerListener.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPickerListener.java index 37e590918..af61bba5c 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPickerListener.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/picker/CardAttachmentPickerListener.java @@ -1,7 +1,12 @@ package it.niedermann.nextcloud.deck.ui.card.attachments.picker; +import androidx.annotation.RequiresApi; + +import static android.os.Build.VERSION_CODES.LOLLIPOP; + public interface CardAttachmentPickerListener { + @RequiresApi(api = LOLLIPOP) void pickCamera(); void pickContact(); void pickFile(); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java index f349561de..c68def2de 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java @@ -59,14 +59,13 @@ public class AttachmentUtil { } public static File copyContentUriToTempFile(@NonNull Context context, @NonNull Uri currentUri, long accountId, Long localCardId) throws IOException, IllegalArgumentException { - String fullTempPath = context.getApplicationContext().getFilesDir().getAbsolutePath() + "/attachments/account-" + accountId + "/card-" + (localCardId == null ? "pending-creation" : localCardId) + '/' + UriUtils.getDisplayNameForUri(currentUri, context); - DeckLog.verbose("----- fullTempPath: " + fullTempPath); - InputStream inputStream = context.getContentResolver().openInputStream(currentUri); + final InputStream inputStream = context.getContentResolver().openInputStream(currentUri); if (inputStream == null) { throw new IOException("Could not open input stream for " + currentUri.getPath()); } - File cacheFile = new File(fullTempPath); - File tempDir = cacheFile.getParentFile(); + final File cacheFile = getTempCacheFile(context, accountId, localCardId, UriUtils.getDisplayNameForUri(currentUri, context)); + DeckLog.verbose("----- fullTempPath: " + cacheFile.getAbsolutePath()); + final File tempDir = cacheFile.getParentFile(); if (tempDir == null) { throw new FileNotFoundException("could not cacheFile.getParentFile()"); } @@ -78,7 +77,7 @@ public class AttachmentUtil { if (!cacheFile.createNewFile()) { throw new IOException("Failed to create cacheFile"); } - final FileOutputStream outputStream = new FileOutputStream(fullTempPath); + final FileOutputStream outputStream = new FileOutputStream(cacheFile); byte[] buffer = new byte[4096]; int count; @@ -88,4 +87,8 @@ public class AttachmentUtil { DeckLog.verbose("----- wrote"); return cacheFile; } + + public static File getTempCacheFile(@NonNull Context context, long accountId, Long localCardId, String fileName) { + return new File(context.getApplicationContext().getFilesDir().getAbsolutePath() + "/attachments/account-" + accountId + "/card-" + (localCardId == null ? "pending-creation" : localCardId) + '/' + fileName); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2d3b16cda..fddc5ef59 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -314,4 +314,5 @@ <string name="simple_contact">Contact</string> <string name="simple_file">File</string> <string name="simple_camera">Camera</string> + <string name="min_api_21">This feature requires at least Android 5</string> </resources> |