Skip to content

Commit c5bc2cc

Browse files
committed
permissionGranted helper
1 parent 8dd95b0 commit c5bc2cc

File tree

1 file changed

+39
-33
lines changed
  • samples/MobileBuyIntegration/app/src/main/java/com/shopify/checkout_sdk_mobile_buy_integration_sample

1 file changed

+39
-33
lines changed
Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* MIT License
3-
*
3+
*
44
* Copyright 2023-present, Shopify Inc.
5-
*
5+
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
88
* in the Software without restriction, including without limitation the rights
99
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
* copies of the Software, and to permit persons to whom the Software is
1111
* furnished to do so, subject to the following conditions:
12-
*
12+
*
1313
* The above copyright notice and this permission notice shall be included in all
1414
* copies or substantial portions of the Software.
15-
*
15+
*
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -25,17 +25,14 @@ package com.shopify.checkout_sdk_mobile_buy_integration_sample
2525
import android.Manifest
2626
import android.content.pm.PackageManager
2727
import android.net.Uri
28-
import android.os.Build
2928
import android.os.Bundle
30-
import android.webkit.GeolocationPermissions
3129
import android.webkit.ValueCallback
3230
import android.webkit.WebChromeClient.FileChooserParams
3331
import android.webkit.WebView.setWebContentsDebuggingEnabled
3432
import androidx.activity.ComponentActivity
3533
import androidx.activity.compose.setContent
3634
import androidx.activity.result.ActivityResultLauncher
3735
import androidx.activity.result.contract.ActivityResultContracts
38-
import androidx.core.content.ContextCompat
3936
import timber.log.Timber
4037
import timber.log.Timber.DebugTree
4138

@@ -44,7 +41,8 @@ class MainActivity : ComponentActivity() {
4441
private lateinit var requestPermissionLauncher: ActivityResultLauncher<String>
4542
private lateinit var showFileChooserLauncher: ActivityResultLauncher<FileChooserParams>
4643

47-
// State related to file chooser requests (e.g. for using a file chooser/camera for proving identity)
44+
// State related to file chooser requests (e.g. for using a file chooser/camera for proving
45+
// identity)
4846
private var filePathCallback: ValueCallback<Array<Uri>>? = null
4947
private var fileChooserParams: FileChooserParams? = null
5048

@@ -63,45 +61,53 @@ class MainActivity : ComponentActivity() {
6361
Timber.plant(DebugTree())
6462
}
6563

66-
setContent {
67-
CheckoutSdkApp()
68-
}
64+
setContent { CheckoutSdkApp() }
6965

70-
requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
71-
val fileChooserParams = this.fileChooserParams
72-
if (isGranted && fileChooserParams != null) {
73-
this.showFileChooserLauncher.launch(fileChooserParams)
74-
this.fileChooserParams = null
75-
}
76-
// N.B. a file chooser intent (without camera) could be launched here if the permission was denied
77-
}
66+
requestPermissionLauncher =
67+
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted
68+
->
69+
val fileChooserParams = this.fileChooserParams
70+
if (isGranted && fileChooserParams != null) {
71+
this.showFileChooserLauncher.launch(fileChooserParams)
72+
this.fileChooserParams = null
73+
}
74+
// N.B. a file chooser intent (without camera) could be launched here if the
75+
// permission was denied
76+
}
7877

79-
showFileChooserLauncher = registerForActivityResult(FileChooserResultContract()) { uri: Uri? ->
80-
// invoke the callback with the selected file
81-
filePathCallback?.onReceiveValue(if (uri != null) arrayOf(uri) else null)
78+
showFileChooserLauncher =
79+
registerForActivityResult(FileChooserResultContract()) { uri: Uri? ->
80+
// invoke the callback with the selected file
81+
filePathCallback?.onReceiveValue(if (uri != null) arrayOf(uri) else null)
8282

83-
// reset fileChooser state
84-
filePathCallback = null
85-
fileChooserParams = null
86-
}
83+
// reset fileChooser state
84+
filePathCallback = null
85+
fileChooserParams = null
86+
}
8787
}
8888

8989
fun requestGeolocationPermission() {
90-
val fineLocationGranted = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
91-
val coarseLocationGranted = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
90+
val fineLocationGranted = permissionGranted(Manifest.permission.ACCESS_FINE_LOCATION)
91+
val coarseLocationGranted = permissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION)
9292

9393
if (!fineLocationGranted && !coarseLocationGranted) {
9494
requestPermissions(
95-
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION),
95+
arrayOf(
96+
Manifest.permission.ACCESS_FINE_LOCATION,
97+
Manifest.permission.ACCESS_COARSE_LOCATION
98+
),
9699
LOCATION_PERMISSION_REQUEST_CODE
97100
)
98101
}
99102
}
100103

101104
// Show a file chooser when prompted by the event processor
102-
fun onShowFileChooser(filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: FileChooserParams): Boolean {
105+
fun onShowFileChooser(
106+
filePathCallback: ValueCallback<Array<Uri>>,
107+
fileChooserParams: FileChooserParams
108+
): Boolean {
103109
this.filePathCallback = filePathCallback
104-
if (permissionAlreadyGranted(Manifest.permission.CAMERA)) {
110+
if (permissionGranted(Manifest.permission.CAMERA)) {
105111
// Permissions already granted, launch chooser immediately
106112
showFileChooserLauncher.launch(fileChooserParams)
107113
this.fileChooserParams = null
@@ -113,7 +119,7 @@ class MainActivity : ComponentActivity() {
113119
return true
114120
}
115121

116-
private fun permissionAlreadyGranted(permission: String): Boolean {
117-
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
122+
private fun permissionGranted(permission: String): Boolean {
123+
return checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
118124
}
119125
}

0 commit comments

Comments
 (0)