Skip to content

Commit fbe8c59

Browse files
authored
Merge pull request #1503 from madeline-underwood/Selfie_app_v2
Selfie app v2_KB to review
2 parents a7f3142 + 1e3267c commit fbe8c59

File tree

10 files changed

+207
-174
lines changed

10 files changed

+207
-174
lines changed

content/learning-paths/smartphones-and-mobile/build-android-selfie-app-using-mediapipe-multimodality/2-app-scaffolding.md

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,59 @@
11
---
2-
title: Create a new Android project
2+
title: Set up the Development Environment
33
weight: 2
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
8+
## Download Android Studio
89

9-
This learning path will teach you to architect an app following [modern Android architecture](https://developer.android.com/courses/pathways/android-architecture) design with a focus on the [UI layer](https://developer.android.com/topic/architecture/ui-layer).
10+
Start by downloading and installing the latest version of [Android Studio](https://developer.android.com/studio/) on your host machine.
1011

11-
## Development environment setup
12+
The instructions for this learning path were tested on a host machine running macOS, but you can use any of the supported hardware systems listed on the [Install Android Studio](https://developer.android.com/studio/install) webpage on the Android Developer website.
1213

13-
Download and install the latest version of [Android Studio](https://developer.android.com/studio/) on your host machine.
14+
After installation, open Android Studio and do the following:
1415

15-
The instructions for this learning path were tested on a Apple Silicon host machine running macOS, but you may choose any of the supported hardware systems as described [here](https://developer.android.com/studio/install).
16+
* Accept license agreements.
17+
* Download all the required assets.
18+
* Select the default or recommended settings.
1619

17-
Upon first installation, open Android Studio and proceed with the default or recommended settings. Accept license agreements and let Android Studio download all the required assets.
20+
{{% notice Tips %}}
21+
Before you start coding, here are some useful tips:
1822

19-
Before you proceed to coding, here are some tips that might come handy:
23+
1. To navigate to a file, simply press the Shift key twice, input the file name, select the correct result using the up and down arrow keys, and then press Enter.
2024

21-
{{% notice Tip %}}
22-
1. To navigate to a file, simply double-tap `Shift` key and input the file name, then select the correct result using `Up` & `Down` arrow keys and then tap `Enter`.
23-
24-
2. Every time after you copy-paste a code block from this learning path, make sure you **import the correct classes** and resolved the errors. Refer to [this doc](https://www.jetbrains.com/help/idea/creating-and-optimizing-imports.html) to learn more.
25+
2. Every time after you copy-and-paste a code block from this Learning Path, ensure that you import the correct classes and resolve any errors. For more information, see the [Auto import](https://www.jetbrains.com/help/idea/creating-and-optimizing-imports.html) web page.
2526
{{% /notice %}}
2627

2728
## Create a new Android project
2829

29-
1. Navigate to File > New > New Project....
30+
1. Navigate to **File** > **New** > **New Project**.
3031

31-
2. Select Empty Views Activity in the Phone and Tablet gallery as shown below, then click Next.
32-
![Empty Views Activity](images/2/empty%20project.png)
32+
2. Select **Empty Views Activity** in the **Phone and Tablet** gallery as Figure 1 shows, then select **Next**.
33+
![Empty Views Activity.png alt-text#center](images/2/empty%20project.png "Figure 1: Select Empty Views Activity.")
3334

34-
3. Enter a project name and use the default configurations as shown below. Make sure that Language is set to Kotlin, and that Build configuration language is set to Kotlin DSL.
35-
![Project configuration](images/2/project%20config.png)
35+
3. Choose a project name, and select the default configurations as Figure 2 shows.
3636

37-
### Introduce CameraX dependencies
37+
Make sure that the **Language** field is set to **Kotlin**, and the **Build configuration language** field is set to **Kotlin DSL**.
38+
![Project configuration.png alt-text#center](images/2/project%20config.png "Figure 2: Project Configuration.")
3839

39-
[CameraX](https://developer.android.com/media/camera/camerax) is a Jetpack library, built to help make camera app development easier. It provides a consistent, easy-to-use API that works across the vast majority of Android devices with a great backward-compatibility.
40+
## Add CameraX dependencies
4041

41-
1. Wait for Android Studio to sync project with Gradle files, this make take up to several minutes.
42+
[CameraX](https://developer.android.com/media/camera/camerax) is a Jetpack library, built to help make camera app development easier. It provides a consistent, easy-to-use API that works across the vast majority of Android devices with great backward-compatibility.
4243

43-
2. Once project is synced, navigate to `libs.versions.toml` in your project's root directory as shown below. This file serves as the version catalog for all dependencies used in the project.
44+
1. Wait for Android Studio to sync project with Gradle files. This might take several minutes.
4445

45-
![version catalog](images/2/dependency%20version%20catalog.png)
46+
2. Once the project is synced, navigate to `libs.versions.toml` in your project's root directory. See Figure 3. This file serves as the version catalog for all dependencies that the project uses.
47+
48+
![Version Catalog.png alt-text#center](images/2/dependency%20version%20catalog.png "Figure 3: Version Catalog.")
4649

4750
{{% notice Info %}}
4851

49-
For more information on version catalogs, please refer to [this doc](https://developer.android.com/build/migrate-to-catalogs).
52+
For more information on version catalogs, see [Migrate your build to version catalogs](https://developer.android.com/build/migrate-to-catalogs).
5053

5154
{{% /notice %}}
5255

53-
3. Append the following line to the end of `[versions]` section. This defines the version of CameraX libraries we will be using.
56+
3. Append the following line to the end of `[versions]` section. This defines the version of CameraX libraries that you will be using.
5457
```toml
5558
camerax = "1.4.0"
5659
```
@@ -64,7 +67,7 @@ camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", versi
6467
camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "camerax" }
6568
```
6669

67-
5. Navigate to `build.gradle.kts` in your project's `app` directory, then insert the following lines into `dependencies` block. This introduces the above dependencies into the `app` subproject.
70+
5. Navigate to `build.gradle.kts` in your project's `app` directory, then insert the following lines into `dependencies` block. This introduces the dependencies listed above into the `app` subproject:
6871

6972
```kotlin
7073
implementation(libs.camera.core)
@@ -75,32 +78,34 @@ camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "
7578

7679
## Enable view binding
7780

78-
1. Within the above `build.gradle.kts` file, append the following lines to the end of `android` block to enable view binding feature.
81+
1. Within the above `build.gradle.kts` file, append the following lines to the end of `android` block to enable the view binding feature:
7982

8083
```kotlin
8184
buildFeatures {
8285
viewBinding = true
8386
}
8487
```
8588

86-
2. You should be seeing a notification shows up, as shown below. Click **"Sync Now"** to sync your project.
89+
2. You should see that a notification appears. See Figure 4. Click **Sync Now** to sync your project.
8790

88-
![Gradle sync](images/2/gradle%20sync.png)
91+
![Gradle sync.png alt-text#center](images/2/gradle%20sync.png "Figure 4: Gradle Sync.")
8992

9093
{{% notice Tip %}}
9194

92-
You may also click the __"Sync Project with Gradle Files"__ button in the toolbar or pressing the corresponding shortcut to start a sync.
95+
You can also click the **Sync Project with Gradle Files** button in the toolbar, or enter the corresponding shortcut to start a sync.
9396

9497
![Sync Project with Gradle Files](images/2/sync%20project%20with%20gradle%20files.png)
9598
{{% /notice %}}
9699

97-
3. Navigate to `MainActivity.kt` source file and make following changes. This inflates the layout file into a view binding object and stores it in a member variable within the view controller for easier access later.
100+
3. Navigate to the `MainActivity.kt` source file and make the changes that Figure 5 shows in the View Binding screenshot.
101+
102+
This inflates the layout file into a view binding object, and stores it in a member variable within the view controller for easier access later.
98103

99-
![view binding](images/2/view%20binding.png)
104+
![view binding alt-text#center](images/2/view%20binding.png "Figure 5: View Binding.")
100105

101106
## Configure CameraX preview
102107

103-
1. **Replace** the placeholder "Hello World!" `TextView` within the layout file `activity_main.xml` with a camera preview view:
108+
1. Within the layout file `activity_main.xml`, replace the placeholder "Hello World!" in `TextView` with a camera preview view:
104109

105110
```xml
106111
<androidx.camera.view.PreviewView
@@ -111,7 +116,7 @@ You may also click the __"Sync Project with Gradle Files"__ button in the toolba
111116
```
112117

113118

114-
2. Add the following member variables to `MainActivity.kt` to store camera related objects:
119+
2. Add the following member variables to `MainActivity.kt` to store camera-related objects:
115120

116121
```kotlin
117122
// Camera
@@ -145,7 +150,7 @@ You may also click the __"Sync Project with Gradle Files"__ button in the toolba
145150
}
146151
```
147152

148-
4. Implement the above `bindCameraUseCases()` method:
153+
4. Implement the `bindCameraUseCases()` method:
149154

150155
```kotlin
151156
private fun bindCameraUseCases() {
@@ -186,12 +191,12 @@ private fun bindCameraUseCases() {
186191
}
187192
```
188193

189-
5. Add a [companion object](https://kotlinlang.org/docs/object-declarations.html#companion-objects) to `MainActivity.kt` and declare a `TAG` constant value for `Log` calls to work correctly. This companion object comes handy for us to define all the constants and shared values accessible across the entire class.
194+
5. Add a [companion object](https://kotlinlang.org/docs/object-declarations.html#companion-objects) to `MainActivity.kt`, and declare a `TAG` constant value for `Log` calls to work correctly. This companion object is useful in enabling you to define all the constants and shared values accessible across the entire class.
190195

191196
```kotlin
192197
companion object {
193198
private const val TAG = "MainActivity"
194199
}
195200
```
196201

197-
In the next section, you will build and run the app to make sure the camera works well.
202+
In the next section, you will build and run the app to ensure that the camera works as expected.

content/learning-paths/smartphones-and-mobile/build-android-selfie-app-using-mediapipe-multimodality/3-camera-permission.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
---
2-
title: Handle camera permissions
2+
title: Manage Camera Permissions
33
weight: 3
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
## Run the app on your device
9+
### Connect your device to your machine with a USB data cable
1010

11-
1. Connect your Android device to your computer via a USB data cable. If this is your first time running and debugging Android apps, follow [this guide](https://developer.android.com/studio/run/device#setting-up) and double check this checklist:
11+
If this is your first time running and debugging Android apps, follow the guidance on the Android Developer website. See [Set up a device for development](https://developer.android.com/studio/run/device#setting-up), and check that you have completed these steps:
1212

13-
1. You have enabled USB debugging on your Android device following [this doc](https://developer.android.com/studio/debug/dev-options#Enable-debugging).
13+
1. You have followed the instructions [Enable USB debugging on your device](https://developer.android.com/studio/debug/dev-options#Enable-debugging).
1414

15-
2. You have confirmed by tapping "OK" on your Android device when an "Allow USB debugging" dialog pops up, and checked "Always allow from this computer".
15+
2. You have confirmed that you have enabled USB debugging by tapping **OK** on your device when the **Allow USB debugging** dialog appears, and that you have checked **Always allow from this computer**. See Figure 6.
1616

17-
![Allow USB debugging dialog](https://ftc-docs.firstinspires.org/en/latest/_images/AllowUSBDebugging.jpg)
17+
![AllowUSBDebugging.jpg #center](https://ftc-docs.firstinspires.org/en/latest/_images/AllowUSBDebugging.jpg "Figure 6: Allow USB Debugging.")
1818

1919

20-
2. Make sure your device model name and SDK version correctly show up on the top right toolbar. Click the "Run" button to build and run the app.
20+
3. Make sure that your device model name and SDK version correctly show up in the top-right toolbar.
2121

22-
3. After a while, you should see a success notification in Android Studio and the app showing up on your Android device.
22+
Click the **Run** button to build and run the app.
2323

24-
4. However, the app shows only a black screen while printing error messages in your [Logcat](https://developer.android.com/tools/logcat) which looks like this:
24+
4. After a while, you should see a notification of a successful build in Android Studio and the new app will be displayed on your Android device.
25+
26+
5. However, you will also see that the app shows a black screen while printing error messages in your [Logcat](https://developer.android.com/tools/logcat), which looks like this:
2527

2628
```
2729
2024-11-20 11:15:00.398 18782-18818 Camera2CameraImpl com.example.holisticselfiedemo E Camera reopening attempted for 10000ms without success.
@@ -30,11 +32,13 @@ layout: learningpathall
3032
2024-11-20 11:43:03.408 2709-3807 PackageManager pid-2709 E Permission android.permission.CAMERA isn't requested by package com.example.holisticselfiedemo
3133
```
3234

33-
5. Do not worry. This is expected behavior because you haven't correctly configured this app's [permissions](https://developer.android.com/guide/topics/permissions/overview) yet. Android OS restricts this app's access to camera features due to privacy reasons.
35+
This is the expected behavior from having not yet correctly configured the app's permissions. See[Permissions](https://developer.android.com/guide/topics/permissions/overview) on the Android Developer website. Android OS restricts the app's access to camera features due to privacy constraints.
36+
37+
### Request camera permission at runtime
3438

35-
## Request camera permission at runtime
39+
1. Navigate to `manifest.xml` in your `app` sub-project's `src/main` path.
3640

37-
1. Navigate to `manifest.xml` in your `app` subproject's `src/main` path. Declare camera hardware and permission by inserting the following lines into the `<manifest>` element. Make sure it's declared outside and above `<application>` element.
41+
Declare the camera hardware, and set the permissions by inserting the following lines into the `<manifest>` element, ensuring that it is declared outside and above the `<application>` element:
3842

3943
```xml
4044
<uses-feature
@@ -43,30 +47,34 @@ layout: learningpathall
4347
<uses-permission android:name="android.permission.CAMERA" />
4448
```
4549

46-
2. Navigate to `strings.xml` in your `app` subproject's `src/main/res/values` path. Insert the following lines of text resources, which will be used later.
50+
2. Navigate to `strings.xml` in your `app` sub-project's `src/main/res/values` path.
51+
Insert the following lines of text resources, which you will use at a later stage:
4752

4853
```xml
4954
<string name="permission_request_camera_message">Camera permission is required to recognize face and hands</string>
5055
<string name="permission_request_camera_rationale">To grant Camera permission to this app, please go to system settings</string>
5156
```
5257

53-
3. Navigate to `MainActivity.kt` and add the following permission related values to companion object:
58+
3. Navigate to `MainActivity.kt` and add the following permission-related values to companion
59+
object:
5460

5561
```kotlin
5662
// Permissions
5763
private val PERMISSIONS_REQUIRED = arrayOf(Manifest.permission.CAMERA)
5864
private const val REQUEST_CODE_CAMERA_PERMISSION = 233
5965
```
6066

61-
4. Add a new method named `hasPermissions()` to check on runtime whether camera permission has been granted:
67+
4. Add a new method named `hasPermissions()` to check on runtime whether the camera permission has
68+
been granted:
6269

6370
```kotlin
6471
private fun hasPermissions(context: Context) = PERMISSIONS_REQUIRED.all {
6572
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
6673
}
6774
```
6875

69-
5. Add a condition check in `onCreate()` wrapping `setupCamera()` method, to request camera permission on runtime.
76+
5. Add a condition check in on the `onCreate()` wrapping `setupCamera()` method, to request camera
77+
permission on runtime:
7078

7179
```kotlin
7280
if (!hasPermissions(baseContext)) {
@@ -79,7 +87,7 @@ layout: learningpathall
7987
}
8088
```
8189

82-
6. Override `onRequestPermissionsResult` method to handle permission request results:
90+
6. Override the `onRequestPermissionsResult` method to handle permission request results:
8391

8492
```kotlin
8593
override fun onRequestPermissionsResult(
@@ -105,14 +113,16 @@ layout: learningpathall
105113
}
106114
```
107115

108-
## Verify camera permission
116+
### Verify camera permission
117+
118+
1. Rebuild and run the app. Now you should see a dialog pop up requesting camera permissions.
109119

110-
1. Rebuild and run the app. Now you should see a dialog pop up requesting camera permissions!
120+
2. Depending on your Android OS version, tap **Allow** or **While using the app**().
111121

112-
2. Tap `Allow` or `While using the app` (depending on your Android OS versions). Then you should see your own face in the camera preview. Good job!
122+
You should then see your own face in the camera preview. Good job!
113123

114124
{{% notice Tip %}}
115-
Sometimes you might need to restart the app to observe the permission change take effect.
125+
You might need to restart the app to ensure that the permission change takes effect.
116126
{{% /notice %}}
117127

118128
In the next section, you will learn how to integrate MediaPipe vision solutions.

0 commit comments

Comments
 (0)