Skip to content

Commit 5cda0a9

Browse files
gabrielle-laucopybara-github
authored andcommitted
Add catch the ball Android app
PiperOrigin-RevId: 830525875
1 parent 92db6e2 commit 5cda0a9

30 files changed

+2321
-42
lines changed

android_env/apps/MODULE.bazel

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2025 DeepMind Technologies Limited.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Bazel dependencies for building Catch.
16+
module(
17+
name = "catch",
18+
version = "1.0",
19+
)
20+
21+
bazel_dep(name = "rules_android", version = "0.6.6")
22+
bazel_dep(name = "rules_kotlin", version = "2.1.8")
23+
bazel_dep(name = "rules_jvm_external", version = "6.7")
24+
bazel_dep(name = "rules_robolectric", version = "4.16", repo_name = "robolectric")
25+
bazel_dep(name = "rules_java", version = "9.0.3")
26+
bazel_dep(name = "protobuf", version = "30.0")
27+
28+
# To avoid conflict with different protobuf versions.
29+
single_version_override(
30+
module_name = "protobuf",
31+
version = "30.0",
32+
)
33+
34+
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
35+
36+
# Need to set testonly = True because the package depends on testonly targets.
37+
maven.artifact(
38+
testonly = True,
39+
artifact = "runner",
40+
group = "androidx.test",
41+
version = "1.7.0",
42+
)
43+
maven.artifact(
44+
testonly = True,
45+
artifact = "junit",
46+
group = "androidx.test.ext",
47+
version = "1.3.0",
48+
)
49+
maven.artifact(
50+
testonly = True,
51+
artifact = "mockito-kotlin",
52+
group = "org.mockito.kotlin",
53+
version = "6.1.0",
54+
)
55+
maven.install(
56+
artifacts = [
57+
"androidx.test.ext:junit:1.3.0",
58+
"androidx.test:runner:1.7.0",
59+
"com.google.guava:guava:32.0.1-jre",
60+
"com.google.truth:truth:1.4.0",
61+
"org.mockito.kotlin:mockito-kotlin:6.1.0",
62+
"org.mockito:mockito-core:5.20.0",
63+
"org.robolectric:robolectric:4.16",
64+
"org.yaml:snakeyaml:2.5",
65+
],
66+
repositories = [
67+
"https://maven.google.com",
68+
"https://repo1.maven.org/maven2",
69+
],
70+
)
71+
use_repo(maven, "maven")
72+
73+
remote_android_extensions = use_extension(
74+
"@rules_android//bzlmod_extensions:android_extensions.bzl",
75+
"remote_android_tools_extensions",
76+
)
77+
use_repo(remote_android_extensions, "android_tools")
78+
79+
android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
80+
use_repo(android_sdk_repository_extension, "androidsdk")

android_env/apps/java/com/google/androidenv/accessibilityforwarder/AndroidManifest.xml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// Copyright 2025 DeepMind Technologies Limited.
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
151
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2025 DeepMind Technologies Limited.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.-->
15+
16+
1617

1718
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1819
package="com.google.androidenv.accessibilityforwarder">

android_env/apps/java/com/google/androidenv/accessibilityforwarder/AndroidManifest_lite.xml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// Copyright 2025 DeepMind Technologies Limited.
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
151
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2025 DeepMind Technologies Limited.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.-->
15+
16+
1617

1718
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1819
package="com.google.androidenv.accessibilityforwarder">

android_env/apps/java/com/google/androidenv/accessibilityforwarder/res/xml/accessibility_forwarder_service.xml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// Copyright 2025 DeepMind Technologies Limited.
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
151
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2025 DeepMind Technologies Limited.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.-->
15+
16+
1617

1718
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
1819
android:accessibilityEventTypes="typeAllMask"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright 2025 DeepMind Technologies Limited.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.-->
15+
16+
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
package="com.google.androidenv.catch">
20+
<uses-sdk android:minSdkVersion="26"
21+
android:targetSdkVersion="35"/>
22+
<application
23+
android:allowBackup="false"
24+
android:label="@string/app_name"
25+
android:supportsRtl="false"
26+
android:taskAffinity=""
27+
tools:ignore="AllowBackup">
28+
<activity
29+
android:name=".MainActivity"
30+
android:configChanges="orientation|keyboardHidden|screenSize"
31+
android:exported="true"
32+
android:hardwareAccelerated="true">
33+
<intent-filter>
34+
<action android:name="android.intent.action.MAIN" />
35+
<category android:name="android.intent.category.LAUNCHER" />
36+
</intent-filter>
37+
</activity>
38+
</application>
39+
</manifest>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2025 DeepMind Technologies Limited.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Classic RL task implemented as an Android app.
16+
load("@rules_android//rules:rules.bzl", "android_binary")
17+
load("@rules_kotlin//kotlin:android.bzl", "kt_android_library")
18+
19+
package(
20+
default_visibility = [":catch_packages"],
21+
)
22+
23+
package_group(
24+
name = "catch_packages",
25+
packages = [
26+
"//java/com/google/androidenv/catch/...",
27+
"//javatests/com/google/androidenv/catch/...",
28+
],
29+
)
30+
31+
licenses(["notice"])
32+
33+
android_binary(
34+
name = "app",
35+
manifest = "AndroidManifest.xml",
36+
multidex = "native",
37+
deps = [":MainActivity"],
38+
)
39+
40+
kt_android_library(
41+
name = "GameLogic",
42+
srcs = ["GameLogic.kt"],
43+
deps = [
44+
"//java/com/google/androidenv/catch/sprite:Background",
45+
"//java/com/google/androidenv/catch/sprite:Ball",
46+
"//java/com/google/androidenv/catch/sprite:LineSegment",
47+
"//java/com/google/androidenv/catch/sprite:Paddle",
48+
],
49+
)
50+
51+
kt_android_library(
52+
name = "GameLogicThread",
53+
srcs = ["GameLogicThread.kt"],
54+
deps = [
55+
":GameLogic",
56+
],
57+
)
58+
59+
kt_android_library(
60+
name = "MainActivity",
61+
srcs = ["MainActivity.kt"],
62+
manifest = "AndroidManifest.xml",
63+
resource_files = glob(["res/**"]),
64+
deps = [
65+
":GameLogic",
66+
":GameLogicThread",
67+
":RenderThread",
68+
"//java/com/google/androidenv/catch/sprite:Background",
69+
"//java/com/google/androidenv/catch/sprite:Ball",
70+
"//java/com/google/androidenv/catch/sprite:Paddle",
71+
],
72+
)
73+
74+
kt_android_library(
75+
name = "RenderThread",
76+
srcs = ["RenderThread.kt"],
77+
deps = [
78+
":GameLogic",
79+
],
80+
)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2025 DeepMind Technologies Limited.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.androidenv.catch
16+
17+
import android.graphics.Canvas
18+
import android.view.MotionEvent
19+
import com.google.androidenv.catch.sprite.Background
20+
import com.google.androidenv.catch.sprite.Ball
21+
import com.google.androidenv.catch.sprite.LineSegment
22+
import com.google.androidenv.catch.sprite.Paddle
23+
import java.time.Duration
24+
import java.time.Instant
25+
import kotlin.random.Random
26+
27+
/** The class that contains the game logic. */
28+
open class GameLogic(
29+
// Expected number of frames per second.
30+
fps: Int = 60,
31+
// Pseudo random number generator.
32+
private val rand: Random = Random.Default,
33+
// Width and height of the game in pixels.
34+
private val width: Int,
35+
private val height: Int,
36+
// UI objects in the game.
37+
private var background: Background = Background(),
38+
private var ball: Ball = Ball(maxX = width, maxY = height, rand = rand),
39+
private var paddle: Paddle = Paddle(maxX = width, y = height),
40+
) {
41+
42+
private val sleepTime: Duration = Duration.ofMillis((1000.0 / fps).toLong())
43+
44+
/** Reinitializes the state of the game. */
45+
// Need to make this open to allow for testing.
46+
open fun reset() {
47+
this.ball.reset()
48+
}
49+
50+
/** Runs one "throw" of a [ball] that needs to be caught by the [paddle]. */
51+
// Need to make this open to allow for testing.
52+
open fun run(): Boolean {
53+
var lastTimestamp = Instant.now()
54+
do {
55+
Thread.sleep(sleepTime.toMillis())
56+
val now = Instant.now()
57+
val interval = Duration.between(lastTimestamp, now)
58+
lastTimestamp = now
59+
ball.update(interval)
60+
} while (!ball.isOutOfBounds())
61+
62+
return ball.intersects(LineSegment(paddle.topLeft(), paddle.topRight()))
63+
}
64+
65+
/** Processes a user event (e.g. a touchscreen event) and updates the [paddle] accordingly. */
66+
fun handleTouch(event: MotionEvent) {
67+
paddle.x = event.x.toInt()
68+
}
69+
70+
/** Renders the game on [c]. */
71+
open fun render(c: Canvas) {
72+
background.draw(c)
73+
ball.draw(c)
74+
paddle.draw(c)
75+
}
76+
}

0 commit comments

Comments
 (0)