2020-05-11 08:19:26 +02:00
|
|
|
|
package org.thoughtcrime.securesms.loki.dialogs
|
2020-01-07 06:11:02 +01:00
|
|
|
|
|
|
|
|
|
import android.app.Dialog
|
2021-06-18 08:01:34 +02:00
|
|
|
|
import android.content.DialogInterface
|
2020-01-07 06:11:02 +01:00
|
|
|
|
import android.graphics.Color
|
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.LayoutInflater
|
2021-06-17 10:29:05 +02:00
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
|
import androidx.core.view.isVisible
|
|
|
|
|
import androidx.fragment.app.DialogFragment
|
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
|
import kotlinx.android.synthetic.main.dialog_clear_all_data.*
|
2020-01-07 06:11:02 +01:00
|
|
|
|
import kotlinx.android.synthetic.main.dialog_clear_all_data.view.*
|
2021-06-18 08:01:34 +02:00
|
|
|
|
import kotlinx.coroutines.*
|
2020-01-07 06:11:02 +01:00
|
|
|
|
import network.loki.messenger.R
|
2021-06-18 08:01:34 +02:00
|
|
|
|
import nl.komponents.kovenant.Promise
|
2021-06-17 10:29:05 +02:00
|
|
|
|
import org.session.libsession.messaging.MessagingModuleConfiguration
|
|
|
|
|
import org.session.libsession.snode.SnodeAPI
|
2021-03-12 03:37:16 +01:00
|
|
|
|
import org.session.libsession.utilities.KeyPairUtilities
|
2021-06-18 08:01:34 +02:00
|
|
|
|
import org.session.libsignal.utilities.Log
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext
|
|
|
|
|
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
|
|
|
|
|
import java.util.concurrent.Executors
|
2020-01-07 06:11:02 +01:00
|
|
|
|
|
2021-06-18 08:01:34 +02:00
|
|
|
|
class ClearAllDataDialog(val deleteNetworkMessages: Boolean) : DialogFragment() {
|
2020-01-07 06:11:02 +01:00
|
|
|
|
|
2021-06-17 10:29:05 +02:00
|
|
|
|
var clearJob: Job? = null
|
2021-06-18 08:01:34 +02:00
|
|
|
|
set(value) {
|
|
|
|
|
field = value
|
|
|
|
|
}
|
2021-06-17 10:29:05 +02:00
|
|
|
|
|
2020-01-07 06:11:02 +01:00
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
2020-12-17 06:18:01 +01:00
|
|
|
|
val builder = AlertDialog.Builder(requireContext())
|
|
|
|
|
val contentView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_clear_all_data, null)
|
2020-01-07 06:11:02 +01:00
|
|
|
|
contentView.cancelButton.setOnClickListener { dismiss() }
|
|
|
|
|
contentView.clearAllDataButton.setOnClickListener { clearAllData() }
|
|
|
|
|
builder.setView(contentView)
|
2021-06-17 10:29:05 +02:00
|
|
|
|
builder.setCancelable(false)
|
2020-01-07 06:11:02 +01:00
|
|
|
|
val result = builder.create()
|
|
|
|
|
result.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 08:01:34 +02:00
|
|
|
|
override fun onDismiss(dialog: DialogInterface) {
|
|
|
|
|
super.onDismiss(dialog)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onStart() {
|
|
|
|
|
super.onStart()
|
|
|
|
|
isCancelable = false
|
|
|
|
|
dialog?.setCanceledOnTouchOutside(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun updateUI(isLoading: Boolean) {
|
|
|
|
|
dialog?.let { view ->
|
|
|
|
|
view.cancelButton.isVisible = !isLoading
|
|
|
|
|
view.clearAllDataButton.isVisible = !isLoading
|
|
|
|
|
view.progressBar.isVisible = isLoading
|
2021-06-17 10:29:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 06:11:02 +01:00
|
|
|
|
private fun clearAllData() {
|
2020-12-17 06:18:01 +01:00
|
|
|
|
if (KeyPairUtilities.hasV2KeyPair(requireContext())) {
|
2021-06-18 08:01:34 +02:00
|
|
|
|
clearJob = lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
updateUI(true)
|
|
|
|
|
}
|
2021-06-17 10:29:05 +02:00
|
|
|
|
|
2021-06-18 08:01:34 +02:00
|
|
|
|
if (!deleteNetworkMessages) {
|
|
|
|
|
try {
|
|
|
|
|
MultiDeviceProtocol.forceSyncConfigurationNowIfNeeded(requireContext()).get()
|
|
|
|
|
ApplicationContext.getInstance(context).clearAllData(false)
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
dismiss()
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
Log.e("Loki", "Failed to force sync", e)
|
2021-06-21 07:48:42 +02:00
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
updateUI(false)
|
|
|
|
|
}
|
2021-06-18 08:01:34 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// finish
|
2021-06-21 07:48:42 +02:00
|
|
|
|
val promises = try {
|
|
|
|
|
SnodeAPI.deleteAllMessages(requireContext()).get()
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
null
|
|
|
|
|
}
|
2021-06-17 10:29:05 +02:00
|
|
|
|
|
2021-06-21 07:48:42 +02:00
|
|
|
|
val rawResponses = promises?.map {
|
2021-06-18 08:01:34 +02:00
|
|
|
|
try {
|
|
|
|
|
it.get()
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
null
|
|
|
|
|
}
|
2021-06-21 07:48:42 +02:00
|
|
|
|
} ?: listOf(null)
|
2021-06-18 08:01:34 +02:00
|
|
|
|
// TODO: process the responses here
|
2021-06-21 07:48:42 +02:00
|
|
|
|
if (rawResponses.all { it != null }) {
|
2021-06-18 08:01:34 +02:00
|
|
|
|
// don't force sync because all the messages are deleted?
|
|
|
|
|
ApplicationContext.getInstance(context).clearAllData(false)
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
dismiss()
|
|
|
|
|
}
|
2021-06-21 07:48:42 +02:00
|
|
|
|
} else if (rawResponses.any { it == null || it["failed"] as? Boolean == true }) {
|
|
|
|
|
// didn't succeed (at least one)
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
updateUI(false)
|
|
|
|
|
}
|
2021-06-18 08:01:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-17 10:29:05 +02:00
|
|
|
|
}
|
2020-12-17 06:18:01 +01:00
|
|
|
|
} else {
|
|
|
|
|
val dialog = AlertDialog.Builder(requireContext())
|
|
|
|
|
val message = "We’ve upgraded the way Session IDs are generated, so you will be unable to restore your current Session ID."
|
|
|
|
|
dialog.setMessage(message)
|
|
|
|
|
dialog.setPositiveButton("Yes") { _, _ ->
|
2021-06-18 08:01:34 +02:00
|
|
|
|
ApplicationContext.getInstance(context).clearAllData(false)
|
2020-12-17 06:18:01 +01:00
|
|
|
|
}
|
|
|
|
|
dialog.setNegativeButton("Cancel") { _, _ ->
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
dialog.create().show()
|
|
|
|
|
}
|
2020-01-07 06:11:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|