diff --git a/res/layout/activity_seed.xml b/res/layout/activity_seed.xml
index 1ac6b7e57a..cb77523182 100644
--- a/res/layout/activity_seed.xml
+++ b/res/layout/activity_seed.xml
@@ -96,6 +96,17 @@
app:labeledEditText_background="@color/loki_darkest_gray"
app:labeledEditText_label="@string/activity_key_pair_public_key_edit_text_label"/>
+
+
diff --git a/res/layout/fragment_scan_qr_code.xml b/res/layout/fragment_scan_qr_code.xml
index e86ae68ba8..4a3aa5fe79 100644
--- a/res/layout/fragment_scan_qr_code.xml
+++ b/res/layout/fragment_scan_qr_code.xml
@@ -24,13 +24,14 @@
android:layout_height="match_parent"/>
diff --git a/res/layout/view_device_linking.xml b/res/layout/view_device_linking.xml
index 1a793dd83f..6b173e27a1 100644
--- a/res/layout/view_device_linking.xml
+++ b/res/layout/view_device_linking.xml
@@ -16,6 +16,13 @@
android:indeterminate="true"
android:progressTint="@color/white" />
+
+
Cancel
Scan QR Code
- 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\".
+ 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\".
+ Scan the QR code of the device you would like to link.
Loki Messenger needs camera access to scan QR codes.
Copy public key
diff --git a/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt b/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt
index 393ad58398..bf91bf80ec 100644
--- a/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt
+++ b/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt
@@ -4,13 +4,17 @@ import android.content.Context
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Handler
-import android.text.Editable
-import android.text.TextWatcher
import android.util.AttributeSet
+import android.util.DisplayMetrics
import android.view.View
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.view_device_linking.view.*
+import kotlinx.android.synthetic.main.view_device_linking.view.cancelButton
+import kotlinx.android.synthetic.main.view_device_linking.view.explanationTextView
+import kotlinx.android.synthetic.main.view_device_linking.view.titleTextView
import network.loki.messenger.R
+import org.thoughtcrime.securesms.qr.QrCode
+import org.thoughtcrime.securesms.util.ServiceUtil
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.loki.api.PairingAuthorisation
import org.whispersystems.signalservice.loki.crypto.MnemonicCodec
@@ -55,6 +59,19 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
}
authorizeButton.visibility = View.GONE
authorizeButton.setOnClickListener { authorizePairing() }
+
+ // QR Code
+ spinner.visibility = if (mode == Mode.Master) View.GONE else View.VISIBLE
+ qrCodeImageView.visibility = if (mode == Mode.Master) View.VISIBLE else View.GONE
+ if (mode == Mode.Master) {
+ val hexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
+ val displayMetrics = DisplayMetrics()
+ ServiceUtil.getWindowManager(context).defaultDisplay.getMetrics(displayMetrics)
+ val size = displayMetrics.widthPixels - 2 * toPx(96, resources)
+ val qrCode = QrCode.create(hexEncodedPublicKey, size)
+ qrCodeImageView.setImageBitmap(qrCode)
+ }
+
cancelButton.setOnClickListener { cancel() }
}
// endregion
@@ -64,6 +81,7 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
if (mode != Mode.Master || pairingAuthorisation.type != PairingAuthorisation.Type.REQUEST || this.pairingAuthorisation != null) { return }
this.pairingAuthorisation = pairingAuthorisation
spinner.visibility = View.GONE
+ qrCodeImageView.visibility = View.GONE
val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams
titleTextViewLayoutParams.topMargin = toPx(16, resources)
titleTextView.layoutParams = titleTextViewLayoutParams
diff --git a/src/org/thoughtcrime/securesms/loki/ScanQRCodeFragment.kt b/src/org/thoughtcrime/securesms/loki/ScanQRCodeFragment.kt
index a4686bef21..874f143ee7 100644
--- a/src/org/thoughtcrime/securesms/loki/ScanQRCodeFragment.kt
+++ b/src/org/thoughtcrime/securesms/loki/ScanQRCodeFragment.kt
@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.loki
import android.content.res.Configuration
import android.os.Bundle
import android.support.v4.app.Fragment
+import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -14,8 +15,15 @@ import org.thoughtcrime.securesms.qr.ScanningThread
class ScanQRCodeFragment : Fragment() {
private val scanningThread = ScanningThread()
+ private var viewCreated = false
var scanListener: ScanListener? = null
set(value) { field = value; scanningThread.setScanListener(scanListener) }
+ var mode: Mode = Mode.NewConversation
+ set(value) { field = value; updateDescription(); }
+
+ // region Types
+ enum class Mode { NewConversation, LinkDevice }
+ // endregion
override fun onCreateView(layoutInflater: LayoutInflater, viewGroup: ViewGroup?, bundle: Bundle?): View? {
return layoutInflater.inflate(R.layout.fragment_scan_qr_code, viewGroup, false)
@@ -23,10 +31,12 @@ class ScanQRCodeFragment : Fragment() {
override fun onViewCreated(view: View, bundle: Bundle?) {
super.onViewCreated(view, bundle)
+ viewCreated = true
when (resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> overlayView.orientation = LinearLayout.HORIZONTAL
else -> overlayView.orientation = LinearLayout.VERTICAL
}
+ updateDescription()
}
override fun onResume() {
@@ -35,8 +45,10 @@ class ScanQRCodeFragment : Fragment() {
this.cameraView.onResume()
this.cameraView.setPreviewCallback(scanningThread)
this.scanningThread.start()
- val activity = activity as NewConversationActivity
- activity.supportActionBar!!.setTitle(R.string.fragment_scan_qr_code_title)
+ if (activity is AppCompatActivity) {
+ val activity = activity as AppCompatActivity
+ activity.supportActionBar?.setTitle(R.string.fragment_scan_qr_code_title)
+ }
}
override fun onPause() {
@@ -55,4 +67,13 @@ class ScanQRCodeFragment : Fragment() {
cameraView.onResume()
cameraView.setPreviewCallback(scanningThread)
}
+
+ 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)
+ }
}
\ No newline at end of file
diff --git a/src/org/thoughtcrime/securesms/loki/SeedActivity.kt b/src/org/thoughtcrime/securesms/loki/SeedActivity.kt
index 04c788bf5e..e2b48a3ef1 100644
--- a/src/org/thoughtcrime/securesms/loki/SeedActivity.kt
+++ b/src/org/thoughtcrime/securesms/loki/SeedActivity.kt
@@ -1,11 +1,13 @@
package org.thoughtcrime.securesms.loki
+import android.Manifest
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.AsyncTask
import android.os.Bundle
+import android.support.v4.app.FragmentManager
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
@@ -19,6 +21,8 @@ import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.IdentityDatabase
import org.thoughtcrime.securesms.logging.Log
+import org.thoughtcrime.securesms.permissions.Permissions
+import org.thoughtcrime.securesms.qr.ScanListener
import org.thoughtcrime.securesms.util.Hex
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.curve25519.Curve25519
@@ -32,7 +36,7 @@ import org.whispersystems.signalservice.loki.utilities.retryIfNeeded
import java.io.File
import java.io.FileOutputStream
-class SeedActivity : BaseActionBarActivity(), DeviceLinkingDelegate {
+class SeedActivity : BaseActionBarActivity(), DeviceLinkingDelegate, ScanListener {
private lateinit var languageFileDirectory: File
private var mode = Mode.Register
set(newValue) { field = newValue; updateUI() }
@@ -57,6 +61,23 @@ class SeedActivity : BaseActionBarActivity(), DeviceLinkingDelegate {
toggleRestoreModeButton.setOnClickListener { mode = Mode.Restore }
toggleLinkModeButton.setOnClickListener { mode = Mode.Link }
mainButton.setOnClickListener { handleMainButtonTapped() }
+ scanQRButton.setOnClickListener {
+ Permissions.with(this)
+ .request(Manifest.permission.CAMERA)
+ .ifNecessary()
+ .withPermanentDenialDialog(getString(R.string.fragment_scan_qr_code_camera_permission_dialog_message))
+ .onAllGranted {
+ val fragment = ScanQRCodeFragment()
+ fragment.mode = ScanQRCodeFragment.Mode.LinkDevice
+ fragment.scanListener = this
+ supportFragmentManager.beginTransaction().replace(android.R.id.content, fragment).addToBackStack("QR").commitAllowingStateLoss()
+ publicKeyEditText.clearFocus()
+ val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
+ inputMethodManager.hideSoftInputFromWindow(publicKeyEditText.windowToken, 0)
+ }
+ .onAnyDenied { Toast.makeText(this, R.string.fragment_scan_qr_code_camera_permission_dialog_message, Toast.LENGTH_SHORT).show() }
+ .execute()
+ }
Analytics.shared.track("Seed Screen Viewed")
}
// endregion
@@ -106,6 +127,7 @@ class SeedActivity : BaseActionBarActivity(), DeviceLinkingDelegate {
mnemonicEditText.visibility = restoreModeVisibility
linkExplanationTextView.visibility = linkModeVisibility
publicKeyEditText.visibility = linkModeVisibility
+ scanQRButton.visibility = linkModeVisibility
toggleRegisterModeButton.visibility = if (mode != Mode.Register) View.VISIBLE else View.GONE
toggleRestoreModeButton.visibility = if (mode != Mode.Restore) View.VISIBLE else View.GONE
toggleLinkModeButton.visibility = if (mode != Mode.Link) View.VISIBLE else View.GONE
@@ -236,4 +258,14 @@ class SeedActivity : BaseActionBarActivity(), DeviceLinkingDelegate {
TextSecurePreferences.setPromptedPushRegistration(this, false)
}
// endregion
+ override fun onQrDataFound(data: String?) {
+ runOnUiThread {
+ if (data != null && PublicKeyValidation.isValid(data.trim())) {
+ publicKeyEditText.setText(data.trim())
+ supportFragmentManager.popBackStackImmediate("QR", FragmentManager.POP_BACK_STACK_INCLUSIVE)
+ handleMainButtonTapped()
+ }
+ }
+ }
+ // endregion
}
\ No newline at end of file
diff --git a/src/org/thoughtcrime/securesms/qr/QrCode.java b/src/org/thoughtcrime/securesms/qr/QrCode.java
index f561daa2d2..65574aba20 100644
--- a/src/org/thoughtcrime/securesms/qr/QrCode.java
+++ b/src/org/thoughtcrime/securesms/qr/QrCode.java
@@ -5,12 +5,15 @@ import android.graphics.Color;
import android.support.annotation.NonNull;
import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.thoughtcrime.securesms.logging.Log;
+import java.util.HashMap;
+
public class QrCode {
public static final String TAG = QrCode.class.getSimpleName();
@@ -18,10 +21,12 @@ public class QrCode {
public static @NonNull Bitmap create(String data) {
return create(data, 1024);
}
-
- public static @NonNull Bitmap create(String data, int size) {
+ public static @NonNull Bitmap create(String data, int size) { return create(data, size, 2); }
+ public static @NonNull Bitmap create(String data, int size, int margin) {
try {
- BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, size, size);
+ HashMap hintMap = new HashMap<>();
+ hintMap.put(EncodeHintType.MARGIN, margin);
+ BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, size, size, hintMap);
Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
for (int y = 0; y < result.getHeight(); y++) {