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

85 lines
4.0 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
2019-08-26 03:55:45 +00:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
2019-09-04 23:38:36 +00:00
import org.whispersystems.signalservice.loki.utilities.Analytics
2019-07-18 05:59:41 +00:00
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()
2019-09-30 05:38:07 +00:00
.withPermanentDenialDialog(getString(R.string.fragment_scan_qr_code_camera_permission_dialog_message))
2019-08-23 01:46:28 +00:00
.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()
}
2019-09-30 05:38:07 +00:00
.onAnyDenied { Toast.makeText(this, R.string.fragment_scan_qr_code_camera_permission_dialog_message, Toast.LENGTH_SHORT).show() }
2019-08-23 01:46:28 +00:00
.execute()
}
override fun onQrDataFound(hexEncodedPublicKey: String) {
2019-09-04 23:38:36 +00:00
Analytics.shared.track("QR Code Scanned")
2019-08-23 01:46:28 +00:00
startNewConversationIfPossible(hexEncodedPublicKey)
}
fun startNewConversationIfPossible(hexEncodedPublicKey: String) {
2019-08-29 00:21:23 +00:00
if (!PublicKeyValidation.isValid(hexEncodedPublicKey)) { return Toast.makeText(this, R.string.fragment_new_conversation_invalid_public_key_message, Toast.LENGTH_SHORT).show() }
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
// If we try to contact our master then redirect to note to self
val contactPublicKey = if (TextSecurePreferences.getMasterHexEncodedPublicKey(this) == hexEncodedPublicKey) userHexEncodedPublicKey else hexEncodedPublicKey
val contact = Recipient.from(this, Address.fromSerialized(contactPublicKey), true)
2019-08-29 00:21:23 +00:00
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)
2019-09-04 23:38:36 +00:00
Analytics.shared.track("New Conversation Started")
2019-08-29 00:21:23 +00:00
startActivity(intent)
finish()
2019-07-18 05:59:41 +00:00
}
}