Do not use deprecated Activity.startActivityForResult anymore - step 3
This commit is contained in:
parent
a80f1538c7
commit
e224ae62b4
|
@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerAudioType
|
|||
/**
|
||||
* 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).
|
||||
|
|
|
@ -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<Intent>): Uri? {
|
||||
|
|
|
@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerContactType
|
|||
/**
|
||||
* Contact Picker implementation
|
||||
*/
|
||||
class ContactPicker(override val requestCode: Int) : Picker<MultiPickerContactType>(requestCode) {
|
||||
class ContactPicker : Picker<MultiPickerContactType>() {
|
||||
|
||||
/**
|
||||
* Call this function from onActivityResult(int, int, Intent).
|
||||
|
|
|
@ -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<MultiPickerFileType>(requestCode) {
|
||||
class FilePicker : Picker<MultiPickerFileType>() {
|
||||
|
||||
/**
|
||||
* Call this function from onActivityResult(int, int, Intent).
|
||||
|
|
|
@ -25,7 +25,7 @@ import im.vector.lib.multipicker.utils.ImageUtils
|
|||
/**
|
||||
* Image Picker implementation
|
||||
*/
|
||||
class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(requestCode) {
|
||||
class ImagePicker : Picker<MultiPickerImageType>() {
|
||||
|
||||
/**
|
||||
* Call this function from onActivityResult(int, int, Intent).
|
||||
|
|
|
@ -26,23 +26,16 @@ class MultiPicker<T> {
|
|||
val CONTACT by lazy { MultiPicker<ContactPicker>() }
|
||||
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")
|
||||
fun <T> get(type: MultiPicker<T>): 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T>(open val requestCode: Int) {
|
||||
abstract class Picker<T> {
|
||||
|
||||
protected var single = false
|
||||
|
||||
|
@ -73,15 +72,7 @@ abstract class Picker<T>(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<Intent>) {
|
||||
|
|
|
@ -25,7 +25,7 @@ import im.vector.lib.multipicker.entity.MultiPickerVideoType
|
|||
/**
|
||||
* Video Picker implementation
|
||||
*/
|
||||
class VideoPicker(override val requestCode: Int) : Picker<MultiPickerVideoType>(requestCode) {
|
||||
class VideoPicker : Picker<MultiPickerVideoType>() {
|
||||
|
||||
/**
|
||||
* Call this function from onActivityResult(int, int, Intent).
|
||||
|
|
|
@ -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]}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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<out String>, grantResults: IntArray) {
|
||||
|
|
|
@ -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<Intent>) {
|
||||
activityResultLauncher.launch(Intent(activity, QrCodeScannerActivity::class.java))
|
||||
}
|
||||
|
|
|
@ -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?) {
|
||||
|
|
|
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue