2019-12-18 11:36:09 +01:00
|
|
|
package org.thoughtcrime.securesms.loki.redesign.activities
|
|
|
|
|
2019-12-18 14:31:12 +01:00
|
|
|
import android.content.ClipData
|
|
|
|
import android.content.ClipboardManager
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2019-12-18 11:36:09 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.support.v4.app.Fragment
|
|
|
|
import android.support.v4.app.FragmentPagerAdapter
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-12-18 14:31:12 +01:00
|
|
|
import android.widget.Toast
|
2019-12-18 15:25:23 +01:00
|
|
|
import kotlinx.android.synthetic.main.activity_create_private_chat.*
|
2019-12-18 11:36:09 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_enter_public_key.*
|
|
|
|
import network.loki.messenger.R
|
2020-01-06 14:26:52 +11:00
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
2019-12-18 14:31:12 +01:00
|
|
|
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-12-18 15:25:23 +01:00
|
|
|
import org.thoughtcrime.securesms.loki.redesign.fragments.ScanQRCodeWrapperFragment
|
|
|
|
import org.thoughtcrime.securesms.loki.redesign.fragments.ScanQRCodeWrapperFragmentDelegate
|
2019-12-18 14:31:12 +01:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation
|
2019-12-18 11:36:09 +01:00
|
|
|
|
2020-01-06 14:26:52 +11:00
|
|
|
class CreatePrivateChatActivity : PassphraseRequiredActionBarActivity(), ScanQRCodeWrapperFragmentDelegate {
|
2019-12-18 15:25:23 +01:00
|
|
|
private val adapter = CreatePrivateChatActivityAdapter(this)
|
2019-12-18 11:36:09 +01:00
|
|
|
|
|
|
|
// region Lifecycle
|
2020-01-06 14:26:52 +11:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
|
|
|
|
super.onCreate(savedInstanceState, isReady)
|
2019-12-18 11:36:09 +01:00
|
|
|
// Set content view
|
2019-12-18 15:25:23 +01:00
|
|
|
setContentView(R.layout.activity_create_private_chat)
|
2019-12-18 11:36:09 +01:00
|
|
|
// Set title
|
|
|
|
supportActionBar!!.title = "New Conversation"
|
|
|
|
// Set up view pager
|
|
|
|
viewPager.adapter = adapter
|
|
|
|
tabLayout.setupWithViewPager(viewPager)
|
|
|
|
}
|
|
|
|
// endregion
|
2019-12-18 14:31:12 +01:00
|
|
|
|
|
|
|
// region Interaction
|
2019-12-18 15:25:23 +01:00
|
|
|
override fun handleQRCodeScanned(hexEncodedPublicKey: String) {
|
|
|
|
createPrivateChatIfPossible(hexEncodedPublicKey)
|
|
|
|
}
|
|
|
|
|
2019-12-18 14:31:12 +01:00
|
|
|
fun createPrivateChatIfPossible(hexEncodedPublicKey: String) {
|
|
|
|
if (!PublicKeyValidation.isValid(hexEncodedPublicKey)) { return Toast.makeText(this, "Invalid Session ID", Toast.LENGTH_SHORT).show() }
|
|
|
|
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this)
|
|
|
|
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
|
|
|
|
val targetHexEncodedPublicKey = if (hexEncodedPublicKey == masterHexEncodedPublicKey) userHexEncodedPublicKey else hexEncodedPublicKey
|
|
|
|
val recipient = Recipient.from(this, Address.fromSerialized(targetHexEncodedPublicKey), true)
|
|
|
|
val intent = Intent(this, ConversationActivity::class.java)
|
|
|
|
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, recipient.address)
|
|
|
|
intent.putExtra(ConversationActivity.TEXT_EXTRA, getIntent().getStringExtra(ConversationActivity.TEXT_EXTRA))
|
|
|
|
intent.setDataAndType(getIntent().data, getIntent().type)
|
|
|
|
val existingThread = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(recipient)
|
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, existingThread)
|
|
|
|
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, ThreadDatabase.DistributionTypes.DEFAULT)
|
|
|
|
startActivity(intent)
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
// endregion
|
2019-12-18 11:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// region Adapter
|
2019-12-18 15:25:23 +01:00
|
|
|
private class CreatePrivateChatActivityAdapter(val activity: CreatePrivateChatActivity) : FragmentPagerAdapter(activity.supportFragmentManager) {
|
2019-12-18 11:36:09 +01:00
|
|
|
|
|
|
|
override fun getCount(): Int {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItem(index: Int): Fragment {
|
2019-12-18 14:31:12 +01:00
|
|
|
return when (index) {
|
|
|
|
0 -> EnterPublicKeyFragment()
|
2019-12-18 15:25:23 +01:00
|
|
|
1 -> {
|
|
|
|
val result = ScanQRCodeWrapperFragment()
|
|
|
|
result.delegate = activity
|
2019-12-19 11:15:58 +01:00
|
|
|
result.message = "Users can share their QR code by going into their account settings and tapping \"Share QR Code\""
|
2019-12-18 15:25:23 +01:00
|
|
|
result
|
|
|
|
}
|
2019-12-18 14:31:12 +01:00
|
|
|
else -> throw IllegalStateException()
|
|
|
|
}
|
2019-12-18 11:36:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getPageTitle(index: Int): CharSequence? {
|
|
|
|
return when (index) {
|
2019-12-19 11:49:23 +01:00
|
|
|
0 -> "Enter Session ID"
|
2019-12-18 11:36:09 +01:00
|
|
|
1 -> "Scan QR Code"
|
|
|
|
else -> throw IllegalStateException()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
2019-12-18 14:31:12 +01:00
|
|
|
// region Enter Public Key Fragment
|
2019-12-18 11:36:09 +01:00
|
|
|
class EnterPublicKeyFragment : Fragment() {
|
2019-12-18 15:25:23 +01:00
|
|
|
|
2019-12-18 14:31:12 +01:00
|
|
|
private val hexEncodedPublicKey: String
|
|
|
|
get() {
|
|
|
|
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context!!)
|
|
|
|
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context!!)
|
|
|
|
return masterHexEncodedPublicKey ?: userHexEncodedPublicKey
|
|
|
|
}
|
2019-12-18 11:36:09 +01:00
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
return inflater.inflate(R.layout.fragment_enter_public_key, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2019-12-18 14:31:12 +01:00
|
|
|
publicKeyTextView.text = hexEncodedPublicKey
|
|
|
|
copyButton.setOnClickListener { copyPublicKey() }
|
|
|
|
shareButton.setOnClickListener { sharePublicKey() }
|
|
|
|
createPrivateChatButton.setOnClickListener { createPrivateChatIfPossible() }
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun copyPublicKey() {
|
|
|
|
val clipboard = activity!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("Session ID", hexEncodedPublicKey)
|
|
|
|
clipboard.primaryClip = clip
|
|
|
|
Toast.makeText(context!!, R.string.activity_register_public_key_copied_message, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sharePublicKey() {
|
|
|
|
val intent = Intent()
|
|
|
|
intent.action = Intent.ACTION_SEND
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, hexEncodedPublicKey)
|
|
|
|
intent.type = "text/plain"
|
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createPrivateChatIfPossible() {
|
|
|
|
val hexEncodedPublicKey = publicKeyEditText.text.trim().toString()
|
2019-12-18 15:25:23 +01:00
|
|
|
(activity!! as CreatePrivateChatActivity).createPrivateChatIfPossible(hexEncodedPublicKey)
|
2019-12-18 11:36:09 +01:00
|
|
|
}
|
2019-12-18 14:31:12 +01:00
|
|
|
}
|
|
|
|
// endregion
|