2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.activities
|
2020-01-31 03:57:24 +01:00
|
|
|
|
2020-08-14 07:23:06 +02:00
|
|
|
import android.content.Context
|
2020-02-03 00:23:24 +01:00
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.os.AsyncTask
|
2020-01-31 03:57:24 +01:00
|
|
|
import android.os.Bundle
|
2020-08-19 02:06:26 +02:00
|
|
|
import androidx.loader.app.LoaderManager
|
|
|
|
import androidx.loader.content.Loader
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2020-01-31 03:57:24 +01:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
2020-03-12 01:16:07 +01:00
|
|
|
import android.view.View
|
2020-01-31 03:57:24 +01:00
|
|
|
import android.widget.Toast
|
2020-02-03 00:23:24 +01:00
|
|
|
import kotlinx.android.synthetic.main.activity_create_closed_group.*
|
|
|
|
import kotlinx.android.synthetic.main.activity_linked_devices.recyclerView
|
2020-01-31 03:57:24 +01:00
|
|
|
import network.loki.messenger.R
|
2020-09-02 03:47:54 +02:00
|
|
|
import nl.komponents.kovenant.ui.successUi
|
2020-01-31 03:57:24 +01:00
|
|
|
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
2020-02-03 00:23:24 +01:00
|
|
|
import org.thoughtcrime.securesms.conversation.ConversationActivity
|
|
|
|
import org.thoughtcrime.securesms.database.Address
|
2020-08-12 04:22:16 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2020-02-03 00:23:24 +01:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase
|
|
|
|
import org.thoughtcrime.securesms.groups.GroupManager
|
2020-08-10 08:38:40 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol
|
2020-09-02 03:47:54 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.utilities.fadeIn
|
|
|
|
import org.thoughtcrime.securesms.loki.utilities.fadeOut
|
2020-01-31 03:57:24 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp
|
2020-02-03 00:23:24 +01:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
2020-11-26 00:07:45 +01:00
|
|
|
import org.session.libsignal.libsignal.util.guava.Optional
|
2020-02-03 00:23:24 +01:00
|
|
|
import java.lang.ref.WeakReference
|
2020-01-31 03:57:24 +01:00
|
|
|
|
2020-08-14 07:23:06 +02:00
|
|
|
class CreateClosedGroupActivity : PassphraseRequiredActionBarActivity(), LoaderManager.LoaderCallbacks<List<String>> {
|
2020-10-06 05:19:52 +02:00
|
|
|
private var isLoading = false
|
|
|
|
set(newValue) { field = newValue; invalidateOptionsMenu() }
|
2020-08-14 07:23:06 +02:00
|
|
|
private var members = listOf<String>()
|
2020-10-06 05:19:52 +02:00
|
|
|
set(value) { field = value; selectContactsAdapter.members = value }
|
2020-03-12 01:16:07 +01:00
|
|
|
|
2020-08-18 00:55:17 +02:00
|
|
|
private val selectContactsAdapter by lazy {
|
|
|
|
SelectContactsAdapter(this, GlideApp.with(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val closedGroupCreatedResultCode = 100
|
|
|
|
}
|
|
|
|
|
2020-01-31 03:57:24 +01:00
|
|
|
// region Lifecycle
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
|
|
|
|
super.onCreate(savedInstanceState, isReady)
|
|
|
|
setContentView(R.layout.activity_create_closed_group)
|
2020-05-25 08:38:36 +02:00
|
|
|
supportActionBar!!.title = resources.getString(R.string.activity_create_closed_group_title)
|
2020-08-14 07:23:06 +02:00
|
|
|
recyclerView.adapter = this.selectContactsAdapter
|
2020-01-31 03:57:24 +01:00
|
|
|
recyclerView.layoutManager = LinearLayoutManager(this)
|
2020-10-06 05:19:52 +02:00
|
|
|
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
|
2020-01-31 03:57:24 +01:00
|
|
|
LoaderManager.getInstance(this).initLoader(0, null, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
2020-06-12 06:37:21 +02:00
|
|
|
menuInflater.inflate(R.menu.menu_done, menu)
|
2020-10-06 05:19:52 +02:00
|
|
|
return members.isNotEmpty() && !isLoading
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
|
|
|
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<List<String>> {
|
2020-08-18 07:40:24 +02:00
|
|
|
return SelectContactsLoader(this, setOf())
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFinished(loader: Loader<List<String>>, members: List<String>) {
|
|
|
|
update(members)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoaderReset(loader: Loader<List<String>>) {
|
|
|
|
update(listOf())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun update(members: List<String>) {
|
|
|
|
this.members = members
|
2020-03-12 01:16:07 +01:00
|
|
|
mainContentContainer.visibility = if (members.isEmpty()) View.GONE else View.VISIBLE
|
|
|
|
emptyStateContainer.visibility = if (members.isEmpty()) View.VISIBLE else View.GONE
|
|
|
|
invalidateOptionsMenu()
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Interaction
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
2020-08-14 07:23:06 +02:00
|
|
|
when(item.itemId) {
|
2020-10-06 05:19:52 +02:00
|
|
|
R.id.doneButton -> if (!isLoading) { createClosedGroup() }
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:16:07 +01:00
|
|
|
private fun createNewPrivateChat() {
|
2020-08-18 00:55:17 +02:00
|
|
|
setResult(Companion.closedGroupCreatedResultCode)
|
2020-03-12 01:16:07 +01:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
2020-01-31 03:57:24 +01:00
|
|
|
private fun createClosedGroup() {
|
2020-08-10 08:38:40 +02:00
|
|
|
if (ClosedGroupsProtocol.isSharedSenderKeysEnabled) {
|
|
|
|
createSSKBasedClosedGroup()
|
|
|
|
} else {
|
|
|
|
createLegacyClosedGroup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createSSKBasedClosedGroup() {
|
|
|
|
val name = nameEditText.text.trim()
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_missing_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
|
|
|
if (name.length >= 64) {
|
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_too_long_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
2020-08-14 09:47:48 +02:00
|
|
|
val selectedMembers = this.selectContactsAdapter.selectedMembers
|
2020-08-26 03:49:19 +02:00
|
|
|
if (selectedMembers.count() < 1) {
|
2020-08-10 08:38:40 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_not_enough_group_members_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
2020-08-31 05:53:31 +02:00
|
|
|
if (selectedMembers.count() >= ClosedGroupsProtocol.groupSizeLimit) { // Minus one because we're going to include self later
|
2020-08-10 08:38:40 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_too_many_group_members_error, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
2020-08-11 01:59:07 +02:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(this)
|
2020-10-06 05:19:52 +02:00
|
|
|
isLoading = true
|
2020-09-02 03:47:54 +02:00
|
|
|
loader.fadeIn()
|
|
|
|
ClosedGroupsProtocol.createClosedGroup(this, name.toString(), selectedMembers + setOf( userPublicKey )).successUi { groupID ->
|
|
|
|
loader.fadeOut()
|
2020-10-06 05:19:52 +02:00
|
|
|
isLoading = false
|
2020-11-20 08:59:13 +01:00
|
|
|
val threadID = DatabaseFactory.getThreadDatabase(this).getOrCreateThreadIdFor(Recipient.from(this, Address.fromSerialized(groupID), false))
|
2020-09-02 03:47:54 +02:00
|
|
|
if (!isFinishing) {
|
|
|
|
openConversationActivity(this, threadID, Recipient.from(this, Address.fromSerialized(groupID), false))
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 08:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun createLegacyClosedGroup() {
|
2020-02-03 00:23:24 +01:00
|
|
|
val name = nameEditText.text.trim()
|
2020-01-31 03:57:24 +01:00
|
|
|
if (name.isEmpty()) {
|
2020-05-25 08:38:36 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_missing_error, Toast.LENGTH_LONG).show()
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
if (name.length >= 64) {
|
2020-05-25 08:38:36 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_group_name_too_long_error, Toast.LENGTH_LONG).show()
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
2020-08-14 07:23:06 +02:00
|
|
|
val selectedMembers = this.selectContactsAdapter.selectedMembers
|
2020-08-26 03:49:19 +02:00
|
|
|
if (selectedMembers.count() < 1) {
|
2020-05-25 08:38:36 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_not_enough_group_members_error, Toast.LENGTH_LONG).show()
|
2020-02-05 00:36:26 +01:00
|
|
|
}
|
|
|
|
if (selectedMembers.count() > 10) {
|
2020-05-25 08:38:36 +02:00
|
|
|
return Toast.makeText(this, R.string.activity_create_closed_group_too_many_group_members_error, Toast.LENGTH_LONG).show()
|
2020-02-03 00:23:24 +01:00
|
|
|
}
|
|
|
|
val recipients = selectedMembers.map {
|
|
|
|
Recipient.from(this, Address.fromSerialized(it), false)
|
|
|
|
}.toSet()
|
2020-02-12 06:42:33 +01:00
|
|
|
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this) ?: TextSecurePreferences.getLocalNumber(this)
|
|
|
|
val admin = Recipient.from(this, Address.fromSerialized(masterHexEncodedPublicKey), false)
|
2020-08-14 07:23:06 +02:00
|
|
|
CreateClosedGroupTask(WeakReference(this), null, name.toString(), recipients, setOf( admin ))
|
2020-08-18 00:55:17 +02:00
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
|
2020-02-03 00:23:24 +01:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
2020-08-18 00:55:17 +02:00
|
|
|
// region Group Creation Task (Legacy)
|
2020-02-03 00:23:24 +01:00
|
|
|
internal class CreateClosedGroupTask(
|
2020-08-18 00:55:17 +02:00
|
|
|
private val activity: WeakReference<CreateClosedGroupActivity>,
|
|
|
|
private val profilePicture: Bitmap?,
|
|
|
|
private val name: String?,
|
|
|
|
private val members: Set<Recipient>,
|
|
|
|
private val admins: Set<Recipient>
|
2020-02-03 00:23:24 +01:00
|
|
|
) : AsyncTask<Void, Void, Optional<GroupManager.GroupActionResult>>() {
|
|
|
|
|
|
|
|
override fun doInBackground(vararg params: Void?): Optional<GroupManager.GroupActionResult> {
|
|
|
|
val activity = activity.get() ?: return Optional.absent()
|
2020-02-12 06:42:33 +01:00
|
|
|
return Optional.of(GroupManager.createGroup(activity, members, profilePicture, name, false, admins))
|
2020-02-03 00:23:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPostExecute(result: Optional<GroupManager.GroupActionResult>) {
|
2020-02-12 06:42:33 +01:00
|
|
|
val activity = activity.get() ?: return super.onPostExecute(result)
|
2020-02-03 00:23:24 +01:00
|
|
|
if (result.isPresent && result.get().threadId > -1) {
|
|
|
|
if (!activity.isFinishing) {
|
2020-08-14 07:23:06 +02:00
|
|
|
openConversationActivity(activity, result.get().threadId, result.get().groupRecipient)
|
|
|
|
activity.finish()
|
2020-02-03 00:23:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
super.onPostExecute(result)
|
2020-05-25 08:38:36 +02:00
|
|
|
Toast.makeText(activity.applicationContext, R.string.activity_create_closed_group_invalid_session_id_error, Toast.LENGTH_LONG).show()
|
2020-02-03 00:23:24 +01:00
|
|
|
}
|
2020-01-31 03:57:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-14 07:23:06 +02:00
|
|
|
}
|
2020-08-18 00:55:17 +02:00
|
|
|
// endregion
|
2020-08-14 07:23:06 +02:00
|
|
|
|
2020-08-18 00:55:17 +02:00
|
|
|
// region Convenience
|
2020-08-14 07:23:06 +02:00
|
|
|
private fun openConversationActivity(context: Context, threadId: Long, recipient: Recipient) {
|
|
|
|
val intent = Intent(context, ConversationActivity::class.java)
|
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId)
|
|
|
|
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, ThreadDatabase.DistributionTypes.DEFAULT)
|
|
|
|
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, recipient.address)
|
|
|
|
context.startActivity(intent)
|
2020-08-18 00:55:17 +02:00
|
|
|
}
|
|
|
|
// endregion
|