From cf24442cd7b18ffd7dfe3998ea40d8e5e1d3f16e Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 24 Aug 2017 15:54:31 +0200 Subject: Add invitation URL QR code scanning ; Refactor configuration dialogs --- .../pacien/tincapp/activities/ConfigureActivity.kt | 75 ++++++++++++---------- .../pacien/tincapp/activities/StatusActivity.kt | 10 +-- 2 files changed, 47 insertions(+), 38 deletions(-) (limited to 'app/src/main/java/org/pacien') diff --git a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt index ea37944..9027b14 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt @@ -1,14 +1,17 @@ package org.pacien.tincapp.activities +import android.app.Activity +import android.content.ActivityNotFoundException +import android.content.Intent +import android.net.Uri import android.os.Bundle import android.support.annotation.StringRes import android.support.v7.app.AlertDialog import android.view.View -import android.view.ViewGroup -import android.widget.EditText -import android.widget.FrameLayout import java8.util.concurrent.CompletableFuture import kotlinx.android.synthetic.main.base.* +import kotlinx.android.synthetic.main.dialog_network_generate.view.* +import kotlinx.android.synthetic.main.dialog_network_join.view.* import kotlinx.android.synthetic.main.page_configure.* import org.pacien.tincapp.R import org.pacien.tincapp.commands.Tinc @@ -22,6 +25,20 @@ import org.pacien.tincapp.extensions.Java.exceptionallyAccept */ class ConfigureActivity : BaseActivity() { + companion object { + val REQUEST_SCAN = 0 + val SCAN_PROVIDER = "com.google.zxing.client.android" + } + + private var joinDialog: View? = null + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) + joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) supportActionBar!!.setDisplayHomeAsUpEnabled(true) @@ -29,40 +46,32 @@ class ConfigureActivity : BaseActivity() { writeContent() } - fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val netNameField = EditText(this) - netNameField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) - netNameField.setHint(R.string.field_net_name) - - val nodeNameField = EditText(this) - nodeNameField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) - nodeNameField.setHint(R.string.field_node_name) - - val dialogFrame = layoutInflater.inflate(R.layout.dialog_frame, main_content, false) as ViewGroup - dialogFrame.addView(netNameField) - dialogFrame.addView(nodeNameField) + fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { + try { + startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) + } catch (e: ActivityNotFoundException) { + AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) + .setMessage(R.string.message_no_qr_code_scanner) + .setPositiveButton(R.string.action_install) { _, _ -> + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + } - AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(dialogFrame) - .setPositiveButton(R.string.action_create) { _, _ -> generateConf(netNameField.text.toString(), nodeNameField.text.toString()) } - .setNegativeButton(R.string.action_cancel, App.dismissAction).show() + fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) + .setPositiveButton(R.string.action_create) { _, _ -> + generateConf(genDialog.new_net_name.text.toString(), genDialog.new_node_name.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() } fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val netNameField = EditText(this) - netNameField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) - netNameField.setHint(R.string.field_net_name) - - val joinUrlField = EditText(this) - joinUrlField.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) - joinUrlField.setHint(R.string.field_invitation_url) - - val dialogFrame = layoutInflater.inflate(R.layout.dialog_frame, main_content, false) as ViewGroup - dialogFrame.addView(netNameField) - dialogFrame.addView(joinUrlField) - - AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(dialogFrame) - .setPositiveButton(R.string.action_join) { _, _ -> joinNetwork(netNameField.text.toString(), joinUrlField.text.toString()) } - .setNegativeButton(R.string.action_cancel, App.dismissAction).show() + joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) + .setPositiveButton(R.string.action_join) { _, _ -> + joinNetwork(joinDialog!!.net_name.text.toString(), joinDialog!!.invitation_url.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() } private fun writeContent() { diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt index 6d97dca..356ff6e 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt @@ -12,7 +12,7 @@ import android.widget.ArrayAdapter import android.widget.TextView import java8.util.concurrent.CompletableFuture import kotlinx.android.synthetic.main.base.* -import kotlinx.android.synthetic.main.dialog_text_monopsace.view.* +import kotlinx.android.synthetic.main.dialog_node_details.view.* import kotlinx.android.synthetic.main.fragment_list_view.* import kotlinx.android.synthetic.main.fragment_network_status_header.* import org.pacien.tincapp.R @@ -36,7 +36,7 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - nodeListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) + nodeListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) refreshTimer = Timer(true) layoutInflater.inflate(R.layout.fragment_list_view, main_content) @@ -81,7 +81,7 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef getNodeNames().thenAccept { runOnUiThread { nodeListAdapter?.setElements(it) - node_list_placeholder.visibility = if (nodeListAdapter?.isEmpty ?: true) View.VISIBLE else View.GONE + node_list_placeholder.visibility = if (nodeListAdapter?.isEmpty != false) View.VISIBLE else View.GONE list_wrapper.isRefreshing = false if (!TincVpnService.isConnected()) openStartActivity() } @@ -90,10 +90,10 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { val nodeName = (view as TextView).text.toString() - val dialogTextView = layoutInflater.inflate(R.layout.dialog_text_monopsace, main_content, false) + val dialogTextView = layoutInflater.inflate(R.layout.dialog_node_details, main_content, false) Tinc.info(TincVpnService.getCurrentNetName()!!, nodeName).thenAccept { runOnUiThread { - dialogTextView.dialog_text_monospace.text = it + dialogTextView.dialog_node_details.text = it AlertDialog.Builder(this) .setTitle(R.string.title_node_info) .setView(dialogTextView) -- cgit v1.2.3