|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 Tobias Kaminsky <[email protected]> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.owncloud.android.ui.dialog |
| 9 | + |
| 10 | +import android.app.Dialog |
| 11 | +import android.os.Bundle |
| 12 | +import android.view.View |
| 13 | +import android.widget.AdapterView |
| 14 | +import android.widget.ArrayAdapter |
| 15 | +import androidx.core.app.ActivityCompat.finishAffinity |
| 16 | +import androidx.fragment.app.DialogFragment |
| 17 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder |
| 18 | +import com.nextcloud.android.lib.resources.tos.GetTermsRemoteOperation |
| 19 | +import com.nextcloud.android.lib.resources.tos.SignTermRemoteOperation |
| 20 | +import com.nextcloud.android.lib.resources.tos.Term |
| 21 | +import com.nextcloud.client.account.UserAccountManager |
| 22 | +import com.nextcloud.client.di.Injectable |
| 23 | +import com.nextcloud.client.network.ClientFactory |
| 24 | +import com.nextcloud.common.NextcloudClient |
| 25 | +import com.nextcloud.utils.extensions.setHtmlContent |
| 26 | +import com.owncloud.android.R |
| 27 | +import com.owncloud.android.databinding.DialogShowTosBinding |
| 28 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 29 | +import com.owncloud.android.lib.common.utils.Log_OC |
| 30 | +import com.owncloud.android.utils.DisplayUtils |
| 31 | +import com.owncloud.android.utils.theme.ViewThemeUtils |
| 32 | +import kotlinx.coroutines.CoroutineScope |
| 33 | +import kotlinx.coroutines.Dispatchers |
| 34 | +import kotlinx.coroutines.launch |
| 35 | +import kotlinx.coroutines.withContext |
| 36 | +import javax.inject.Inject |
| 37 | + |
| 38 | +class TermsOfServiceDialog : DialogFragment(), Injectable { |
| 39 | + private lateinit var binding: DialogShowTosBinding |
| 40 | + |
| 41 | + @Inject |
| 42 | + lateinit var clientFactory: ClientFactory |
| 43 | + |
| 44 | + @Inject |
| 45 | + lateinit var accountManager: UserAccountManager |
| 46 | + |
| 47 | + @Inject |
| 48 | + lateinit var viewThemeUtils: ViewThemeUtils |
| 49 | + |
| 50 | + lateinit var client: NextcloudClient |
| 51 | + lateinit var terms: List<Term> |
| 52 | + lateinit var languages: Map<String, String> |
| 53 | + |
| 54 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 55 | + super.onCreate(savedInstanceState) |
| 56 | + |
| 57 | + fetchTerms() |
| 58 | + } |
| 59 | + |
| 60 | + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
| 61 | + binding = DialogShowTosBinding.inflate(requireActivity().layoutInflater) |
| 62 | + |
| 63 | + return createDialogBuilder().create() |
| 64 | + } |
| 65 | + |
| 66 | + private fun updateDialog() { |
| 67 | + binding.message.setHtmlContent(terms[0].renderedBody) |
| 68 | + |
| 69 | + val arrayAdapter: ArrayAdapter<String> = ArrayAdapter( |
| 70 | + binding.root.context, |
| 71 | + android.R.layout.simple_spinner_item |
| 72 | + ) |
| 73 | + |
| 74 | + for ((_, _, languageCode) in terms) { |
| 75 | + arrayAdapter.add(languages[languageCode]) |
| 76 | + } |
| 77 | + |
| 78 | + arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) |
| 79 | + |
| 80 | + binding.languageDropdown.adapter = arrayAdapter |
| 81 | + binding.languageDropdown.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { |
| 82 | + override fun onItemSelected(adapterView: AdapterView<*>?, view: View, position: Int, l: Long) { |
| 83 | + binding.message |
| 84 | + .setHtmlContent(terms[position].renderedBody) |
| 85 | + } |
| 86 | + |
| 87 | + override fun onNothingSelected(adapterView: AdapterView<*>?) = Unit |
| 88 | + } |
| 89 | + |
| 90 | + if (terms.size == 1) { |
| 91 | + binding.languageDropdown.visibility = View.GONE |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private fun fetchTerms() { |
| 96 | + // TODO use viewLifecycleOwner instead |
| 97 | + CoroutineScope(Dispatchers.IO).launch { |
| 98 | + try { |
| 99 | + client = clientFactory.createNextcloudClient(accountManager.getUser()) |
| 100 | + val result = GetTermsRemoteOperation().execute(client) |
| 101 | + |
| 102 | + if (result.isSuccess && |
| 103 | + !result.resultData.hasSigned && |
| 104 | + result.resultData.terms.isNotEmpty() |
| 105 | + ) { |
| 106 | + languages = result.resultData.languages |
| 107 | + terms = result.resultData.terms |
| 108 | + |
| 109 | + withContext(Dispatchers.Main) { |
| 110 | + updateDialog() |
| 111 | + } |
| 112 | + } |
| 113 | + } catch (exception: ClientFactory.CreationException) { |
| 114 | + Log_OC.e(TAG, "Error creating client!") |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private fun createDialogBuilder(): MaterialAlertDialogBuilder { |
| 120 | + return MaterialAlertDialogBuilder(binding.root.context) |
| 121 | + .setView(binding.root) |
| 122 | + .setTitle(R.string.terms_of_service_title) |
| 123 | + .setNegativeButton(R.string.dialog_close) { _, _ -> |
| 124 | + activity?.let { finishAffinity(it) } |
| 125 | + } |
| 126 | + .setPositiveButton(R.string.terms_of_services_agree) { dialog, _ -> |
| 127 | + dialog.dismiss() |
| 128 | + Thread { |
| 129 | + val id = binding.languageDropdown.selectedItemPosition |
| 130 | + val signResult: RemoteOperationResult<Void> = |
| 131 | + SignTermRemoteOperation(terms.get(id).id).execute(client) |
| 132 | + if (!signResult.isSuccess) { |
| 133 | + DisplayUtils.showSnackMessage(view, R.string.sign_tos_failed) |
| 134 | + } |
| 135 | + }.start() |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + companion object { |
| 140 | + private const val TAG = "TermsOfServiceDialog" |
| 141 | + } |
| 142 | +} |
0 commit comments