Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #108 from readium/fixes/debug
Browse files Browse the repository at this point in the history
wrap timber logs to only show in debug builds and source formatting
  • Loading branch information
aferditamuriqi authored Dec 28, 2019
2 parents fce1a99 + 806dfa5 commit 7883676
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 91 deletions.
6 changes: 3 additions & 3 deletions r2-navigator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ dependencies {
if (findProject(':r2-shared')) {
implementation project(':r2-shared')
} else {
implementation "com.github.readium:r2-shared-kotlin:1.1.4"
implementation "com.github.readium:r2-shared-kotlin:1.1.5"
}
implementation "androidx.appcompat:appcompat:1.2.0-alpha01"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.webkit:webkit:1.1.0'
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "com.google.android.material:material:1.2.0-alpha02"
implementation "com.google.android.material:material:1.2.0-alpha03"
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'joda-time:joda-time:2.9.9'
implementation "androidx.legacy:legacy-support-core-ui:1.0.0"
Expand All @@ -56,7 +56,7 @@ dependencies {

implementation 'com.jakewharton.timber:timber:4.7.1'

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'

implementation 'org.zeroturnaround:zt-zip:1.13'
Expand Down
85 changes: 41 additions & 44 deletions r2-navigator/src/main/java/org/readium/r2/navigator/R2WebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import androidx.core.view.WindowInsetsCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.readium.r2.navigator.BuildConfig.DEBUG
import timber.log.Timber
import kotlin.math.roundToInt
import kotlin.math.*

/**
* Created by Aferdita Muriqi on 12/2/17.
Expand Down Expand Up @@ -62,16 +63,14 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
}
}

private val DEBUG = false

private val USE_CACHE = false

private val MAX_SETTLE_DURATION = 600 // ms
private val MIN_DISTANCE_FOR_FLING = 25 // dips
private val MIN_FLING_VELOCITY = 400 // dips


internal fun getContentWidth(): Int {
private fun getContentWidth(): Int {
return this.computeHorizontalScrollRange()//working after load of page
}

Expand Down Expand Up @@ -161,23 +160,23 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
* Indicates that the pager is in an idle, settled state. The current page
* is fully in view and no animation is in progress.
*/
val SCROLL_STATE_IDLE = 0
private val SCROLL_STATE_IDLE = 0

/**
* Indicates that the pager is currently being dragged by the user.
*/
val SCROLL_STATE_DRAGGING = 1
private val SCROLL_STATE_DRAGGING = 1

/**
* Indicates that the pager is in the process of settling to a final position.
*/
val SCROLL_STATE_SETTLING = 2
private val SCROLL_STATE_SETTLING = 2

private val mEndScrollRunnable = Runnable { setScrollState(SCROLL_STATE_IDLE) }

private var mScrollState = SCROLL_STATE_IDLE

internal fun initWebPager() {
private fun initWebPager() {
setWillNotDraw(false)
descendantFocusability = ViewGroup.FOCUS_AFTER_DESCENDANTS
isFocusable = true
Expand Down Expand Up @@ -233,13 +232,13 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
.dispatchApplyWindowInsets(getChildAt(i), applied)
// Now keep track of any consumed by tracking each dimension's min
// value
res.left = Math.min(childInsets.systemWindowInsetLeft,
res.left = min(childInsets.systemWindowInsetLeft,
res.left)
res.top = Math.min(childInsets.systemWindowInsetTop,
res.top = min(childInsets.systemWindowInsetTop,
res.top)
res.right = Math.min(childInsets.systemWindowInsetRight,
res.right = min(childInsets.systemWindowInsetRight,
res.right)
res.bottom = Math.min(childInsets.systemWindowInsetBottom,
res.bottom = min(childInsets.systemWindowInsetBottom,
res.bottom)
i++
}
Expand Down Expand Up @@ -292,14 +291,14 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,

fun calculateCurrentItem() {
val currentPage = numPages * progression
mCurItem = Math.abs(currentPage).roundToInt()
mCurItem = abs(currentPage).roundToInt()
}

internal fun setCurrentItemInternal(item: Int, smoothScroll: Boolean, always: Boolean) {
private fun setCurrentItemInternal(item: Int, smoothScroll: Boolean, always: Boolean) {
setCurrentItemInternal(item, smoothScroll, 0)
}

internal fun setCurrentItemInternal(item: Int, smoothScroll: Boolean, velocity: Int) {
private fun setCurrentItemInternal(item: Int, smoothScroll: Boolean, velocity: Int) {
if (mFirstLayout) {
// We don't have any idea how big we are yet and shouldn't have any pages either.
// Just set things up and let the pending layout handle things.
Expand Down Expand Up @@ -334,11 +333,11 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
// the screen has to travel, however, we don't want this duration to be effected in a
// purely linear fashion. Instead, we use this method to moderate the effect that the distance
// of travel has on the overall snap duration.
internal fun distanceInfluenceForSnapDuration(f: Float): Float {
private fun distanceInfluenceForSnapDuration(f: Float): Float {
var float = f
float -= 0.5f // center the values about 0.
float *= 0.3f * Math.PI.toFloat() / 2.0f
return Math.sin(float.toDouble()).toFloat()
return sin(float.toDouble()).toFloat()
}


Expand All @@ -349,7 +348,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
* @param y the number of pixels to scroll by on the Y axis
* @param velocity the velocity associated with a fling, if applicable. (0 otherwise)
*/
internal fun smoothScrollTo(x: Int, y: Int, velocity: Int) {
private fun smoothScrollTo(x: Int, y: Int, velocity: Int) {
var v = velocity
val sx: Int
val wasScrolling = mScroller != null && !mScroller!!.isFinished
Expand Down Expand Up @@ -379,19 +378,19 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,

val width = getClientWidth()
val halfWidth = width / 2
val distanceRatio = Math.min(1f, 1.0f * Math.abs(dx) / width)
val distanceRatio = min(1f, 1.0f * abs(dx) / width)
val distance = halfWidth + halfWidth * distanceInfluenceForSnapDuration(distanceRatio)

var duration: Int
v = Math.abs(v)
v = abs(v)
duration = if (v > 0) {
4 * Math.round(1000 * Math.abs(distance / v))
4 * (1000 * abs(distance / v)).roundToInt()
} else {
// final float pageWidth = width * mAdapter.getPageWidth(mCurItem);
val pageDelta = Math.abs(dx).toFloat() / (width + mPageMargin)
val pageDelta = abs(dx).toFloat() / (width + mPageMargin)
((pageDelta + 1) * 100).toInt()
}
duration = Math.min(duration, MAX_SETTLE_DURATION)
duration = min(duration, MAX_SETTLE_DURATION)

// Reset the "scroll started" flag. It will be flipped to true in all places
// where we call computeScrollOffset().
Expand Down Expand Up @@ -435,7 +434,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
}
} else {
val ii = infoForPosition(mCurItem)
val scrollOffset: Float = if (ii != null) Math.min(ii.offset, mLastOffset) else 0.0F
val scrollOffset: Float = if (ii != null) min(ii.offset, mLastOffset) else 0.0F
val scrollPos = (scrollOffset * (width - paddingLeft - paddingRight)).toInt()
if (scrollPos != scrollX) {
completeScroll(false)
Expand Down Expand Up @@ -472,7 +471,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
childLeft = paddingLeft
paddingLeft += child.measuredWidth
}
Gravity.CENTER_HORIZONTAL -> childLeft = Math.max((width - child.measuredWidth) / 2,
Gravity.CENTER_HORIZONTAL -> childLeft = max((width - child.measuredWidth) / 2,
paddingLeft)
Gravity.END -> {
childLeft = width - paddingRight - child.measuredWidth
Expand All @@ -485,7 +484,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
childTop = paddingTop
paddingTop += child.measuredHeight
}
Gravity.CENTER_VERTICAL -> childTop = Math.max((height - child.measuredHeight) / 2,
Gravity.CENTER_VERTICAL -> childTop = max((height - child.measuredHeight) / 2,
paddingTop)
Gravity.BOTTOM -> {
childTop = height - paddingBottom - child.measuredHeight
Expand Down Expand Up @@ -588,7 +587,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
childLeft = paddingLeft
paddingLeft += child.width
}
Gravity.CENTER_HORIZONTAL -> childLeft = Math.max((width - child.measuredWidth) / 2,
Gravity.CENTER_HORIZONTAL -> childLeft = max((width - child.measuredWidth) / 2,
paddingLeft)
Gravity.END -> {
childLeft = width - paddingRight - child.measuredWidth
Expand Down Expand Up @@ -669,12 +668,10 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
if (!mIsBeingDragged) {
val pointerIndex = ev.findPointerIndex(mActivePointerId)
val x = ev.getX(pointerIndex)
val xDiff = Math.abs(x - mLastMotionX)
val xDiff = abs(x - mLastMotionX)
val y = ev.getY(pointerIndex)
val yDiff = Math.abs(y - mLastMotionY)
if (DEBUG) {
Timber.v("Moved x to $x,$y diff=$xDiff,$yDiff")
}
val yDiff = abs(y - mLastMotionY)
if (DEBUG) Timber.v("Moved x to $x,$y diff=$xDiff,$yDiff")

if (xDiff > mTouchSlop && xDiff > yDiff) {
if (DEBUG) Timber.v("Starting drag!")
Expand Down Expand Up @@ -702,13 +699,13 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
val nextPage = determineTargetPage(currentPage, 0f, initialVelocity, totalDelta)

if (nextPage == currentPage && nextPage == 0 && scrollX == 0) {
Timber.tag(this::class.java.simpleName).d("onTouchEvent scrollLeft")
if (DEBUG) Timber.tag(this::class.java.simpleName).d("onTouchEvent scrollLeft")
scrollLeft(animated = true)
} else if (nextPage == numPages) {
Timber.tag(this::class.java.simpleName).d("onTouchEvent scrollRight")
if (DEBUG) Timber.tag(this::class.java.simpleName).d("onTouchEvent scrollRight")
scrollRight(animated = true)
} else {
Timber.tag(this::class.java.simpleName).d("onTouchEvent setCurrentItemInternal")
if (DEBUG) Timber.tag(this::class.java.simpleName).d("onTouchEvent setCurrentItemInternal")
setCurrentItemInternal(nextPage, true, initialVelocity)
}
}
Expand Down Expand Up @@ -784,10 +781,10 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,

private fun determineTargetPage(currentPage: Int, pageOffset: Float, velocity: Int, deltaX: Int): Int {
val targetPage: Int
val d = Math.abs(deltaX)
val v = Math.abs(velocity)
val d = abs(deltaX)
val v = abs(velocity)

targetPage = if (Math.abs(deltaX) > mFlingDistance && Math.abs(velocity) > mMinimumVelocity) {
targetPage = if (abs(deltaX) > mFlingDistance && abs(velocity) > mMinimumVelocity) {
if (velocity > 0) currentPage else currentPage + 1
} else {
val truncator = if (currentPage >= mCurItem) 0.4f else 0.6f
Expand Down Expand Up @@ -841,7 +838,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
fun executeKeyEvent(event: KeyEvent): Boolean {
private fun executeKeyEvent(event: KeyEvent): Boolean {
var handled = false
if (event.action == KeyEvent.ACTION_DOWN) {
when (event.keyCode) {
Expand Down Expand Up @@ -872,7 +869,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
* either [View.FOCUS_LEFT] or [View.FOCUS_RIGHT].
* @return Whether the scrolling was handled successfully.
*/
fun arrowScroll(direction: Int): Boolean {
private fun arrowScroll(direction: Int): Boolean {
var currentFocused: View? = findFocus()
if (currentFocused === this) {
currentFocused = null
Expand All @@ -897,7 +894,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
sb.append(" => ").append(parent.javaClass.simpleName)
parent = parent.parent
}
Timber.e("arrowScroll tried to find focus based on non-child current focused view %s", sb.toString())
if (DEBUG) Timber.e("arrowScroll tried to find focus based on non-child current focused view %s", sb.toString())
currentFocused = null
}
}
Expand Down Expand Up @@ -968,15 +965,15 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
return outRect
}

internal fun pageLeft(): Boolean {
private fun pageLeft(): Boolean {
if (mCurItem > 0) {
setCurrentItem(mCurItem - 1, true)
return true
}
return false
}

internal fun pageRight(): Boolean {
private fun pageRight(): Boolean {
if (mCurItem < numPages) {
setCurrentItem(mCurItem + 1, true)
return true
Expand Down Expand Up @@ -1010,7 +1007,7 @@ class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView(context,
*/
var isDecor: Boolean = false

val LAYOUT_ATTRS = intArrayOf(android.R.attr.layout_gravity)
private val LAYOUT_ATTRS = intArrayOf(android.R.attr.layout_gravity)

/**
* Gravity setting for use on decor views only:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.android.synthetic.main.activity_r2_audiobook.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.readium.r2.navigator.*
import org.readium.r2.navigator.BuildConfig.*
import org.readium.r2.shared.Link
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Locator
Expand Down Expand Up @@ -79,10 +80,10 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit
var currentResource = 0

var startTime = 0.0
var finalTime = 0.0
private var finalTime = 0.0

val forwardTime = 10000
val backwardTime = 10000
private val forwardTime = 10000
private val backwardTime = 10000

var mediaPlayer: R2MediaPlayer? = null

Expand Down Expand Up @@ -130,7 +131,7 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit
return
}
mediaPlayer?.seekTo(progress)
Timber.tag("AUDIO").d("progress $progress")
if (DEBUG) Timber.tag("AUDIO").d("progress $progress")
}

/**
Expand All @@ -141,7 +142,7 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit
override fun onStartTrackingTouch(seekBar: SeekBar?) {
// do nothing
isSeekTracking = true
Timber.tag("AUDIO").d("start tracking")
if (DEBUG) Timber.tag("AUDIO").d("start tracking")
}

/**
Expand All @@ -152,7 +153,7 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// do nothing
isSeekTracking = false
Timber.tag("AUDIO").d("stop tracking")
if (DEBUG) Timber.tag("AUDIO").d("stop tracking")
}

})
Expand Down Expand Up @@ -190,18 +191,18 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit
}

next_chapter!!.setOnClickListener { view ->
goForward(false, {})
goForward(false) {}
}

prev_chapter!!.setOnClickListener { view ->
goBackward(false, {})
goBackward(false) {}
}

}
}, 100)
}

fun updateUI() {
private fun updateUI() {

if (currentResource == publication.readingOrder.size - 1) {
next_chapter!!.isEnabled = false
Expand Down Expand Up @@ -253,8 +254,8 @@ open class R2AudiobookActivity : AppCompatActivity(), CoroutineScope, IR2Activit

}

var seekLocation: Locations? = null
var isSeekNeeded = false
private var seekLocation: Locations? = null
private var isSeekNeeded = false
var isSeekTracking = false
private fun seekIfNeeded() {
if (isSeekNeeded) {
Expand Down
Loading

0 comments on commit 7883676

Please sign in to comment.