From e849ce3004ccd1132a121cf513bbcb7996d95c30 Mon Sep 17 00:00:00 2001 From: Sergey Vasilinets Date: Sat, 1 Jul 2023 13:36:11 +0100 Subject: [PATCH] Use ActivityContracts in WorkManagerSample bug: 208344986 Test: Manual --- WorkManagerSample/app/build.gradle | 1 + .../example/background/SelectImageActivity.kt | 43 +++++-------------- WorkManagerSample/versions.gradle | 2 +- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/WorkManagerSample/app/build.gradle b/WorkManagerSample/app/build.gradle index 690123123..ebe096d56 100644 --- a/WorkManagerSample/app/build.gradle +++ b/WorkManagerSample/app/build.gradle @@ -53,6 +53,7 @@ dependencies { implementation project(path: ':lib') implementation deps.kotlin.stdlib + implementation deps.activity.activity_ktx implementation deps.work.runtime_ktx implementation deps.app_compat implementation deps.cardview diff --git a/WorkManagerSample/app/src/main/java/com/example/background/SelectImageActivity.kt b/WorkManagerSample/app/src/main/java/com/example/background/SelectImageActivity.kt index a20170853..58938167a 100644 --- a/WorkManagerSample/app/src/main/java/com/example/background/SelectImageActivity.kt +++ b/WorkManagerSample/app/src/main/java/com/example/background/SelectImageActivity.kt @@ -17,8 +17,6 @@ package com.example.background import android.Manifest -import android.app.Activity -import android.content.Intent import android.content.pm.PackageManager import android.os.Build import android.os.Bundle @@ -28,13 +26,15 @@ import android.text.Spanned import android.text.method.LinkMovementMethod import android.util.Log import android.view.View +import androidx.activity.result.PickVisualMediaRequest +import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia +import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageOnly import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import com.example.background.databinding.ActivitySelectBinding import com.google.android.material.snackbar.Snackbar -import java.util.ArrayList /** * Helps select an image for the [FilterActivity] and handles permission requests. @@ -46,6 +46,13 @@ class SelectImageActivity : AppCompatActivity() { private var permissionRequestCount = 0 private var hasPermissions = false + private val pickPictureCallback = registerForActivityResult(PickVisualMedia()) { uri -> + if (uri == null) + Log.e(TAG, "Invalid input image Uri.") + else + startActivity(FilterActivity.newIntent(this, uri)) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding = ActivitySelectBinding.inflate(layoutInflater).apply { @@ -69,11 +76,7 @@ class SelectImageActivity : AppCompatActivity() { requestPermissionsIfNecessary() binding.selectImage.setOnClickListener { - val chooseIntent = Intent( - Intent.ACTION_PICK, - MediaStore.Images.Media.EXTERNAL_CONTENT_URI - ) - startActivityForResult(chooseIntent, REQUEST_CODE_IMAGE) + pickPictureCallback.launch(PickVisualMediaRequest(ImageOnly)) } binding.selectStockImage.setOnClickListener { @@ -90,18 +93,6 @@ class SelectImageActivity : AppCompatActivity() { outState.putInt(KEY_PERMISSIONS_REQUEST_COUNT, permissionRequestCount) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - if (resultCode == Activity.RESULT_OK && data != null) { - when (requestCode) { - REQUEST_CODE_IMAGE -> handleImageRequestResult(data) - else -> Log.d(TAG, "Unknown request code.") - } - } else { - Log.e(TAG, String.format("Unexpected Result code \"%s\" or missing data.", resultCode)) - } - } - override fun onRequestPermissionsResult( requestCode: Int, permissions: Array, @@ -139,17 +130,6 @@ class SelectImageActivity : AppCompatActivity() { } } - private fun handleImageRequestResult(data: Intent) { - // Get the imageUri the user picked, from the Intent.ACTION_PICK result. - val imageUri = data.clipData!!.getItemAt(0).uri - - if (imageUri == null) { - Log.e(TAG, "Invalid input image Uri.") - return - } - startActivity(FilterActivity.newIntent(this, imageUri)) - } - private fun checkAllPermissions(): Boolean { var hasPermissions = true for (permission in sPermissions) { @@ -166,7 +146,6 @@ class SelectImageActivity : AppCompatActivity() { private const val KEY_PERMISSIONS_REQUEST_COUNT = "KEY_PERMISSIONS_REQUEST_COUNT" private const val MAX_NUMBER_REQUEST_PERMISSIONS = 2 - private const val REQUEST_CODE_IMAGE = 100 private const val REQUEST_CODE_PERMISSIONS = 101 // A list of permissions the application needs. diff --git a/WorkManagerSample/versions.gradle b/WorkManagerSample/versions.gradle index 633a89b40..eeb71e0d0 100644 --- a/WorkManagerSample/versions.gradle +++ b/WorkManagerSample/versions.gradle @@ -63,7 +63,7 @@ versions.rxjava2 = "2.2.21" versions.timber = "4.7.1" versions.transition = "1.4.1" versions.truth = "1.1.3" -versions.work = "2.7.1" +versions.work = "2.8.1" ext.versions = versions def build_versions = [:]