aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt
blob: b8a71fa139ce25d451b42845002221825673f534 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.activities

import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.customview.widget.ViewDragHelper
import ca.allanwang.kau.internal.KauBaseActivity
import ca.allanwang.kau.logging.KauLoggerExtension
import ca.allanwang.kau.utils.adjustAlpha
import ca.allanwang.kau.utils.colorToForeground
import ca.allanwang.kau.utils.copyFromInputStream
import ca.allanwang.kau.utils.fadeIn
import ca.allanwang.kau.utils.fadeOut
import ca.allanwang.kau.utils.gone
import ca.allanwang.kau.utils.invisible
import ca.allanwang.kau.utils.isHidden
import ca.allanwang.kau.utils.isVisible
import ca.allanwang.kau.utils.materialDialog
import ca.allanwang.kau.utils.scaleXY
import ca.allanwang.kau.utils.setIcon
import ca.allanwang.kau.utils.tint
import ca.allanwang.kau.utils.toast
import ca.allanwang.kau.utils.withAlpha
import ca.allanwang.kau.utils.withMinAlpha
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.R
import com.pitchedapps.frost.databinding.ActivityImageBinding
import com.pitchedapps.frost.facebook.FB_IMAGE_ID_MATCHER
import com.pitchedapps.frost.facebook.get
import com.pitchedapps.frost.facebook.requests.call
import com.pitchedapps.frost.facebook.requests.getFullSizedImageUrl
import com.pitchedapps.frost.facebook.requests.requestBuilder
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.services.LocalService
import com.pitchedapps.frost.utils.ARG_COOKIE
import com.pitchedapps.frost.utils.ARG_IMAGE_URL
import com.pitchedapps.frost.utils.ARG_TEXT
import com.pitchedapps.frost.utils.ActivityThemer
import com.pitchedapps.frost.utils.frostDownload
import com.pitchedapps.frost.utils.frostSnackbar
import com.pitchedapps.frost.utils.frostUriFromFile
import com.pitchedapps.frost.utils.isIndirectImageUrl
import com.pitchedapps.frost.utils.logFrostEvent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import javax.inject.Inject
import kotlin.math.abs
import kotlin.math.max

/**
 * Created by Allan Wang on 2017-07-15.
 */
@AndroidEntryPoint
class ImageActivity : KauBaseActivity() {

    @Inject
    lateinit var activityThemer: ActivityThemer

    @Inject
    lateinit var prefs: Prefs

    @Inject
    lateinit var themeProvider: ThemeProvider

    @Volatile
    internal var errorRef: Throwable? = null

    /**
     * Reference to the temporary file path
     */
    internal var tempFile: File? = null

    private lateinit var dragHelper: ViewDragHelper

    companion object {
        /**
         * Cache folder to store images
         * Linked to the uri provider
         */
        private const val IMAGE_FOLDER = "images"
        private const val TIME_FORMAT = "yyyyMMdd_HHmmss"
        private const val IMG_TAG = "Frost"
        const val PURGE_TIME: Long = 10 * 60 * 1000 // 10 min block
        private val L = KauLoggerExtension("Image", com.pitchedapps.frost.utils.L)

        fun cacheDir(context: Context): File =
            File(context.cacheDir, IMAGE_FOLDER)
    }

    private val cookie: String? by lazy { intent.getStringExtra(ARG_COOKIE) }

    val imageUrl: String by lazy { intent.getStringExtra(ARG_IMAGE_URL)?.trim('"') ?: "" }

    private lateinit var trueImageUrl: Deferred<String>

    private val imageText: String? by lazy { intent.getStringExtra(ARG_TEXT) }

    // a unique image identifier based on the id (if it exists), and its hash
    private val imageHash: String by lazy {
        "${abs(FB_IMAGE_ID_MATCHER.find(imageUrl)[1]?.hashCode() ?: 0)}_${abs(imageUrl.hashCode())}"
    }

    lateinit var binding: ActivityImageBinding
    private var bottomBehavior: BottomSheetBehavior<View>? = null

    private val baseBackgroundColor: Int
        get() = if (prefs.blackMediaBg) Color.BLACK
        else themeProvider.bgColor.withMinAlpha(235)

    private fun loadError(e: Throwable) {
        if (e.message?.contains("<!DOCTYPE html>") == true) {
            applicationContext.toast(R.string.image_not_found)
            finish()
            return
        }
        errorRef = e
        e.logFrostEvent("Image load error")
        with(binding) {
            if (imageProgress.isVisible)
                imageProgress.fadeOut()
        }
        tempFile?.delete()
        binding.error.fadeIn()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (imageUrl.isEmpty()) {
            return finish()
        }
        L.i { "Displaying image" }
        trueImageUrl = async(Dispatchers.IO) {
            val result = if (!imageUrl.isIndirectImageUrl) imageUrl
            else cookie?.getFullSizedImageUrl(imageUrl) ?: imageUrl
            if (result != imageUrl)
                L.v { "Launching with true url $result" }
            result
        }
        binding = ActivityImageBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.init()
        launch(CoroutineExceptionHandler { _, throwable -> loadError(throwable) }) {
            val tempFile = downloadTempImage()
            this@ImageActivity.tempFile = tempFile
            binding.imageProgress.fadeOut()
            binding.imagePhoto.setImage(ImageSource.uri(frostUriFromFile(tempFile)))
            binding.imagePhoto.animate().alpha(1f).scaleXY(1f).start()
        }
    }

    private fun ActivityImageBinding.init() {
        imageContainer.setBackgroundColor(baseBackgroundColor)
        toolbar.setBackgroundColor(baseBackgroundColor)
        this@ImageActivity.imageText.also { text ->
            if (text.isNullOrBlank()) {
                imageText.gone()
            } else {
                imageText.setTextColor(if (prefs.blackMediaBg) Color.WHITE else themeProvider.textColor)
                imageText.setBackgroundColor(
                    baseBackgroundColor.colorToForeground(0.2f).withAlpha(255)
                )
                imageText.text = text
                bottomBehavior = BottomSheetBehavior.from<View>(imageText).apply {
                    addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                        override fun onSlide(bottomSheet: View, slideOffset: Float) {
                            imageText.alpha = slideOffset / 2 + 0.5f
                        }

                        override fun onStateChanged(bottomSheet: View, newState: Int) {
                            // No op
                        }
                    })
                }
                imageText.bringToFront()
            }
        }
        val foregroundTint = if (prefs.blackMediaBg) Color.WHITE else themeProvider.accentColor

        fun ImageView.setState(state: FabStates) {
            setIcon(state.iicon, color = foregroundTint, sizeDp = 24)
            setOnClickListener { state.onClick(this@ImageActivity) }
        }

        imageProgress.tint(foregroundTint)
        error.apply {
            invisible()
            setState(FabStates.ERROR)
        }
        download.apply {
            setState(FabStates.DOWNLOAD)
        }
        share.apply {
            setState(FabStates.SHARE)
        }
        imagePhoto.setOnImageEventListener(object :
                SubsamplingScaleImageView.DefaultOnImageEventListener() {
                override fun onImageLoadError(e: Exception) {
                    loadError(e)
                }
            })
        activityThemer.setFrostColors {
            themeWindow = false
        }
        dragHelper = ViewDragHelper.create(imageDrag, ViewDragCallback()).apply {
            setEdgeTrackingEnabled(ViewDragHelper.EDGE_TOP or ViewDragHelper.EDGE_BOTTOM)
        }
        imageDrag.dragHelper = dragHelper
        imageDrag.viewToIgnore = imageText
    }

    private inner class ViewDragCallback : ViewDragHelper.Callback() {
        private var scrollPercent: Float = 0f
        private var scrollThreshold = 0.5f
        private var scrollToTop = false

        override fun tryCaptureView(view: View, i: Int): Boolean {
            L.d { "Try capture ${view.id} $i ${binding.imagePhoto.id} ${binding.imageText.id}" }
            return view === binding.imagePhoto
        }

        override fun getViewHorizontalDragRange(child: View): Int = 0

        override fun getViewVerticalDragRange(child: View): Int = child.height

        override fun onViewPositionChanged(
            changedView: View,
            left: Int,
            top: Int,
            dx: Int,
            dy: Int
        ) {
            super.onViewPositionChanged(changedView, left, top, dx, dy)
            with(binding) {
                // make sure that we are using the proper axis
                scrollPercent = abs(top.toFloat() / imageContainer.height)
                scrollToTop = top < 0
                val multiplier = max(1f - scrollPercent, 0f)

                toolbar.alpha = multiplier
                bottomBehavior?.also {
                    imageText.alpha =
                        multiplier * (if (it.state == BottomSheetBehavior.STATE_COLLAPSED) 0.5f else 1f)
                }
                imageContainer.setBackgroundColor(baseBackgroundColor.adjustAlpha(multiplier))

                if (scrollPercent >= 1) {
                    if (!isFinishing) {
                        finish()
                        overridePendingTransition(0, 0)
                    }
                }
            }
        }

        override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) {
            val overScrolled = scrollPercent > scrollThreshold
            val maxOffset = releasedChild.height + 10
            val finalTop = when {
                scrollToTop && (overScrolled || yvel < -dragHelper.minVelocity) -> -maxOffset
                !scrollToTop && (overScrolled || yvel > dragHelper.minVelocity) -> maxOffset
                else -> 0
            }
            dragHelper.settleCapturedViewAt(0, finalTop)
            binding.imageDrag.invalidate()
        }

        override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int = 0

        override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int = top
    }

    private fun getImageExtension(type: String?): String? {
        if (type?.startsWith("image/") != true) {
            return null
        }
        return when (type.substring(6)) {
            "jpeg" -> "jpg"
            "png" -> "png"
            "gif" -> "gif"
            else -> null
        }
    }

    @Throws(IOException::class)
    private suspend fun downloadTempImage(): File = withContext(Dispatchers.IO) {

        // We assume all images are jpg
        // Activity launcher may be able to provide specifics, but this beats sending a request
        // just to get the content header
        val file = File(cacheDir(this@ImageActivity), "$imageHash.jpg")

        if (!file.isFile) {
            file.parentFile?.mkdirs()
            file.createNewFile()
        } else {
            file.setLastModified(System.currentTimeMillis())
        }

        // Forbid overwrites
        if (file.isFile && file.length() > 0) {
            L.i { "Forbid image overwrite" }
            return@withContext file
        }

        val response = cookie.requestBuilder()
            .url(trueImageUrl.await())
            .get()
            .call()
            .execute()

        if (!response.isSuccessful) {
            throw IOException("Unsuccessful response for image: ${response.peekBody(128).string()}")
        }

        val body = response.body ?: throw IOException("Failed to retrieve image body")
        file.copyFromInputStream(body.byteStream())
        file
    }

    internal suspend fun saveImage() {
        frostDownload(cookie = cookie, url = trueImageUrl.await())
    }

    override fun onDestroy() {
        LocalService.schedule(this, LocalService.Flag.PURGE_IMAGE)
        super.onDestroy()
    }
}

internal enum class FabStates(
    val iicon: IIcon,
    val iconColorProvider: (ThemeProvider) -> Int = { it.iconColor },
    val backgroundTint: Int = Int.MAX_VALUE
) {
    ERROR(GoogleMaterial.Icon.gmd_error, { Color.WHITE }, Color.RED) {
        override fun onClick(activity: ImageActivity) {
            val err =
                activity.errorRef?.takeIf { it !is FileNotFoundException && it.message != "Image failed to decode using JPEG decoder" }
                    ?: return
            activity.materialDialog {
                title(R.string.kau_error)
                message(text = err.message ?: err.javaClass.name)
            }
        }
    },
    NOTHING(GoogleMaterial.Icon.gmd_adjust) {
        override fun onClick(activity: ImageActivity) {}
    },
    DOWNLOAD(GoogleMaterial.Icon.gmd_file_download) {
        override fun onClick(activity: ImageActivity) {
            activity.launch {
                activity.binding.download.fadeOut()
                activity.saveImage()
            }
        }
    },
    SHARE(GoogleMaterial.Icon.gmd_share) {
        override fun onClick(activity: ImageActivity) {
            val file = activity.tempFile ?: return
            try {
                val photoURI = activity.frostUriFromFile(file)
                val intent = Intent(Intent.ACTION_SEND).apply {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    putExtra(Intent.EXTRA_STREAM, photoURI)
                    type = "image/png"
                }
                activity.startActivity(intent)
            } catch (e: Exception) {
                activity.errorRef = e
                e.logFrostEvent("Image share failed")
                activity.frostSnackbar(R.string.image_share_failed, activity.themeProvider)
            }
        }
    };

    /**
     * Change the fab look
     * If it's in view, give it some animations
     *
     * TODO investigate what is wrong with fadeScaleTransition
     *
     * https://github.com/AllanWang/KAU/issues/184
     *
     */
    fun update(fab: FloatingActionButton, themeProvider: ThemeProvider) {
        val tint =
            if (backgroundTint != Int.MAX_VALUE) backgroundTint else themeProvider.accentColor
        val iconColor = iconColorProvider(themeProvider)
        if (fab.isHidden) {
            fab.setIcon(iicon, color = iconColor)
            fab.backgroundTintList = ColorStateList.valueOf(tint)
            fab.show()
        } else {
            fab.hide(object : FloatingActionButton.OnVisibilityChangedListener() {
                override fun onHidden(fab: FloatingActionButton) {
                    fab.setIcon(iicon, color = iconColor)
                    fab.show()
                }
            })
        }
    }

    abstract fun onClick(activity: ImageActivity)
}