aboutsummaryrefslogtreecommitdiff
path: root/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaPickerCore.kt
blob: 4b0e9d640b58b2f342540f441d09a5afcb5b9eb7 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.mediapicker

import android.Manifest
import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.provider.BaseColumns
import android.provider.DocumentsContract
import android.provider.MediaStore
import androidx.loader.app.LoaderManager
import androidx.loader.content.CursorLoader
import androidx.loader.content.Loader
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import ca.allanwang.kau.adapters.fastAdapter
import ca.allanwang.kau.animators.FadeScaleAnimatorAdd
import ca.allanwang.kau.animators.KauAnimator
import ca.allanwang.kau.internal.KauBaseActivity
import ca.allanwang.kau.kotlin.lazyContext
import ca.allanwang.kau.kotlin.lazyUi
import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.permissions.kauRequestPermissions
import ca.allanwang.kau.utils.dimenPixelSize
import ca.allanwang.kau.utils.toast
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.mikepenz.fastadapter.GenericItem
import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.mikepenz.iconics.dsl.iconicsDrawable
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import kotlinx.coroutines.CancellationException
import java.io.File
import kotlin.math.min

/**
 * Created by Allan Wang on 2017-07-23.
 *
 * Container for the main logic behind the both pickers
 */
abstract class MediaPickerCore<T : GenericItem>(
    val mediaType: MediaType,
    val mediaActions: List<MediaAction>
) : KauBaseActivity(), LoaderManager.LoaderCallbacks<Cursor> {

    companion object {
        val viewSize = lazyContext { computeViewSize(it) }
        /**
         * Given the dimensions of our device and a minimum image size,
         * Computer the optimal column count for our grid layout
         *
         * @return column count
         */
        private fun computeColumnCount(context: Context): Int {
            val minImageSizePx = context.dimenPixelSize(R.dimen.kau_image_minimum_size)
            val screenWidthPx = context.resources.displayMetrics.widthPixels
            return screenWidthPx / minImageSizePx
        }

        /**
         * Compute our resulting image size
         */
        private fun computeViewSize(context: Context): Int {
            val screenWidthPx = context.resources.displayMetrics.widthPixels
            return screenWidthPx / computeColumnCount(context)
        }

        /**
         * Create error tile for a given item
         */
        fun getErrorDrawable(context: Context) =
            getIconDrawable(context, GoogleMaterial.Icon.gmd_error, accentColor)

        fun getIconDrawable(context: Context, iicon: IIcon, color: Int): Drawable {
            val sizePx = computeViewSize(context)
            return context.iconicsDrawable(iicon) {
                size = sizePx(sizePx)
                backgroundColor = colorInt(color)
                padding = sizePx(sizePx / 3)
                this.color = colorInt(Color.WHITE)
            }
        }

        var accentColor: Int = 0xff666666.toInt()

        /**
         * Helper method to retrieve the media from our media picker
         * This is used for both single and multiple photo picks
         */
        fun onMediaPickerResult(resultCode: Int, data: Intent?): List<MediaModel> {
            if (resultCode != Activity.RESULT_OK ||
                data?.hasExtra(MEDIA_PICKER_RESULT) != true
            ) {
                return emptyList()
            }
            return data.getParcelableArrayListExtra(MEDIA_PICKER_RESULT) ?: emptyList()
        }

        /**
         * Number of loaded items we should cache
         * This is arbitrary
         */
        const val CACHE_SIZE = 80
    }

    lateinit var glide: RequestManager
    private var hasPreloaded = false

    val adapter = ItemAdapter<T>()

    /**
     * Further improve preloading by extending the layout space
     */
    val extraSpace: Int by lazyUi { resources.displayMetrics.heightPixels }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        glide = Glide.with(this)
    }

    fun initializeRecycler(recycler: RecyclerView) {
        val adapterHeader = ItemAdapter<MediaActionItem>()
        val fulladapter = fastAdapter<GenericItem>(adapterHeader, adapter)
        adapterHeader.add(mediaActions.map { MediaActionItem(it, mediaType) })
        recycler.apply {
            val manager = object : GridLayoutManager(context, computeColumnCount(context)) {
                override fun getExtraLayoutSpace(state: RecyclerView.State?): Int {
                    return if (mediaType != MediaType.VIDEO) extraSpace
                    else super.getExtraLayoutSpace(state)
                }
            }
            setItemViewCacheSize(CACHE_SIZE)
            layoutManager = manager
            adapter = fulladapter
            setHasFixedSize(true)
            itemAnimator = KauAnimator(FadeScaleAnimatorAdd(0.8f))
        }
    }

    // Sort by descending date
    var sortQuery = MediaStore.MediaColumns.DATE_MODIFIED + " DESC"

    override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
        return CursorLoader(
            this,
            mediaType.contentUri,
            MediaModel.projection,
            null,
            null,
            sortQuery
        )
    }

    /**
     * Request read permissions and load all external items
     * The result will be filtered through {@link #onLoadFinished(Loader, Cursor)}
     * Call this to make sure that we request permissions each time
     * The adapter will be cleared on each successful call
     */
    open fun loadItems() {
        kauRequestPermissions(Manifest.permission.READ_EXTERNAL_STORAGE) { granted, _ ->
            if (granted) {
                LoaderManager.getInstance(this).initLoader(LOADER_ID, null, this)
                onStatusChange(true)
            } else {
                toast(R.string.kau_permission_denied)
                onStatusChange(false)
            }
        }
    }

    override fun onLoadFinished(loader: Loader<Cursor>, data: Cursor?) {
        reset()
        if (data == null || !data.moveToFirst()) {
            toast(R.string.kau_no_items_found)
            onStatusChange(false)
            return
        }
        val models = mutableListOf<MediaModel>()
        do {
            val model = MediaModel(data)
            if (!shouldLoad(model)) continue
            models.add(model)
        } while (data.moveToNext())
        addItems(models.map { converter(it) })
        if (!hasPreloaded && mediaType == MediaType.VIDEO) {
            hasPreloaded = true
            val preloads = models.subList(0, min(models.size, 50)).map {
                glide.load(it.data)
                    .applyMediaOptions(this@MediaPickerCore)
                    .preload()
            }
            job.invokeOnCompletion {
                if (it is CancellationException) {
                    preloads.forEach(glide::clear)
                }
            }
        }
    }

    abstract fun converter(model: MediaModel): T

    override fun onLoaderReset(loader: Loader<Cursor>) = reset()

    /**
     * Called at the end of [onLoadFinished]
     * when the adapter should add the items
     */
    open fun addItems(items: List<T>) {
        adapter.add(items)
    }

    /**
     * Clears the adapter to prepare for a new load
     */
    open fun reset() {
        adapter.clear()
    }

    /**
     * Optional filter to decide which items get displayed
     * Defaults to checking their sizes to filter out
     * very small items such as lurking drawables/icons
     *
     * Returns true if model should be displayed, false otherwise
     */
    open fun shouldLoad(model: MediaModel): Boolean = model.size > 10000L

    open fun onStatusChange(loaded: Boolean) {}

    /**
     * Method used to retrieve uri data for API 19+
     * See <a href="http://hmkcode.com/android-display-selected-image-and-its-real-path/"></a>
     */
    private fun <R> ContentResolver.query(
        baseUri: Uri,
        uris: List<Uri>,
        block: (cursor: Cursor) -> R
    ) {
        val ids = uris.filter {
            val valid = DocumentsContract.isDocumentUri(this@MediaPickerCore, it)
            if (!valid) KL.d { "Non document uri: ${it.encodedPath}" }
            valid
        }.mapNotNull {
            DocumentsContract.getDocumentId(it).split(":").getOrNull(1)
        }.joinToString(prefix = "(", separator = ",", postfix = ")")
        // ? query replacements are done for one arg at a time
        // since we potentially have a list of ids, we'll just format the WHERE clause ourself
        query(baseUri, MediaModel.projection, "${BaseColumns._ID} IN $ids", null, sortQuery)?.use(
            block
        )
    }

    internal var tempPath: String? = null

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode != RESULT_OK) {
            tempPath?.let {
                val f = File(it)
                if (f.exists()) f.delete()
                tempPath = null
            }
            return super.onActivityResult(requestCode, resultCode, data)
        }
        KL.d { "Media result received" }
        when (requestCode) {
            MEDIA_ACTION_REQUEST_CAMERA -> onCameraResult(data)
            MEDIA_ACTION_REQUEST_PICKER -> onPickerResult(data)
            else -> super.onActivityResult(requestCode, resultCode, data)
        }
    }

    private fun onCameraResult(data: Intent?) {
        val f: File
        val tempPath = tempPath
        val dataPath = data?.data?.path
        when {
            tempPath != null -> {
                f = File(tempPath)
                this.tempPath = null
            }
            dataPath != null -> f = File(dataPath)
            else -> {
                KL.d { "Media camera no file found" }
                return
            }
        }
        if (f.exists()) {
            KL.v { "Media camera path found: ${f.absolutePath}" }
            scanMedia(f)
            finish(arrayListOf(MediaModel(f)))
        } else {
            KL.d { "Media camera file not found" }
        }
    }

    private fun onPickerResult(data: Intent?) {
        val items = mutableListOf<Uri>()
        val _data = data?.data
        if (_data != null) {
            KL.v { "Media picker data uri: ${_data.path}" }
            items.add(_data)
        } else if (data != null) {
            val clip = data.clipData
            if (clip != null) {
                items.addAll(
                    (0 until clip.itemCount).map {
                        clip.getItemAt(it).uri.apply {
                            KL.v { "Media picker clip uri $path" }
                        }
                    }
                )
            }
        }
        if (items.isEmpty()) return KL.d { "Media picker empty intent" }
        contentResolver.query(mediaType.contentUri, items) {
            if (it.moveToFirst()) {
                val models = arrayListOf<MediaModel>()
                do {
                    models.add(MediaModel(it))
                } while (it.moveToNext())
                finish(models)
            }
        }
    }
}