1
1
package com.ismartcoding.plain.features.media
2
2
3
+ import android.content.ContentValues
3
4
import android.content.Context
4
5
import android.database.Cursor
5
6
import android.net.Uri
7
+ import android.os.Build
8
+ import android.os.Environment
6
9
import android.provider.BaseColumns
7
10
import android.provider.MediaStore
11
+ import androidx.annotation.RequiresApi
8
12
import com.ismartcoding.lib.content.ContentWhere
9
13
import com.ismartcoding.lib.data.SortBy
10
14
import com.ismartcoding.lib.extensions.count
@@ -102,6 +106,17 @@ abstract class BaseMediaContentHelper {
102
106
}?.toSet() ? : emptySet()
103
107
}
104
108
109
+ suspend fun getTrashedIdsAsync (
110
+ context : Context ,
111
+ query : String ,
112
+ ): Set <String > {
113
+ return context.contentResolver.getSearchCursor(
114
+ uriExternal, arrayOf(BaseColumns ._ID ), buildWhere(query).apply { trash = true }
115
+ )?.map { cursor, cache ->
116
+ cursor.getStringValue(BaseColumns ._ID , cache)
117
+ }?.toSet() ? : emptySet()
118
+ }
119
+
105
120
protected suspend fun getSearchCursorAsync (
106
121
context : Context ,
107
122
query : String ,
@@ -114,12 +129,14 @@ abstract class BaseMediaContentHelper {
114
129
fun deleteRecordsAndFilesByIdsAsync (
115
130
context : Context ,
116
131
ids : Set <String >,
132
+ trash : Boolean? = null,
117
133
): Set <String > {
118
134
val paths = mutableSetOf<String >()
119
135
val projection = arrayOf(BaseColumns ._ID , MediaStore .MediaColumns .DATA )
120
136
ids.chunked(500 ).forEach { chunk ->
121
137
val where = ContentWhere ()
122
138
where.addIn(BaseColumns ._ID , chunk)
139
+ where.trash = trash
123
140
context.contentResolver.getSearchCursor(uriExternal, projection, where)?.forEach { cursor, cache ->
124
141
val id = cursor.getStringValue(BaseColumns ._ID , cache)
125
142
val path = cursor.getStringValue(MediaStore .MediaColumns .DATA , cache)
@@ -143,6 +160,52 @@ abstract class BaseMediaContentHelper {
143
160
return paths
144
161
}
145
162
163
+ fun getPathsByIdsAsync (
164
+ context : Context ,
165
+ ids : Set <String >,
166
+ ): Set <String > {
167
+ val paths = mutableSetOf<String >()
168
+ val projection = arrayOf(BaseColumns ._ID , MediaStore .MediaColumns .DATA )
169
+ ids.chunked(500 ).forEach { chunk ->
170
+ val where = ContentWhere ()
171
+ where.addIn(BaseColumns ._ID , chunk)
172
+ context.contentResolver.getSearchCursor(uriExternal, projection, where)?.forEach { cursor, cache ->
173
+ val path = cursor.getStringValue(MediaStore .MediaColumns .DATA , cache)
174
+ paths.add(path)
175
+ }
176
+ }
177
+
178
+ return paths
179
+ }
180
+
181
+ @RequiresApi(Build .VERSION_CODES .R )
182
+ fun trashByIdsAsync (
183
+ context : Context ,
184
+ ids : Set <String >,
185
+ ) {
186
+ val contentValues = ContentValues ().apply {
187
+ // put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Trash")
188
+ put(MediaStore .MediaColumns .IS_TRASHED , 1 )
189
+ }
190
+ ids.forEach { id ->
191
+ context.contentResolver.update(getItemUri(id), contentValues, null , null )
192
+ }
193
+ }
194
+
195
+ @RequiresApi(Build .VERSION_CODES .R )
196
+ fun restoreByIdsAsync (
197
+ context : Context ,
198
+ ids : Set <String >,
199
+ ) {
200
+ val contentValues = ContentValues ().apply {
201
+ // put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Trash")
202
+ put(MediaStore .MediaColumns .IS_TRASHED , 0 )
203
+ }
204
+ ids.forEach { id ->
205
+ context.contentResolver.update(getItemUri(id), contentValues, null , null )
206
+ }
207
+ }
208
+
146
209
fun getBucketsAsync (context : Context ): List <DMediaBucket > {
147
210
val bucketMap = mutableMapOf<String , DMediaBucket >()
148
211
0 commit comments