session-android/src/org/thoughtcrime/securesms/loki/NewConversationActivity.kt

81 lines
3.5 KiB
Kotlin
Raw Normal View History

2019-07-18 05:59:41 +00:00
package org.thoughtcrime.securesms.loki
2019-08-23 01:46:28 +00:00
import android.Manifest
2019-07-18 05:59:41 +00:00
import android.content.Intent
import android.os.Bundle
2019-07-22 04:05:51 +00:00
import android.view.MenuItem
2019-07-18 05:59:41 +00:00
import android.widget.Toast
2019-08-23 01:46:28 +00:00
import network.loki.messenger.R
2019-07-18 05:59:41 +00:00
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
2019-08-23 01:46:28 +00:00
import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.qr.ScanListener
2019-07-18 05:59:41 +00:00
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.util.DynamicTheme
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation
2019-08-23 01:46:28 +00:00
class NewConversationActivity : PassphraseRequiredActionBarActivity(), ScanListener {
2019-07-18 05:59:41 +00:00
private val dynamicTheme = DynamicTheme()
override fun onPreCreate() {
dynamicTheme.onCreate(this)
}
override fun onCreate(bundle: Bundle?, isReady: Boolean) {
2019-08-23 03:22:52 +00:00
supportActionBar!!.setTitle(R.string.fragment_new_conversation_title)
2019-07-18 05:59:41 +00:00
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
2019-08-23 01:46:28 +00:00
val fragment = NewConversationFragment()
initFragment(android.R.id.content, fragment, null)
2019-07-18 05:59:41 +00:00
}
public override fun onResume() {
super.onResume()
dynamicTheme.onResume(this)
}
2019-07-22 04:05:51 +00:00
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
onBackPressed()
return true
}
return super.onOptionsItemSelected(item)
}
2019-08-23 01:46:28 +00:00
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()
2019-08-23 03:22:52 +00:00
fragment.scanListener = this
2019-08-23 01:46:28 +00:00
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) {
2019-07-18 05:59:41 +00:00
if (PublicKeyValidation.isValid(hexEncodedPublicKey)) {
val contact = Recipient.from(this, Address.fromSerialized(hexEncodedPublicKey), true)
val intent = Intent(this, ConversationActivity::class.java)
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, contact.address)
intent.putExtra(ConversationActivity.TEXT_EXTRA, getIntent().getStringExtra(ConversationActivity.TEXT_EXTRA))
intent.setDataAndType(getIntent().data, getIntent().type)
val existingThread = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(contact)
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, existingThread)
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, ThreadDatabase.DistributionTypes.DEFAULT)
startActivity(intent)
finish()
} else {
2019-08-23 03:22:52 +00:00
Toast.makeText(this, R.string.fragment_new_conversation_invalid_public_key_message, Toast.LENGTH_SHORT).show()
2019-07-18 05:59:41 +00:00
}
}
}