diff options
author | Guillaume Jacquart <guillaume.jacquart@hoodbrains.com> | 2022-11-24 07:55:11 +0000 |
---|---|---|
committer | Guillaume Jacquart <guillaume.jacquart@hoodbrains.com> | 2022-11-24 07:55:11 +0000 |
commit | 4df37b50588addf54723b575edfaccea6f34c440 (patch) | |
tree | 18ea5e84b52d775e7130ef0b7e66847555c49524 /app/src/main/java/foundation/e/privacycentralapp/common | |
parent | d0c4e69b9628b56c1e6f9ec84c407cda98c3f8f9 (diff) | |
parent | 8aca1315ebb0e2fa8ef299d65b7f58f2fcb50edb (diff) | |
download | advanced-privacy-4df37b50588addf54723b575edfaccea6f34c440.tar.gz |
Merge branch '5903-warn_user_on_feature_activation' into 'main'
5903 warn user on feature activation
See merge request e/os/advanced-privacy!104
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/common')
-rw-r--r-- | app/src/main/java/foundation/e/privacycentralapp/common/WarningDialog.kt | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/app/src/main/java/foundation/e/privacycentralapp/common/WarningDialog.kt b/app/src/main/java/foundation/e/privacycentralapp/common/WarningDialog.kt new file mode 100644 index 0000000..cbbeffa --- /dev/null +++ b/app/src/main/java/foundation/e/privacycentralapp/common/WarningDialog.kt @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2022 E FOUNDATION + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +package foundation.e.privacycentralapp.common + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.graphics.drawable.ColorDrawable +import android.os.Bundle +import android.util.Log +import android.view.View +import android.widget.CheckBox +import androidx.appcompat.app.AlertDialog +import foundation.e.privacycentralapp.PrivacyCentralApplication +import foundation.e.privacycentralapp.R +import foundation.e.privacycentralapp.domain.entities.MainFeatures +import foundation.e.privacycentralapp.domain.entities.MainFeatures.FAKE_LOCATION +import foundation.e.privacycentralapp.domain.entities.MainFeatures.IP_SCRAMBLING +import foundation.e.privacycentralapp.domain.entities.MainFeatures.TRACKERS_CONTROL +import foundation.e.privacycentralapp.domain.usecases.ShowFeaturesWarningUseCase +import foundation.e.privacycentralapp.main.MainActivity +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.map + +class WarningDialog : Activity() { + companion object { + private const val PARAM_FEATURE = "feature" + + fun startListening( + showFeaturesWarningUseCase: ShowFeaturesWarningUseCase, + appScope: CoroutineScope, + appContext: Context + ) { + showFeaturesWarningUseCase.showWarning().map { feature -> + appContext.startActivity( + createIntent(context = appContext, feature = feature) + ) + }.launchIn(appScope) + } + + private fun createIntent( + context: Context, + feature: MainFeatures, + ): Intent { + val intent = Intent(context, WarningDialog::class.java) + intent.putExtra(PARAM_FEATURE, feature.name) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + return intent + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + getWindow().setBackgroundDrawable(ColorDrawable(0)) + + val feature = try { + MainFeatures.valueOf(intent.getStringExtra(PARAM_FEATURE) ?: "") + } catch (e: Exception) { + Log.e("WarningDialog", "Missing mandatory activity parameter", e) + finish() + return + } + + showWarningDialog(feature) + } + + private fun showWarningDialog(feature: MainFeatures) { + val builder = AlertDialog.Builder(this) + builder.setOnDismissListener { finish() } + + val content: View = layoutInflater.inflate(R.layout.alertdialog_do_not_show_again, null) + val checkbox = content.findViewById<CheckBox>(R.id.checkbox) + builder.setView(content) + + builder.setMessage( + when (feature) { + TRACKERS_CONTROL -> R.string.warningdialog_trackers_message + FAKE_LOCATION -> R.string.warningdialog_location_message + IP_SCRAMBLING -> R.string.warningdialog_ipscrambling_message + } + ) + + builder.setTitle( + when (feature) { + TRACKERS_CONTROL -> R.string.warningdialog_trackers_title + FAKE_LOCATION -> R.string.warningdialog_location_title + IP_SCRAMBLING -> R.string.warningdialog_ipscrambling_title + } + ) + + builder.setPositiveButton( + when (feature) { + IP_SCRAMBLING -> R.string.warningdialog_ipscrambling_cta + else -> R.string.ok + } + ) { _, _ -> + if (checkbox.isChecked()) { + (application as PrivacyCentralApplication) + .dependencyContainer.showFeaturesWarningUseCase + .doNotShowAgain(feature) + } + finish() + } + + if (feature == TRACKERS_CONTROL) { + builder.setNeutralButton(R.string.warningdialog_trackers_secondary_cta) { _, _ -> + startActivity(MainActivity.createTrackersIntent(this)) + finish() + } + } + + builder.show() + } +} |