|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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.app.browser.webview |
| 18 | + |
| 19 | +import android.content.Intent |
| 20 | +import android.os.Bundle |
| 21 | +import android.widget.Toast |
| 22 | +import androidx.activity.result.ActivityResultLauncher |
| 23 | +import androidx.activity.result.contract.ActivityResultContracts |
| 24 | +import androidx.core.net.toUri |
| 25 | +import androidx.lifecycle.Lifecycle |
| 26 | +import androidx.lifecycle.flowWithLifecycle |
| 27 | +import androidx.lifecycle.lifecycleScope |
| 28 | +import com.duckduckgo.anvil.annotations.ContributeToActivityStarter |
| 29 | +import com.duckduckgo.anvil.annotations.InjectWith |
| 30 | +import com.duckduckgo.app.browser.R |
| 31 | +import com.duckduckgo.app.browser.databinding.ActivityWebViewDevSettingsBinding |
| 32 | +import com.duckduckgo.app.browser.webview.WebViewDevSettingsViewModel.ViewState |
| 33 | +import com.duckduckgo.app.clipboard.ClipboardInteractor |
| 34 | +import com.duckduckgo.common.ui.DuckDuckGoActivity |
| 35 | +import com.duckduckgo.common.ui.viewbinding.viewBinding |
| 36 | +import com.duckduckgo.common.utils.playstore.PlayStoreAndroidUtils.Companion.PLAY_STORE_PACKAGE |
| 37 | +import com.duckduckgo.di.scopes.ActivityScope |
| 38 | +import com.duckduckgo.navigation.api.GlobalActivityStarter |
| 39 | +import javax.inject.Inject |
| 40 | +import kotlinx.coroutines.flow.launchIn |
| 41 | +import kotlinx.coroutines.flow.onEach |
| 42 | + |
| 43 | +@InjectWith(ActivityScope::class) |
| 44 | +@ContributeToActivityStarter(WebViewDevSettingsScreen::class) |
| 45 | +class WebViewDevSettingsActivity : DuckDuckGoActivity() { |
| 46 | + |
| 47 | + @Inject |
| 48 | + lateinit var viewModel: WebViewDevSettingsViewModel |
| 49 | + |
| 50 | + @Inject |
| 51 | + lateinit var clipboardManager: ClipboardInteractor |
| 52 | + |
| 53 | + private val binding: ActivityWebViewDevSettingsBinding by viewBinding() |
| 54 | + |
| 55 | + private lateinit var webViewDebugLauncher: ActivityResultLauncher<Intent> |
| 56 | + |
| 57 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 58 | + super.onCreate(savedInstanceState) |
| 59 | + setContentView(binding.root) |
| 60 | + setupToolbar(binding.includeToolbar.toolbar) |
| 61 | + registerActivityResultLauncher() |
| 62 | + configureListeners() |
| 63 | + observeViewModel() |
| 64 | + } |
| 65 | + |
| 66 | + private fun observeViewModel() { |
| 67 | + viewModel.viewState() |
| 68 | + .flowWithLifecycle(lifecycle, Lifecycle.State.CREATED) |
| 69 | + .onEach { viewState -> |
| 70 | + updateViews(viewState) |
| 71 | + }.launchIn(lifecycleScope) |
| 72 | + } |
| 73 | + |
| 74 | + private fun updateViews(viewState: ViewState) { |
| 75 | + binding.webViewPackage.setSecondaryText(viewState.webViewPackage) |
| 76 | + binding.webViewVersion.setSecondaryText(viewState.webViewVersion) |
| 77 | + } |
| 78 | + |
| 79 | + override fun onStart() { |
| 80 | + super.onStart() |
| 81 | + viewModel.start() |
| 82 | + } |
| 83 | + |
| 84 | + private fun configureListeners() { |
| 85 | + binding.webViewDevTools.setOnClickListener { |
| 86 | + launchExternalIntentForAction(Intent(WEBVIEW_DEV_TOOLS_INTENT_ACTION), getString(R.string.webview_dev_ui_not_available)) |
| 87 | + } |
| 88 | + binding.webViewVersion.setClickListener { |
| 89 | + clipboardManager.copyToClipboard(viewModel.viewState().value.webViewVersion, false) |
| 90 | + } |
| 91 | + binding.webViewPackage.setClickListener { |
| 92 | + clipboardManager.copyToClipboard(viewModel.viewState().value.webViewPackage, false) |
| 93 | + } |
| 94 | + binding.webViewPlayStore.setClickListener { |
| 95 | + val intent = Intent(Intent.ACTION_VIEW).also { |
| 96 | + it.setData(PLAYSTORE_WEBVIEW_INTENT_URI.toUri()) |
| 97 | + it.setPackage(PLAY_STORE_PACKAGE) |
| 98 | + } |
| 99 | + launchExternalIntentForAction(intent, getString(R.string.webview_play_store_unavailable)) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private fun registerActivityResultLauncher() { |
| 104 | + webViewDebugLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun launchExternalIntentForAction(intent: Intent, errorIfCannotLaunch: String) { |
| 109 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) |
| 110 | + |
| 111 | + if (intent.resolveActivity(packageManager) != null) { |
| 112 | + startActivity(intent) |
| 113 | + } else { |
| 114 | + Toast.makeText(this, errorIfCannotLaunch, Toast.LENGTH_SHORT).show() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + companion object { |
| 119 | + private const val PLAYSTORE_WEBVIEW_INTENT_URI = "market://details?id=com.google.android.webview" |
| 120 | + private const val WEBVIEW_DEV_TOOLS_INTENT_ACTION = "com.android.webview.SHOW_DEV_UI" |
| 121 | + private const val PLAY_STORE_PACKAGE = "com.android.vending" |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +data object WebViewDevSettingsScreen : GlobalActivityStarter.ActivityParams { |
| 126 | + private fun readResolve(): Any = WebViewDevSettingsScreen |
| 127 | +} |
0 commit comments