1 /* 2 * Copyright (C) 2023 The Android Open Source Project 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.android.credentialmanager.common 18 19 import android.app.ActivityOptions 20 import android.content.Context 21 import android.content.Intent 22 import androidx.activity.result.ActivityResult 23 import androidx.activity.result.IntentSenderRequest 24 import androidx.activity.result.contract.ActivityResultContract 25 import androidx.activity.result.contract.ActivityResultContracts 26 27 /** 28 * A custom StartIntentSenderForResult contract implementation that attaches an [ActivityOptions] 29 * that opts in for background activity launch. 30 */ 31 class StartBalIntentSenderForResultContract : 32 ActivityResultContract<IntentSenderRequest, ActivityResult>() { 33 override fun createIntent(context: Context, input: IntentSenderRequest): Intent { 34 val activityOptionBundle = 35 ActivityOptions.makeBasic().setPendingIntentBackgroundActivityStartMode( 36 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED 37 ).toBundle() 38 return Intent( 39 ActivityResultContracts.StartIntentSenderForResult.ACTION_INTENT_SENDER_REQUEST 40 ).putExtra( 41 ActivityResultContracts.StartActivityForResult.EXTRA_ACTIVITY_OPTIONS_BUNDLE, 42 activityOptionBundle 43 ).putExtra( 44 ActivityResultContracts.StartIntentSenderForResult.EXTRA_INTENT_SENDER_REQUEST, 45 input 46 ) 47 } 48 49 override fun parseResult( 50 resultCode: Int, 51 intent: Intent? 52 ): ActivityResult = ActivityResult(resultCode, intent) 53 }