• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.launcher3.util
18 
19 import android.content.Context
20 import com.android.launcher3.LauncherApplication
21 
22 /** Abstract Context which allows custom implementations for dagger components. */
23 open class SandboxContext(base: Context?) : LauncherApplication() {
24     init {
<lambda>null25         base?.let { attachBaseContext(it) }
26     }
27 
getApplicationContextnull28     override fun getApplicationContext(): Context {
29         return this
30     }
31 
32     /**
33      * Returns whether this sandbox should cleanup all objects when its destroyed or leave it to the
34      * GC. These objects can have listeners attached to the system server and mey not be able to get
35      * GCed themselves when running on a device. Some environments like Robolectric tear down the
36      * whole system at the end of the test, so manual cleanup may not be required.
37      */
shouldCleanUpOnDestroynull38     open fun shouldCleanUpOnDestroy(): Boolean {
39         return (getBaseContext().getApplicationContext() as? SandboxContext)
40             ?.shouldCleanUpOnDestroy() ?: true
41     }
42 
onDestroynull43     fun onDestroy() {
44         if (shouldCleanUpOnDestroy()) {
45             cleanUpObjects()
46         }
47     }
48 
cleanUpObjectsnull49     open protected fun cleanUpObjects() {
50         appComponent.daggerSingletonTracker.close()
51     }
52 
53     companion object {
54         private const val TAG = "SandboxContext"
55     }
56 }
57