2017-08-25 20:24:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Christian Basler
|
|
|
|
*
|
|
|
|
* 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 ch.dissem.apps.abit
|
|
|
|
|
|
|
|
import android.app.Activity.RESULT_OK
|
2018-02-13 07:24:24 +01:00
|
|
|
import android.content.Context
|
2017-08-25 20:24:25 +02:00
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.support.v4.app.Fragment
|
|
|
|
import android.view.*
|
|
|
|
import android.widget.AdapterView
|
|
|
|
import android.widget.Toast
|
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_BROADCAST
|
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_CONTENT
|
2018-02-13 07:24:24 +01:00
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_DRAFT
|
2017-08-25 20:24:25 +02:00
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_ENCODING
|
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_IDENTITY
|
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_PARENT
|
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_RECIPIENT
|
|
|
|
import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_SUBJECT
|
|
|
|
import ch.dissem.apps.abit.adapter.ContactAdapter
|
|
|
|
import ch.dissem.apps.abit.dialog.SelectEncodingDialogFragment
|
|
|
|
import ch.dissem.apps.abit.service.Singleton
|
2018-07-11 16:59:50 +02:00
|
|
|
import ch.dissem.apps.abit.util.preferences
|
2017-08-25 20:24:25 +02:00
|
|
|
import ch.dissem.bitmessage.entity.BitmessageAddress
|
|
|
|
import ch.dissem.bitmessage.entity.Plaintext
|
|
|
|
import ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST
|
|
|
|
import ch.dissem.bitmessage.entity.Plaintext.Type.MSG
|
2018-02-13 07:24:24 +01:00
|
|
|
import ch.dissem.bitmessage.entity.valueobject.ExtendedEncoding
|
|
|
|
import ch.dissem.bitmessage.entity.valueobject.InventoryVector
|
|
|
|
import ch.dissem.bitmessage.entity.valueobject.Label
|
2017-08-25 20:24:25 +02:00
|
|
|
import ch.dissem.bitmessage.entity.valueobject.extended.Message
|
|
|
|
import kotlinx.android.synthetic.main.fragment_compose_message.*
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose a new message.
|
|
|
|
*/
|
|
|
|
class ComposeMessageFragment : Fragment() {
|
|
|
|
private lateinit var identity: BitmessageAddress
|
|
|
|
private var recipient: BitmessageAddress? = null
|
|
|
|
private var subject: String = ""
|
|
|
|
private var content: String = ""
|
|
|
|
|
|
|
|
private var broadcast: Boolean = false
|
|
|
|
private var encoding: Plaintext.Encoding = Plaintext.Encoding.SIMPLE
|
2018-02-13 07:24:24 +01:00
|
|
|
private val parents = mutableListOf<InventoryVector>()
|
|
|
|
|
|
|
|
private var draft: Plaintext? = null
|
2017-08-25 20:24:25 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2018-02-24 07:39:06 +01:00
|
|
|
retainInstance = true
|
2018-02-13 07:24:24 +01:00
|
|
|
arguments?.apply {
|
|
|
|
val draft = getSerializable(EXTRA_DRAFT) as Plaintext?
|
|
|
|
if (draft != null) {
|
|
|
|
this@ComposeMessageFragment.draft = draft
|
|
|
|
identity = draft.from
|
|
|
|
recipient = draft.to
|
|
|
|
subject = draft.subject ?: ""
|
|
|
|
content = draft.text ?: ""
|
|
|
|
encoding = draft.encoding ?: Plaintext.Encoding.SIMPLE
|
|
|
|
parents.addAll(draft.parents)
|
2017-08-25 20:24:25 +02:00
|
|
|
} else {
|
2018-02-13 07:24:24 +01:00
|
|
|
var id = getSerializable(EXTRA_IDENTITY) as? BitmessageAddress
|
2018-07-05 19:44:04 +02:00
|
|
|
if (context != null && id?.privateKey == null) {
|
2018-02-13 07:24:24 +01:00
|
|
|
id = Singleton.getIdentity(context!!)
|
|
|
|
}
|
|
|
|
if (id?.privateKey != null) {
|
|
|
|
identity = id
|
|
|
|
} else {
|
|
|
|
throw IllegalStateException("No identity set for ComposeMessageFragment")
|
|
|
|
}
|
|
|
|
broadcast = getBoolean(EXTRA_BROADCAST, false)
|
|
|
|
if (containsKey(EXTRA_RECIPIENT)) {
|
|
|
|
recipient = getSerializable(EXTRA_RECIPIENT) as BitmessageAddress
|
|
|
|
}
|
|
|
|
if (containsKey(EXTRA_SUBJECT)) {
|
|
|
|
subject = getString(EXTRA_SUBJECT)
|
|
|
|
}
|
|
|
|
if (containsKey(EXTRA_CONTENT)) {
|
|
|
|
content = getString(EXTRA_CONTENT)
|
|
|
|
}
|
2018-07-11 16:59:50 +02:00
|
|
|
encoding = getSerializable(EXTRA_ENCODING) as? Plaintext.Encoding ?: Plaintext.Encoding.SIMPLE
|
2017-08-25 20:24:25 +02:00
|
|
|
|
2018-02-13 07:24:24 +01:00
|
|
|
if (containsKey(EXTRA_PARENT)) {
|
|
|
|
val parent = getSerializable(EXTRA_PARENT) as Plaintext
|
|
|
|
parent.inventoryVector?.let { parents.add(it) }
|
|
|
|
}
|
2017-08-25 20:24:25 +02:00
|
|
|
}
|
2018-02-14 17:49:39 +01:00
|
|
|
} ?: throw IllegalStateException("No identity set for ComposeMessageFragment")
|
|
|
|
|
2017-08-25 20:24:25 +02:00
|
|
|
setHasOptionsMenu(true)
|
|
|
|
}
|
|
|
|
|
2018-02-13 07:24:24 +01:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View = inflater.inflate(R.layout.fragment_compose_message, container, false)
|
2017-08-25 20:24:25 +02:00
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2018-02-14 17:49:39 +01:00
|
|
|
context?.let { ctx ->
|
|
|
|
val identities = Singleton.getAddressRepository(ctx).getIdentities()
|
|
|
|
sender_input.adapter = ContactAdapter(ctx, identities, true)
|
|
|
|
val index = identities.indexOf(Singleton.getIdentity(ctx))
|
|
|
|
if (index >= 0) {
|
|
|
|
sender_input.setSelection(index)
|
|
|
|
}
|
2017-08-25 20:24:25 +02:00
|
|
|
|
2018-02-14 17:49:39 +01:00
|
|
|
if (broadcast) {
|
|
|
|
recipient_input.visibility = View.GONE
|
|
|
|
} else {
|
|
|
|
val adapter = ContactAdapter(
|
|
|
|
ctx,
|
|
|
|
Singleton.getAddressRepository(ctx).getContacts()
|
|
|
|
)
|
|
|
|
recipient_input.setAdapter(adapter)
|
|
|
|
recipient_input.onItemClickListener =
|
2018-02-25 23:35:03 +01:00
|
|
|
AdapterView.OnItemClickListener { _, _, pos, _ -> recipient = adapter.getItem(pos) }
|
|
|
|
|
2018-02-14 17:49:39 +01:00
|
|
|
recipient_input.onItemSelectedListener =
|
|
|
|
object : AdapterView.OnItemSelectedListener {
|
|
|
|
override fun onItemSelected(
|
|
|
|
parent: AdapterView<*>,
|
|
|
|
view: View,
|
|
|
|
position: Int,
|
|
|
|
id: Long
|
|
|
|
) {
|
|
|
|
recipient = adapter.getItem(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onNothingSelected(parent: AdapterView<*>) =
|
|
|
|
Unit // leave current selection
|
|
|
|
}
|
|
|
|
recipient?.let { recipient_input.setText(it.toString()) }
|
2017-08-25 20:24:25 +02:00
|
|
|
}
|
2018-02-14 17:49:39 +01:00
|
|
|
subject_input.setText(subject)
|
|
|
|
body_input.setText(content)
|
|
|
|
|
|
|
|
when {
|
|
|
|
recipient == null -> recipient_input.requestFocus()
|
|
|
|
subject.isEmpty() -> subject_input.requestFocus()
|
|
|
|
else -> {
|
|
|
|
body_input.requestFocus()
|
|
|
|
body_input.setSelection(0)
|
|
|
|
}
|
2017-08-29 21:14:50 +02:00
|
|
|
}
|
2017-08-25 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater) {
|
|
|
|
inflater.inflate(R.menu.compose, menu)
|
|
|
|
super.onCreateOptionsMenu(menu, inflater)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
when (item.itemId) {
|
|
|
|
R.id.send -> {
|
|
|
|
send()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
R.id.select_encoding -> {
|
|
|
|
val encodingDialog = SelectEncodingDialogFragment()
|
|
|
|
val args = Bundle()
|
|
|
|
args.putSerializable(EXTRA_ENCODING, encoding)
|
|
|
|
encodingDialog.arguments = args
|
|
|
|
encodingDialog.setTargetFragment(this, 0)
|
|
|
|
encodingDialog.show(fragmentManager, "select encoding dialog")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else -> return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 07:24:24 +01:00
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) =
|
|
|
|
if (requestCode == 0 && data != null && resultCode == RESULT_OK) {
|
|
|
|
encoding = data.getSerializableExtra(EXTRA_ENCODING) as Plaintext.Encoding
|
|
|
|
} else {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
}
|
2017-08-25 20:24:25 +02:00
|
|
|
|
2018-02-13 07:24:24 +01:00
|
|
|
private fun build(ctx: Context): Plaintext {
|
2017-08-25 20:24:25 +02:00
|
|
|
val builder: Plaintext.Builder
|
|
|
|
if (broadcast) {
|
2018-02-14 17:49:39 +01:00
|
|
|
builder = Plaintext.Builder(BROADCAST)
|
2017-08-25 20:24:25 +02:00
|
|
|
} else {
|
|
|
|
val inputString = recipient_input.text.toString()
|
|
|
|
if (recipient == null || recipient?.toString() != inputString) {
|
|
|
|
try {
|
|
|
|
recipient = BitmessageAddress(inputString)
|
|
|
|
} catch (e: Exception) {
|
2017-10-31 07:50:57 +01:00
|
|
|
val contacts = Singleton.getAddressRepository(ctx).getContacts()
|
2017-08-25 20:24:25 +02:00
|
|
|
for (contact in contacts) {
|
|
|
|
if (inputString.equals(contact.alias, ignoreCase = true)) {
|
|
|
|
recipient = contact
|
|
|
|
if (inputString == contact.alias)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
builder = Plaintext.Builder(MSG)
|
2018-02-13 07:24:24 +01:00
|
|
|
.to(recipient)
|
2017-08-25 20:24:25 +02:00
|
|
|
}
|
2018-02-14 17:49:39 +01:00
|
|
|
val sender = sender_input.selectedItem as? ch.dissem.bitmessage.entity.BitmessageAddress
|
|
|
|
sender?.let { builder.from(it) }
|
2018-07-11 16:59:50 +02:00
|
|
|
if (!ctx.preferences.requestAcknowledgements) {
|
2017-08-25 20:24:25 +02:00
|
|
|
builder.preventAck()
|
|
|
|
}
|
|
|
|
when (encoding) {
|
|
|
|
Plaintext.Encoding.SIMPLE -> builder.message(
|
2018-02-13 07:24:24 +01:00
|
|
|
subject_input.text.toString(),
|
|
|
|
body_input.text.toString()
|
2017-08-25 20:24:25 +02:00
|
|
|
)
|
|
|
|
Plaintext.Encoding.EXTENDED -> builder.message(
|
2018-02-13 07:24:24 +01:00
|
|
|
ExtendedEncoding(
|
|
|
|
Message(
|
|
|
|
subject = subject_input.text.toString(),
|
|
|
|
body = body_input.text.toString(),
|
|
|
|
parents = parents,
|
|
|
|
files = emptyList()
|
|
|
|
)
|
|
|
|
)
|
2017-08-25 20:24:25 +02:00
|
|
|
)
|
|
|
|
else -> {
|
|
|
|
Toast.makeText(
|
2018-02-13 07:24:24 +01:00
|
|
|
ctx,
|
|
|
|
ctx.getString(R.string.error_unsupported_encoding, encoding),
|
|
|
|
Toast.LENGTH_LONG
|
2017-08-25 20:24:25 +02:00
|
|
|
).show()
|
|
|
|
builder.message(
|
2018-02-13 07:24:24 +01:00
|
|
|
subject_input.text.toString(),
|
|
|
|
body_input.text.toString()
|
2017-08-25 20:24:25 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 07:24:24 +01:00
|
|
|
draft?.id?.let { builder.id(it) }
|
|
|
|
return builder.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
if (draft?.labels?.any { it.type == Label.Type.DRAFT } != false) {
|
|
|
|
context?.let { ctx ->
|
|
|
|
draft = build(ctx).also { msg ->
|
|
|
|
Singleton.labeler.markAsDraft(msg)
|
|
|
|
Singleton.getMessageRepository(ctx).save(msg)
|
|
|
|
}
|
|
|
|
Toast.makeText(ctx, "Message saved as draft", Toast.LENGTH_LONG).show()
|
|
|
|
} ?: throw IllegalStateException("Context is not available")
|
|
|
|
}
|
|
|
|
super.onPause()
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:39:06 +01:00
|
|
|
override fun onDestroyView() {
|
|
|
|
identity = sender_input.selectedItem as BitmessageAddress
|
|
|
|
// recipient is set when one is selected
|
|
|
|
subject = subject_input.text?.toString() ?: ""
|
|
|
|
content = body_input.text?.toString() ?: ""
|
|
|
|
super.onDestroyView()
|
|
|
|
}
|
|
|
|
|
2018-02-13 07:24:24 +01:00
|
|
|
private fun send() {
|
|
|
|
val ctx = activity ?: throw IllegalStateException("Fragment is not attached to an activity")
|
|
|
|
if (recipient == null) {
|
|
|
|
Toast.makeText(ctx, R.string.error_msg_recipient_missing, Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
build(ctx).let { message ->
|
|
|
|
draft = message
|
|
|
|
Singleton.getBitmessageContext(ctx).send(message)
|
|
|
|
}
|
2017-10-31 07:50:57 +01:00
|
|
|
ctx.finish()
|
2017-08-25 20:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|