Skip to content

Commit

Permalink
Allow setting of window flags on Stripe Activities (#1947)
Browse files Browse the repository at this point in the history
Added `PaymentSessionConfig.Builder#setWindowFlags()` for
applying to all Stripe Activites launched during a
`PaymentSession`.

Added the following for individual Stripe Activities:
- `PaymentFlowActivityStarter.Args.Builder#setWindowFlags()`
- `AddPaymentMethodActivityStarter.Args.Builder#setWindowFlags()`
- `PaymentMethodsActivityStarter.Args.Builder#setWindowFlags()`

Fixes #1945
  • Loading branch information
mshafrir-stripe authored Dec 17, 2019
1 parent 46dde8a commit fa289b1
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.example.activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -87,6 +88,7 @@ class PaymentSessionActivity : AppCompatActivity() {
.setAllowedShippingCountryCodes(setOf("US", "CA"))
.setShippingInformationValidator(ShippingInformationValidator())
.setShippingMethodsFactory(ShippingMethodsFactory())
.setWindowFlags(WindowManager.LayoutParams.FLAG_SECURE)
.setBillingAddressFields(BillingAddressFields.Full)
.build(),
savedInstanceState = savedInstanceState,
Expand Down
2 changes: 2 additions & 0 deletions stripe/src/main/java/com/stripe/android/PaymentSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class PaymentSession @VisibleForTesting internal constructor(
.setIsPaymentSessionActive(true)
.setPaymentConfiguration(PaymentConfiguration.getInstance(context))
.setPaymentMethodTypes(config?.paymentMethodTypes.orEmpty())
.setWindowFlags(config?.windowFlags)
.setBillingAddressFields(config?.billingAddressFields ?: BillingAddressFields.None)
.build()
)
Expand Down Expand Up @@ -283,6 +284,7 @@ class PaymentSession @VisibleForTesting internal constructor(
.setPaymentSessionConfig(config)
.setPaymentSessionData(paymentSessionData)
.setIsPaymentSessionActive(true)
.setWindowFlags(config?.windowFlags)
.build()
)
}
Expand Down
14 changes: 13 additions & 1 deletion stripe/src/main/java/com/stripe/android/PaymentSessionConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ data class PaymentSessionConfig internal constructor(
val billingAddressFields: BillingAddressFields = BillingAddressFields.None,

internal val shippingInformationValidator: ShippingInformationValidator? = null,
internal val shippingMethodsFactory: ShippingMethodsFactory? = null
internal val shippingMethodsFactory: ShippingMethodsFactory? = null,
internal val windowFlags: Int? = null
) : Parcelable {
init {
val countryCodes = Locale.getISOCountries()
Expand Down Expand Up @@ -95,6 +96,7 @@ data class PaymentSessionConfig internal constructor(
private var allowedShippingCountryCodes: Set<String> = emptySet()
private var shippingInformationValidator: ShippingInformationValidator? = null
private var shippingMethodsFactory: ShippingMethodsFactory? = null
private var windowFlags: Int? = null

@LayoutRes
private var addPaymentMethodFooterLayoutId: Int = 0
Expand Down Expand Up @@ -191,6 +193,15 @@ data class PaymentSessionConfig internal constructor(
this.allowedShippingCountryCodes = allowedShippingCountryCodes
}

/**
* @param windowFlags optional flags to set on the `Window` object of Stripe Activities
*
* See [WindowManager.LayoutParams](https://developer.android.com/reference/android/view/WindowManager.LayoutParams)
*/
fun setWindowFlags(windowFlags: Int?): Builder = apply {
this.windowFlags = windowFlags
}

/**
* @param shippingInformationValidator if specified, will be used to validate
* [ShippingInformation] in [PaymentFlowActivity] instead of sending a broadcast with
Expand Down Expand Up @@ -229,6 +240,7 @@ data class PaymentSessionConfig internal constructor(
allowedShippingCountryCodes = allowedShippingCountryCodes,
shippingInformationValidator = shippingInformationValidator,
shippingMethodsFactory = shippingMethodsFactory,
windowFlags = windowFlags,
billingAddressFields = billingAddressFields
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class AddPaymentMethodActivity : StripeActivity() {
}

private fun configureView(args: AddPaymentMethodActivityStarter.Args) {
args.windowFlags?.let {
window.addFlags(it)
}

viewStub.layoutResource = R.layout.add_payment_method_layout
val scrollView = viewStub.inflate() as ViewGroup
val contentRoot: ViewGroup =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class AddPaymentMethodActivityStarter constructor(
internal val shouldInitCustomerSessionTokens: Boolean,
internal val paymentMethodType: PaymentMethod.Type,
internal val paymentConfiguration: PaymentConfiguration?,
@LayoutRes internal val addPaymentMethodFooterLayoutId: Int
@LayoutRes internal val addPaymentMethodFooterLayoutId: Int,
internal val windowFlags: Int? = null
) : ActivityStarter.Args {

class Builder : ObjectBuilder<Args> {
Expand All @@ -48,6 +49,7 @@ class AddPaymentMethodActivityStarter constructor(
private var paymentConfiguration: PaymentConfiguration? = null
@LayoutRes
private var addPaymentMethodFooterLayoutId: Int = 0
private var windowFlags: Int? = null

/**
* If true, the created Payment Method will be attached to the current Customer
Expand Down Expand Up @@ -99,6 +101,15 @@ class AddPaymentMethodActivityStarter constructor(
this.addPaymentMethodFooterLayoutId = addPaymentMethodFooterLayoutId
}

/**
* @param windowFlags optional flags to set on the `Window` object of Stripe Activities
*
* See [WindowManager.LayoutParams](https://developer.android.com/reference/android/view/WindowManager.LayoutParams)
*/
fun setWindowFlags(windowFlags: Int?): Builder = apply {
this.windowFlags = windowFlags
}

@JvmSynthetic
internal fun setIsPaymentSessionActive(
isPaymentSessionActive: Boolean
Expand Down Expand Up @@ -129,7 +140,8 @@ class AddPaymentMethodActivityStarter constructor(
shouldInitCustomerSessionTokens = shouldInitCustomerSessionTokens,
paymentMethodType = paymentMethodType ?: PaymentMethod.Type.Card,
paymentConfiguration = paymentConfiguration,
addPaymentMethodFooterLayoutId = addPaymentMethodFooterLayoutId
addPaymentMethodFooterLayoutId = addPaymentMethodFooterLayoutId,
windowFlags = windowFlags
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ internal class AddPaymentMethodCardRowView internal constructor(
.setPaymentMethodType(PaymentMethod.Type.Card)
.setAddPaymentMethodFooter(args.addPaymentMethodFooterLayoutId)
.setPaymentConfiguration(args.paymentConfiguration)
.setWindowFlags(args.windowFlags)
.build()
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PaymentFlowActivity : StripeActivity() {
super.onCreate(savedInstanceState)

val args = PaymentFlowActivityStarter.Args.create(intent)
args.windowFlags?.let { window.addFlags(it) }

customerSession = CustomerSession.getInstance()
customerSession.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ class PaymentFlowActivityStarter :
data class Args internal constructor(
internal val paymentSessionConfig: PaymentSessionConfig,
internal val paymentSessionData: PaymentSessionData,
internal val isPaymentSessionActive: Boolean
internal val isPaymentSessionActive: Boolean,
internal val windowFlags: Int? = null
) : ActivityStarter.Args {
class Builder : ObjectBuilder<Args> {
private var paymentSessionConfig: PaymentSessionConfig? = null
private var paymentSessionData: PaymentSessionData? = null
private var isPaymentSessionActive = false
private var windowFlags: Int? = null

fun setPaymentSessionConfig(
paymentSessionConfig: PaymentSessionConfig?
Expand All @@ -44,14 +46,24 @@ class PaymentFlowActivityStarter :
this.isPaymentSessionActive = isPaymentSessionActive
}

/**
* @param windowFlags optional flags to set on the `Window` object of Stripe Activities
*
* See [WindowManager.LayoutParams](https://developer.android.com/reference/android/view/WindowManager.LayoutParams)
*/
fun setWindowFlags(windowFlags: Int?): Builder = apply {
this.windowFlags = windowFlags
}

override fun build(): Args {
return Args(
paymentSessionConfig = paymentSessionConfig
?: PaymentSessionConfig.Builder().build(),
paymentSessionData = requireNotNull(paymentSessionData) {
"PaymentFlowActivity launched without PaymentSessionData"
},
isPaymentSessionActive = isPaymentSessionActive
isPaymentSessionActive = isPaymentSessionActive,
windowFlags = windowFlags
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class PaymentMethodsActivity : AppCompatActivity() {
PaymentMethodsViewModel.Factory(customerSession, args.initialPaymentMethodId)
)[PaymentMethodsViewModel::class.java]

args.windowFlags?.let {
window.addFlags(it)
}

startedFromPaymentSession = args.isPaymentSessionActive
cardDisplayTextFactory = CardDisplayTextFactory.create(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PaymentMethodsActivityStarter : ActivityStarter<PaymentMethodsActivity, Ar
internal val isPaymentSessionActive: Boolean,
internal val paymentMethodTypes: List<PaymentMethod.Type>,
internal val paymentConfiguration: PaymentConfiguration?,
internal val windowFlags: Int? = null,
internal val billingAddressFields: BillingAddressFields
) : ActivityStarter.Args {
class Builder : ObjectBuilder<Args> {
Expand All @@ -56,6 +57,7 @@ class PaymentMethodsActivityStarter : ActivityStarter<PaymentMethodsActivity, Ar
private var paymentConfiguration: PaymentConfiguration? = null
@LayoutRes
private var addPaymentMethodFooterLayoutId: Int = 0
private var windowFlags: Int? = null

/**
* @param billingAddressFields the billing address fields to require on [AddPaymentMethodActivity]
Expand Down Expand Up @@ -113,6 +115,15 @@ class PaymentMethodsActivityStarter : ActivityStarter<PaymentMethodsActivity, Ar
this.addPaymentMethodFooterLayoutId = addPaymentMethodFooterLayoutId
}

/**
* @param windowFlags optional flags to set on the `Window` object of Stripe Activities
*
* See [WindowManager.LayoutParams](https://developer.android.com/reference/android/view/WindowManager.LayoutParams)
*/
fun setWindowFlags(windowFlags: Int?): Builder = apply {
this.windowFlags = windowFlags
}

override fun build(): Args {
return Args(
initialPaymentMethodId = initialPaymentMethodId,
Expand All @@ -121,6 +132,7 @@ class PaymentMethodsActivityStarter : ActivityStarter<PaymentMethodsActivity, Ar
paymentMethodTypes = paymentMethodTypes ?: listOf(PaymentMethod.Type.Card),
paymentConfiguration = paymentConfiguration,
addPaymentMethodFooterLayoutId = addPaymentMethodFooterLayoutId,
windowFlags = windowFlags,
billingAddressFields = billingAddressFields
)
}
Expand Down

0 comments on commit fa289b1

Please sign in to comment.