2020-02-27 22:29:47 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.browser
|
|
|
|
|
|
|
|
import android.animation.ValueAnimator
|
2020-03-30 21:07:00 +02:00
|
|
|
import android.content.Context
|
2020-02-27 22:29:47 +01:00
|
|
|
import android.graphics.Color
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import android.view.animation.DecelerateInterpolator
|
|
|
|
import androidx.core.animation.doOnEnd
|
|
|
|
import androidx.core.graphics.drawable.toDrawable
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
2020-03-30 21:07:00 +02:00
|
|
|
import androidx.navigation.NavOptions
|
2020-02-27 22:29:47 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import mozilla.components.concept.engine.EngineView
|
2020-03-30 21:07:00 +02:00
|
|
|
import org.mozilla.fenix.R
|
2020-07-23 04:23:38 +02:00
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarPosition
|
2020-03-30 21:07:00 +02:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2020-02-27 22:29:47 +01:00
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles properly animating the browser engine based on `SHOULD_ANIMATE_FLAG` passed in through
|
|
|
|
* nav arguments.
|
|
|
|
*/
|
|
|
|
class BrowserAnimator(
|
|
|
|
private val fragment: WeakReference<Fragment>,
|
|
|
|
private val engineView: WeakReference<EngineView>,
|
|
|
|
private val swipeRefresh: WeakReference<View>,
|
2020-07-14 20:07:11 +02:00
|
|
|
private val viewLifecycleScope: WeakReference<LifecycleCoroutineScope>,
|
2020-05-18 18:58:53 +02:00
|
|
|
private val arguments: Bundle
|
2020-02-27 22:29:47 +01:00
|
|
|
) {
|
|
|
|
|
|
|
|
private val unwrappedEngineView: EngineView?
|
|
|
|
get() = engineView.get()
|
|
|
|
|
|
|
|
private val unwrappedSwipeRefresh: View?
|
|
|
|
get() = swipeRefresh.get()
|
|
|
|
|
2020-03-05 21:29:23 +01:00
|
|
|
private val browserZoomInValueAnimator = ValueAnimator.ofFloat(0f, END_ANIMATOR_VALUE).apply {
|
2020-03-02 23:13:13 +01:00
|
|
|
addUpdateListener {
|
2020-03-30 21:07:00 +02:00
|
|
|
unwrappedSwipeRefresh?.scaleX =
|
|
|
|
STARTING_XY_SCALE + XY_SCALE_MULTIPLIER * it.animatedFraction
|
|
|
|
unwrappedSwipeRefresh?.scaleY =
|
|
|
|
STARTING_XY_SCALE + XY_SCALE_MULTIPLIER * it.animatedFraction
|
2020-03-02 23:13:13 +01:00
|
|
|
unwrappedSwipeRefresh?.alpha = it.animatedFraction
|
|
|
|
}
|
|
|
|
|
|
|
|
doOnEnd {
|
2020-05-18 18:58:53 +02:00
|
|
|
unwrappedEngineView?.asView()?.visibility = View.VISIBLE
|
|
|
|
unwrappedSwipeRefresh?.background = null
|
|
|
|
arguments.putBoolean(SHOULD_ANIMATE_FLAG, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
interpolator = DecelerateInterpolator()
|
|
|
|
duration = ANIMATION_DURATION
|
|
|
|
}
|
|
|
|
|
|
|
|
private val browserFadeInValueAnimator = ValueAnimator.ofFloat(0f, END_ANIMATOR_VALUE).apply {
|
|
|
|
addUpdateListener {
|
|
|
|
unwrappedSwipeRefresh?.alpha = it.animatedFraction
|
|
|
|
}
|
|
|
|
|
|
|
|
doOnEnd {
|
|
|
|
unwrappedEngineView?.asView()?.visibility = View.VISIBLE
|
2020-03-05 21:29:23 +01:00
|
|
|
unwrappedSwipeRefresh?.background = null
|
|
|
|
arguments.putBoolean(SHOULD_ANIMATE_FLAG, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
interpolator = DecelerateInterpolator()
|
|
|
|
duration = ANIMATION_DURATION
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:29:47 +01:00
|
|
|
/**
|
2020-03-05 21:29:23 +01:00
|
|
|
* Triggers the *zoom in* browser animation to run if necessary (based on the SHOULD_ANIMATE_FLAG).
|
|
|
|
* Also removes the flag from the bundle so it is not played on future entries into the fragment.
|
2020-02-27 22:29:47 +01:00
|
|
|
*/
|
2020-03-02 23:13:13 +01:00
|
|
|
fun beginAnimateInIfNecessary() {
|
2020-02-27 22:29:47 +01:00
|
|
|
val shouldAnimate = arguments.getBoolean(SHOULD_ANIMATE_FLAG, false)
|
|
|
|
if (shouldAnimate) {
|
2020-07-14 20:07:11 +02:00
|
|
|
viewLifecycleScope.get()?.launch(Dispatchers.Main) {
|
2020-02-27 22:29:47 +01:00
|
|
|
delay(ANIMATION_DELAY)
|
|
|
|
captureEngineViewAndDrawStatically {
|
2020-04-16 22:31:37 +02:00
|
|
|
unwrappedSwipeRefresh?.alpha = 0f
|
2020-03-05 21:29:23 +01:00
|
|
|
browserZoomInValueAnimator.start()
|
2020-02-27 22:29:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unwrappedSwipeRefresh?.alpha = 1f
|
2020-05-18 18:58:53 +02:00
|
|
|
unwrappedEngineView?.asView()?.visibility = View.VISIBLE
|
2020-02-27 22:29:47 +01:00
|
|
|
unwrappedSwipeRefresh?.background = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-05 21:29:23 +01:00
|
|
|
* Triggers the *zoom out* browser animation to run.
|
2020-02-27 22:29:47 +01:00
|
|
|
*/
|
2020-03-02 23:13:13 +01:00
|
|
|
fun beginAnimateOut() {
|
2020-07-14 20:07:11 +02:00
|
|
|
viewLifecycleScope.get()?.launch(Dispatchers.Main) {
|
2020-03-02 23:13:13 +01:00
|
|
|
captureEngineViewAndDrawStatically {
|
|
|
|
unwrappedEngineView?.asView()?.visibility = View.GONE
|
2020-03-05 21:29:23 +01:00
|
|
|
browserZoomInValueAnimator.reverse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:29:47 +01:00
|
|
|
/**
|
|
|
|
* Makes the swipeRefresh background a screenshot of the engineView in its current state.
|
|
|
|
* This allows us to "animate" the engineView.
|
|
|
|
*/
|
|
|
|
fun captureEngineViewAndDrawStatically(onComplete: () -> Unit) {
|
|
|
|
unwrappedEngineView?.asView()?.context.let {
|
2020-07-14 20:07:11 +02:00
|
|
|
viewLifecycleScope.get()?.launch {
|
2020-02-27 22:29:47 +01:00
|
|
|
// isAdded check is necessary because of a bug in viewLifecycleOwner. See AC#3828
|
2020-05-06 20:35:38 +02:00
|
|
|
if (!fragment.isAdded()) {
|
|
|
|
return@launch
|
|
|
|
}
|
2020-02-27 22:29:47 +01:00
|
|
|
unwrappedEngineView?.captureThumbnail { bitmap ->
|
2020-05-06 20:35:38 +02:00
|
|
|
if (!fragment.isAdded()) {
|
|
|
|
return@captureThumbnail
|
|
|
|
}
|
2020-02-27 22:29:47 +01:00
|
|
|
|
|
|
|
unwrappedSwipeRefresh?.apply {
|
|
|
|
// If the bitmap is null, the best we can do to reduce the flash is set transparent
|
|
|
|
background = bitmap?.toDrawable(context.resources)
|
|
|
|
?: ColorDrawable(Color.TRANSPARENT)
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:31:37 +02:00
|
|
|
unwrappedEngineView?.asView()?.visibility = View.GONE
|
|
|
|
|
2020-02-27 22:29:47 +01:00
|
|
|
onComplete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun WeakReference<Fragment>.isAdded(): Boolean {
|
|
|
|
val unwrapped = get()
|
|
|
|
return unwrapped != null && unwrapped.isAdded
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val SHOULD_ANIMATE_FLAG = "shouldAnimate"
|
|
|
|
private const val ANIMATION_DELAY = 50L
|
|
|
|
private const val ANIMATION_DURATION = 115L
|
|
|
|
private const val END_ANIMATOR_VALUE = 500f
|
|
|
|
private const val XY_SCALE_MULTIPLIER = .05f
|
|
|
|
private const val STARTING_XY_SCALE = .95f
|
2020-03-30 21:07:00 +02:00
|
|
|
|
|
|
|
fun getToolbarNavOptions(context: Context): NavOptions {
|
|
|
|
val navOptions = NavOptions.Builder()
|
|
|
|
|
2020-07-23 04:23:38 +02:00
|
|
|
when (context.settings().toolbarPosition) {
|
|
|
|
ToolbarPosition.TOP -> {
|
|
|
|
navOptions.setEnterAnim(R.anim.fade_in)
|
|
|
|
navOptions.setExitAnim(R.anim.fade_out)
|
|
|
|
}
|
|
|
|
ToolbarPosition.BOTTOM -> {
|
|
|
|
navOptions.setEnterAnim(R.anim.fade_in_up)
|
|
|
|
navOptions.setExitAnim(R.anim.fade_out_down)
|
|
|
|
}
|
2020-03-30 21:07:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return navOptions.build()
|
|
|
|
}
|
2020-02-27 22:29:47 +01:00
|
|
|
}
|
|
|
|
}
|