Do not use deprecated Activity.startActivityForResult anymore - step 3

This commit is contained in:
Benoit Marty 2020-10-07 18:27:25 +02:00
parent a80f1538c7
commit e224ae62b4
15 changed files with 78 additions and 103 deletions

View File

@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerAudioType
/** /**
* Audio file picker implementation * Audio file picker implementation
*/ */
class AudioPicker(override val requestCode: Int) : Picker<MultiPickerAudioType>(requestCode) { class AudioPicker : Picker<MultiPickerAudioType>() {
/** /**
* Call this function from onActivityResult(int, int, Intent). * Call this function from onActivityResult(int, int, Intent).

View File

@ -33,24 +33,10 @@ import java.util.Locale
/** /**
* Implementation of taking a photo with Camera * Implementation of taking a photo with Camera
*/ */
class CameraPicker(val requestCode: Int) { class CameraPicker {
/** /**
* Start camera by using an Activity * Start camera by using a ActivityResultLauncher
* @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
* @return Uri of taken photo or null if the operation is cancelled. * @return Uri of taken photo or null if the operation is cancelled.
*/ */
fun startWithExpectingFile(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>): Uri? { fun startWithExpectingFile(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>): Uri? {

View File

@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerContactType
/** /**
* Contact Picker implementation * Contact Picker implementation
*/ */
class ContactPicker(override val requestCode: Int) : Picker<MultiPickerContactType>(requestCode) { class ContactPicker : Picker<MultiPickerContactType>() {
/** /**
* Call this function from onActivityResult(int, int, Intent). * Call this function from onActivityResult(int, int, Intent).

View File

@ -24,7 +24,7 @@ import im.vector.lib.multipicker.entity.MultiPickerFileType
/** /**
* Implementation of selecting any type of files * Implementation of selecting any type of files
*/ */
class FilePicker(override val requestCode: Int) : Picker<MultiPickerFileType>(requestCode) { class FilePicker : Picker<MultiPickerFileType>() {
/** /**
* Call this function from onActivityResult(int, int, Intent). * Call this function from onActivityResult(int, int, Intent).

View File

@ -25,7 +25,7 @@ import im.vector.lib.multipicker.utils.ImageUtils
/** /**
* Image Picker implementation * Image Picker implementation
*/ */
class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(requestCode) { class ImagePicker : Picker<MultiPickerImageType>() {
/** /**
* Call this function from onActivityResult(int, int, Intent). * Call this function from onActivityResult(int, int, Intent).

View File

@ -26,23 +26,16 @@ class MultiPicker<T> {
val CONTACT by lazy { MultiPicker<ContactPicker>() } val CONTACT by lazy { MultiPicker<ContactPicker>() }
val CAMERA by lazy { MultiPicker<CameraPicker>() } val CAMERA by lazy { MultiPicker<CameraPicker>() }
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") @Suppress("UNCHECKED_CAST")
fun <T> get(type: MultiPicker<T>): T { fun <T> get(type: MultiPicker<T>): T {
return when (type) { return when (type) {
IMAGE -> ImagePicker(REQUEST_CODE_PICK_IMAGE) as T IMAGE -> ImagePicker() as T
VIDEO -> VideoPicker(REQUEST_CODE_PICK_VIDEO) as T VIDEO -> VideoPicker() as T
FILE -> FilePicker(REQUEST_CODE_PICK_FILE) as T FILE -> FilePicker() as T
AUDIO -> AudioPicker(REQUEST_CODE_PICK_AUDIO) as T AUDIO -> AudioPicker() as T
CONTACT -> ContactPicker(REQUEST_CODE_PICK_CONTACT) as T CONTACT -> ContactPicker() as T
CAMERA -> CameraPicker(REQUEST_CODE_TAKE_PHOTO) as T CAMERA -> CameraPicker() as T
else -> throw IllegalArgumentException("Unsupported type $type") else -> throw IllegalArgumentException("Unsupported type $type")
} }
} }
} }

View File

@ -16,7 +16,6 @@
package im.vector.lib.multipicker package im.vector.lib.multipicker
import android.app.Activity
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
@ -27,7 +26,7 @@ import androidx.activity.result.ActivityResultLauncher
/** /**
* Abstract class to provide all types of Pickers * Abstract class to provide all types of Pickers
*/ */
abstract class Picker<T>(open val requestCode: Int) { abstract class Picker<T> {
protected var single = false protected var single = false
@ -73,15 +72,7 @@ abstract class Picker<T>(open val requestCode: Int) {
abstract fun createIntent(): Intent abstract fun createIntent(): Intent
/** /**
* Start Storage Access Framework UI by using an Activity. * Start Storage Access Framework UI by using a ActivityResultLauncher.
* @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.
* @param activityResultLauncher to handle the result. * @param activityResultLauncher to handle the result.
*/ */
fun startWith(activityResultLauncher: ActivityResultLauncher<Intent>) { fun startWith(activityResultLauncher: ActivityResultLauncher<Intent>) {

View File

@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerVideoType
/** /**
* Video Picker implementation * Video Picker implementation
*/ */
class VideoPicker(override val requestCode: Int) : Picker<MultiPickerVideoType>(requestCode) { class VideoPicker : Picker<MultiPickerVideoType>() {
/** /**
* Call this function from onActivityResult(int, int, Intent). * Call this function from onActivityResult(int, int, Intent).

View File

@ -28,6 +28,7 @@ import butterknife.OnClick
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.ScreenComponent 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.platform.VectorBaseActivity
import im.vector.app.core.utils.PERMISSIONS_FOR_TAKING_PHOTO import im.vector.app.core.utils.PERMISSIONS_FOR_TAKING_PHOTO
import im.vector.app.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA import im.vector.app.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA
@ -196,33 +197,29 @@ class DebugMenuActivity : VectorBaseActivity() {
} }
private fun doScanQRCode() { private fun doScanQRCode() {
QrCodeScannerActivity.startForResult(this) QrCodeScannerActivity.startForResult(this, qrStartForActivityResult)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { private val qrStartForActivityResult = registerStartForActivityResult { activityResult ->
super.onActivityResult(requestCode, resultCode, data) if (activityResult.resultCode == Activity.RESULT_OK) {
if (resultCode == Activity.RESULT_OK) { toast("QrCode: " + QrCodeScannerActivity.getResultText(activityResult.data)
when (requestCode) { + " is QRCode: " + QrCodeScannerActivity.getResultIsQrCode(activityResult.data))
QrCodeScannerActivity.QR_CODE_SCANNER_REQUEST_CODE -> {
toast("QrCode: " + QrCodeScannerActivity.getResultText(data) + " is QRCode: " + QrCodeScannerActivity.getResultIsQrCode(data))
// Also update the current QR Code (reverse operation) // Also update the current QR Code (reverse operation)
// renderQrCode(QrCodeScannerActivity.getResultText(data) ?: "") // renderQrCode(QrCodeScannerActivity.getResultText(data) ?: "")
val result = QrCodeScannerActivity.getResultText(data)!! val result = QrCodeScannerActivity.getResultText(activityResult.data)!!
val qrCodeData = result.toQrCodeData() val qrCodeData = result.toQrCodeData()
Timber.e("qrCodeData: $qrCodeData") Timber.e("qrCodeData: $qrCodeData")
if (result.length != buffer.size) { if (result.length != buffer.size) {
Timber.e("Error, length are not the same") Timber.e("Error, length are not the same")
} else { } else {
// Convert to ByteArray // Convert to ByteArray
val byteArrayResult = result.toByteArray(Charsets.ISO_8859_1) val byteArrayResult = result.toByteArray(Charsets.ISO_8859_1)
for (i in byteArrayResult.indices) { for (i in byteArrayResult.indices) {
if (buffer[i] != byteArrayResult[i]) { if (buffer[i] != byteArrayResult[i]) {
Timber.e("Error for byte $i, expecting ${buffer[i]} and get ${byteArrayResult[i]}") Timber.e("Error for byte $i, expecting ${buffer[i]} and get ${byteArrayResult[i]}")
}
}
} }
} }
} }

View File

@ -81,6 +81,10 @@ class AttachmentsPreviewFragment @Inject constructor(
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 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 (resultCode == RESULT_OK) {
if (requestCode == UCrop.REQUEST_CROP && data != null) { if (requestCode == UCrop.REQUEST_CROP && data != null) {
Timber.v("Crop success") Timber.v("Crop success")

View File

@ -30,6 +30,7 @@ import com.yalantis.ucrop.UCrop
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.ScreenComponent 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.platform.VectorBaseActivity
import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
@ -112,10 +113,10 @@ class BigImageViewerActivity : VectorBaseActivity() {
private fun onAvatarTypeSelected(isCamera: Boolean) { private fun onAvatarTypeSelected(isCamera: Boolean) {
if (isCamera) { if (isCamera) {
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, this, PERMISSION_REQUEST_CODE_LAUNCH_CAMERA)) { 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 { } else {
MultiPicker.get(MultiPicker.IMAGE).single().startWith(this) MultiPicker.get(MultiPicker.IMAGE).single().startWith(pickImageActivityResultLauncher)
} }
} }
@ -127,30 +128,39 @@ class BigImageViewerActivity : VectorBaseActivity() {
.start(this) .start(this)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { private val takePhotoActivityResultLauncher = registerStartForActivityResult { activityResult ->
if (resultCode == Activity.RESULT_OK) { if (activityResult.resultCode == Activity.RESULT_OK) {
when (requestCode) { avatarCameraUri?.let { uri ->
MultiPicker.REQUEST_CODE_TAKE_PHOTO -> { MultiPicker.get(MultiPicker.CAMERA)
avatarCameraUri?.let { uri -> .getTakenPhoto(this, uri)
MultiPicker.get(MultiPicker.CAMERA) ?.let {
.getTakenPhoto(this, uri) onRoomAvatarSelected(it)
?.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 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) 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<out String>, grantResults: IntArray) { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {

View File

@ -75,13 +75,6 @@ class QrCodeScannerActivity : VectorBaseActivity() {
private const val EXTRA_OUT_TEXT = "EXTRA_OUT_TEXT" private const val EXTRA_OUT_TEXT = "EXTRA_OUT_TEXT"
private const val EXTRA_OUT_IS_QR_CODE = "EXTRA_OUT_IS_QR_CODE" 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<Intent>) { fun startForResult(activity: Activity, activityResultLauncher: ActivityResultLauncher<Intent>) {
activityResultLauncher.launch(Intent(activity, QrCodeScannerActivity::class.java)) activityResultLauncher.launch(Intent(activity, QrCodeScannerActivity::class.java))
} }

View File

@ -345,14 +345,15 @@ class RoomProfileFragment @Inject constructor(
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 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) { if (resultCode == Activity.RESULT_OK) {
when (requestCode) { when (requestCode) {
UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } 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?) { private fun onAvatarCropped(uri: Uri?) {

View File

@ -304,12 +304,12 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() {
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// TODO handle this one (Ucrop lib)
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
when (requestCode) { when (requestCode) {
// TODO Migrate this too
UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) } UCrop.REQUEST_CROP -> data?.let { onAvatarCropped(UCrop.getOutput(it)) }
} }
} }

View File

@ -128,7 +128,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
// Even if using foreground service with foreground notif, it stops to work // Even if using foreground service with foreground notif, it stops to work
// in doze mode for certain devices :/ // in doze mode for certain devices :/
if (!isIgnoringBatteryOptimizations(requireContext())) { if (!isIgnoringBatteryOptimizations(requireContext())) {
requestDisablingBatteryOptimization(requireActivity(), batteryStartForActivityResult, 0) requestDisablingBatteryOptimization(requireActivity(), batteryStartForActivityResult)
} }
} }
vectorPreferences.setFdroidSyncBackgroundMode(mode) vectorPreferences.setFdroidSyncBackgroundMode(mode)