Skip to content

Commit f83af12

Browse files
committed
Camera permission handler
1 parent 9e501c2 commit f83af12

File tree

7 files changed

+127
-91
lines changed

7 files changed

+127
-91
lines changed

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Welcome to the documentation for the KinesteX SDK for Kotlin. This SDK allows yo
2323
Add the following permissions to your `AndroidManifest.xml`:
2424

2525
```xml
26+
<uses-feature
27+
android:name="android.hardware.camera"
28+
android:required="false" />
2629
<uses-permission android:name="android.permission.INTERNET" />
2730
<uses-permission android:name="android.permission.CAMERA" />
2831
```
@@ -45,7 +48,7 @@ dependencyResolutionManagement {
4548
Add the KinesteX SDK dependency in your app’s `build.gradle`:
4649

4750
```gradle
48-
implementation("com.github.KinesteX:KinesteXSDKKotlin:1.1.4")
51+
implementation("com.github.KinesteX:KinesteXSDKKotlin:1.1.5")
4952
5053
```
5154

@@ -58,8 +61,8 @@ implementation("com.github.KinesteX:KinesteXSDKKotlin:1.1.4")
5861
2. **Launching the view**: To display KinesteX, we will be using WebView. To launch Complete UX call `createMainView` in KinesteXSDK:
5962

6063
```kotlin
61-
private var kinesteXWebView: WebView? = null
62-
64+
private var kinesteXWebView: GenericWebView? = null
65+
6366
@SuppressLint("SetJavaScriptEnabled", "MissingInflatedId")
6467
override fun onCreate(savedInstanceState: Bundle?) {
6568
super.onCreate(savedInstanceState)
@@ -78,11 +81,25 @@ implementation("com.github.KinesteX:KinesteXSDKKotlin:1.1.4")
7881
customParams = data, // example of using custom parameters. CAN BE NULL
7982
viewModel.isLoading,
8083
::handleWebViewMessage
81-
)
84+
) as GenericWebView?
8285
}
8386
```
87+
88+
3. **Handling camera permission request**: We send a request for camera permissiona and it needs to be granted on app level:
89+
```kotlin
90+
override fun onRequestPermissionsResult(
91+
requestCode: Int,
92+
permissions: Array<out String>,
93+
grantResults: IntArray
94+
) {
95+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
96+
// handle the permission result on app level
97+
kinesteXWebView?.handlePermissionResult(requestCode, grantResults)
98+
}
99+
100+
```
84101

85-
3. **Handling the data**: Use a ViewModel to handle changes:
102+
4. **Handling the data**: Use a ViewModel to handle changes:
86103

87104
```kotlin
88105
class ContentViewModel : ViewModel() {
@@ -113,7 +130,7 @@ The KinesteX SDK provides multiple methods to create different views:
113130
customParams = data, // example of using custom parameters
114131
viewModel.isLoading,
115132
::handleWebViewMessage // callback function to handle responses
116-
)
133+
) as GenericWebView?
117134
```
118135

119136
- **Plan View (Stand-alone workout plan page)**:
@@ -129,7 +146,7 @@ The KinesteX SDK provides multiple methods to create different views:
129146
null, // custom parameters is null
130147
viewModel.isLoading,
131148
::handleWebViewMessage
132-
)
149+
) as GenericWebView?
133150
```
134151

135152
- **Workout View (Individual workout page)**:
@@ -144,7 +161,7 @@ The KinesteX SDK provides multiple methods to create different views:
144161
null,
145162
isLoading = viewModel.isLoading,
146163
onMessageReceived = ::handleWebViewMessage
147-
)
164+
) as GenericWebView?
148165
```
149166

150167
- **Challenge View**:
@@ -161,7 +178,7 @@ The KinesteX SDK provides multiple methods to create different views:
161178
customParams = null,
162179
viewModel.isLoading,
163180
::handleWebViewMessage
164-
)
181+
) as GenericWebView?
165182
```
166183

167184
- **Camera Component (Just camera + our motion analysis and feedback)**:
@@ -177,7 +194,7 @@ The KinesteX SDK provides multiple methods to create different views:
177194
user = null,
178195
isLoading = viewModel.isLoading,
179196
onMessageReceived = ::handleWebViewMessage
180-
)
197+
) as GenericWebView?
181198
```
182199

183200
### Handling Messages

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ android {
3838
}
3939

4040
dependencies {
41-
// implementation(project(":kinestexsdkkotlin"))
41+
implementation(project(":kinestexsdkkotlin"))
4242
implementation("androidx.core:core-ktx:1.9.0")
4343
implementation("androidx.appcompat:appcompat:1.6.1")
4444
implementation("com.google.android.material:material:1.11.0")
4545
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
4646
testImplementation("junit:junit:4.13.2")
4747
androidTestImplementation("androidx.test.ext:junit:1.1.5")
48-
implementation("com.github.KinesteX:KinesteX-SDK-Kotlin:1.1.4")
48+
// implementation("com.github.KinesteX:KinesteX-SDK-Kotlin:1.1.4")
4949
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
5050
implementation("androidx.activity:activity-ktx:1.9.0")
5151
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.8.1")

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<uses-feature
1010
android:name="android.hardware.camera"
11-
android:required="true" />
11+
android:required="false" />
1212

1313
<uses-permission android:name="android.permission.INTERNET" />
1414
<uses-permission android:name="android.permission.CAMERA"/>

app/src/main/java/com/kinestex/kotlin_sdk/MainActivity.kt

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.core.app.ActivityCompat
2323
import androidx.core.content.ContextCompat
2424
import androidx.lifecycle.ViewModelProvider
2525
import androidx.lifecycle.lifecycleScope
26+
import com.kinestex.kinestexsdkkotlin.GenericWebView
2627
import com.kinestex.kinestexsdkkotlin.KinesteXSDK
2728
import com.kinestex.kinestexsdkkotlin.PlanCategory
2829
import com.kinestex.kinestexsdkkotlin.WebViewMessage
@@ -31,22 +32,19 @@ import kotlinx.coroutines.launch
3132

3233

3334
class MainActivity : AppCompatActivity() {
34-
companion object {
35-
private const val CAMERA_PERMISSION_REQUEST_CODE = 1001
36-
}
37-
3835
private var tvMistake: TextView? = null
3936
private var tvReps: TextView? = null
4037

4138
private lateinit var viewModel: ContentViewModel
4239
private lateinit var binding: ActivityMainBinding
4340
private val iconSubOptions = mutableListOf<ImageView>()
44-
private var webView: WebView? = null
41+
private var webView: GenericWebView? = null
4542

4643
private val apiKey = "apiKey" // store this key securely
4744
private val company = "companyName"
4845
private val userId = "userId"
4946

47+
5048
@SuppressLint("SetJavaScriptEnabled")
5149
override fun onCreate(savedInstanceState: Bundle?) {
5250
super.onCreate(savedInstanceState)
@@ -56,15 +54,14 @@ class MainActivity : AppCompatActivity() {
5654
setContentView(binding.root)
5755
supportActionBar?.hide()
5856

59-
// checkCameraPermission()
60-
6157
viewModel = ViewModelProvider(this)[ContentViewModel::class.java]
6258

6359
initUiListeners()
6460

6561
observe()
6662
}
6763

64+
6865
private fun showHowToVideo() {
6966
val howToView = KinesteXSDK.createHowToView(
7067
context = this,
@@ -84,52 +81,13 @@ class MainActivity : AppCompatActivity() {
8481
binding.layVideo.addView(howToView)
8582
}
8683

87-
private fun checkCameraPermission() {
88-
val permission = CAMERA
89-
90-
if (ContextCompat.checkSelfPermission(
91-
this, permission
92-
) != PackageManager.PERMISSION_GRANTED
93-
) {
94-
// Permission is not granted, show rationale if necessary
95-
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
96-
// You can show a custom rationale dialog here
97-
AlertDialog.Builder(this).setTitle("Camera Permission Needed")
98-
.setMessage("This app requires access to the camera to take photos.")
99-
.setPositiveButton("OK") { _, _ ->
100-
// Request permission again
101-
ActivityCompat.requestPermissions(
102-
this, arrayOf(permission), CAMERA_PERMISSION_REQUEST_CODE
103-
)
104-
}.setNegativeButton("Cancel") { dialog, _ ->
105-
dialog.dismiss()
106-
}.create().show()
107-
} else {
108-
// No rationale needed, request the permission
109-
ActivityCompat.requestPermissions(
110-
this, arrayOf(permission), CAMERA_PERMISSION_REQUEST_CODE
111-
)
112-
}
113-
} else {
114-
// Permission is already granted
115-
onCameraPermissionGranted()
116-
}
117-
}
118-
11984
override fun onRequestPermissionsResult(
120-
requestCode: Int, permissions: Array<out String>, grantResults: IntArray
85+
requestCode: Int,
86+
permissions: Array<out String>,
87+
grantResults: IntArray
12188
) {
12289
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
123-
when (requestCode) {
124-
CAMERA_PERMISSION_REQUEST_CODE -> {
125-
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
126-
onCameraPermissionGranted()
127-
} else {
128-
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show()
129-
}
130-
return
131-
}
132-
}
90+
webView?.handlePermissionResult(requestCode, grantResults)
13391
}
13492

13593
private fun onCameraPermissionGranted() {
@@ -247,7 +205,7 @@ class MainActivity : AppCompatActivity() {
247205
customParams = data, // example of using custom parameters
248206
viewModel.isLoading,
249207
::handleWebViewMessage
250-
)
208+
) as GenericWebView?
251209
return webView
252210
}
253211

@@ -266,7 +224,7 @@ class MainActivity : AppCompatActivity() {
266224
data,
267225
viewModel.isLoading,
268226
::handleWebViewMessage
269-
)
227+
) as GenericWebView?
270228
return webView
271229

272230
}
@@ -281,7 +239,7 @@ class MainActivity : AppCompatActivity() {
281239
null,
282240
isLoading = viewModel.isLoading,
283241
onMessageReceived = ::handleWebViewMessage
284-
)
242+
) as GenericWebView?
285243
return webView
286244
}
287245

@@ -297,7 +255,7 @@ class MainActivity : AppCompatActivity() {
297255
customParams = null,
298256
viewModel.isLoading,
299257
::handleWebViewMessage
300-
)
258+
) as GenericWebView?
301259
return webView
302260
}
303261

@@ -419,7 +377,7 @@ class MainActivity : AppCompatActivity() {
419377
user = null,
420378
isLoading = viewModel.isLoading,
421379
onMessageReceived = ::handleWebViewMessage
422-
)
380+
) as GenericWebView?
423381

424382
webView?.let { container.addView(setLayoutParamsFullScreen(it)) }
425383

kinestexsdkkotlin/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ afterEvaluate {
3939
from(components["release"])
4040
groupId = "com.github.KinesteX"
4141
artifactId = "kinestexsdkkotlin"
42-
version = "1.1.4"
4342

4443
pom {
4544
name.set("KinesteX SDK Kotlin")

0 commit comments

Comments
 (0)