-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMainActivity.kt
More file actions
103 lines (87 loc) · 3.74 KB
/
MainActivity.kt
File metadata and controls
103 lines (87 loc) · 3.74 KB
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
package com.lazygeniouz.filecompat.example
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.os.storage.StorageManager
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.lazygeniouz.filecompat.example.performance.Performance
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainActivity : AppCompatActivity(R.layout.activity_main) {
private lateinit var buttonDir: Button
private lateinit var buttonFile: Button
private lateinit var textView: TextView
private lateinit var progress: ProgressBar
private val folderResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val documentUri = result.data?.data
if (documentUri != null) {
textView.text = ""
lifecycleScope.launch {
progress.isVisible = true
buttonDir.isVisible = false
buttonFile.isVisible = false
val performanceResult = withContext(Dispatchers.IO) {
Performance.calculateDirectoryPerformance(
this@MainActivity, documentUri
)
}
progress.isVisible = false
buttonDir.isVisible = true
buttonFile.isVisible = true
textView.text = performanceResult
}
}
}
}
@SuppressLint("SetTextI18n")
private val fileResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val documentUri = result.data?.data
if (documentUri != null) {
textView.text = ""
lifecycleScope.launch {
val performance = withContext(Dispatchers.IO) {
Performance.calculateFilesPerformance(
this@MainActivity, documentUri
)
}
textView.text = performance
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
buttonDir = findViewById(R.id.buttonDir)
buttonFile = findViewById(R.id.buttonFile)
textView = findViewById(R.id.fileNames)
progress = findViewById(R.id.progress)
onBackPressedDispatcher.addCallback(this) { finishAffinity() }
buttonDir.setOnClickListener {
folderResultLauncher.launch(getStorageIntent())
}
buttonFile.setOnClickListener {
fileResultLauncher.launch(getStorageIntent(true))
}
}
private fun getStorageIntent(single: Boolean = false): Intent {
return if (single) Intent(Intent.ACTION_GET_CONTENT).setType("*/*") else {
if (SDK_INT >= 30) {
val storageManager = getSystemService(STORAGE_SERVICE) as StorageManager
storageManager.primaryStorageVolume.createOpenDocumentTreeIntent()
} else Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
}
}
}