Implement QR code scanning

This commit is contained in:
Niels Andriesse 2019-08-23 11:46:28 +10:00
parent f3377fe384
commit 5d34314a00
6 changed files with 216 additions and 9 deletions

View File

@ -28,6 +28,17 @@
android:layout_marginTop="24dp"
android:text="@string/activity_new_conversation_public_key_explanation" />
<Button
android:id="@+id/qrCodeButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="@color/transparent"
android:textColor="@color/signal_primary"
android:text="@string/activity_new_conversation_qr_code_button_title"
android:elevation="0dp"
android:stateListAnimator="@null" />
<com.dd.CircularProgressButton
android:id="@+id/nextButton"
android:layout_width="match_parent"
@ -41,7 +52,7 @@
app:cpb_colorProgress="@color/textsecure_primary"
app:cpb_cornerRadius="4dp"
app:cpb_selectorIdle="@drawable/progress_button_state"
app:cpb_textIdle="@string/activity_new_conversation_button_title" />
app:cpb_textIdle="@string/activity_new_conversation_next_button_title" />
</LinearLayout>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<org.thoughtcrime.securesms.components.camera.CameraView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:camera="0" />
<LinearLayout
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<org.thoughtcrime.securesms.components.ShapeScrim
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="32dp"
android:gravity="center"
android:background="@color/loki_darkest_gray"
android:text="@string/fragment_scan_qr_code_explanation"
android:textColor="?android:textColorPrimary" />
</LinearLayout>
</FrameLayout>

View File

@ -1588,7 +1588,8 @@
<string name="activity_new_conversation_title">New Conversation</string>
<string name="activity_new_conversation_public_key_edit_text_label">Public Key</string>
<string name="activity_new_conversation_public_key_explanation">Enter the public key of the person you\'d like to securely message. They can share their public key with you by going into Loki Messenger\'s in-app settings and clicking \"Share Public Key\".</string>
<string name="activity_new_conversation_button_title">Next</string>
<string name="activity_new_conversation_qr_code_button_title">Scan a QR Code Instead</string>
<string name="activity_new_conversation_next_button_title">Next</string>
<string name="activity_new_conversation_invalid_public_key_message">Invalid Public Key</string>
<!-- Friend request view -->
<string name="view_friend_request_accept_button_title">Accept</string>
@ -1606,5 +1607,10 @@
<!-- QR code fragment -->
<string name="fragment_qr_code_title">Your QR Code</string>
<string name="fragment_qr_code_explanation">This is your personal QR code. Other people can scan it to start a secure conversation with you.</string>
<string name="fragment_qr_code_camera_permission_dialog_message">Loki Messenger needs camera access to scan QR codes.</string>
<string name="fragment_qr_code_camera_permission_denied_message">Loki Messenger can\'t scan QR codes without camera access.</string>
<!-- Scan QR code fragment -->
<string name="fragment_scan_qr_code_title">Scan QR Code</string>
<string name="fragment_scan_qr_code_explanation">Scan the QR code of the person you\'d like to securely message. They can find their QR code by going into Loki Messenger\'s in-app settings and clicking \"Show QR Code\".</string>
</resources>

View File

@ -1,21 +1,23 @@
package org.thoughtcrime.securesms.loki
import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_new_conversation.*
import network.loki.messenger.R;
import network.loki.messenger.R
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.conversation.ConversationActivity
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.qr.ScanListener
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.util.DynamicTheme
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation
class NewConversationActivity : PassphraseRequiredActionBarActivity() {
class NewConversationActivity : PassphraseRequiredActionBarActivity(), ScanListener {
private val dynamicTheme = DynamicTheme()
override fun onPreCreate() {
@ -23,10 +25,10 @@ class NewConversationActivity : PassphraseRequiredActionBarActivity() {
}
override fun onCreate(bundle: Bundle?, isReady: Boolean) {
setContentView(R.layout.activity_new_conversation)
supportActionBar!!.setTitle(R.string.activity_new_conversation_title)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
nextButton.setOnClickListener { startNewConversationIfPossible() }
val fragment = NewConversationFragment()
initFragment(android.R.id.content, fragment, null)
}
public override fun onResume() {
@ -42,8 +44,25 @@ class NewConversationActivity : PassphraseRequiredActionBarActivity() {
return super.onOptionsItemSelected(item)
}
private fun startNewConversationIfPossible() {
val hexEncodedPublicKey = publicKeyEditText.text.toString().trim()
fun scanQRCode() {
Permissions.with(this)
.request(Manifest.permission.CAMERA)
.ifNecessary()
.withPermanentDenialDialog(getString(R.string.fragment_qr_code_camera_permission_dialog_message))
.onAllGranted {
val fragment = ScanQRCodeFragment()
fragment.setScanListener(this)
supportFragmentManager.beginTransaction().replace(android.R.id.content, fragment).addToBackStack(null).commitAllowingStateLoss()
}
.onAnyDenied { Toast.makeText(this, R.string.fragment_qr_code_camera_permission_denied_message, Toast.LENGTH_SHORT).show() }
.execute()
}
override fun onQrDataFound(hexEncodedPublicKey: String) {
startNewConversationIfPossible(hexEncodedPublicKey)
}
fun startNewConversationIfPossible(hexEncodedPublicKey: String) {
if (PublicKeyValidation.isValid(hexEncodedPublicKey)) {
val contact = Recipient.from(this, Address.fromSerialized(hexEncodedPublicKey), true)
val intent = Intent(this, ConversationActivity::class.java)

View File

@ -0,0 +1,35 @@
package org.thoughtcrime.securesms.loki
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_new_conversation.*
import network.loki.messenger.R
class NewConversationFragment() : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.activity_new_conversation, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
qrCodeButton.setOnClickListener {
val activity = activity as NewConversationActivity
activity.scanQRCode()
}
nextButton.setOnClickListener {
val activity = activity as NewConversationActivity
val hexEncodedPublicKey = publicKeyEditText.text.toString().trim()
activity.startNewConversationIfPossible(hexEncodedPublicKey)
}
}
override fun onResume() {
super.onResume()
val activity = activity as NewConversationActivity
activity.supportActionBar!!.setTitle(R.string.activity_new_conversation_title)
}
}

View File

@ -0,0 +1,98 @@
package org.thoughtcrime.securesms.loki
import android.annotation.TargetApi
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewAnimationUtils
import android.view.ViewGroup
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import network.loki.messenger.R
import org.thoughtcrime.securesms.components.camera.CameraView
import org.thoughtcrime.securesms.qr.ScanListener
import org.thoughtcrime.securesms.qr.ScanningThread
import org.thoughtcrime.securesms.util.ViewUtil
class ScanQRCodeFragment : Fragment() {
private var container: ViewGroup? = null
private var overlay: LinearLayout? = null
private var scannerView: CameraView? = null
private var scanningThread: ScanningThread? = null
private var scanListener: ScanListener? = null
override fun onCreateView(inflater: LayoutInflater, viewGroup: ViewGroup?, bundle: Bundle?): View? {
this.container = ViewUtil.inflate(inflater, viewGroup!!, R.layout.fragment_scan_qr_code)
this.overlay = ViewUtil.findById(this.container!!, R.id.overlay)
this.scannerView = ViewUtil.findById(this.container!!, R.id.cameraView)
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.overlay!!.orientation = LinearLayout.HORIZONTAL
} else {
this.overlay!!.orientation = LinearLayout.VERTICAL
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.container!!.addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun onLayoutChange(v: View, left: Int, top: Int, right: Int, bottom: Int,
oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
v.removeOnLayoutChangeListener(this)
val reveal = ViewAnimationUtils.createCircularReveal(v, right, bottom, 0f, Math.hypot(right.toDouble(), bottom.toDouble()).toInt().toFloat())
reveal.interpolator = DecelerateInterpolator(2f)
reveal.duration = 800
reveal.start()
}
})
}
return this.container
}
override fun onResume() {
super.onResume()
this.scanningThread = ScanningThread()
this.scanningThread!!.setScanListener(scanListener)
this.scannerView!!.onResume()
this.scannerView!!.setPreviewCallback(scanningThread!!)
this.scanningThread!!.start()
val activity = activity as NewConversationActivity
activity.supportActionBar!!.setTitle(R.string.fragment_scan_qr_code_title)
}
override fun onPause() {
super.onPause()
this.scannerView!!.onPause()
this.scanningThread!!.stopScanning()
}
override fun onConfigurationChanged(newConfiguration: Configuration?) {
super.onConfigurationChanged(newConfiguration)
this.scannerView!!.onPause()
if (newConfiguration!!.orientation == Configuration.ORIENTATION_LANDSCAPE) {
overlay!!.orientation = LinearLayout.HORIZONTAL
} else {
overlay!!.orientation = LinearLayout.VERTICAL
}
this.scannerView!!.onResume()
this.scannerView!!.setPreviewCallback(scanningThread!!)
}
fun setScanListener(scanListener: ScanListener) {
this.scanListener = scanListener
if (this.scanningThread != null) {
this.scanningThread!!.setScanListener(scanListener)
}
}
}