/
LeakActivity.kt
191 lines (167 loc) · 5.87 KB
/
LeakActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package leakcanary.internal.activity
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.view.View
import com.squareup.leakcanary.core.R
import leakcanary.internal.HeapAnalyzerService
import leakcanary.internal.InternalLeakCanary
import leakcanary.internal.activity.db.Db
import leakcanary.internal.activity.screen.AboutScreen
import leakcanary.internal.activity.screen.HeapDumpsScreen
import leakcanary.internal.activity.screen.LeaksScreen
import leakcanary.internal.navigation.NavigatingActivity
import leakcanary.internal.navigation.Screen
import shark.SharkLog
import java.io.FileInputStream
import java.io.IOException
internal class LeakActivity : NavigatingActivity() {
private val leaksButton by lazy {
findViewById<View>(R.id.leak_canary_navigation_button_leaks)
}
private val leaksButtonIconView by lazy {
findViewById<View>(R.id.leak_canary_navigation_button_leaks_icon)
}
private val heapDumpsButton by lazy {
findViewById<View>(R.id.leak_canary_navigation_button_heap_dumps)
}
private val heapDumpsButtonIconView by lazy {
findViewById<View>(R.id.leak_canary_navigation_button_heap_dumps_icon)
}
private val aboutButton by lazy {
findViewById<View>(R.id.leak_canary_navigation_button_about)
}
private val aboutButtonIconView by lazy {
findViewById<View>(R.id.leak_canary_navigation_button_about_icon)
}
private val bottomNavigationBar by lazy {
findViewById<View>(R.id.leak_canary_bottom_navigation_bar)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.leak_canary_leak_activity)
installNavigation(savedInstanceState, findViewById(R.id.leak_canary_main_container))
leaksButton.setOnClickListener { resetTo(LeaksScreen()) }
heapDumpsButton.setOnClickListener { resetTo(HeapDumpsScreen()) }
aboutButton.setOnClickListener { resetTo(AboutScreen()) }
}
override fun onNewScreen(screen: Screen) {
when (screen) {
is LeaksScreen -> {
bottomNavigationBar.visibility = View.VISIBLE
leaksButton.isSelected = true
leaksButtonIconView.alpha = 1.0f
heapDumpsButton.isSelected = false
heapDumpsButtonIconView.alpha = 0.4f
aboutButton.isSelected = false
aboutButtonIconView.alpha = 0.4f
}
is HeapDumpsScreen -> {
bottomNavigationBar.visibility = View.VISIBLE
leaksButton.isSelected = false
leaksButtonIconView.alpha = 0.4f
heapDumpsButton.isSelected = true
heapDumpsButtonIconView.alpha = 1.0f
aboutButton.isSelected = false
aboutButtonIconView.alpha = 0.4f
}
is AboutScreen -> {
bottomNavigationBar.visibility = View.VISIBLE
leaksButton.isSelected = false
leaksButtonIconView.alpha = 0.4f
heapDumpsButton.isSelected = false
heapDumpsButtonIconView.alpha = 0.4f
aboutButton.isSelected = true
aboutButtonIconView.alpha = 1.0f
}
else -> {
bottomNavigationBar.visibility = View.GONE
}
}
}
override fun getLauncherScreen(): Screen {
return LeaksScreen()
}
fun requestImportHprof() {
val requestFileIntent = Intent(Intent.ACTION_GET_CONTENT).apply {
type = "*/*"
addCategory(Intent.CATEGORY_OPENABLE)
}
val chooserIntent = Intent.createChooser(
requestFileIntent, resources.getString(R.string.leak_canary_import_from_title)
)
startActivityForResult(chooserIntent, FILE_REQUEST_CODE)
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
returnIntent: Intent?
) {
SharkLog.d {
"Got activity result with requestCode=$requestCode resultCode=$resultCode returnIntent=$returnIntent"
}
if (requestCode == FILE_REQUEST_CODE && resultCode == RESULT_OK && returnIntent != null) {
returnIntent.data?.let { fileUri ->
AsyncTask.THREAD_POOL_EXECUTOR.execute {
importHprof(fileUri)
}
}
}
}
private fun importHprof(fileUri: Uri) {
try {
contentResolver.openFileDescriptor(fileUri, "r")
?.fileDescriptor?.let { fileDescriptor ->
val inputStream = FileInputStream(fileDescriptor)
InternalLeakCanary.leakDirectoryProvider
.newHeapDumpFile()
?.let { target ->
inputStream.use { input ->
target.outputStream()
.use { output ->
input.copyTo(output, DEFAULT_BUFFER_SIZE)
}
}
HeapAnalyzerService.runAnalysis(this, target)
}
}
} catch (e: IOException) {
SharkLog.d(e) { "Could not imported Hprof file" }
}
}
override fun onDestroy() {
super.onDestroy()
if (!isChangingConfigurations) {
Db.closeDatabase()
}
}
override fun setTheme(resid: Int) {
// We don't want this to be called with an incompatible theme.
// This could happen if you implement runtime switching of themes
// using ActivityLifecycleCallbacks.
if (resid != R.style.leak_canary_LeakCanary_Base) {
return
}
super.setTheme(resid)
}
companion object {
private const val FILE_REQUEST_CODE = 0
fun createPendingIntent(
context: Context,
screens: ArrayList<Screen>
): PendingIntent {
val intent = Intent(context, LeakActivity::class.java)
intent.putExtra("screens", screens)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
return PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun createIntent(context: Context): Intent {
val intent = Intent(context, LeakActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
return intent
}
}
}