-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMainActivity.kt
130 lines (104 loc) · 4.42 KB
/
MainActivity.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
package name.lmj001.saveondevice
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.documentfile.provider.DocumentFile
import java.io.FileOutputStream
lateinit var saveFileResultLauncher: ActivityResultLauncher<Intent>
lateinit var inputUri: Uri
lateinit var inputUriList: MutableList<Uri>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
saveFileResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
result.data?.data?.let{
when (intent?.action) {
Intent.ACTION_SEND -> {
saveFile(it)
finish()
}
Intent.ACTION_SEND_MULTIPLE -> {
saveFile(it)
// seeing if there are other files in queue to be saved
val uri = inputUriList.removeFirstOrNull()
if(uri != null) {
inputUri = uri
callSaveFileResultLauncher(inputUri)
} else finish()
}
else -> finish()
}
}
} else finish()
}
when (intent?.action) {
Intent.ACTION_SEND -> {
inputUri = intent.extras?.get(Intent.EXTRA_STREAM) as Uri
callSaveFileResultLauncher(inputUri)
}
Intent.ACTION_SEND_MULTIPLE -> {
inputUriList = (intent.extras?.getParcelableArrayList<Uri>(Intent.EXTRA_STREAM) as ArrayList<Uri>).toMutableList()
val uri = inputUriList.removeFirstOrNull()
if (uri != null) {
inputUri = uri
callSaveFileResultLauncher(inputUri)
}
}
}
}
private fun isSupportedMimeType(uri: Uri): Boolean {
val mimeType = contentResolver.getType(uri)
return !mimeType.isNullOrBlank()
}
private fun callSaveFileResultLauncher(uri: Uri) {
if(isSupportedMimeType(uri)) {
val fromUriFileName = DocumentFile.fromSingleUri(this, uri)!!.name
val saveFileIntent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
// Filter to only show results that can be "opened", such as
// a file (as opposed to a list of contacts or timezones).
addCategory(Intent.CATEGORY_OPENABLE)
// Create a file with the requested MIME type.
type = contentResolver.getType(inputUri)
putExtra(Intent.EXTRA_TITLE, fromUriFileName)
}
saveFileResultLauncher.launch(saveFileIntent)
} else {
Toast.makeText(applicationContext, getString(R.string.unsupported_mimetype), Toast.LENGTH_SHORT)
.show()
}
}
private fun dumpIntent(intent: Intent?) {
if (intent == null) return
val bundle = intent.extras
Log.d("MainActivity", "intent uri: $intent")
if (bundle != null) {
for (key in bundle.keySet()) {
Log.d("MainActivity", key + " : " + if (bundle.get(key) != null) bundle.get(key) else "NULL")
}
}
}
private fun saveFile(outputUri: Uri) {
val inputStream = contentResolver.openInputStream(inputUri)
contentResolver.openFileDescriptor(outputUri, "w")?.use { p ->
val outputStream = FileOutputStream(p.fileDescriptor)
val buffer = ByteArray(4096)
var length: Int
if (inputStream != null) {
length = inputStream.read(buffer)
while (length > 0) {
outputStream.write(buffer, 0, length)
length = inputStream.read(buffer)
}
outputStream.flush() // apparently this does nothing..
}
outputStream.close()
}
}
}