session-android/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt

433 lines
20 KiB
Kotlin
Raw Normal View History

2020-05-11 08:19:26 +02:00
package org.thoughtcrime.securesms.loki.activities
2019-12-17 14:27:59 +01:00
2020-02-19 05:34:02 +01:00
import android.app.AlertDialog
2020-07-16 04:49:37 +02:00
import android.content.BroadcastReceiver
import android.content.Context
2019-12-17 15:15:13 +01:00
import android.content.Intent
2020-07-16 04:49:37 +02:00
import android.content.IntentFilter
2019-12-19 11:49:23 +01:00
import android.database.Cursor
2020-07-30 08:53:34 +02:00
import android.net.Uri
2019-12-17 14:27:59 +01:00
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
2020-03-16 05:35:14 +01:00
import android.util.DisplayMetrics
import android.view.View
2020-03-16 05:35:14 +01:00
import android.widget.RelativeLayout
2020-02-19 05:34:02 +01:00
import android.widget.Toast
import androidx.lifecycle.Observer
2020-11-23 06:59:44 +01:00
import androidx.lifecycle.lifecycleScope
import androidx.loader.app.LoaderManager
import androidx.loader.content.Loader
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
2019-12-17 14:27:59 +01:00
import kotlinx.android.synthetic.main.activity_home.*
2020-11-23 06:59:44 +01:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
2019-12-17 14:27:59 +01:00
import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
2019-12-17 15:15:13 +01:00
import org.thoughtcrime.securesms.conversation.ConversationActivity
2021-01-13 07:11:30 +01:00
import org.session.libsession.messaging.threads.Address
2019-12-17 14:27:59 +01:00
import org.thoughtcrime.securesms.database.DatabaseFactory
2019-12-17 15:15:13 +01:00
import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.loki.api.ResetThreadSessionJob
2020-05-11 08:54:31 +02:00
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol
2020-07-15 06:26:20 +02:00
import org.thoughtcrime.securesms.loki.protocol.SessionResetImplementation
2020-06-02 07:18:09 +02:00
import org.thoughtcrime.securesms.loki.utilities.*
2020-05-11 08:19:26 +02:00
import org.thoughtcrime.securesms.loki.views.ConversationView
import org.thoughtcrime.securesms.loki.views.NewConversationButtonSetViewDelegate
import org.thoughtcrime.securesms.loki.views.SeedReminderViewDelegate
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.mms.GlideApp
import org.thoughtcrime.securesms.mms.GlideRequests
2019-12-17 14:27:59 +01:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.thoughtcrime.securesms.util.TextSecurePreferences.getBooleanPreference
import org.thoughtcrime.securesms.util.TextSecurePreferences.setBooleanPreference
import org.thoughtcrime.securesms.util.Util
import org.session.libsignal.service.loki.api.fileserver.FileServerAPI
import org.session.libsignal.service.loki.protocol.mentions.MentionsManager
import org.session.libsignal.service.loki.protocol.meta.SessionMetaProtocol
import org.session.libsignal.service.loki.protocol.sessionmanagement.SessionManagementProtocol
import org.session.libsignal.service.loki.protocol.shelved.multidevice.MultiDeviceProtocol
import org.session.libsignal.service.loki.protocol.shelved.syncmessages.SyncMessagesProtocol
import org.session.libsignal.service.loki.utilities.toHexString
2021-01-13 06:13:49 +01:00
import org.thoughtcrime.securesms.loki.dialogs.*
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocolV2
2020-09-11 01:03:57 +02:00
import java.io.IOException
2019-12-17 14:27:59 +01:00
class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListener, SeedReminderViewDelegate, NewConversationButtonSetViewDelegate {
companion object {
private const val PREF_RESET_ALL_SESSIONS_ON_START_UP = "pref_reset_all_sessions_on_start_up"
@JvmStatic
fun requestResetAllSessionsOnStartup(context: Context) {
setBooleanPreference(context, PREF_RESET_ALL_SESSIONS_ON_START_UP, true)
}
@JvmStatic
fun scheduleResetAllSessionsIfRequested(context: Context) {
if (!getBooleanPreference(context, PREF_RESET_ALL_SESSIONS_ON_START_UP, false)) return
setBooleanPreference(context, PREF_RESET_ALL_SESSIONS_ON_START_UP, false)
val jobManager = ApplicationContext.getInstance(context).jobManager
DatabaseFactory.getThreadDatabase(context).conversationListQuick.forEach { tuple ->
val threadId: Long = tuple.first
val recipientAddress: String = tuple.second
jobManager.add(ResetThreadSessionJob(
Address.fromSerialized(recipientAddress),
threadId))
}
}
}
2019-12-19 11:15:58 +01:00
private lateinit var glide: GlideRequests
2020-07-16 04:49:37 +02:00
private var broadcastReceiver: BroadcastReceiver? = null
2019-12-17 14:27:59 +01:00
2020-07-15 06:26:20 +02:00
private val publicKey: String
2020-01-06 04:26:52 +01:00
get() {
2020-07-15 06:26:20 +02:00
val masterPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this)
val userPublicKey = TextSecurePreferences.getLocalNumber(this)
return masterPublicKey ?: userPublicKey
2020-01-06 04:26:52 +01:00
}
// region Lifecycle
2019-12-17 14:27:59 +01:00
constructor() : super()
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)
2020-02-04 04:20:42 +01:00
// Double check that the long poller is up
2020-03-24 03:48:23 +01:00
(applicationContext as ApplicationContext).startPollingIfNeeded()
2019-12-17 14:27:59 +01:00
// Set content view
setContentView(R.layout.activity_home)
2020-01-07 02:00:30 +01:00
// Set custom toolbar
2020-01-06 02:07:55 +01:00
setSupportActionBar(toolbar)
2019-12-19 11:15:58 +01:00
// Set up Glide
glide = GlideApp.with(this)
2020-01-06 02:07:55 +01:00
// Set up toolbar buttons
profileButton.glide = glide
2020-07-15 06:26:20 +02:00
profileButton.publicKey = publicKey
profileButton.displayName = TextSecurePreferences.getProfileName(this)
2020-01-06 02:07:55 +01:00
profileButton.update()
profileButton.setOnClickListener { openSettings() }
2020-09-08 08:21:50 +02:00
pathStatusViewContainer.disableClipping()
2020-05-29 03:16:52 +02:00
pathStatusViewContainer.setOnClickListener { showPath() }
// Set up seed reminder view
val isMasterDevice = (TextSecurePreferences.getMasterHexEncodedPublicKey(this) == null)
val hasViewedSeed = TextSecurePreferences.getHasViewedSeed(this)
if (!hasViewedSeed && isMasterDevice) {
2020-05-25 07:46:53 +02:00
val seedReminderViewTitle = SpannableString("You're almost finished! 80%") // Intentionally not yet translated
seedReminderViewTitle.setSpan(ForegroundColorSpan(resources.getColorWithID(R.color.accent, theme)), 24, 27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
seedReminderView.title = seedReminderViewTitle
2020-05-25 07:46:53 +02:00
seedReminderView.subtitle = resources.getString(R.string.view_seed_reminder_subtitle_1)
seedReminderView.setProgress(80, false)
seedReminderView.delegate = this
} else {
seedReminderView.visibility = View.GONE
}
2019-12-17 14:27:59 +01:00
// Set up recycler view
val cursor = DatabaseFactory.getThreadDatabase(this).conversationList
2019-12-19 11:15:58 +01:00
val homeAdapter = HomeAdapter(this, cursor)
homeAdapter.glide = glide
homeAdapter.conversationClickListener = this
recyclerView.adapter = homeAdapter
2019-12-17 14:27:59 +01:00
recyclerView.layoutManager = LinearLayoutManager(this)
2020-04-20 03:54:56 +02:00
// Set up empty state view
createNewPrivateChatButton.setOnClickListener { createNewPrivateChat() }
2019-12-19 11:49:23 +01:00
// This is a workaround for the fact that CursorRecyclerViewAdapter doesn't actually auto-update (even though it says it will)
LoaderManager.getInstance(this).restartLoader(0, null, object : LoaderManager.LoaderCallbacks<Cursor> {
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<Cursor> {
return HomeLoader(this@HomeActivity)
}
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor?) {
homeAdapter.changeCursor(cursor)
2020-04-20 03:54:56 +02:00
updateEmptyState()
2019-12-19 11:49:23 +01:00
}
override fun onLoaderReset(cursor: Loader<Cursor>) {
homeAdapter.changeCursor(null)
}
})
2020-03-16 05:35:14 +01:00
// Set up gradient view
val gradientViewLayoutParams = gradientView.layoutParams as RelativeLayout.LayoutParams
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
gradientViewLayoutParams.topMargin = (0.15 * height.toFloat()).toInt()
// Set up new conversation button set
newConversationButtonSet.delegate = this
2019-12-19 11:15:58 +01:00
// Set up typing observer
2019-12-19 11:49:23 +01:00
ApplicationContext.getInstance(this).typingStatusRepository.typingThreads.observe(this, Observer<Set<Long>> { threadIDs ->
val adapter = recyclerView.adapter as HomeAdapter
adapter.typingThreadIDs = threadIDs ?: setOf()
2019-12-19 11:15:58 +01:00
})
2020-05-14 05:52:20 +02:00
// Set up remaining components if needed
2020-05-26 00:55:29 +02:00
val application = ApplicationContext.getInstance(this)
2020-05-25 10:01:21 +02:00
val apiDB = DatabaseFactory.getLokiAPIDatabase(this)
val threadDB = DatabaseFactory.getLokiThreadDatabase(this)
val userDB = DatabaseFactory.getLokiUserDatabase(this)
val sskDatabase = DatabaseFactory.getSSKDatabase(this)
2020-05-14 05:52:20 +02:00
val userPublicKey = TextSecurePreferences.getLocalNumber(this)
2020-07-15 06:26:20 +02:00
val sessionResetImpl = SessionResetImplementation(this)
2020-05-14 05:52:20 +02:00
if (userPublicKey != null) {
MentionsManager.configureIfNeeded(userPublicKey, threadDB, userDB)
SessionMetaProtocol.configureIfNeeded(apiDB, userPublicKey)
SyncMessagesProtocol.configureIfNeeded(apiDB, userPublicKey)
2020-07-15 06:26:20 +02:00
application.publicChatManager.startPollersIfNeeded()
2019-12-17 14:27:59 +01:00
}
SessionManagementProtocol.configureIfNeeded(sessionResetImpl, sskDatabase, application)
2020-05-25 10:01:21 +02:00
MultiDeviceProtocol.configureIfNeeded(apiDB)
2020-06-03 03:52:30 +02:00
IP2Country.configureIfNeeded(this)
2020-09-24 01:43:18 +02:00
application.registerForFCMIfNeeded(false)
// Preload device links to make message sending quicker
val publicKeys = ContactUtilities.getAllContacts(this).filter { contact ->
2020-07-15 06:26:20 +02:00
!contact.recipient.isGroupRecipient && !contact.isOurDevice && !contact.isSlave
}.map {
it.recipient.address.toPhoneString()
}.toSet()
2020-07-15 04:24:43 +02:00
FileServerAPI.shared.getDeviceLinks(publicKeys)
2020-07-16 04:49:37 +02:00
// Observe blocked contacts changed events
val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
recyclerView.adapter!!.notifyDataSetChanged()
}
}
this.broadcastReceiver = broadcastReceiver
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, IntentFilter("blockedContactsChanged"))
2019-12-17 14:27:59 +01:00
}
override fun onResume() {
super.onResume()
2020-08-05 01:25:29 +02:00
if (TextSecurePreferences.getLocalNumber(this) == null) { return; } // This can be the case after a secondary device is auto-cleared
profileButton.update()
val isMasterDevice = (TextSecurePreferences.getMasterHexEncodedPublicKey(this) == null)
val hasViewedSeed = TextSecurePreferences.getHasViewedSeed(this)
if (hasViewedSeed || !isMasterDevice) {
seedReminderView.visibility = View.GONE
}
2021-01-13 06:13:49 +01:00
showKeyPairMigrationSheetIfNeeded()
showKeyPairMigrationSuccessSheetIfNeeded()
}
private fun showKeyPairMigrationSheetIfNeeded() {
if (KeyPairUtilities.hasV2KeyPair(this)) { return }
val keyPairMigrationSheet = KeyPairMigrationBottomSheet()
keyPairMigrationSheet.show(supportFragmentManager, keyPairMigrationSheet.tag)
}
private fun showKeyPairMigrationSuccessSheetIfNeeded() {
if (!KeyPairUtilities.hasV2KeyPair(this) || !TextSecurePreferences.getIsMigratingKeyPair(this)) { return }
val keyPairMigrationSuccessSheet = KeyPairMigrationSuccessBottomSheet()
keyPairMigrationSuccessSheet.show(supportFragmentManager, keyPairMigrationSuccessSheet.tag)
TextSecurePreferences.setIsMigratingKeyPair(this, false)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
2020-08-18 00:55:17 +02:00
if (resultCode == CreateClosedGroupActivity.closedGroupCreatedResultCode) {
createNewPrivateChat()
}
}
2020-07-16 04:49:37 +02:00
override fun onDestroy() {
val broadcastReceiver = this.broadcastReceiver
if (broadcastReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver)
}
super.onDestroy()
}
// endregion
2020-04-20 03:54:56 +02:00
// region Updating
private fun updateEmptyState() {
val threadCount = (recyclerView.adapter as HomeAdapter).itemCount
emptyStateContainer.visibility = if (threadCount == 0) View.VISIBLE else View.GONE
}
// endregion
// region Interaction
override fun handleSeedReminderViewContinueButtonTapped() {
val intent = Intent(this, SeedActivity::class.java)
show(intent)
}
2019-12-17 15:15:13 +01:00
override fun onConversationClick(view: ConversationView) {
val thread = view.thread ?: return
openConversation(thread)
}
override fun onLongConversationClick(view: ConversationView) {
val thread = view.thread ?: return
val bottomSheet = ConversationOptionsBottomSheet()
bottomSheet.recipient = thread.recipient
2020-09-07 02:57:25 +02:00
bottomSheet.onViewDetailsTapped = {
bottomSheet.dismiss()
val userDetailsBottomSheet = UserDetailsBottomSheet()
2020-09-07 07:20:32 +02:00
val bundle = Bundle()
bundle.putString("publicKey", thread.recipient.address.toPhoneString())
userDetailsBottomSheet.arguments = bundle
2020-09-07 02:57:25 +02:00
userDetailsBottomSheet.show(supportFragmentManager, userDetailsBottomSheet.tag)
}
bottomSheet.onBlockTapped = {
bottomSheet.dismiss()
if (!thread.recipient.isBlocked) {
blockConversation(thread)
}
}
bottomSheet.onUnblockTapped = {
bottomSheet.dismiss()
if (thread.recipient.isBlocked) {
unblockConversation(thread)
}
}
bottomSheet.onDeleteTapped = {
bottomSheet.dismiss()
deleteConversation(thread)
}
bottomSheet.show(supportFragmentManager, bottomSheet.tag)
}
private fun blockConversation(thread: ThreadRecord) {
AlertDialog.Builder(this)
.setTitle(R.string.RecipientPreferenceActivity_block_this_contact_question)
.setMessage(R.string.RecipientPreferenceActivity_you_will_no_longer_receive_messages_and_calls_from_this_contact)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.RecipientPreferenceActivity_block) { dialog, _ ->
Thread {
DatabaseFactory.getRecipientDatabase(this).setBlocked(thread.recipient, true)
Util.runOnMain {
recyclerView.adapter!!.notifyDataSetChanged()
dialog.dismiss()
}
}.start()
}.show()
}
private fun unblockConversation(thread: ThreadRecord) {
AlertDialog.Builder(this)
.setTitle(R.string.RecipientPreferenceActivity_unblock_this_contact_question)
.setMessage(R.string.RecipientPreferenceActivity_you_will_once_again_be_able_to_receive_messages_and_calls_from_this_contact)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.RecipientPreferenceActivity_unblock) { dialog, _ ->
Thread {
DatabaseFactory.getRecipientDatabase(this).setBlocked(thread.recipient, false)
Util.runOnMain {
recyclerView.adapter!!.notifyDataSetChanged()
dialog.dismiss()
}
}.start()
}.show()
}
private fun deleteConversation(thread: ThreadRecord) {
val threadID = thread.threadId
val recipient = thread.recipient
val threadDB = DatabaseFactory.getThreadDatabase(this)
2021-01-13 06:13:49 +01:00
val isClosedGroup = recipient.address.isClosedGroup
val dialogMessage: String
if (recipient.isGroupRecipient) {
val group = DatabaseFactory.getGroupDatabase(this).getGroup(recipient.address.toString()).orNull()
if (group != null && group.admins.map { it.toPhoneString() }.contains(TextSecurePreferences.getLocalNumber(this))) {
dialogMessage = "Because you are the creator of this group it will be deleted for everyone. This cannot be undone."
} else {
dialogMessage = resources.getString(R.string.activity_home_leave_group_dialog_message)
}
} else {
dialogMessage = resources.getString(R.string.activity_home_delete_conversation_dialog_message)
}
val dialog = AlertDialog.Builder(this)
dialog.setMessage(dialogMessage)
2020-11-23 06:59:44 +01:00
dialog.setPositiveButton(R.string.yes) { _, _ -> lifecycleScope.launch(Dispatchers.Main) {
val context = this@HomeActivity as Context
// Send a leave group message if this is an active closed group
2020-11-23 06:59:44 +01:00
if (isClosedGroup && DatabaseFactory.getGroupDatabase(context).isActive(recipient.address.toGroupString())) {
2020-09-11 01:03:57 +02:00
var isSSKBasedClosedGroup: Boolean
var groupPublicKey: String?
try {
groupPublicKey = ClosedGroupsProtocol.doubleDecodeGroupID(recipient.address.toString()).toHexString()
2020-11-23 06:59:44 +01:00
isSSKBasedClosedGroup = DatabaseFactory.getSSKDatabase(context).isSSKBasedClosedGroup(groupPublicKey)
2020-09-11 01:03:57 +02:00
} catch (e: IOException) {
groupPublicKey = null
isSSKBasedClosedGroup = false
}
2020-08-11 04:20:17 +02:00
if (isSSKBasedClosedGroup) {
2021-01-13 06:13:49 +01:00
ClosedGroupsProtocolV2.leave(context, groupPublicKey!!)
2020-11-23 06:59:44 +01:00
} else if (!ClosedGroupsProtocol.leaveLegacyGroup(context, recipient)) {
Toast.makeText(context, R.string.activity_home_leaving_group_failed_message, Toast.LENGTH_LONG).show()
return@launch
}
}
2020-11-23 06:59:44 +01:00
withContext(Dispatchers.IO) {
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
//TODO Move open group related logic to OpenGroupUtilities / PublicChatManager / GroupManager
if (publicChat != null) {
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
apiDB.removeLastMessageServerID(publicChat.channel, publicChat.server)
apiDB.removeLastDeletionServerID(publicChat.channel, publicChat.server)
apiDB.clearOpenGroupProfilePictureURL(publicChat.channel, publicChat.server)
ApplicationContext.getInstance(context).publicChatAPI!!
.leave(publicChat.channel, publicChat.server)
ApplicationContext.getInstance(context).publicChatManager
.removeChat(publicChat.server, publicChat.channel)
} else {
threadDB.deleteConversation(threadID)
}
2020-11-23 06:59:44 +01:00
ApplicationContext.getInstance(context).messageNotifier.updateNotification(context)
}
2020-11-23 06:59:44 +01:00
// Notify the user
val toastMessage = if (recipient.isGroupRecipient) R.string.MessageRecord_left_group else R.string.activity_home_conversation_deleted_message
2020-11-23 06:59:44 +01:00
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show()
}}
dialog.setNegativeButton(R.string.no) { _, _ ->
// Do nothing
}
dialog.create().show()
2019-12-17 15:15:13 +01:00
}
private fun openConversation(thread: ThreadRecord) {
val intent = Intent(this, ConversationActivity::class.java)
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, thread.recipient.address)
2019-12-17 15:15:13 +01:00
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, thread.threadId)
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, thread.distributionType)
intent.putExtra(ConversationActivity.TIMING_EXTRA, System.currentTimeMillis())
intent.putExtra(ConversationActivity.LAST_SEEN_EXTRA, thread.lastSeen)
intent.putExtra(ConversationActivity.STARTING_POSITION_EXTRA, -1)
push(intent)
}
2020-01-06 02:07:55 +01:00
private fun openSettings() {
2020-01-06 04:26:52 +01:00
val intent = Intent(this, SettingsActivity::class.java)
show(intent)
2020-01-06 02:07:55 +01:00
}
2020-05-28 08:43:37 +02:00
private fun showPath() {
val intent = Intent(this, PathActivity::class.java)
show(intent)
}
override fun createNewPrivateChat() {
val intent = Intent(this, CreatePrivateChatActivity::class.java)
show(intent)
2019-12-17 15:15:13 +01:00
}
override fun createNewClosedGroup() {
2020-01-31 03:57:24 +01:00
val intent = Intent(this, CreateClosedGroupActivity::class.java)
show(intent, true)
2020-01-31 03:57:24 +01:00
}
override fun joinOpenGroup() {
val intent = Intent(this, JoinPublicChatActivity::class.java)
show(intent)
}
// endregion
2019-12-17 14:27:59 +01:00
}