-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tobiasKaminsky <[email protected]>
- Loading branch information
1 parent
a4038fe
commit 31b58d0
Showing
7 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
app/src/main/java/com/owncloud/android/ui/dialog/TermsOfServiceDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2025 Tobias Kaminsky <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.owncloud.android.ui.dialog | ||
|
||
import android.app.Dialog | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.AdapterView | ||
import android.widget.ArrayAdapter | ||
import androidx.core.app.ActivityCompat.finishAffinity | ||
import androidx.fragment.app.DialogFragment | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import com.nextcloud.android.lib.resources.tos.GetTermsRemoteOperation | ||
import com.nextcloud.android.lib.resources.tos.SignTermRemoteOperation | ||
import com.nextcloud.android.lib.resources.tos.Term | ||
import com.nextcloud.client.account.UserAccountManager | ||
import com.nextcloud.client.di.Injectable | ||
import com.nextcloud.client.network.ClientFactory | ||
import com.nextcloud.common.NextcloudClient | ||
import com.nextcloud.utils.extensions.setHtmlContent | ||
import com.owncloud.android.R | ||
import com.owncloud.android.databinding.DialogShowTosBinding | ||
import com.owncloud.android.lib.common.operations.RemoteOperationResult | ||
import com.owncloud.android.lib.common.utils.Log_OC | ||
import com.owncloud.android.utils.DisplayUtils | ||
import com.owncloud.android.utils.theme.ViewThemeUtils | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class TermsOfServiceDialog : DialogFragment(), Injectable { | ||
private lateinit var binding: DialogShowTosBinding | ||
|
||
@Inject | ||
lateinit var clientFactory: ClientFactory | ||
|
||
@Inject | ||
lateinit var accountManager: UserAccountManager | ||
|
||
@Inject | ||
lateinit var viewThemeUtils: ViewThemeUtils | ||
|
||
lateinit var client: NextcloudClient | ||
lateinit var terms: List<Term> | ||
lateinit var languages: Map<String, String> | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
fetchTerms() | ||
} | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
binding = DialogShowTosBinding.inflate(requireActivity().layoutInflater) | ||
|
||
return createDialogBuilder().create() | ||
} | ||
|
||
private fun updateDialog() { | ||
binding.message.setHtmlContent(terms[0].renderedBody) | ||
|
||
val arrayAdapter: ArrayAdapter<String> = ArrayAdapter( | ||
binding.root.context, | ||
android.R.layout.simple_spinner_item | ||
) | ||
|
||
for ((_, _, languageCode) in terms) { | ||
arrayAdapter.add(languages[languageCode]) | ||
} | ||
|
||
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) | ||
|
||
binding.languageDropdown.adapter = arrayAdapter | ||
binding.languageDropdown.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | ||
override fun onItemSelected(adapterView: AdapterView<*>?, view: View, position: Int, l: Long) { | ||
binding.message | ||
.setHtmlContent(terms[position].renderedBody) | ||
} | ||
|
||
override fun onNothingSelected(adapterView: AdapterView<*>?) = Unit | ||
} | ||
|
||
if (terms.size == 1) { | ||
binding.languageDropdown.visibility = View.GONE | ||
} | ||
} | ||
|
||
private fun fetchTerms() { | ||
// TODO use viewLifecycleOwner instead | ||
CoroutineScope(Dispatchers.IO).launch { | ||
try { | ||
client = clientFactory.createNextcloudClient(accountManager.getUser()) | ||
val result = GetTermsRemoteOperation().execute(client) | ||
|
||
if (result.isSuccess && | ||
!result.resultData.hasSigned && | ||
result.resultData.terms.isNotEmpty() | ||
) { | ||
languages = result.resultData.languages | ||
terms = result.resultData.terms | ||
|
||
withContext(Dispatchers.Main) { | ||
updateDialog() | ||
} | ||
} | ||
} catch (exception: ClientFactory.CreationException) { | ||
Log_OC.e(TAG, "Error creating client!") | ||
} | ||
} | ||
} | ||
|
||
private fun createDialogBuilder(): MaterialAlertDialogBuilder { | ||
return MaterialAlertDialogBuilder(binding.root.context) | ||
.setView(binding.root) | ||
.setTitle(R.string.terms_of_service_title) | ||
.setNegativeButton(R.string.dialog_close) { _, _ -> | ||
activity?.let { finishAffinity(it) } | ||
} | ||
.setPositiveButton(R.string.terms_of_services_agree) { dialog, _ -> | ||
dialog.dismiss() | ||
Thread { | ||
val id = binding.languageDropdown.selectedItemPosition | ||
val signResult: RemoteOperationResult<Void> = | ||
SignTermRemoteOperation(terms.get(id).id).execute(client) | ||
if (!signResult.isSuccess) { | ||
DisplayUtils.showSnackMessage(view, R.string.sign_tos_failed) | ||
} | ||
}.start() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val TAG = "TermsOfServiceDialog" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="utf-8"?><!-- | ||
~ Nextcloud - Android Client | ||
~ | ||
~ SPDX-FileCopyrightText: 2022 Álvaro Brey <[email protected]> | ||
~ SPDX-FileCopyrightText: 2022 Nextcloud GmbH | ||
~ SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only | ||
--> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:clickable="false" | ||
android:focusable="true" | ||
android:orientation="vertical" | ||
android:paddingHorizontal="?dialogPreferredPadding"> | ||
|
||
<Spinner | ||
android:id="@+id/language_dropdown" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="end" /> | ||
|
||
<ScrollView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/message" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
</LinearLayout> | ||
</ScrollView> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters