79 lines
2.9 KiB
Kotlin
Raw Normal View History

2019-08-23 11:46:28 +10:00
package org.thoughtcrime.securesms.loki
import android.content.res.Configuration
import android.os.Bundle
import android.support.v4.app.Fragment
2019-11-22 12:08:09 +11:00
import android.support.v7.app.AppCompatActivity
2019-08-23 11:46:28 +10:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
2019-08-23 13:22:52 +10:00
import kotlinx.android.synthetic.main.fragment_scan_qr_code.*
2019-08-23 11:46:28 +10:00
import network.loki.messenger.R
import org.thoughtcrime.securesms.qr.ScanListener
import org.thoughtcrime.securesms.qr.ScanningThread
class ScanQRCodeFragment : Fragment() {
2019-08-23 13:22:52 +10:00
private val scanningThread = ScanningThread()
2019-11-22 12:08:09 +11:00
private var viewCreated = false
2019-08-23 13:22:52 +10:00
var scanListener: ScanListener? = null
set(value) { field = value; scanningThread.setScanListener(scanListener) }
2019-11-22 12:08:09 +11:00
var mode: Mode = Mode.NewConversation
set(value) { field = value; updateDescription(); }
// region Types
enum class Mode { NewConversation, LinkDevice }
// endregion
2019-08-23 11:46:28 +10:00
2019-08-23 13:22:52 +10:00
override fun onCreateView(layoutInflater: LayoutInflater, viewGroup: ViewGroup?, bundle: Bundle?): View? {
return layoutInflater.inflate(R.layout.fragment_scan_qr_code, viewGroup, false)
}
2019-08-23 11:46:28 +10:00
2019-08-23 13:22:52 +10:00
override fun onViewCreated(view: View, bundle: Bundle?) {
super.onViewCreated(view, bundle)
2019-11-22 12:08:09 +11:00
viewCreated = true
2019-08-23 13:22:52 +10:00
when (resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> overlayView.orientation = LinearLayout.HORIZONTAL
else -> overlayView.orientation = LinearLayout.VERTICAL
2019-08-23 11:46:28 +10:00
}
2019-11-22 12:08:09 +11:00
updateDescription()
2019-08-23 11:46:28 +10:00
}
override fun onResume() {
super.onResume()
2019-08-23 13:22:52 +10:00
this.scanningThread.setScanListener(scanListener)
this.cameraView.onResume()
this.cameraView.setPreviewCallback(scanningThread)
this.scanningThread.start()
2019-11-22 12:08:09 +11:00
if (activity is AppCompatActivity) {
val activity = activity as AppCompatActivity
activity.supportActionBar?.setTitle(R.string.fragment_scan_qr_code_title)
}
2019-08-23 11:46:28 +10:00
}
override fun onPause() {
super.onPause()
2019-08-23 13:22:52 +10:00
this.cameraView.onPause()
this.scanningThread.stopScanning()
2019-08-23 11:46:28 +10:00
}
2019-08-23 13:22:52 +10:00
override fun onConfigurationChanged(newConfiguration: Configuration) {
2019-08-23 11:46:28 +10:00
super.onConfigurationChanged(newConfiguration)
2019-08-23 13:22:52 +10:00
this.cameraView.onPause()
when (newConfiguration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> overlayView.orientation = LinearLayout.HORIZONTAL
else -> overlayView.orientation = LinearLayout.VERTICAL
2019-08-23 11:46:28 +10:00
}
2019-08-23 13:22:52 +10:00
cameraView.onResume()
cameraView.setPreviewCallback(scanningThread)
2019-08-23 11:46:28 +10:00
}
2019-11-22 12:08:09 +11:00
fun updateDescription() {
if (!viewCreated) { return }
val text = when (mode) {
Mode.NewConversation -> R.string.fragment_scan_qr_code_explanation_new_conversation
Mode.LinkDevice -> R.string.fragment_scan_qr_code_explanation_link_device
}
descriptionTextView.setText(text)
}
2019-08-23 11:46:28 +10:00
}