|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.autofill.impl.ui.credential.management.viewing |
| 18 | + |
| 19 | +import android.app.Activity |
| 20 | +import android.content.Context |
| 21 | +import android.content.DialogInterface |
| 22 | +import android.content.Intent |
| 23 | +import android.os.Bundle |
| 24 | +import android.os.Parcelable |
| 25 | +import android.view.LayoutInflater |
| 26 | +import android.view.View |
| 27 | +import android.view.ViewGroup |
| 28 | +import androidx.activity.result.ActivityResult |
| 29 | +import androidx.activity.result.contract.ActivityResultContracts |
| 30 | +import androidx.core.content.IntentCompat |
| 31 | +import androidx.fragment.app.setFragmentResult |
| 32 | +import androidx.lifecycle.lifecycleScope |
| 33 | +import com.duckduckgo.anvil.annotations.InjectWith |
| 34 | +import com.duckduckgo.app.browser.favicon.FaviconManager |
| 35 | +import com.duckduckgo.app.statistics.pixels.Pixel |
| 36 | +import com.duckduckgo.autofill.api.AutofillScreens.ImportGooglePassword |
| 37 | +import com.duckduckgo.autofill.impl.R |
| 38 | +import com.duckduckgo.autofill.impl.databinding.ContentChooseImportPasswordMethodBinding |
| 39 | +import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter |
| 40 | +import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.ImportResult.Error |
| 41 | +import com.duckduckgo.autofill.impl.importing.CsvPasswordImporter.ImportResult.Success |
| 42 | +import com.duckduckgo.autofill.impl.ui.credential.dialog.animateClosed |
| 43 | +import com.duckduckgo.di.scopes.FragmentScope |
| 44 | +import com.duckduckgo.navigation.api.GlobalActivityStarter |
| 45 | +import com.google.android.material.bottomsheet.BottomSheetBehavior |
| 46 | +import com.google.android.material.bottomsheet.BottomSheetDialog |
| 47 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment |
| 48 | +import dagger.android.support.AndroidSupportInjection |
| 49 | +import javax.inject.Inject |
| 50 | +import kotlinx.coroutines.launch |
| 51 | +import kotlinx.parcelize.Parcelize |
| 52 | +import timber.log.Timber |
| 53 | + |
| 54 | +@InjectWith(FragmentScope::class) |
| 55 | +class SelectImportPasswordMethodDialog : BottomSheetDialogFragment() { |
| 56 | + |
| 57 | + @Inject |
| 58 | + lateinit var pixel: Pixel |
| 59 | + |
| 60 | + /** |
| 61 | + * To capture all the ways the BottomSheet can be dismissed, we might end up with onCancel being called when we don't want it |
| 62 | + * This flag is set to true when taking an action which dismisses the dialog, but should not be treated as a cancellation. |
| 63 | + */ |
| 64 | + private var ignoreCancellationEvents = false |
| 65 | + |
| 66 | + override fun getTheme(): Int = R.style.AutofillBottomSheetDialogTheme |
| 67 | + |
| 68 | + @Inject |
| 69 | + lateinit var faviconManager: FaviconManager |
| 70 | + |
| 71 | + @Inject |
| 72 | + lateinit var csvPasswordImporter: CsvPasswordImporter |
| 73 | + |
| 74 | + @Inject |
| 75 | + lateinit var globalActivityStarter: GlobalActivityStarter |
| 76 | + |
| 77 | + private val importGooglePasswordsFlowLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> |
| 78 | + Timber.i("cdr onActivityResult for Google Password Manager import flow. resultCode=${result.resultCode}") |
| 79 | + |
| 80 | + if (result.resultCode == Activity.RESULT_OK) { |
| 81 | + when (val resultDetails = parseGooglePasswordImportResultDetails(result)) { |
| 82 | + is ImportGooglePassword.Result.Success -> setResult(Result.UserChoseGcmImport(resultDetails.importedCount)) |
| 83 | + is ImportGooglePassword.Result.Error -> setResult(Result.ErrorDuringImport) |
| 84 | + is ImportGooglePassword.Result.UserCancelled -> {} |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private val importCsvLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> |
| 90 | + if (result.resultCode == Activity.RESULT_OK) { |
| 91 | + val data: Intent? = result.data |
| 92 | + val fileUrl = data?.data |
| 93 | + |
| 94 | + Timber.i("cdr onActivityResult for CSV file request. resultCode=${result.resultCode}. uri=$fileUrl") |
| 95 | + |
| 96 | + if (fileUrl != null) { |
| 97 | + lifecycleScope.launch { |
| 98 | + when (val importResult = csvPasswordImporter.importCsv(fileUrl)) { |
| 99 | + is Success -> { |
| 100 | + setResult(Result.UserChoseCsvImport(importResult.passwordIdsImported.size)) |
| 101 | + } |
| 102 | + |
| 103 | + Error -> setResult(Result.ErrorDuringImport) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + override fun onAttach(context: Context) { |
| 111 | + AndroidSupportInjection.inject(this) |
| 112 | + super.onAttach(context) |
| 113 | + } |
| 114 | + |
| 115 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 116 | + super.onCreate(savedInstanceState) |
| 117 | + |
| 118 | + if (savedInstanceState != null) { |
| 119 | + // If being created after a configuration change, dismiss the dialog as the WebView will be re-created too |
| 120 | + dismiss() |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + override fun onCreateView( |
| 125 | + inflater: LayoutInflater, |
| 126 | + container: ViewGroup?, |
| 127 | + savedInstanceState: Bundle?, |
| 128 | + ): View { |
| 129 | + val binding = ContentChooseImportPasswordMethodBinding.inflate(inflater, container, false) |
| 130 | + configureViews(binding) |
| 131 | + return binding.root |
| 132 | + } |
| 133 | + |
| 134 | + private fun configureViews(binding: ContentChooseImportPasswordMethodBinding) { |
| 135 | + (dialog as BottomSheetDialog).behavior.state = BottomSheetBehavior.STATE_EXPANDED |
| 136 | + configureCloseButton(binding) |
| 137 | + binding.importGcmButton.setOnClickListener { onImportGcmButtonClicked() } |
| 138 | + binding.csvButton.setOnClickListener { onImportCsvButtonClicked() } |
| 139 | + binding.desktopSyncButton.setOnClickListener { onImportDesktopSyncButtonClicked() } |
| 140 | + } |
| 141 | + |
| 142 | + private fun onImportGcmButtonClicked() { |
| 143 | + launchImportGcmFlow() |
| 144 | + } |
| 145 | + |
| 146 | + private fun launchImportGcmFlow() { |
| 147 | + val intent = globalActivityStarter.startIntent(requireContext(), ImportGooglePassword.AutofillImportViaGooglePasswordManagerScreen) |
| 148 | + importGooglePasswordsFlowLauncher.launch(intent) |
| 149 | + } |
| 150 | + |
| 151 | + private fun onImportCsvButtonClicked() { |
| 152 | + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { |
| 153 | + addCategory(Intent.CATEGORY_OPENABLE) |
| 154 | + type = "*/*" |
| 155 | + } |
| 156 | + importCsvLauncher.launch(intent) |
| 157 | + } |
| 158 | + |
| 159 | + private fun onImportDesktopSyncButtonClicked() { |
| 160 | + setResult(Result.UserChoseDesktopSyncImport) |
| 161 | + } |
| 162 | + |
| 163 | + private fun setResult(result: Result) { |
| 164 | + val resultBundle = Bundle().apply { |
| 165 | + putParcelable(RESULT_KEY_DETAILS, result) |
| 166 | + } |
| 167 | + setFragmentResult(RESULT_KEY, resultBundle) |
| 168 | + dismiss() |
| 169 | + } |
| 170 | + |
| 171 | + override fun onCancel(dialog: DialogInterface) { |
| 172 | + if (ignoreCancellationEvents) { |
| 173 | + Timber.v("onCancel: Ignoring cancellation event") |
| 174 | + return |
| 175 | + } |
| 176 | + // parentFragment?.setFragmentResult(CredentialAutofillPickerDialog.resultKey(getTabId()), result) |
| 177 | + setResult(Result.UserCancelled) |
| 178 | + } |
| 179 | + |
| 180 | + private fun configureCloseButton(binding: ContentChooseImportPasswordMethodBinding) { |
| 181 | + binding.closeButton.setOnClickListener { (dialog as BottomSheetDialog).animateClosed() } |
| 182 | + } |
| 183 | + |
| 184 | + private fun parseGooglePasswordImportResultDetails(result: ActivityResult): ImportGooglePassword.Result { |
| 185 | + return IntentCompat.getParcelableExtra(result.data!!, ImportGooglePassword.Result.RESULT_KEY_DETAILS, ImportGooglePassword.Result::class.java)!! |
| 186 | + } |
| 187 | + |
| 188 | + companion object { |
| 189 | + |
| 190 | + fun instance(): SelectImportPasswordMethodDialog { |
| 191 | + val fragment = SelectImportPasswordMethodDialog() |
| 192 | + fragment.arguments = Bundle() |
| 193 | + return fragment |
| 194 | + } |
| 195 | + |
| 196 | + const val RESULT_KEY = "SelectImportPasswordMethodDialogResult" |
| 197 | + const val RESULT_KEY_DETAILS = "SelectImportPasswordMethodDialogResultDetails" |
| 198 | + |
| 199 | + sealed interface Result : Parcelable { |
| 200 | + @Parcelize |
| 201 | + data class UserChoseGcmImport(val numberImported: Int) : Result |
| 202 | + |
| 203 | + @Parcelize |
| 204 | + data class UserChoseCsvImport(val numberImported: Int) : Result |
| 205 | + |
| 206 | + @Parcelize |
| 207 | + data object UserChoseDesktopSyncImport : Result |
| 208 | + |
| 209 | + @Parcelize |
| 210 | + data object UserCancelled : Result |
| 211 | + |
| 212 | + @Parcelize |
| 213 | + data object ErrorDuringImport : Result |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments