• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2024 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  *      https://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.intentresolver.interactive.data.repository
18 
19 import android.os.Bundle
20 import androidx.lifecycle.SavedStateHandle
21 import com.android.intentresolver.IChooserController
22 import com.android.intentresolver.interactive.domain.model.ChooserIntentUpdater
23 import dagger.hilt.android.scopes.ViewModelScoped
24 import java.util.concurrent.atomic.AtomicReference
25 import javax.inject.Inject
26 
27 private const val INTERACTIVE_SESSION_CALLBACK_KEY = "interactive-session-callback"
28 
29 @ViewModelScoped
30 class InteractiveSessionCallbackRepository @Inject constructor(savedStateHandle: SavedStateHandle) {
31     private val intentUpdaterRef =
32         AtomicReference<ChooserIntentUpdater?>(
33             savedStateHandle
34                 .get<Bundle>(INTERACTIVE_SESSION_CALLBACK_KEY)
35                 ?.let { it.getBinder(INTERACTIVE_SESSION_CALLBACK_KEY) }
36                 ?.let { binder ->
37                     binder.queryLocalInterface(IChooserController.DESCRIPTOR)
38                         as? ChooserIntentUpdater
39                 }
40         )
41 
42     val intentUpdater: ChooserIntentUpdater?
43         get() = intentUpdaterRef.get()
44 
45     init {
46         savedStateHandle.setSavedStateProvider(INTERACTIVE_SESSION_CALLBACK_KEY) {
47             Bundle().apply { putBinder(INTERACTIVE_SESSION_CALLBACK_KEY, intentUpdater) }
48         }
49     }
50 
51     fun setChooserIntentUpdater(intentUpdater: ChooserIntentUpdater) {
52         intentUpdaterRef.compareAndSet(null, intentUpdater)
53     }
54 }
55