-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement onShowFileChooser, calling delegate (#124)
- Loading branch information
Showing
17 changed files
with
249 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
.../java/com/shopify/checkout_sdk_mobile_buy_integration_sample/FileChooserResultContract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright 2023-present, Shopify Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.shopify.checkout_sdk_mobile_buy_integration_sample | ||
|
||
import android.app.Activity.RESULT_OK | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
import android.webkit.WebChromeClient.FileChooserParams | ||
import androidx.activity.result.contract.ActivityResultContract | ||
import androidx.core.content.FileProvider | ||
import java.io.File | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
/** | ||
* For handling 3p apps that require a FileChooser / Camera in response to onShowFileChooser() | ||
*/ | ||
class FileChooserResultContract : ActivityResultContract<FileChooserParams, Uri?>() { | ||
private var cameraImageUri: Uri? = null | ||
|
||
override fun createIntent(context: Context, input: FileChooserParams): Intent { | ||
val fileChooserIntent = input.createIntent() | ||
fileChooserIntent.addCategory(Intent.CATEGORY_OPENABLE) | ||
var mimeType = if (input.acceptTypes == null) DEFAULT_MIME_TYPE else input.acceptTypes[0] | ||
if (!ACCEPTABLE_MIME_TYPES.contains(mimeType)) { | ||
mimeType = DEFAULT_MIME_TYPE | ||
} | ||
fileChooserIntent.setType(mimeType) | ||
|
||
val photoFile = createImageFile(context) | ||
cameraImageUri = FileProvider.getUriForFile(context, "${context.packageName}.provider", photoFile) | ||
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply { | ||
putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri) | ||
} | ||
|
||
val chooserIntent = Intent.createChooser(fileChooserIntent, context.getText(R.string.filechooser_title)) | ||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(cameraIntent)) | ||
return chooserIntent | ||
} | ||
|
||
override fun parseResult(resultCode: Int, intent: Intent?) : Uri? { | ||
if (resultCode != RESULT_OK) { | ||
return null | ||
} | ||
|
||
return intent?.data ?: cameraImageUri | ||
} | ||
|
||
private fun createImageFile(context: Context): File { | ||
val timeStamp = SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US).format(Date()) | ||
val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) | ||
return File.createTempFile("$IMG_FILE_PREFIX${timeStamp}_", IMG_FILE_SUFFIX, storageDir) | ||
} | ||
|
||
companion object { | ||
private val ACCEPTABLE_MIME_TYPES = arrayListOf("image/*", "video/*") | ||
private const val DEFAULT_MIME_TYPE = "*/*" | ||
private const val DATE_FORMAT_PATTERN = "yyyyMMdd_HHmmss" | ||
private const val IMG_FILE_PREFIX = "JPEG_" | ||
private const val IMG_FILE_SUFFIX = ".jpg" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.