File tree Expand file tree Collapse file tree 4 files changed +69
-40
lines changed
common/src/main/java/me/li2/android/common Expand file tree Collapse file tree 4 files changed +69
-40
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package me.li2.android.common.sdcard
2
+
3
+ import android.content.Intent
4
+
5
+ enum class SdcardState (val action : String? ) {
6
+ UNKNOWN (null ),
7
+ MOUNTED (Intent .ACTION_MEDIA_MOUNTED ),
8
+ EJECT (Intent .ACTION_MEDIA_EJECT ),
9
+ UNMOUNTED (Intent .ACTION_MEDIA_UNMOUNTED ),
10
+ BAD_REMOVAL (Intent .ACTION_MEDIA_BAD_REMOVAL );
11
+
12
+ fun isEject () = this == EJECT
13
+
14
+ companion object {
15
+ private val map = enumValues<SdcardState >().associateBy(SdcardState ::action)
16
+
17
+ val intentActions: List <String > = enumValues<SdcardState >().mapNotNull { it.action }
18
+
19
+ fun from (action : String? ): SdcardState = map[action] ? : UNKNOWN
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @created: 01 May 2021.
3
+ * @author: Weiyi Li
4
+ */
5
+ package me.li2.android.common.sdcard
6
+
7
+ import android.content.Context
8
+ import android.content.IntentFilter
9
+ import io.reactivex.rxjava3.core.Observable
10
+ import me.li2.android.common.framework.onBroadcast
11
+
12
+ /* *
13
+ * Create an RxJava [Observable] which emits the [SdcardState] when sdcard state changes.
14
+ */
15
+ fun Context.sdcardStateChanges (): Observable <SdcardState > {
16
+ val intentFilter = IntentFilter ()
17
+ .apply {
18
+ SdcardState .intentActions.forEach { action -> addAction(action) }
19
+ addDataScheme(" file" )
20
+ }
21
+ return onBroadcast(intentFilter).map { intent -> SdcardState .from(intent.action) }
22
+ }
Original file line number Diff line number Diff line change
1
+ package me.li2.android.common.sdcard
2
+
3
+ import android.content.Context
4
+ import android.content.IntentFilter
5
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
6
+ import kotlinx.coroutines.flow.Flow
7
+ import kotlinx.coroutines.flow.map
8
+ import me.li2.android.common.framework.broadcastFlow
9
+
10
+ /*
11
+ * Created by Weiyi Li on 12/05/21.
12
+ * https://github.com/li2
13
+ */
14
+
15
+ /* *
16
+ * Create a coroutine [Flow] which emits the [SdcardState] when sdcard state changes.
17
+ */
18
+ @ExperimentalCoroutinesApi
19
+ fun Context.sdcardStateChangesFlow (): Flow <SdcardState > {
20
+ val intentFilter = IntentFilter ()
21
+ .apply {
22
+ SdcardState .intentActions.forEach { action -> addAction(action) }
23
+ addDataScheme(" file" )
24
+ }
25
+ return broadcastFlow(intentFilter).map { intent -> SdcardState .from(intent.action) }
26
+ }
You can’t perform that action at this time.
0 commit comments