<lambda>null1 package leakcanary
2 
3 import android.app.Application
4 import leakcanary.internal.RealHeapAnalysisJob
5 import leakcanary.internal.RealHeapAnalysisJob.Companion.HPROF_PREFIX
6 import leakcanary.internal.RealHeapAnalysisJob.Companion.HPROF_SUFFIX
7 import java.io.File
8 
9 class HeapAnalysisClient(
10   /**
11    * Provides the directory where heap dumps should be stored.
12    * The passed in directory SHOULD be part of the app internal storage, and private to the
13    * app, to guarantee no other app can read the heap dumps.
14    *
15    * You should probably pass in [android.app.Application.getCacheDir] or a sub directory
16    * of the cache directory, as the cache directory will never be backed up and it will be
17    * cleared if the user needs space.
18    *
19    * This will be invoked on the thread used to execute analysis jobs. The main reason for
20    * the delayed invocation is because [android.app.Application.getCacheDir] might perform
21    * IO and trigger StrictMode violations.
22    */
23   private val heapDumpDirectoryProvider: () -> File,
24   private val config: HeapAnalysisConfig,
25   private val interceptors: List<HeapAnalysisInterceptor>
26 ) {
27 
28   fun newJob(context: JobContext = JobContext()): HeapAnalysisJob {
29     return RealHeapAnalysisJob(heapDumpDirectoryProvider, config, interceptors, context)
30   }
31 
32   fun deleteHeapDumpFiles() {
33     val heapDumpFiles = heapDumpDirectoryProvider().listFiles { _, name ->
34       name.startsWith(HPROF_PREFIX) && name.endsWith(HPROF_SUFFIX)
35     }
36     heapDumpFiles?.forEach { it.delete() }
37   }
38 
39   companion object {
40     fun defaultInterceptors(application: Application): List<HeapAnalysisInterceptor> {
41       return listOf(
42         GoodAndroidVersionInterceptor(),
43         MinimumDiskSpaceInterceptor(application),
44         MinimumMemoryInterceptor(application),
45         MinimumElapsedSinceStartInterceptor(),
46         OncePerPeriodInterceptor(application),
47         SaveResourceIdsInterceptor(application.resources)
48       )
49     }
50   }
51 }
52