Skip to content

Commit

Permalink
feat: fake scan QR code to open passport
Browse files Browse the repository at this point in the history
  • Loading branch information
bqliang committed Dec 13, 2022
1 parent ed2499b commit 4808d06
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ android {
res {
val srcDirs = listOf(
"main",
"settings"
"settings",
"scan"
).map { "src/main/java/com/bqliang/leavesheet/$it/res" }.plus("src/main/res")
setSrcDirs(srcDirs)
}
Expand All @@ -86,6 +87,8 @@ android {
}

dependencies {
implementation(libs.guolin.permissionX)
implementation(libs.huawei.scanPlus)
implementation(libs.tencent.mmkv)
implementation(libs.timber)
implementation(libs.rikkaX.preference.simplemenu)
Expand Down
10 changes: 10 additions & 0 deletions app/src/debug/res/xml/shortcuts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@
android:data="alipays://platformapi/startapp?appId=20000067&url=http://nfuedu.zftcloud.com/campusbus_index/ticket/index.html" />
</shortcut>

<shortcut
android:icon="@drawable/ic_fake_scan"
android:shortcutId="fake_scan"
android:shortcutShortLabel="@string/shortcut_fake_scan">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.bqliang.leavesheet.scan.ScanActivity"
android:targetPackage="com.bqliang.leavesheet.debug" />
</shortcut>

</shortcuts>
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</queries>

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

<application
android:name=".MyApp"
Expand All @@ -21,6 +22,11 @@
android:theme="@style/Theme.MyApp"
android:usesCleartextTraffic="true"
tools:targetApi="31">

<activity
android:name=".scan.ScanActivity"
android:exported="false"
android:launchMode="singleTask" />
<activity
android:name=".main.PassPortActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bqliang.leavesheet.main

import android.graphics.Color
import com.bqliang.leavesheet.R
import com.bqliang.leavesheet.utils.SoundPoolUtil
import com.bqliang.leavesheet.utils.launchActivity

class PassPortActivity : WebViewActivity() {
Expand Down Expand Up @@ -36,4 +37,10 @@ class PassPortActivity : WebViewActivity() {
override fun onNavigationOnClick() {
launchActivity<LeaveSheetActivity>()
}


override fun onDestroy() {
super.onDestroy()
SoundPoolUtil.release()
}
}
92 changes: 92 additions & 0 deletions app/src/main/java/com/bqliang/leavesheet/scan/ScanActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.bqliang.leavesheet.scan

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.bqliang.leavesheet.main.PassPortActivity
import com.bqliang.leavesheet.utils.SoundPoolUtil
import com.bqliang.leavesheet.utils.launchActivity
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions
import com.permissionx.guolindev.PermissionX

class ScanActivity : AppCompatActivity() {

companion object {
private const val SCAN_TITLE_QR_CODE = 1
private const val REQUEST_CODE_SCAN_ONE = 0X01
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val isGrantedCameraPermission =
PermissionX.isGranted(this, android.Manifest.permission.CAMERA)
if (isGrantedCameraPermission) {
startScanCamera()
} else {
requestCameraPermission { startScanCamera() }
}
}


private fun requestCameraPermission(onGranted: () -> Unit) = PermissionX.init(this)
.permissions(android.Manifest.permission.CAMERA)
.explainReasonBeforeRequest()
.onExplainRequestReason { scope, deniedList ->
scope.showRequestReasonDialog(
permissions = deniedList,
message = "扫码功能需要您授予我们相机权限",
positiveText = "好的",
negativeText = "取消"
)
}
.onForwardToSettings { scope, deniedList ->
scope.showForwardToSettingsDialog(
permissions = deniedList,
message = "您需要在设置中手动授予相机权限",
positiveText = "好的",
negativeText = "取消"
)
}
.request { allGranted, _, _ ->
if (allGranted) {
onGranted()
} else {
Toast.makeText(this, "无法获取照相机权限", Toast.LENGTH_SHORT).show()
finish()
}
}


private fun startScanCamera() {
val scanOptions = HmsScanAnalyzerOptions.Creator()
.setHmsScanTypes(HmsScan.QRCODE_SCAN_TYPE)
.setViewType(SCAN_TITLE_QR_CODE)
.setPhotoMode(false)
.create()
ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, scanOptions)
}


@Deprecated(
"Deprecated in Java", ReplaceWith(
"super.onActivityResult(requestCode, resultCode, data)",
"androidx.appcompat.app.AppCompatActivity"
)
)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@Suppress("deprecation")
super.onActivityResult(requestCode, resultCode, data)

val scanQrCodeSuccess =
requestCode == REQUEST_CODE_SCAN_ONE && resultCode == RESULT_OK && data != null
if (scanQrCodeSuccess) {
launchActivity<PassPortActivity>()
SoundPoolUtil.beep()
}
finish()
}
}
Binary file not shown.
42 changes: 42 additions & 0 deletions app/src/main/java/com/bqliang/leavesheet/utils/SoundPoolUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.bqliang.leavesheet.utils

import android.media.AudioAttributes
import android.media.SoundPool
import com.bqliang.leavesheet.MyApp
import com.bqliang.leavesheet.R

object SoundPoolUtil {
private val audioAttributes by lazy {
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
.build()
}

private var soundPool: SoundPool? = null

fun beep() {
if (soundPool == null) {
soundPool = SoundPool.Builder()
.setAudioAttributes(audioAttributes)
.build()
}

val soundId = soundPool?.load(MyApp.context, R.raw.beep, 1)
soundPool?.setOnLoadCompleteListener { _, _, _ ->
soundPool?.play(
/* soundID = */ soundId!!,
/* leftVolume = */ 1f,
/* rightVolume = */ 1f,
/* priority = */ 0,
/* loop = */ 0,
/* rate = */ 1f
)
}
}

fun release() {
soundPool?.release()
soundPool = null
}
}
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/ic_fake_scan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,12m-11,0a11,11 0,1 1,22 0a11,11 0,1 1,-22 0"
android:strokeLineJoin="round"
android:strokeWidth="1.15086"
android:fillColor="#f5f5f5"
android:fillType="nonZero"
android:strokeLineCap="round"/>
<path
android:pathData="m10.594,8.906l0,1.688l-1.688,0l0,-1.688l1.688,0M11.438,8.063L8.063,8.063l0,3.375l3.375,0zM10.594,13.406l0,1.688l-1.688,0l0,-1.688l1.688,0M11.438,12.563L8.063,12.563l0,3.375l3.375,0zM15.094,8.906l0,1.688l-1.688,0l0,-1.688l1.688,0M15.938,8.063l-3.375,0l0,3.375l3.375,0zM12.563,12.563l0.844,0l0,0.844L12.563,13.406ZM13.406,13.406L14.25,13.406L14.25,14.25L13.406,14.25ZM14.25,12.563l0.844,0l0,0.844L14.25,13.406ZM12.563,14.25l0.844,0l0,0.844L12.563,15.094ZM13.406,15.094L14.25,15.094L14.25,15.938L13.406,15.938ZM14.25,14.25l0.844,0l0,0.844L14.25,15.094ZM15.094,13.406L15.938,13.406L15.938,14.25l-0.844,0zM15.094,15.094L15.938,15.094L15.938,15.938L15.094,15.938ZM17.063,9.188L17.063,9.188C16.753,9.188 16.5,8.934 16.5,8.625L16.5,7.5L15.375,7.5C15.066,7.5 14.813,7.247 14.813,6.938l0,0c0,-0.309 0.253,-0.563 0.563,-0.563l1.688,0c0.309,0 0.563,0.253 0.563,0.563l0,1.688c0,0.309 -0.253,0.563 -0.563,0.563zM17.625,17.063l0,-1.688c0,-0.309 -0.253,-0.563 -0.563,-0.563l0,0c-0.309,0 -0.563,0.253 -0.563,0.563l0,1.125l-1.125,0c-0.309,0 -0.563,0.253 -0.563,0.563l0,0c0,0.309 0.253,0.563 0.563,0.563l1.688,0c0.309,0 0.563,-0.253 0.563,-0.563zM6.938,17.625l1.688,0c0.309,0 0.563,-0.253 0.563,-0.563l0,0C9.188,16.753 8.934,16.5 8.625,16.5L7.5,16.5L7.5,15.375C7.5,15.066 7.247,14.813 6.938,14.813l0,0c-0.309,0 -0.563,0.253 -0.563,0.563l0,1.688c0,0.309 0.253,0.563 0.563,0.563zM6.375,6.938L6.375,8.625C6.375,8.934 6.628,9.188 6.938,9.188L6.938,9.188C7.247,9.188 7.5,8.934 7.5,8.625L7.5,7.5L8.625,7.5C8.934,7.5 9.188,7.247 9.188,6.938L9.188,6.938C9.188,6.628 8.934,6.375 8.625,6.375L6.938,6.375C6.628,6.375 6.375,6.628 6.375,6.938Z"
android:strokeWidth="0.5"
android:fillColor="#1677ff"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="shortcut_pass_code_label">出入通行证</string>
<string name="shortcut_health_code">粤康码</string>
<string name="shortcut_nfu_bus">南苑BUS</string>
<string name="shortcut_fake_scan">假装扫码</string>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/shortcuts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@
android:data="alipays://platformapi/startapp?appId=20000067&amp;url=http://nfuedu.zftcloud.com/campusbus_index/ticket/index.html" />
</shortcut>

<shortcut
android:icon="@drawable/ic_fake_scan"
android:shortcutId="fake_scan"
android:shortcutShortLabel="@string/shortcut_fake_scan">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.bqliang.leavesheet.scan.ScanActivity"
android:targetPackage="com.bqliang.leavesheet" />
</shortcut>

</shortcuts>
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ androidX-dataStore-preferences = "androidx.datastore:datastore-preferences:1.0.0

google-material = "com.google.android.material:material:1.7.0"
tencent-mmkv = "com.tencent:mmkv:1.2.14"
huawei-scanPlus = "com.huawei.hms:scanplus:2.8.0.300"
timber = "com.jakewharton.timber:timber:5.0.1"
xabaras-recyclerViewSwipeDecorator = "com.github.xabaras:RecyclerViewSwipeDecorator:1.4"
faruktoptas-FancyShowCaseView = "com.github.faruktoptas:FancyShowCaseView:1.3.9"
guolin-permissionX = "com.guolindev.permissionx:permissionx:1.7.1"

rikkaX-html = "dev.rikka.rikkax.html:html:1.1.2"
rikkaX-html-ktx = "dev.rikka.rikkax.html:html-ktx:1.1.2"
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencyResolutionManagement {
google()
mavenCentral()
maven("https://jitpack.io") //增加 jitPack Maven 仓库
maven("https://developer.huawei.com/repo/") // 配置 HMS Core SDK 的 Maven 仓地址
}
}
rootProject.name = "Leave Sheet"
Expand Down

0 comments on commit 4808d06

Please sign in to comment.