@ -4,38 +4,77 @@ import android.app.Dialog
import android.graphics.Color
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.appcompat.app.AlertDialog
import android.view.LayoutInflater
import android.view.LayoutInflater
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.*
import kotlinx.android.synthetic.main.dialog_clear_all_data.view.*
import kotlinx.android.synthetic.main.dialog_clear_all_data.view.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import network.loki.messenger.R
import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
import org.session.libsession.messaging.MessagingModuleConfiguration
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
import org.session.libsession.snode.SnodeAPI
import org.session.libsession.snode.SnodeDeleteMessage
import org.session.libsession.utilities.KeyPairUtilities
import org.session.libsession.utilities.KeyPairUtilities
class ClearAllDataDialog : DialogFragment ( ) {
class ClearAllDataDialog : DialogFragment ( ) {
var clearJob : Job ? = null
set ( value ) {
field = value
updateUI ( )
}
override fun onCreateDialog ( savedInstanceState : Bundle ? ) : Dialog {
override fun onCreateDialog ( savedInstanceState : Bundle ? ) : Dialog {
val builder = AlertDialog . Builder ( requireContext ( ) )
val builder = AlertDialog . Builder ( requireContext ( ) )
val contentView = LayoutInflater . from ( requireContext ( ) ) . inflate ( R . layout . dialog _clear _all _data , null )
val contentView = LayoutInflater . from ( requireContext ( ) ) . inflate ( R . layout . dialog _clear _all _data , null )
contentView . cancelButton . setOnClickListener { dismiss ( ) }
contentView . cancelButton . setOnClickListener { dismiss ( ) }
contentView . clearAllDataButton . setOnClickListener { clearAllData ( ) }
contentView . clearAllDataButton . setOnClickListener { clearAllData ( ) }
builder . setView ( contentView )
builder . setView ( contentView )
builder . setCancelable ( false )
val result = builder . create ( )
val result = builder . create ( )
result . window ?. setBackgroundDrawable ( ColorDrawable ( Color . TRANSPARENT ) )
result . window ?. setBackgroundDrawable ( ColorDrawable ( Color . TRANSPARENT ) )
return result
return result
}
}
private fun updateUI ( ) {
if ( clearJob ?. isActive == true ) {
// clear background job is running, prevent interaction
dialog ?. let { view ->
view . cancelButton . isVisible = false
view . clearAllDataButton . isVisible = false
}
} else {
dialog ?. let { view ->
view . cancelButton . isVisible = false
view . clearAllDataButton . isVisible = false
}
}
}
private fun clearAllData ( ) {
private fun clearAllData ( ) {
if ( KeyPairUtilities . hasV2KeyPair ( requireContext ( ) ) ) {
if ( KeyPairUtilities . hasV2KeyPair ( requireContext ( ) ) ) {
MultiDeviceProtocol . forceSyncConfigurationNowIfNeeded ( requireContext ( ) )
clearJob = lifecycleScope . launch {
ApplicationContext . getInstance ( context ) . clearAllData ( false )
delay ( 5 _000 )
// finish
val userPublicKey = MessagingModuleConfiguration . shared . storage . getUserPublicKey ( )
val deleteMessage = SnodeDeleteMessage ( userKey , System . currentTimeMillis ( ) , )
SnodeAPI . deleteAllMessages ( )
// TODO: re-add the clear data here
//ApplicationContext.getInstance(context).clearAllData(false)
}
} else {
} else {
val dialog = AlertDialog . Builder ( requireContext ( ) )
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. "
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 . setMessage ( message )
dialog . setPositiveButton ( " Yes " ) { _ , _ ->
dialog . setPositiveButton ( " Yes " ) { _ , _ ->
ApplicationContext . getInstance ( context ) . clearAllData ( false )
// TODO: re-add the clear data here
// ApplicationContext.getInstance(context).clearAllData(false)
}
}
dialog . setNegativeButton ( " Cancel " ) { _ , _ ->
dialog . setNegativeButton ( " Cancel " ) { _ , _ ->
// Do nothing
// Do nothing