Skip to content

Commit 6761fa1

Browse files
gabrielle-laucopybara-github
authored andcommitted
Add Catch app and tests to AndroidEnv
PiperOrigin-RevId: 829137965
1 parent 8270470 commit 6761fa1

30 files changed

Lines changed: 2312 additions & 42 deletions

‎android_env/apps/MODULE.bazel‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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-android",
52+
group = "org.mockito",
53+
version = "5.20.0",
54+
)
55+
maven.artifact(
56+
testonly = True,
57+
artifact = "mockito-kotlin",
58+
group = "org.mockito.kotlin",
59+
version = "6.1.0",
60+
)
61+
maven.install(
62+
artifacts = [
63+
"androidx.test.ext:junit:1.3.0",
64+
"androidx.test:runner:1.7.0",
65+
"com.google.guava:guava:32.0.1-jre",
66+
"com.google.truth:truth:1.4.0",
67+
"org.mockito.kotlin:mockito-kotlin:6.1.0",
68+
"org.mockito:mockito-core:5.20.0",
69+
"org.robolectric:robolectric:4.16",
70+
"org.yaml:snakeyaml:2.5",
71+
],
72+
repositories = [
73+
"https://maven.google.com",
74+
"https://repo1.maven.org/maven2",
75+
],
76+
)
77+
use_repo(maven, "maven")
78+
79+
remote_android_extensions = use_extension(
80+
"@rules_android//bzlmod_extensions:android_extensions.bzl",
81+
"remote_android_tools_extensions",
82+
)
83+
use_repo(remote_android_extensions, "android_tools")
84+
85+
android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
86+
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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
17+
load("@rules_android//rules:rules.bzl", "android_binary")
18+
load("@rules_kotlin//kotlin:android.bzl", "kt_android_library")
19+
20+
package(
21+
default_visibility = [":catch_packages"],
22+
)
23+
24+
package_group(
25+
name = "catch_packages",
26+
packages = [
27+
"//java/com/google/androidenv/catch/...",
28+
"//javatests/com/google/androidenv/catch/...",
29+
],
30+
)
31+
32+
licenses(["notice"])
33+
34+
android_binary(
35+
name = "app",
36+
manifest = "AndroidManifest.xml",
37+
multidex = "native",
38+
deps = [":MainActivity"],
39+
)
40+
41+
kt_android_library(
42+
name = "GameLogic",
43+
srcs = ["GameLogic.kt"],
44+
deps = [
45+
"//java/com/google/androidenv/catch/sprite:Background",
46+
"//java/com/google/androidenv/catch/sprite:Ball",
47+
"//java/com/google/androidenv/catch/sprite:LineSegment",
48+
"//java/com/google/androidenv/catch/sprite:Paddle",
49+
],
50+
)
51+
52+
kt_android_library(
53+
name = "GameLogicThread",
54+
srcs = ["GameLogicThread.kt"],
55+
deps = [
56+
":GameLogic",
57+
],
58+
)
59+
60+
kt_android_library(
61+
name = "MainActivity",
62+
srcs = ["MainActivity.kt"],
63+
manifest = "AndroidManifest.xml",
64+
resource_files = glob(["res/**"]),
65+
deps = [
66+
":GameLogic",
67+
":GameLogicThread",
68+
":RenderThread",
69+
"//java/com/google/androidenv/catch/sprite:Background",
70+
"//java/com/google/androidenv/catch/sprite:Ball",
71+
"//java/com/google/androidenv/catch/sprite:Paddle",
72+
],
73+
)
74+
75+
kt_android_library(
76+
name = "RenderThread",
77+
srcs = ["RenderThread.kt"],
78+
deps = [
79+
":GameLogic",
80+
],
81+
)
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)