diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt index e796137af2..516022100d 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/AudioPicker.kt @@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerAudioType /** * Audio file picker implementation */ -class AudioPicker(override val requestCode: Int) : Picker(requestCode) { +class AudioPicker : Picker() { /** * Call this function from onActivityResult(int, int, Intent). diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/CameraPicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/CameraPicker.kt index 319bc31af9..e70e3c574f 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/CameraPicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/CameraPicker.kt @@ -33,24 +33,10 @@ import java.util.Locale /** * Implementation of taking a photo with Camera */ -class CameraPicker(val requestCode: Int) { +class CameraPicker { /** - * Start camera by using an Activity - * @param activity Activity to handle onActivityResult(). - * @return Uri of taken photo or null if the operation is cancelled. - */ - fun startWithExpectingFile(activity: Activity): Uri? { - val photoUri = createPhotoUri(activity) - val intent = createIntent().apply { - putExtra(MediaStore.EXTRA_OUTPUT, photoUri) - } - activity.startActivityForResult(intent, requestCode) - return photoUri - } - - /** - * Start camera by using a Fragment + * Start camera by using a ActivityResultLauncher * @return Uri of taken photo or null if the operation is cancelled. */ fun startWithExpectingFile(context: Context, activityResultLauncher: ActivityResultLauncher): Uri? { diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt index 5eb8a98999..315fe6cbf2 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/ContactPicker.kt @@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerContactType /** * Contact Picker implementation */ -class ContactPicker(override val requestCode: Int) : Picker(requestCode) { +class ContactPicker : Picker() { /** * Call this function from onActivityResult(int, int, Intent). diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt index 7510d06b2b..39bd93d03e 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/FilePicker.kt @@ -24,7 +24,7 @@ import im.vector.lib.multipicker.entity.MultiPickerFileType /** * Implementation of selecting any type of files */ -class FilePicker(override val requestCode: Int) : Picker(requestCode) { +class FilePicker : Picker() { /** * Call this function from onActivityResult(int, int, Intent). diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt index 054bfba365..ce73058039 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/ImagePicker.kt @@ -25,7 +25,7 @@ import im.vector.lib.multipicker.utils.ImageUtils /** * Image Picker implementation */ -class ImagePicker(override val requestCode: Int) : Picker(requestCode) { +class ImagePicker : Picker() { /** * Call this function from onActivityResult(int, int, Intent). diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/MultiPicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/MultiPicker.kt index d28dcf9586..7e639a9bd3 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/MultiPicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/MultiPicker.kt @@ -26,23 +26,16 @@ class MultiPicker { val CONTACT by lazy { MultiPicker() } val CAMERA by lazy { MultiPicker() } - const val REQUEST_CODE_PICK_IMAGE = 5000 - const val REQUEST_CODE_PICK_VIDEO = 5001 - const val REQUEST_CODE_PICK_FILE = 5002 - const val REQUEST_CODE_PICK_AUDIO = 5003 - const val REQUEST_CODE_PICK_CONTACT = 5004 - const val REQUEST_CODE_TAKE_PHOTO = 5005 - @Suppress("UNCHECKED_CAST") fun get(type: MultiPicker): T { return when (type) { - IMAGE -> ImagePicker(REQUEST_CODE_PICK_IMAGE) as T - VIDEO -> VideoPicker(REQUEST_CODE_PICK_VIDEO) as T - FILE -> FilePicker(REQUEST_CODE_PICK_FILE) as T - AUDIO -> AudioPicker(REQUEST_CODE_PICK_AUDIO) as T - CONTACT -> ContactPicker(REQUEST_CODE_PICK_CONTACT) as T - CAMERA -> CameraPicker(REQUEST_CODE_TAKE_PHOTO) as T - else -> throw IllegalArgumentException("Unsupported type $type") + IMAGE -> ImagePicker() as T + VIDEO -> VideoPicker() as T + FILE -> FilePicker() as T + AUDIO -> AudioPicker() as T + CONTACT -> ContactPicker() as T + CAMERA -> CameraPicker() as T + else -> throw IllegalArgumentException("Unsupported type $type") } } } diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt index 33da23e2b6..ba765a3b1d 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/Picker.kt @@ -16,7 +16,6 @@ package im.vector.lib.multipicker -import android.app.Activity import android.content.Context import android.content.Intent import android.content.pm.PackageManager @@ -27,7 +26,7 @@ import androidx.activity.result.ActivityResultLauncher /** * Abstract class to provide all types of Pickers */ -abstract class Picker(open val requestCode: Int) { +abstract class Picker { protected var single = false @@ -73,15 +72,7 @@ abstract class Picker(open val requestCode: Int) { abstract fun createIntent(): Intent /** - * Start Storage Access Framework UI by using an Activity. - * @param activity Activity to handle onActivityResult(). - */ - fun startWith(activity: Activity) { - activity.startActivityForResult(createIntent().apply { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }, requestCode) - } - - /** - * Start Storage Access Framework UI by using a Fragment. + * Start Storage Access Framework UI by using a ActivityResultLauncher. * @param activityResultLauncher to handle the result. */ fun startWith(activityResultLauncher: ActivityResultLauncher) { diff --git a/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt b/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt index c1d1f74aab..c7c06f795f 100644 --- a/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt +++ b/multipicker/src/main/java/im/vector/lib/multipicker/VideoPicker.kt @@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerVideoType /** * Video Picker implementation */ -class VideoPicker(override val requestCode: Int) : Picker(requestCode) { +class VideoPicker : Picker() { /** * Call this function from onActivityResult(int, int, Intent). diff --git a/vector/src/debug/java/im/vector/app/features/debug/DebugMenuActivity.kt b/vector/src/debug/java/im/vector/app/features/debug/DebugMenuActivity.kt index 5590e19c10..9cca462d1a 100644 --- a/vector/src/debug/java/im/vector/app/features/debug/DebugMenuActivity.kt +++ b/vector/src/debug/java/im/vector/app/features/debug/DebugMenuActivity.kt @@ -28,6 +28,7 @@ import butterknife.OnClick import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ScreenComponent +import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.utils.PERMISSIONS_FOR_TAKING_PHOTO import im.vector.app.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA @@ -196,33 +197,29 @@ class DebugMenuActivity : VectorBaseActivity() { } private fun doScanQRCode() { - QrCodeScannerActivity.startForResult(this) + QrCodeScannerActivity.startForResult(this, qrStartForActivityResult) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - if (resultCode == Activity.RESULT_OK) { - when (requestCode) { - QrCodeScannerActivity.QR_CODE_SCANNER_REQUEST_CODE -> { - toast("QrCode: " + QrCodeScannerActivity.getResultText(data) + " is QRCode: " + QrCodeScannerActivity.getResultIsQrCode(data)) + private val qrStartForActivityResult = registerStartForActivityResult { activityResult -> + if (activityResult.resultCode == Activity.RESULT_OK) { + toast("QrCode: " + QrCodeScannerActivity.getResultText(activityResult.data) + + " is QRCode: " + QrCodeScannerActivity.getResultIsQrCode(activityResult.data)) - // Also update the current QR Code (reverse operation) - // renderQrCode(QrCodeScannerActivity.getResultText(data) ?: "") - val result = QrCodeScannerActivity.getResultText(data)!! + // Also update the current QR Code (reverse operation) + // renderQrCode(QrCodeScannerActivity.getResultText(data) ?: "") + val result = QrCodeScannerActivity.getResultText(activityResult.data)!! - val qrCodeData = result.toQrCodeData() - Timber.e("qrCodeData: $qrCodeData") + val qrCodeData = result.toQrCodeData() + Timber.e("qrCodeData: $qrCodeData") - if (result.length != buffer.size) { - Timber.e("Error, length are not the same") - } else { - // Convert to ByteArray - val byteArrayResult = result.toByteArray(Charsets.ISO_8859_1) - for (i in byteArrayResult.indices) { - if (buffer[i] != byteArrayResult[i]) { - Timber.e("Error for byte $i, expecting ${buffer[i]} and get ${byteArrayResult[i]}") - } - } + if (result.length != buffer.size) { + Timber.e("Error, length are not the same") + } else { + // Convert to ByteArray + val byteArrayResult = result.toByteArray(Charsets.ISO_8859_1) + for (i in byteArrayResult.indices) { + if (buffer[i] != byteArrayResult[i]) { + Timber.e("Error for byte $i, expecting ${buffer[i]} and get ${byteArrayResult[i]}") } } } diff --git a/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt b/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt index 387cfb2261..f6a6e3abcf 100644 --- a/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt +++ b/vector/src/main/java/im/vector/app/features/attachments/preview/AttachmentsPreviewFragment.kt @@ -81,6 +81,10 @@ class AttachmentsPreviewFragment @Inject constructor( } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + // TODO handle this one (Ucrop lib) + @Suppress("DEPRECATION") + super.onActivityResult(requestCode, resultCode, data) + if (resultCode == RESULT_OK) { if (requestCode == UCrop.REQUEST_CROP && data != null) { Timber.v("Crop success") diff --git a/vector/src/main/java/im/vector/app/features/media/BigImageViewerActivity.kt b/vector/src/main/java/im/vector/app/features/media/BigImageViewerActivity.kt index 04fd6e3850..81d6f1f996 100644 --- a/vector/src/main/java/im/vector/app/features/media/BigImageViewerActivity.kt +++ b/vector/src/main/java/im/vector/app/features/media/BigImageViewerActivity.kt @@ -30,6 +30,7 @@ import com.yalantis.ucrop.UCrop import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ScreenComponent +import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.StringProvider @@ -112,10 +113,10 @@ class BigImageViewerActivity : VectorBaseActivity() { private fun onAvatarTypeSelected(isCamera: Boolean) { if (isCamera) { if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, this, PERMISSION_REQUEST_CODE_LAUNCH_CAMERA)) { - avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(this) + avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(this, takePhotoActivityResultLauncher) } } else { - MultiPicker.get(MultiPicker.IMAGE).single().startWith(this) + MultiPicker.get(MultiPicker.IMAGE).single().startWith(pickImageActivityResultLauncher) } } @@ -127,30 +128,39 @@ class BigImageViewerActivity : VectorBaseActivity() { .start(this) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (resultCode == Activity.RESULT_OK) { - when (requestCode) { - MultiPicker.REQUEST_CODE_TAKE_PHOTO -> { - avatarCameraUri?.let { uri -> - MultiPicker.get(MultiPicker.CAMERA) - .getTakenPhoto(this, uri) - ?.let { - onRoomAvatarSelected(it) - } - } - } - MultiPicker.REQUEST_CODE_PICK_IMAGE -> { - MultiPicker - .get(MultiPicker.IMAGE) - .getSelectedFiles(this, data) - .firstOrNull()?.let { - onRoomAvatarSelected(it) - } - } - UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } + private val takePhotoActivityResultLauncher = registerStartForActivityResult { activityResult -> + if (activityResult.resultCode == Activity.RESULT_OK) { + avatarCameraUri?.let { uri -> + MultiPicker.get(MultiPicker.CAMERA) + .getTakenPhoto(this, uri) + ?.let { + onRoomAvatarSelected(it) + } } } + } + + private val pickImageActivityResultLauncher = registerStartForActivityResult { activityResult -> + if (activityResult.resultCode == Activity.RESULT_OK) { + MultiPicker + .get(MultiPicker.IMAGE) + .getSelectedFiles(this, activityResult.data) + .firstOrNull()?.let { + onRoomAvatarSelected(it) + } + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + // TODO handle this one (Ucrop lib) + @Suppress("DEPRECATION") super.onActivityResult(requestCode, resultCode, data) + + if (resultCode == Activity.RESULT_OK) { + when (requestCode) { + UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } + } + } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { diff --git a/vector/src/main/java/im/vector/app/features/qrcode/QrCodeScannerActivity.kt b/vector/src/main/java/im/vector/app/features/qrcode/QrCodeScannerActivity.kt index 563f9c65fa..8a5126a160 100644 --- a/vector/src/main/java/im/vector/app/features/qrcode/QrCodeScannerActivity.kt +++ b/vector/src/main/java/im/vector/app/features/qrcode/QrCodeScannerActivity.kt @@ -75,13 +75,6 @@ class QrCodeScannerActivity : VectorBaseActivity() { private const val EXTRA_OUT_TEXT = "EXTRA_OUT_TEXT" private const val EXTRA_OUT_IS_QR_CODE = "EXTRA_OUT_IS_QR_CODE" - const val QR_CODE_SCANNER_REQUEST_CODE = 429 - - // For test only - fun startForResult(activity: Activity, requestCode: Int = QR_CODE_SCANNER_REQUEST_CODE) { - activity.startActivityForResult(Intent(activity, QrCodeScannerActivity::class.java), requestCode) - } - fun startForResult(activity: Activity, activityResultLauncher: ActivityResultLauncher) { activityResultLauncher.launch(Intent(activity, QrCodeScannerActivity::class.java)) } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileFragment.kt b/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileFragment.kt index 2b67624ed1..399c1ecf32 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileFragment.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/RoomProfileFragment.kt @@ -345,14 +345,15 @@ class RoomProfileFragment @Inject constructor( } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + // TODO handle this one (Ucrop lib) + @Suppress("DEPRECATION") + super.onActivityResult(requestCode, resultCode, data) + if (resultCode == Activity.RESULT_OK) { when (requestCode) { UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } } } - // TODO handle this one (Ucrop lib) - @Suppress("DEPRECATION") - super.onActivityResult(requestCode, resultCode, data) } private fun onAvatarCropped(uri: Uri?) { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt index 9928325289..3f7ddbb9c8 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsGeneralFragment.kt @@ -304,12 +304,12 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + // TODO handle this one (Ucrop lib) @Suppress("DEPRECATION") super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) { when (requestCode) { - // TODO Migrate this too UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt index 5f3a0e32ca..fdff5253d5 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt @@ -128,7 +128,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( // Even if using foreground service with foreground notif, it stops to work // in doze mode for certain devices :/ if (!isIgnoringBatteryOptimizations(requireContext())) { - requestDisablingBatteryOptimization(requireActivity(), batteryStartForActivityResult, 0) + requestDisablingBatteryOptimization(requireActivity(), batteryStartForActivityResult) } } vectorPreferences.setFdroidSyncBackgroundMode(mode)