blob: f037e08abdc4e9d252a268eac85e6ea3fad1e108 [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.binarytransparency
import android.graphics.Matrix
import android.graphics.SurfaceTexture
import android.os.Bundle
import android.util.Log
import android.util.Size
import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.text.Layout
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.android.settings.R
import com.android.settings.binarytransparency.VerifyAnotherDeviceActivity.Step
import com.android.settingslib.qrcode.QrCamera
import com.google.android.setupcompat.template.FooterBarMixin
import com.google.android.setupcompat.template.FooterButton
import com.google.android.setupdesign.GlifLayout
import com.google.android.setupdesign.template.HeaderMixin
/** Fragment for step 3 of verifying the device with another device. */
class VerifyAnotherDeviceStep3Fragment :
Fragment(), TextureView.SurfaceTextureListener, QrCamera.ScannerCallback {
private var mCamera: QrCamera? = null
private lateinit var mTextureView: TextureView
private lateinit var mErrorMessage: TextView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
return inflater.inflate(R.layout.verify_another_device_step3, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val glifLayout = view.findViewById<GlifLayout>(R.id.setup_wizard_layout)
val headerMixin = glifLayout.getMixin(HeaderMixin::class.java)
headerMixin?.textView?.hyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE
val mixin = glifLayout.getMixin(FooterBarMixin::class.java)
mixin.secondaryButton =
FooterButton.Builder(requireContext())
.setText(R.string.wizard_back)
.setListener { parentFragmentManager.popBackStack() }
.setButtonType(FooterButton.ButtonType.CANCEL)
.setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
.build()
mTextureView = view.findViewById(R.id.preview_view)
mTextureView.surfaceTextureListener = this
mErrorMessage = view.findViewById(R.id.error_message)
}
override fun onResume() {
super.onResume()
(activity as? VerifyAnotherDeviceActivity)?.scannedInformation = ""
restartCamera()
}
override fun onPause() {
mCamera?.stop()
super.onPause()
}
private fun initCamera(surface: SurfaceTexture) {
if (mCamera == null) {
mCamera = QrCamera(requireContext(), this)
mCamera?.start(surface)
}
}
private fun destroyCamera() {
mCamera?.stop()
mCamera = null
}
private fun restartCamera() {
if (mCamera == null) {
Log.d(TAG, "mCamera is not available for restarting camera")
return
}
if (mCamera?.isDecodeTaskAlive == false) {
mCamera?.start(mTextureView.surfaceTexture)
}
}
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
initCamera(surface)
}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
destroyCamera()
return true
}
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}
override fun handleSuccessfulResult(qrCode: String) {
Log.i(TAG, "Successfully scanned QR code $qrCode")
activity?.runOnUiThread {
val verifierActivity = activity as? VerifyAnotherDeviceActivity
verifierActivity?.scannedInformation = qrCode
verifierActivity?.showStep(Step.PROCESSING)
}
}
override fun handleCameraFailure() {
destroyCamera()
}
override fun isValid(qrCode: String): Boolean {
return qrCode.startsWith("transparency://challenge")
}
override fun getViewSize(): Size {
return Size(mTextureView.width, mTextureView.height)
}
override fun setTransform(transform: Matrix) {
activity?.runOnUiThread { mTextureView.setTransform(transform) }
}
companion object {
private const val TAG = "VerifyAnotherDeviceStep3Fragment"
}
}