Skip to content

Commit 0da6f5e

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 65bbf88 commit 0da6f5e

1 file changed

Lines changed: 35 additions & 59 deletions

File tree

app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.owncloud.android.lib.resources.users.GetPublicKeyRemoteOperation
3636
import com.owncloud.android.lib.resources.users.GetServerPublicKeyRemoteOperation
3737
import com.owncloud.android.lib.resources.users.SendCSRRemoteOperation
3838
import com.owncloud.android.lib.resources.users.StorePrivateKeyRemoteOperation
39+
import com.owncloud.android.ui.dialog.setupEncryption.model.DownloadKeyResult
3940
import com.owncloud.android.utils.ClipboardUtil
4041
import com.owncloud.android.utils.DisplayUtils
4142
import com.owncloud.android.utils.EncryptionUtils
@@ -49,9 +50,6 @@ import java.io.IOException
4950
import java.lang.ref.WeakReference
5051
import javax.inject.Inject
5152

52-
/*
53-
* Dialog to setup encryption
54-
*/
5553
class SetupEncryptionDialogFragment :
5654
DialogFragment(),
5755
Injectable {
@@ -177,10 +175,6 @@ class SetupEncryptionDialogFragment :
177175
}
178176

179177
val privateKey = (downloadKeyResult as DownloadKeyResult.Success).privateKey
180-
if (privateKey.isNullOrEmpty()) {
181-
Log_OC.e(TAG, "privateKey is null or empty")
182-
return@launch
183-
}
184178
val mnemonicUnchanged = binding.encryptionPasswordInput.text.toString().trim()
185179
val mnemonic =
186180
binding.encryptionPasswordInput.text.toString().replace("\\s".toRegex(), "")
@@ -283,30 +277,6 @@ class SetupEncryptionDialogFragment :
283277
super.onSaveInstanceState(outState)
284278
}
285279

286-
sealed class DownloadKeyResult(open val descriptionId: Int? = null) {
287-
data class CertificateVerificationFailed(
288-
override val descriptionId: Int = R.string.end_to_end_encryption_certificate_verification_failed
289-
) : DownloadKeyResult(descriptionId)
290-
291-
data class ServerPublicKeyUnavailable(
292-
override val descriptionId: Int = R.string.end_to_end_encryption_server_public_key_unavailable
293-
) : DownloadKeyResult(descriptionId)
294-
295-
data class ServerPrivateKeyUnavailable(
296-
override val descriptionId: Int = R.string.end_to_end_encryption_server_private_key_unavailable
297-
) : DownloadKeyResult(descriptionId)
298-
299-
data class CertificateUnavailable(
300-
override val descriptionId: Int = R.string.end_to_end_encryption_certificate_unavailable
301-
) : DownloadKeyResult(descriptionId)
302-
303-
data class UnexpectedError(
304-
override val descriptionId: Int = R.string.end_to_end_encryption_unexpected_error_occurred
305-
) : DownloadKeyResult(descriptionId)
306-
307-
data class Success(val privateKey: String?) : DownloadKeyResult()
308-
}
309-
310280
private suspend fun downloadKeys() {
311281
binding.encryptionStatus.setText(R.string.end_to_end_encryption_retrieving_keys)
312282
positiveButton?.visibility = View.INVISIBLE
@@ -323,7 +293,7 @@ class SetupEncryptionDialogFragment :
323293
// The certificate might not be available on the server yet.
324294
// Therefore, the user needs to generate a new passphrase first, send csr.
325295
return@withContext if (certificateResult.httpCode == HttpStatus.SC_NOT_FOUND) {
326-
DownloadKeyResult.Success(null)
296+
DownloadKeyResult.GeneratePassphraseSendCSR
327297
} else {
328298
DownloadKeyResult.CertificateUnavailable()
329299
}
@@ -355,15 +325,19 @@ class SetupEncryptionDialogFragment :
355325
Log_OC.d(TAG, "private key successful downloaded for " + user.accountName)
356326
keyResult = KEY_EXISTING_USED
357327
val privateKey = privateKeyResult.resultData?.getKey()
358-
DownloadKeyResult.Success(privateKey)
328+
if (privateKey == null) {
329+
DownloadKeyResult.ServerPrivateKeyUnavailable()
330+
} else {
331+
DownloadKeyResult.Success(privateKey)
332+
}
359333
} else {
360334
DownloadKeyResult.ServerPrivateKeyUnavailable()
361335
}
362336
}
363337

364338
downloadKeyResult?.let { result ->
365-
if (result is DownloadKeyResult.Success) {
366-
handlePrivateKey(result.privateKey)
339+
if (result is DownloadKeyResult.Success || result is DownloadKeyResult.GeneratePassphraseSendCSR) {
340+
handlePrivateKey(result)
367341
} else {
368342
val descriptionId = result.descriptionId ?: return
369343
val description = getString(descriptionId)
@@ -373,23 +347,28 @@ class SetupEncryptionDialogFragment :
373347
}
374348
}
375349

376-
private fun handlePrivateKey(privateKey: String?) {
377-
if (privateKey == null) {
378-
// first show info
379-
try {
380-
if (keyWords == null || keyWords!!.isEmpty()) {
381-
keyWords = EncryptionUtils.getRandomWords(NUMBER_OF_WORDS, context)
350+
private fun handlePrivateKey(result: DownloadKeyResult) {
351+
when (result) {
352+
is DownloadKeyResult.Success -> {
353+
binding.encryptionStatus.setText(R.string.end_to_end_encryption_enter_passphrase_to_access_files)
354+
binding.encryptionPasswordInputContainer.visibility = View.VISIBLE
355+
positiveButton?.visibility = View.VISIBLE
356+
}
357+
358+
is DownloadKeyResult.GeneratePassphraseSendCSR -> {
359+
try {
360+
if (keyWords.isNullOrEmpty()) {
361+
keyWords = EncryptionUtils.getRandomWords(NUMBER_OF_WORDS, context)
362+
}
363+
showMnemonicInfo()
364+
} catch (_: IOException) {
365+
binding.encryptionStatus.setText(R.string.common_error)
382366
}
383-
showMnemonicInfo()
384-
} catch (e: IOException) {
385-
binding.encryptionStatus.setText(R.string.common_error)
386367
}
387-
} else if (privateKey.isNotEmpty()) {
388-
binding.encryptionStatus.setText(R.string.end_to_end_encryption_enter_passphrase_to_access_files)
389-
binding.encryptionPasswordInputContainer.visibility = View.VISIBLE
390-
positiveButton?.visibility = View.VISIBLE
391-
} else {
392-
Log_OC.e(TAG, "Got empty private key string")
368+
369+
else -> {
370+
Log_OC.e(TAG, "Got empty private key string")
371+
}
393372
}
394373
}
395374

@@ -574,15 +553,12 @@ class SetupEncryptionDialogFragment :
574553
private const val KEY_GENERATE = "KEY_GENERATE"
575554

576555
@JvmStatic
577-
fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment {
578-
val bundle = Bundle().apply {
579-
putParcelable(ARG_USER, user)
580-
putString(ARG_FILE_PATH, filePath)
581-
}
582-
583-
return SetupEncryptionDialogFragment().apply {
584-
arguments = bundle
556+
fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment =
557+
SetupEncryptionDialogFragment().apply {
558+
arguments = Bundle().apply {
559+
putParcelable(ARG_USER, user)
560+
putString(ARG_FILE_PATH, filePath)
561+
}
585562
}
586-
}
587563
}
588564
}

0 commit comments

Comments
 (0)