From 95b123465602d44b1a8b4e3921dd3f6feb6b0fbb Mon Sep 17 00:00:00 2001 From: Sinuhe Jaime Date: Mon, 14 Oct 2019 15:43:26 -0500 Subject: [PATCH 1/2] Adding 'when' to substitute if ALSO: * Removed the 'm' from 'mFilePathCallback'. The hungarian notation is not recommended anywhere, only on the AOSP but not for kotlin files. Here's a good discussion about it: https://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation\#112080 --- .../view/main/view/MainActivity.kt | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt b/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt index 71cea49..94d815c 100644 --- a/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt +++ b/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt @@ -6,7 +6,7 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.webkit.ValueCallback -import androidx.core.app.ActivityCompat +import androidx.core.app.ActivityCompat.startActivityForResult import to.dev.dev_android.R import to.dev.dev_android.base.BuildConfig import to.dev.dev_android.base.activity.BaseActivity @@ -14,7 +14,7 @@ import to.dev.dev_android.databinding.ActivityMainBinding class MainActivity : BaseActivity(), CustomWebChromeClient.CustomListener { - private var mFilePathCallback: ValueCallback>? = null + private var filePathCallback: ValueCallback>? = null override fun layout(): Int { return R.layout.activity_main @@ -60,15 +60,16 @@ class MainActivity : BaseActivity(), CustomWebChromeClient. } override fun launchGallery(filePathCallback: ValueCallback>?) { - mFilePathCallback = filePathCallback + this.filePathCallback = filePathCallback - val galleryIntent = Intent() - // Show only images, no videos or anything else - galleryIntent.type = "image/*" - galleryIntent.action = Intent.ACTION_PICK + val galleryIntent = Intent().apply { + // Show only images, no videos or anything else + type = "image/*" + action = Intent.ACTION_PICK + } // Always show the chooser (if there are multiple options available) - ActivityCompat.startActivityForResult( + startActivityForResult( this, Intent.createChooser(galleryIntent, "Select Picture"), PIC_CHOOSER_REQUEST, @@ -81,14 +82,15 @@ class MainActivity : BaseActivity(), CustomWebChromeClient. return super.onActivityResult(requestCode, resultCode, data) } - if (resultCode == Activity.RESULT_OK) { - if (data != null) { - mFilePathCallback?.onReceiveValue(arrayOf(data.data)) - mFilePathCallback = null + when (resultCode) { + Activity.RESULT_OK -> data?.data?.let { + filePathCallback?.onReceiveValue(arrayOf(it)) + filePathCallback = null + } + Activity.RESULT_CANCELED -> { + filePathCallback?.onReceiveValue(null) + filePathCallback = null } - } else if (resultCode == Activity.RESULT_CANCELED) { - mFilePathCallback?.onReceiveValue(null) - mFilePathCallback = null } } From 799c6c7058dd86f0995a2b606341d8214f8a4b2c Mon Sep 17 00:00:00 2001 From: Sinuhe Jaime Date: Thu, 17 Oct 2019 11:36:52 -0500 Subject: [PATCH 2/2] Removing redundant usage of ActivityCompat The ActivityCompat is a helper class to validate versions of the SDK and remove that code from the developer side. It's not needed in this case as the minSDK and the targetSDK are pointing to highe values than 16. The method startActivityForResult on this helper class checks for SDK >= 16. --- app/src/main/AndroidManifest.xml | 26 +++++++++---------- .../view/main/view/MainActivity.kt | 2 -- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c6631d8..b4d3c69 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,37 +1,37 @@ + xmlns:tools="http://schemas.android.com/tools" + package="to.dev.dev_android"> - + + android:usesCleartextTraffic="true" + tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"> - + - + - - - + + + + + + android:scheme="https" /> diff --git a/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt b/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt index 94d815c..ab42f14 100644 --- a/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt +++ b/app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt @@ -6,7 +6,6 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.webkit.ValueCallback -import androidx.core.app.ActivityCompat.startActivityForResult import to.dev.dev_android.R import to.dev.dev_android.base.BuildConfig import to.dev.dev_android.base.activity.BaseActivity @@ -70,7 +69,6 @@ class MainActivity : BaseActivity(), CustomWebChromeClient. // Always show the chooser (if there are multiple options available) startActivityForResult( - this, Intent.createChooser(galleryIntent, "Select Picture"), PIC_CHOOSER_REQUEST, null // No additional data