aboutsummaryrefslogtreecommitdiff
path: root/mediapicker/src/main/kotlin/ca/allanwang/kau/mediapicker/MediaActionItem.kt
blob: 636dcf0b725d42b663acbe60fde078079e045340 (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
/*
 * 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.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import ca.allanwang.kau.iitems.KauIItem
import ca.allanwang.kau.permissions.PERMISSION_READ_EXTERNAL_STORAGE
import ca.allanwang.kau.permissions.PERMISSION_WRITE_EXTERNAL_STORAGE
import ca.allanwang.kau.permissions.kauRequestPermissions
import ca.allanwang.kau.utils.materialDialog
import ca.allanwang.kau.utils.string
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import java.io.File

/**
 * Created by Allan Wang on 2017-08-17.
 */
class MediaActionItem(
    val action: MediaAction,
    val mediaType: MediaType
) : KauIItem<MediaItemBasic.ViewHolder>(
    R.layout.kau_iitem_image_basic,
    { MediaItemBasic.ViewHolder(it) },
    R.id.kau_item_media_action
) {

    override var isSelectable: Boolean
        get() = false
        set(_) {}

    override fun bindView(holder: MediaItemBasic.ViewHolder, payloads: List<Any>) {
        super.bindView(holder, payloads)
        holder.image.apply {
            setImageDrawable(
                MediaPickerCore.getIconDrawable(
                    context,
                    action.iicon(this@MediaActionItem),
                    action.color
                )
            )
            setOnClickListener { action(context, this@MediaActionItem) }
        }
    }

    override fun unbindView(holder: MediaItemBasic.ViewHolder) {
        super.unbindView(holder)
        holder.image.apply {
            setImageDrawable(null)
            setOnClickListener(null)
        }
    }
}

interface MediaAction {
    var color: Int
    fun iicon(item: MediaActionItem): IIcon
    operator fun invoke(c: Context, item: MediaActionItem)
}

internal const val MEDIA_ACTION_REQUEST_CAMERA = 100
internal const val MEDIA_ACTION_REQUEST_PICKER = 101

/**
 * Dynamic camera items for both images and videos
 * Given that images require a uri to save the file, they must be implemented on top
 * of this abstract class.
 *
 * If you just wish to use videos, see [MediaActionCameraVideo]
 */
abstract class MediaActionCamera(
    override var color: Int = MediaPickerCore.accentColor
) : MediaAction {

    abstract fun createFile(context: Context): File
    abstract fun createUri(context: Context, file: File): Uri

    override fun iicon(item: MediaActionItem) = when (item.mediaType) {
        MediaType.IMAGE -> GoogleMaterial.Icon.gmd_photo_camera
        MediaType.VIDEO -> GoogleMaterial.Icon.gmd_videocam
    }

    override operator fun invoke(c: Context, item: MediaActionItem) {
        c.kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ ->
            if (granted) {
                val intent = Intent(item.mediaType.captureType)
                if (intent.resolveActivity(c.packageManager) == null) {
                    c.materialDialog {
                        title(R.string.kau_no_camera_found)
                        message(R.string.kau_no_camera_found_content)
                    }
                    return@kauRequestPermissions
                }
                if (item.mediaType == MediaType.IMAGE) {
                    val file: File = try {
                        createFile(c)
                    } catch (e: java.io.IOException) {
                        c.materialDialog {
                            title(R.string.kau_error)
                            message(R.string.kau_temp_file_creation_failed)
                        }
                        return@kauRequestPermissions
                    }
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, createUri(c, file))
                    (c as? MediaPickerCore<*>)?.tempPath = file.absolutePath
                }
                (c as Activity).startActivityForResult(intent, MEDIA_ACTION_REQUEST_CAMERA)
            }
        }
    }
}

/**
 * Basic camera action just for videos
 */
class MediaActionCameraVideo(
    override var color: Int = MediaPickerCore.accentColor
) : MediaAction {
    override fun iicon(item: MediaActionItem) = GoogleMaterial.Icon.gmd_videocam
    override operator fun invoke(c: Context, item: MediaActionItem) {
        val intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
        if (intent.resolveActivity(c.packageManager) == null) {
            c.materialDialog {
                title(R.string.kau_no_camera_found)
                message(R.string.kau_no_camera_found_content)
            }
            return
        }
        (c as Activity).startActivityForResult(intent, MEDIA_ACTION_REQUEST_CAMERA)
    }
}

/**
 * Opens a picker for the type specified in the activity
 * The type will be added programmatically
 */
class MediaActionGallery(
    val multiple: Boolean = false,
    override var color: Int = MediaPickerCore.accentColor
) : MediaAction {

    override fun iicon(item: MediaActionItem) = when (item.mediaType) {
        MediaType.IMAGE -> if (multiple) GoogleMaterial.Icon.gmd_photo_library else GoogleMaterial.Icon.gmd_photo
        MediaType.VIDEO -> GoogleMaterial.Icon.gmd_video_library
    }

    override operator fun invoke(c: Context, item: MediaActionItem) {
        c.kauRequestPermissions(PERMISSION_READ_EXTERNAL_STORAGE) { granted, _ ->
            if (granted) {
                val intent = Intent().apply {
                    type = item.mediaType.mimeType
                    action = Intent.ACTION_GET_CONTENT
                    putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple)
                }
                (c as Activity).startActivityForResult(
                    Intent.createChooser(intent, c.string(R.string.kau_select_media)),
                    MEDIA_ACTION_REQUEST_PICKER
                )
            }
        }
    }
}