Skip to content

Commit

Permalink
Merge branch 'prepare-for-compose-navigation-droid-442'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Oct 27, 2023
2 parents 44676ed + a275b7b commit ae31f2c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.SecureFlagPolicy
import net.mullvad.mullvadvpn.BuildConfig
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.button.VariantButton
Expand Down Expand Up @@ -171,7 +174,12 @@ fun RedeemVoucherDialog(
titleContentColor = MaterialTheme.colorScheme.onBackground,
onDismissRequest = {
onDismiss(uiState.voucherViewModelState is VoucherDialogState.Success)
}
},
properties =
DialogProperties(
securePolicy =
if (BuildConfig.DEBUG) SecureFlagPolicy.Inherit else SecureFlagPolicy.SecureOn
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import net.mullvad.mullvadvpn.compose.component.MissingPolicy
import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton
import net.mullvad.mullvadvpn.compose.component.ScaffoldWithMediumTopBar
import net.mullvad.mullvadvpn.compose.dialog.DeviceNameInfoDialog
import net.mullvad.mullvadvpn.compose.util.SecureScreenWhileInView
import net.mullvad.mullvadvpn.constant.IS_PLAY_BUILD
import net.mullvad.mullvadvpn.lib.common.util.openAccountPageInBrowser
import net.mullvad.mullvadvpn.lib.theme.AppTheme
Expand Down Expand Up @@ -74,6 +75,9 @@ fun AccountScreen(
onLogoutClick: () -> Unit = {},
onBackClick: () -> Unit = {}
) {
// This will enable SECURE_FLAG while this screen is visible to preview screenshot
SecureScreenWhileInView()

val context = LocalContext.current
val backgroundColor = MaterialTheme.colorScheme.background
val systemUiController = rememberSystemUiController()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.mullvad.mullvadvpn.compose.util

import android.app.Activity
import android.view.WindowManager
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalContext
import net.mullvad.mullvadvpn.BuildConfig

@Composable
fun SecureScreenWhileInView() {
if (BuildConfig.DEBUG) {
return
}
val context = LocalContext.current
val window = (context as Activity).window
val secureScreenWasEnabled = rememberSaveable {
window.attributes.flags and WindowManager.LayoutParams.FLAG_SECURE != 0
}

DisposableEffect(Unit) {
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
onDispose {
if (!secureScreenWasEnabled) {
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import android.content.res.Configuration
import android.net.VpnService
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -59,22 +56,18 @@ import org.koin.android.ext.android.getKoin
import org.koin.core.context.loadKoinModules

open class MainActivity : FragmentActivity() {
private var requestNotificationPermissionLauncher: ActivityResultLauncher<String> =
private val requestNotificationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
// NotificationManager.areNotificationsEnabled is used to check the state rather than
// handling the callback value.
}

private var visibleSecureScreens = HashSet<Fragment>()

private val deviceIsTv by lazy {
val uiModeManager = getSystemService(UI_MODE_SERVICE) as UiModeManager

uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION
}

var backButtonHandler: (() -> Boolean)? = null

private lateinit var accountRepository: AccountRepository
private lateinit var deviceRepository: DeviceRepository
private lateinit var privacyDisclaimerRepository: PrivacyDisclaimerRepository
Expand Down Expand Up @@ -135,14 +128,6 @@ open class MainActivity : FragmentActivity() {
serviceConnectionManager.onVpnPermissionResult(resultCode == Activity.RESULT_OK)
}

override fun onBackPressed() {
val handled = backButtonHandler?.invoke() ?: false

if (!handled) {
super.onBackPressed()
}
}

override fun onStop() {
Log.d("mullvad", "Stopping main activity")
super.onStop()
Expand All @@ -159,26 +144,6 @@ open class MainActivity : FragmentActivity() {
super.onDestroy()
}

fun enterSecureScreen(screen: Fragment) {
synchronized(this) {
visibleSecureScreens.add(screen)

if (!BuildConfig.DEBUG) {
window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}
}

fun leaveSecureScreen(screen: Fragment) {
synchronized(this) {
visibleSecureScreens.remove(screen)

if (!BuildConfig.DEBUG && visibleSecureScreens.isEmpty()) {
window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}
}

fun openAccount() {
supportFragmentManager.beginTransaction().apply {
setCustomAnimations(
Expand Down Expand Up @@ -208,7 +173,6 @@ open class MainActivity : FragmentActivity() {
}

private fun launchDeviceStateHandler(): Job {

return lifecycleScope.launch {
launch {
deviceRepository.deviceState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.mullvad.mullvadvpn.ui.fragment

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -11,7 +10,6 @@ import androidx.compose.ui.platform.ComposeView
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.screen.AccountScreen
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.ui.extension.requireMainActivity
import net.mullvad.mullvadvpn.viewmodel.AccountViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand Down Expand Up @@ -42,16 +40,6 @@ class AccountFragment : BaseFragment() {
}
}

override fun onAttach(activity: Activity) {
super.onAttach(activity)
requireMainActivity().enterSecureScreen(this)
}

override fun onDetach() {
super.onDetach()
requireMainActivity().leaveSecureScreen(this)
}

override fun onEnterTransitionAnimationEnd() {
vm.onTransitionAnimationEnd()
}
Expand Down

0 comments on commit ae31f2c

Please sign in to comment.