2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.activities
|
2019-12-18 15:25:23 +01:00
|
|
|
|
2020-01-08 00:50:11 +01:00
|
|
|
import android.animation.Animator
|
|
|
|
import android.animation.AnimatorListenerAdapter
|
2019-12-18 15:25:23 +01:00
|
|
|
import android.os.Bundle
|
2020-01-08 00:50:11 +01:00
|
|
|
import android.util.Patterns
|
2019-12-18 15:25:23 +01:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2020-01-08 00:50:11 +01:00
|
|
|
import android.view.inputmethod.InputMethodManager
|
|
|
|
import android.widget.Toast
|
2020-12-09 08:23:05 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.fragment.app.FragmentPagerAdapter
|
2020-11-20 08:59:13 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-12-18 15:25:23 +01:00
|
|
|
import kotlinx.android.synthetic.main.activity_join_public_chat.*
|
|
|
|
import kotlinx.android.synthetic.main.fragment_enter_chat_url.*
|
2020-11-20 08:59:13 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
2019-12-18 15:25:23 +01:00
|
|
|
import network.loki.messenger.R
|
2020-01-08 00:50:11 +01:00
|
|
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
2020-01-06 04:26:52 +01:00
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
2021-02-03 02:22:40 +01:00
|
|
|
import org.session.libsignal.utilities.logging.Log
|
2020-05-11 08:19:26 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragment
|
|
|
|
import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragmentDelegate
|
2021-02-09 05:12:21 +01:00
|
|
|
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
|
2020-05-11 08:19:26 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.utilities.OpenGroupUtilities
|
2019-12-18 15:25:23 +01:00
|
|
|
|
2020-01-06 04:26:52 +01:00
|
|
|
class JoinPublicChatActivity : PassphraseRequiredActionBarActivity(), ScanQRCodeWrapperFragmentDelegate {
|
2019-12-18 15:25:23 +01:00
|
|
|
private val adapter = JoinPublicChatActivityAdapter(this)
|
|
|
|
|
|
|
|
// region Lifecycle
|
2020-01-06 04:26:52 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
|
|
|
|
super.onCreate(savedInstanceState, isReady)
|
2019-12-18 15:25:23 +01:00
|
|
|
// Set content view
|
|
|
|
setContentView(R.layout.activity_join_public_chat)
|
|
|
|
// Set title
|
2020-05-25 08:12:24 +02:00
|
|
|
supportActionBar!!.title = resources.getString(R.string.activity_join_public_chat_title)
|
2019-12-18 15:25:23 +01:00
|
|
|
// Set up view pager
|
|
|
|
viewPager.adapter = adapter
|
|
|
|
tabLayout.setupWithViewPager(viewPager)
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
2020-01-08 00:50:11 +01:00
|
|
|
// region Updating
|
|
|
|
private fun showLoader() {
|
|
|
|
loader.visibility = View.VISIBLE
|
|
|
|
loader.animate().setDuration(150).alpha(1.0f).start()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun hideLoader() {
|
|
|
|
loader.animate().setDuration(150).alpha(0.0f).setListener(object : AnimatorListenerAdapter() {
|
|
|
|
|
|
|
|
override fun onAnimationEnd(animation: Animator?) {
|
|
|
|
super.onAnimationEnd(animation)
|
|
|
|
loader.visibility = View.GONE
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
2019-12-18 15:25:23 +01:00
|
|
|
// region Interaction
|
|
|
|
override fun handleQRCodeScanned(url: String) {
|
|
|
|
joinPublicChatIfPossible(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun joinPublicChatIfPossible(url: String) {
|
2020-03-13 00:56:25 +01:00
|
|
|
if (!Patterns.WEB_URL.matcher(url).matches() || !url.startsWith("https://")) {
|
2020-05-25 08:12:24 +02:00
|
|
|
return Toast.makeText(this, R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
2020-03-13 00:56:25 +01:00
|
|
|
}
|
|
|
|
showLoader()
|
|
|
|
val channel: Long = 1
|
2020-11-20 08:59:13 +01:00
|
|
|
|
|
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
try {
|
|
|
|
OpenGroupUtilities.addGroup(this@JoinPublicChatActivity, url, channel)
|
2021-02-09 05:12:21 +01:00
|
|
|
MultiDeviceProtocol.forceSyncConfigurationNowIfNeeded(this@JoinPublicChatActivity)
|
2020-11-20 08:59:13 +01:00
|
|
|
} catch (e: Exception) {
|
2020-12-17 02:47:18 +01:00
|
|
|
Log.e("JoinPublicChatActivity", "Fialed to join open group.", e)
|
2020-11-20 08:59:13 +01:00
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
hideLoader()
|
|
|
|
Toast.makeText(this@JoinPublicChatActivity, R.string.activity_join_public_chat_error, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
return@launch
|
|
|
|
}
|
|
|
|
withContext(Dispatchers.Main) { finish() }
|
2020-01-08 00:50:11 +01:00
|
|
|
}
|
2019-12-18 15:25:23 +01:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
// region Adapter
|
|
|
|
private class JoinPublicChatActivityAdapter(val activity: JoinPublicChatActivity) : FragmentPagerAdapter(activity.supportFragmentManager) {
|
|
|
|
|
|
|
|
override fun getCount(): Int {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItem(index: Int): Fragment {
|
|
|
|
return when (index) {
|
|
|
|
0 -> EnterChatURLFragment()
|
|
|
|
1 -> {
|
|
|
|
val result = ScanQRCodeWrapperFragment()
|
|
|
|
result.delegate = activity
|
2020-05-25 08:12:24 +02:00
|
|
|
result.message = activity.resources.getString(R.string.activity_join_public_chat_scan_qr_code_explanation)
|
2019-12-18 15:25:23 +01:00
|
|
|
result
|
|
|
|
}
|
|
|
|
else -> throw IllegalStateException()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getPageTitle(index: Int): CharSequence? {
|
|
|
|
return when (index) {
|
2020-05-25 08:12:24 +02:00
|
|
|
0 -> activity.resources.getString(R.string.activity_join_public_chat_enter_group_url_tab_title)
|
|
|
|
1 -> activity.resources.getString(R.string.activity_join_public_chat_scan_qr_code_tab_title)
|
2019-12-18 15:25:23 +01:00
|
|
|
else -> throw IllegalStateException()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
2020-01-08 05:16:34 +01:00
|
|
|
// region Enter Chat URL Fragment
|
2019-12-18 15:25:23 +01:00
|
|
|
class EnterChatURLFragment : Fragment() {
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
return inflater.inflate(R.layout.fragment_enter_chat_url, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2020-01-10 06:35:32 +01:00
|
|
|
chatURLEditText.imeOptions = chatURLEditText.imeOptions or 16777216 // Always use incognito keyboard
|
2019-12-18 15:25:23 +01:00
|
|
|
joinPublicChatButton.setOnClickListener { joinPublicChatIfPossible() }
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun joinPublicChatIfPossible() {
|
2020-11-20 08:59:13 +01:00
|
|
|
val inputMethodManager = requireContext().getSystemService(BaseActionBarActivity.INPUT_METHOD_SERVICE) as InputMethodManager
|
2020-01-08 00:50:11 +01:00
|
|
|
inputMethodManager.hideSoftInputFromWindow(chatURLEditText.windowToken, 0)
|
2020-02-10 03:40:12 +01:00
|
|
|
var chatURL = chatURLEditText.text.trim().toString().toLowerCase().replace("http://", "https://")
|
|
|
|
if (!chatURL.toLowerCase().startsWith("https")) {
|
|
|
|
chatURL = "https://$chatURL"
|
|
|
|
}
|
2020-11-20 08:59:13 +01:00
|
|
|
(requireActivity() as JoinPublicChatActivity).joinPublicChatIfPossible(chatURL)
|
2019-12-18 15:25:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// endregion
|