Skip to content

Commit 45c5dd1

Browse files
authored
Extract result retriever and add pick directory intent (#99)
Signed-off-by: Kyle Corry <kylecorry31@gmail.com>
1 parent da72204 commit 45c5dd1

File tree

6 files changed

+140
-202
lines changed

6 files changed

+140
-202
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.kylecorry.andromeda.core.system
2+
3+
import android.content.Intent
4+
5+
interface IntentResultRetriever {
6+
fun getResult(intent: Intent, action: (successful: Boolean, data: Intent?) -> Unit)
7+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.kylecorry.andromeda.core.system
2+
3+
import android.net.Uri
4+
5+
fun IntentResultRetriever.createFile(
6+
filename: String,
7+
type: String,
8+
message: String = filename,
9+
action: (uri: Uri?) -> Unit
10+
) {
11+
val intent = Intents.createFile(filename, type, message)
12+
getResult(intent) { successful, data ->
13+
if (successful) {
14+
action(data?.data)
15+
} else {
16+
action(null)
17+
}
18+
}
19+
}
20+
21+
fun IntentResultRetriever.createFile(
22+
filename: String,
23+
types: List<String>,
24+
message: String = filename,
25+
action: (uri: Uri?) -> Unit
26+
) {
27+
val intent = Intents.createFile(filename, types, message)
28+
getResult(intent) { successful, data ->
29+
if (successful) {
30+
action(data?.data)
31+
} else {
32+
action(null)
33+
}
34+
}
35+
}
36+
37+
fun IntentResultRetriever.pickFile(
38+
type: String,
39+
message: String,
40+
useSAF: Boolean = true,
41+
action: (uri: Uri?) -> Unit
42+
) {
43+
val intent = Intents.pickFile(type, message, useSAF)
44+
getResult(intent) { successful, data ->
45+
if (successful) {
46+
action(data?.data)
47+
} else {
48+
action(null)
49+
}
50+
}
51+
}
52+
53+
fun IntentResultRetriever.pickFile(
54+
types: List<String>,
55+
message: String,
56+
useSAF: Boolean = true,
57+
action: (uri: Uri?) -> Unit
58+
) {
59+
val intent = Intents.pickFile(types, message, useSAF)
60+
getResult(intent) { successful, data ->
61+
if (successful) {
62+
action(data?.data)
63+
} else {
64+
action(null)
65+
}
66+
}
67+
}
68+
69+
fun IntentResultRetriever.pickDirectory(
70+
message: String,
71+
requirePersistentAccess: Boolean = false,
72+
requireReadAccess: Boolean = true,
73+
requireWriteAccess: Boolean = false,
74+
action: (uri: Uri?) -> Unit
75+
) {
76+
val intent = Intents.pickDirectory(
77+
message,
78+
requirePersistentAccess,
79+
requireReadAccess,
80+
requireWriteAccess
81+
)
82+
getResult(intent) { successful, data ->
83+
if (successful) {
84+
action(data?.data)
85+
} else {
86+
action(null)
87+
}
88+
}
89+
}

core/src/main/java/com/kylecorry/andromeda/core/system/Intents.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,40 @@ object Intents {
143143
return Intent.createChooser(requestFileIntent, message)
144144
}
145145

146+
fun pickDirectory(
147+
message: String,
148+
requirePersistentAccess: Boolean = false,
149+
requireReadAccess: Boolean = true,
150+
requireWriteAccess: Boolean = false
151+
): Intent {
152+
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
153+
if (requirePersistentAccess) {
154+
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
155+
}
156+
157+
if (requireReadAccess) {
158+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
159+
}
160+
161+
if (requireWriteAccess) {
162+
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
163+
}
164+
165+
return Intent.createChooser(intent, message)
166+
}
167+
168+
fun acceptPersistentUri(
169+
context: Context,
170+
uri: Uri,
171+
takeRead: Boolean = true,
172+
takeWrite: Boolean = true
173+
) {
174+
context.contentResolver.takePersistableUriPermission(
175+
uri,
176+
(if (takeRead) Intent.FLAG_GRANT_READ_URI_PERMISSION else 0) or (if (takeWrite) Intent.FLAG_GRANT_WRITE_URI_PERMISSION else 0)
177+
)
178+
}
179+
146180
fun openChooser(context: Context, intent: Intent, title: String) {
147181
context.startActivity(Intent.createChooser(intent, title))
148182
}

fragments/src/main/java/com/kylecorry/andromeda/fragments/AndromedaActivity.kt

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package com.kylecorry.andromeda.fragments
22

33
import android.content.Intent
4-
import android.net.Uri
54
import android.os.Bundle
65
import android.os.PersistableBundle
76
import android.view.KeyEvent
87
import androidx.appcompat.app.AppCompatActivity
98
import androidx.appcompat.app.AppCompatDelegate
109
import androidx.core.app.ActivityCompat
1110
import androidx.fragment.app.Fragment
12-
import com.kylecorry.andromeda.core.system.Intents
11+
import com.kylecorry.andromeda.core.system.IntentResultRetriever
1312
import com.kylecorry.andromeda.permissions.PermissionRationale
1413
import com.kylecorry.andromeda.permissions.Permissions
1514
import com.kylecorry.andromeda.permissions.SpecialPermission
1615
import com.kylecorry.luna.hooks.Hooks
1716
import kotlin.math.absoluteValue
1817
import kotlin.random.Random
1918

20-
open class AndromedaActivity : AppCompatActivity(), IPermissionRequester {
19+
open class AndromedaActivity : AppCompatActivity(), IPermissionRequester, IntentResultRetriever {
2120

2221
private var resultAction: ((successful: Boolean, data: Intent?) -> Unit)? = null
2322
private var permissionAction: (() -> Unit)? = null
@@ -68,77 +67,13 @@ open class AndromedaActivity : AppCompatActivity(), IPermissionRequester {
6867
specialPermissionLauncher.requestPermission(permission, rationale, action)
6968
}
7069

71-
fun getResult(intent: Intent, action: (successful: Boolean, data: Intent?) -> Unit) {
70+
override fun getResult(intent: Intent, action: (successful: Boolean, data: Intent?) -> Unit) {
7271
val requestCode = Random.nextInt().absoluteValue
7372
resultRequestCode = requestCode
7473
resultAction = action
7574
startActivityForResult(intent, requestCode)
7675
}
7776

78-
fun createFile(
79-
filename: String,
80-
type: String,
81-
message: String = filename,
82-
action: (uri: Uri?) -> Unit
83-
) {
84-
val intent = Intents.createFile(filename, type, message)
85-
getResult(intent) { successful, data ->
86-
if (successful) {
87-
action(data?.data)
88-
} else {
89-
action(null)
90-
}
91-
}
92-
}
93-
94-
fun createFile(
95-
filename: String,
96-
types: List<String>,
97-
message: String = filename,
98-
action: (uri: Uri?) -> Unit
99-
) {
100-
val intent = Intents.createFile(filename, types, message)
101-
getResult(intent) { successful, data ->
102-
if (successful) {
103-
action(data?.data)
104-
} else {
105-
action(null)
106-
}
107-
}
108-
}
109-
110-
fun pickFile(
111-
type: String,
112-
message: String,
113-
useSAF: Boolean = true,
114-
action: (uri: Uri?) -> Unit
115-
) {
116-
val intent = Intents.pickFile(type, message, useSAF)
117-
getResult(intent) { successful, data ->
118-
if (successful) {
119-
action(data?.data)
120-
} else {
121-
action(null)
122-
}
123-
}
124-
}
125-
126-
fun pickFile(
127-
types: List<String>,
128-
message: String,
129-
useSAF: Boolean = true,
130-
action: (uri: Uri?) -> Unit
131-
) {
132-
val intent = Intents.pickFile(types, message, useSAF)
133-
getResult(intent) { successful, data ->
134-
if (successful) {
135-
action(data?.data)
136-
} else {
137-
action(null)
138-
}
139-
}
140-
}
141-
14277
fun onVolumeButtonChange(action: ((button: VolumeButton, isPressed: Boolean) -> Boolean)?) {
14378
volumeAction = action ?: { _, _ -> false }
14479
}

fragments/src/main/java/com/kylecorry/andromeda/fragments/AndromedaFragment.kt

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package com.kylecorry.andromeda.fragments
22

33
import android.app.Activity
44
import android.content.Intent
5-
import android.net.Uri
65
import android.os.Bundle
76
import androidx.activity.result.ActivityResultLauncher
87
import androidx.activity.result.contract.ActivityResultContracts
98
import androidx.fragment.app.Fragment
109
import androidx.lifecycle.LifecycleEventObserver
1110
import androidx.lifecycle.lifecycleScope
12-
import com.kylecorry.andromeda.core.system.Intents
11+
import com.kylecorry.andromeda.core.system.IntentResultRetriever
1312
import com.kylecorry.andromeda.core.time.Throttle
1413
import com.kylecorry.andromeda.permissions.PermissionRationale
1514
import com.kylecorry.andromeda.permissions.Permissions
@@ -24,7 +23,7 @@ import java.time.Duration
2423
import kotlin.coroutines.CoroutineContext
2524
import kotlin.coroutines.EmptyCoroutineContext
2625

27-
open class AndromedaFragment : Fragment(), IPermissionRequester {
26+
open class AndromedaFragment : Fragment(), IPermissionRequester, IntentResultRetriever {
2827

2928
protected var hasUpdates: Boolean = true
3029

@@ -149,75 +148,11 @@ open class AndromedaFragment : Fragment(), IPermissionRequester {
149148
specialPermissionLauncher.requestPermission(permission, rationale, action)
150149
}
151150

152-
fun getResult(intent: Intent, action: (successful: Boolean, data: Intent?) -> Unit) {
151+
override fun getResult(intent: Intent, action: (successful: Boolean, data: Intent?) -> Unit) {
153152
resultAction = action
154153
resultLauncher?.launch(intent)
155154
}
156155

157-
fun createFile(
158-
filename: String,
159-
type: String,
160-
message: String = filename,
161-
action: (uri: Uri?) -> Unit
162-
) {
163-
val intent = Intents.createFile(filename, type, message)
164-
getResult(intent) { successful, data ->
165-
if (successful) {
166-
action(data?.data)
167-
} else {
168-
action(null)
169-
}
170-
}
171-
}
172-
173-
fun createFile(
174-
filename: String,
175-
types: List<String>,
176-
message: String = filename,
177-
action: (uri: Uri?) -> Unit
178-
) {
179-
val intent = Intents.createFile(filename, types, message)
180-
getResult(intent) { successful, data ->
181-
if (successful) {
182-
action(data?.data)
183-
} else {
184-
action(null)
185-
}
186-
}
187-
}
188-
189-
fun pickFile(
190-
type: String,
191-
message: String,
192-
useSAF: Boolean = true,
193-
action: (uri: Uri?) -> Unit
194-
) {
195-
val intent = Intents.pickFile(type, message, useSAF)
196-
getResult(intent) { successful, data ->
197-
if (successful) {
198-
action(data?.data)
199-
} else {
200-
action(null)
201-
}
202-
}
203-
}
204-
205-
fun pickFile(
206-
types: List<String>,
207-
message: String,
208-
useSAF: Boolean = true,
209-
action: (uri: Uri?) -> Unit
210-
) {
211-
val intent = Intents.pickFile(types, message, useSAF)
212-
getResult(intent) { successful, data ->
213-
if (successful) {
214-
action(data?.data)
215-
} else {
216-
action(null)
217-
}
218-
}
219-
}
220-
221156
protected fun effect(key: String, vararg values: Any?, action: () -> Unit) {
222157
hooks.effect(key, *values, action = action)
223158
}

0 commit comments

Comments
 (0)