Skip to content

Commit 5276e0c

Browse files
MLKit: Add support for ACTION_SCAN_BARCODE activity (#2787)
Co-authored-by: Marvin W <git@larma.de>
1 parent fbd84a1 commit 5276e0c

File tree

10 files changed

+540
-217
lines changed

10 files changed

+540
-217
lines changed

play-services-core/src/main/AndroidManifest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,19 @@
10321032
</intent-filter>
10331033
</service>
10341034

1035+
<activity
1036+
android:name="org.microg.gms.mlkit.BarcodeScanningActivity"
1037+
android:exported="true"
1038+
android:process=":ui"
1039+
android:excludeFromRecents="true"
1040+
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
1041+
<intent-filter>
1042+
<action android:name="com.google.android.gms.mlkit.ACTION_SCAN_BARCODE" />
1043+
1044+
<category android:name="android.intent.category.DEFAULT" />
1045+
</intent-filter>
1046+
</activity>
1047+
10351048
<service android:name="org.microg.gms.DummyService">
10361049
<intent-filter>
10371050
<action android:name="com.google.android.contextmanager.service.ContextManagerService.START" />
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 microG Project Team
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.microg.gms.mlkit
7+
8+
import android.Manifest.permission
9+
import android.content.Intent
10+
import android.content.pm.PackageManager
11+
import android.os.Build
12+
import android.os.Bundle
13+
import android.widget.ImageView
14+
import android.widget.TextView
15+
import androidx.activity.result.contract.ActivityResultContracts
16+
import androidx.appcompat.app.AlertDialog
17+
import androidx.appcompat.app.AppCompatActivity
18+
import androidx.core.content.ContextCompat
19+
import androidx.lifecycle.lifecycleScope
20+
import com.google.android.gms.R
21+
import com.google.android.gms.common.internal.safeparcel.SafeParcelableSerializer
22+
import org.microg.gms.vision.barcode.QRCodeScannerView
23+
24+
private const val KEY_CALLING_APP_NAME = "extra_calling_app_name"
25+
private const val KEY_BARCODE_RESULT = "extra_barcode_result"
26+
27+
class BarcodeScanningActivity : AppCompatActivity() {
28+
29+
private val clientPackageName: String?
30+
get() = runCatching {
31+
intent?.extras?.takeIf { it.containsKey(KEY_CALLING_APP_NAME) }?.getString(KEY_CALLING_APP_NAME)
32+
}.getOrNull()
33+
34+
private val requestPermissionLauncher =
35+
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
36+
if (isGranted) {
37+
startScanning()
38+
} else {
39+
showPermissionDialog(clientPackageName)
40+
}
41+
}
42+
43+
override fun onCreate(savedInstanceState: Bundle?) {
44+
super.onCreate(savedInstanceState)
45+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
46+
finish()
47+
return
48+
}
49+
setContentView(R.layout.activity_barcode_scanning)
50+
findViewById<ImageView>(R.id.barcode_scanning_cancel).setOnClickListener {
51+
finish()
52+
}
53+
if (clientPackageName != null) {
54+
findViewById<TextView>(R.id.barcode_scanning_tips).text = String.format(getString(R.string.barcode_scanner_brand), clientPackageName)
55+
}
56+
if (ContextCompat.checkSelfPermission(this, permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
57+
requestPermissionLauncher.launch(permission.CAMERA)
58+
} else {
59+
startScanning()
60+
}
61+
}
62+
63+
private fun startScanning(){
64+
lifecycleScope.launchWhenCreated {
65+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
66+
val scannerView = findViewById<QRCodeScannerView>(R.id.scannerView)
67+
scannerView.startScanner { result ->
68+
if (result != null) {
69+
val resultIntent = Intent().apply {
70+
putExtra(KEY_BARCODE_RESULT, SafeParcelableSerializer.serializeToBytes(result))
71+
}
72+
setResult(RESULT_OK, resultIntent)
73+
finish()
74+
}
75+
}
76+
}
77+
}
78+
}
79+
80+
private fun showPermissionDialog(callingApp: String?) {
81+
AlertDialog.Builder(this).apply {
82+
setTitle(getString(R.string.camera_permission_dialog_title))
83+
setMessage(String.format(getString(R.string.camera_permission_dialog_message), callingApp))
84+
setPositiveButton(getString(R.string.camera_permission_dialog_button)){ dialog, _ ->
85+
dialog.dismiss()
86+
finish()
87+
}
88+
}.show()
89+
}
90+
91+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<org.microg.gms.vision.barcode.QRCodeScannerView
7+
android:id="@+id/scannerView"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent"/>
10+
11+
<ImageView
12+
android:id="@+id/barcode_scanning_cancel"
13+
android:layout_width="32dp"
14+
android:layout_height="32dp"
15+
android:layout_gravity="start"
16+
android:contentDescription="@android:string/cancel"
17+
android:scaleType="centerCrop"
18+
android:layout_margin="20dp"
19+
android:src="@drawable/ic_close_btn" />
20+
21+
<TextView
22+
android:id="@+id/barcode_scanning_tips"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:layout_margin="20dp"
26+
android:textSize="16dp"
27+
android:textColor="@android:color/darker_gray"
28+
android:layout_gravity="bottom|center"/>
29+
30+
</FrameLayout>

play-services-core/src/main/res/values-zh-rCN/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ microG GmsCore 内置一套自由的 SafetyNet 实现,但是官方服务器要
279279
<string name="auth_action_notification_content">你的 Google 账户需要额外设置。</string>
280280
<string name="auth_action_activity_explanation">要能在这台设备上使用你的 Google 账户 %s 请完成下列步骤。</string>
281281
<string name="auth_action_step_enable_lockscreen_description">你的 Google 账户受工作场所或教育机构管理。你的管理员决定设备在可以访问账户数据前需要设置安全屏幕锁。\n\n请设置一个密码、PIN或手势屏幕锁。</string>
282+
<string name="barcode_scanner_brand">由 microG 代表“%1$s”扫描</string>
283+
<string name="camera_permission_dialog_button">确定</string>
284+
<string name="camera_permission_dialog_message">microG 服务需要访问设备的摄像头,才能为%1$s扫描二维码。\n\n若要启用该权限,请在“设置”中向 microG 服务授予相机权限。</string>
285+
<string name="camera_permission_dialog_title">需要相机使用权限</string>
282286
<string name="pref_vending_asset_delivery_summary">当使用 Play 资产传递的应用请求时下载额外的资产</string>
283287
<string name="pref_vending_asset_delivery_category">Google Play 资产传递</string>
284288
<string name="pref_vending_asset_delivery_switch">启用按需资产传递</string>

play-services-core/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,9 @@ Please set up a password, PIN, or pattern lock screen."</string>
334334
<string name="games_delete_snapshot_dialog_ok">OK</string>
335335
<string name="games_delete_snapshot_error">Deletion failed, please try again later</string>
336336

337+
<string name="barcode_scanner_brand">Scanned by microG on behalf of %1$s</string>
338+
<string name="camera_permission_dialog_button">OK</string>
339+
<string name="camera_permission_dialog_message">microG services needs to access your device\'s camera to scan a code for %1$s.\n\nTo enable, please grant camera permission to microG services in Settings.</string>
340+
<string name="camera_permission_dialog_title">Camera permission required</string>
341+
337342
</resources>

play-services-vision/core/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ dependencies {
1515
implementation project(':play-services-base-core')
1616
implementation "androidx.annotation:annotation:$annotationVersion"
1717
implementation "com.google.zxing:core:3.5.2"
18+
implementation "androidx.camera:camera-core:1.3.0"
19+
implementation "androidx.camera:camera-camera2:1.3.0"
20+
implementation "androidx.camera:camera-lifecycle:1.3.0"
21+
implementation "androidx.camera:camera-view:1.3.0"
1822
}
1923

2024
android {

play-services-vision/core/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
-->
66
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
77

8+
<uses-permission android:name="android.permission.CAMERA"/>
9+
<uses-feature
10+
android:name="android.hardware.camera"
11+
android:required="false" />
812

913
<application>
1014
</application>

0 commit comments

Comments
 (0)