Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable-3.29] BugFix - Setup Encryption Dialog Pop Up Twice #13446

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Nextcloud Android client application
* Nextcloud - Android Client
*
* @author Tobias Kaminsky
* @author TSI-mc
Expand Down Expand Up @@ -52,9 +52,8 @@ import javax.inject.Inject
*/
class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

@JvmField
@Inject
var viewThemeUtils: ViewThemeUtils? = null
lateinit var viewThemeUtils: ViewThemeUtils

private var user: User? = null
private var arbitraryDataProvider: ArbitraryDataProvider? = null
Expand All @@ -77,15 +76,14 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {
val alertDialog = dialog as AlertDialog?

if (alertDialog != null) {
positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as MaterialButton?
negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as MaterialButton?

if (positiveButton != null) {
viewThemeUtils?.material?.colorMaterialButtonPrimaryTonal(positiveButton!!)
positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as? MaterialButton?
positiveButton?.let {
viewThemeUtils.material.colorMaterialButtonPrimaryTonal(it)
}

if (negativeButton != null) {
viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(negativeButton!!)
negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as? MaterialButton?
negativeButton?.let {
viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(it)
}
}
}
Expand All @@ -111,30 +109,25 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {
binding = SetupEncryptionDialogBinding.inflate(inflater, null, false)

// Setup layout
viewThemeUtils?.material?.colorTextInputLayout(binding.encryptionPasswordInputContainer)

return createDialog(binding.root)
viewThemeUtils.material.colorTextInputLayout(binding.encryptionPasswordInputContainer)

val builder = buildMaterialAlertDialog(binding.root)
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(requireContext(), builder)
return builder.create().apply {
setCanceledOnTouchOutside(false)
setOnShowListener { dialog1: DialogInterface ->
val button = (dialog1 as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE)
button.setOnClickListener { positiveButtonOnClick(this) }
}
}
}

private fun createDialog(v: View): Dialog {
val builder = MaterialAlertDialogBuilder(v.context)

builder
private fun buildMaterialAlertDialog(v: View): MaterialAlertDialogBuilder {
return MaterialAlertDialogBuilder(requireContext())
.setView(v)
.setPositiveButton(R.string.common_ok, null)
.setNegativeButton(R.string.common_cancel) { dialog: DialogInterface, _: Int -> dialog.cancel() }
.setTitle(R.string.end_to_end_encryption_title)

viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(v.context, builder)

val dialog: Dialog = builder.create()
dialog.setCanceledOnTouchOutside(false)
dialog.setOnShowListener { dialog1: DialogInterface ->
val button = (dialog1 as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE)
button.setOnClickListener { positiveButtonOnClick(dialog) }
}

return dialog
}

private fun positiveButtonOnClick(dialog: DialogInterface) {
Expand Down Expand Up @@ -243,23 +236,26 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

private val resultIntent: Intent
get() {
val intentCreated = Intent()
intentCreated.putExtra(SUCCESS, true)
intentCreated.putExtra(ARG_POSITION, requireArguments().getInt(ARG_POSITION))
return intentCreated
return Intent().apply {
putExtra(SUCCESS, true)
putExtra(ARG_POSITION, requireArguments().getInt(ARG_POSITION))
}
}
private val resultBundle: Bundle
get() {
val bundle = Bundle()
bundle.putBoolean(SUCCESS, true)
bundle.putInt(ARG_POSITION, requireArguments().getInt(ARG_POSITION))
return bundle
return Bundle().apply {
putBoolean(SUCCESS, true)
putInt(ARG_POSITION, requireArguments().getInt(ARG_POSITION))
}
}

override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
val bundle = Bundle()
bundle.putBoolean(RESULT_KEY_CANCELLED, true)

val bundle = Bundle().apply {
putBoolean(RESULT_KEY_CANCELLED, true)
}

parentFragmentManager.setFragmentResult(RESULT_REQUEST_KEY, bundle)
}

Expand All @@ -270,11 +266,7 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

@SuppressLint("StaticFieldLeak")
inner class DownloadKeysAsyncTask(context: Context) : AsyncTask<Void?, Void?, String?>() {
private val mWeakContext: WeakReference<Context>

init {
mWeakContext = WeakReference(context)
}
private val mWeakContext: WeakReference<Context> = WeakReference(context)

@Suppress("ReturnCount", "LongMethod")
@Deprecated("Deprecated in Java")
Expand All @@ -289,26 +281,26 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

val publicKeyResult = publicKeyOperation.executeNextcloudClient(user, context)

if (publicKeyResult.isSuccess) {
Log_OC.d(TAG, "public key successful downloaded for " + user.accountName)
if (!publicKeyResult.isSuccess) {
return null
}

val publicKeyFromServer = publicKeyResult.resultData
if (arbitraryDataProvider != null) {
arbitraryDataProvider?.storeOrUpdateKeyValue(
user.accountName,
EncryptionUtils.PUBLIC_KEY,
publicKeyFromServer
)
} else {
return null
}
} else {
Log_OC.d(TAG, "public key successful downloaded for " + user.accountName)

if (arbitraryDataProvider == null) {
return null
}

val publicKeyFromServer = publicKeyResult.resultData
arbitraryDataProvider?.storeOrUpdateKeyValue(
user.accountName,
EncryptionUtils.PUBLIC_KEY,
publicKeyFromServer
)

val privateKeyResult = GetPrivateKeyRemoteOperation().executeNextcloudClient(user, context)
if (privateKeyResult.isSuccess) {
Log_OC.d(TAG, "private key successful downloaded for " + user!!.accountName)
Log_OC.d(TAG, "private key successful downloaded for " + user.accountName)
keyResult = KEY_EXISTING_USED
return privateKeyResult.resultData.getKey()
}
Expand All @@ -333,6 +325,7 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {
Log_OC.e(TAG, "Context lost after fetching private keys.")
return
}

if (privateKey == null) {
// first show info
try {
Expand All @@ -355,11 +348,7 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

@SuppressLint("StaticFieldLeak")
inner class GenerateNewKeysAsyncTask(context: Context) : AsyncTask<Void?, Void?, String>() {
private val mWeakContext: WeakReference<Context>

init {
mWeakContext = WeakReference(context)
}
private val mWeakContext: WeakReference<Context> = WeakReference(context)

@Deprecated("Deprecated in Java")
override fun onPreExecute() {
Expand Down Expand Up @@ -470,12 +459,16 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

private fun generateMnemonicString(withWhitespace: Boolean): String {
val stringBuilder = StringBuilder()
for (string in keyWords!!) {
stringBuilder.append(string)
if (withWhitespace) {
stringBuilder.append(' ')

keyWords?.let {
for (string in it) {
stringBuilder.append(string)
if (withWhitespace) {
stringBuilder.append(' ')
}
}
}

return stringBuilder.toString()
}

Expand All @@ -487,13 +480,20 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {
}
requireDialog().setTitle(R.string.end_to_end_encryption_passphrase_title)
binding.encryptionStatus.setText(R.string.end_to_end_encryption_keywords_description)
viewThemeUtils!!.material.colorTextInputLayout(binding.encryptionPasswordInputContainer)
viewThemeUtils.material.colorTextInputLayout(binding.encryptionPasswordInputContainer)
binding.encryptionPassphrase.text = generateMnemonicString(true)
binding.encryptionPassphrase.visibility = View.VISIBLE
positiveButton!!.setText(R.string.end_to_end_encryption_confirm_button)
positiveButton!!.visibility = View.VISIBLE
negativeButton!!.visibility = View.VISIBLE
viewThemeUtils!!.platform.colorTextButtons(positiveButton!!, negativeButton!!)

positiveButton?.setText(R.string.end_to_end_encryption_confirm_button)
positiveButton?.visibility = View.VISIBLE
negativeButton?.visibility = View.VISIBLE

positiveButton?.let { positiveButton ->
negativeButton?.let { negativeButton ->
viewThemeUtils.platform.colorTextButtons(positiveButton, negativeButton)
}
}

keyResult = KEY_GENERATE
}

Expand All @@ -511,9 +511,8 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {

positiveButton?.setText(R.string.end_to_end_encryption_dialog_close)
positiveButton?.visibility = View.VISIBLE

if (positiveButton != null) {
viewThemeUtils?.platform?.colorTextButtons(positiveButton!!)
positiveButton?.let {
viewThemeUtils.platform.colorTextButtons(it)
}
}

Expand Down Expand Up @@ -545,12 +544,14 @@ class SetupEncryptionDialogFragment : DialogFragment(), Injectable {
*/
@JvmStatic
fun newInstance(user: User?, position: Int): SetupEncryptionDialogFragment {
val fragment = SetupEncryptionDialogFragment()
val args = Bundle()
args.putParcelable(ARG_USER, user)
args.putInt(ARG_POSITION, position)
fragment.arguments = args
return fragment
val bundle = Bundle().apply {
putParcelable(ARG_USER, user)
putInt(ARG_POSITION, position)
}

return SetupEncryptionDialogFragment().apply {
arguments = bundle
}
}
}
}