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 #90 from readium/feature/DiViNa
Browse files Browse the repository at this point in the history
Feature: DiViNa
  • Loading branch information
aferditamuriqi authored Sep 7, 2019
2 parents cc73867 + fa467b8 commit 90476b5
Show file tree
Hide file tree
Showing 13 changed files with 589 additions and 71 deletions.
8 changes: 4 additions & 4 deletions r2-navigator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ dependencies {
if (findProject(':r2-shared')) {
implementation project(':r2-shared')
} else {
implementation "com.github.readium:r2-shared-kotlin:1.1.2"
implementation "com.github.readium:r2-shared-kotlin:1.1.3"
}
implementation "androidx.appcompat:appcompat:1.1.0-rc01"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.webkit:webkit:1.0.0'
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "com.google.android.material:material:1.1.0-alpha08"
implementation "androidx.recyclerview:recyclerview:1.1.0-beta01"
implementation "com.google.android.material:material:1.1.0-alpha09"
implementation "androidx.recyclerview:recyclerview:1.1.0-beta03"
implementation 'joda-time:joda-time:2.9.9'
implementation "androidx.legacy:legacy-support-core-ui:1.0.0"

implementation 'com.duolingo.open:rtl-viewpager:1.0.3'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'

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

Expand Down
27 changes: 27 additions & 0 deletions r2-navigator/src/main/assets/divina/divinaPlayer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>DiViNa Player</title>
<style type="text/css">
body, html {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(0, 0, 0);
}
</style>
<script type="text/javascript" src="./divinaTouchHandling.js"></script>
<script type="text/javascript" src="./divinaPlayer.js"></script>
</head>
<body>
<script type="text/javascript">
var player = null
window.onload = function() {
const Player = window.divinaPlayer
player = new Player(window.document.body)
}
</script>
</body>
</html>
271 changes: 271 additions & 0 deletions r2-navigator/src/main/assets/divina/divinaPlayer.js

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions r2-navigator/src/main/assets/divina/divinaTouchHandling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var singleTouchGesture = false;
var startX = 0;
var startY = 0;
var availWidth = window.screen.availWidth;
var availHeight = window.screen.availHeight;


window.addEventListener("load", function(){ // on page load
// Get screen X and Y sizes.
// Events listeners for the touches.
window.document.addEventListener("touchstart", handleTouchStart, false);
window.document.addEventListener("touchend", handleTouchEnd, false);
// When device orientation changes, screen X and Y sizes are recalculated.
}, false);



// When a touch is detected records its starting coordinates and if it's a singleTouchGesture.
var handleTouchStart = function(event) {
if (event.target.nodeName.toUpperCase() === 'A') {
console.log("Touched a link.");
// singleTouchGesture = false;
return;
}
console.log("Touch sent to native code.");
singleTouchGesture = event.touches.length == 1;

var touch = event.changedTouches[0];

startX = touch.screenX % availWidth;
startY = touch.screenY % availHeight;

};

// When a touch ends, check if any action has to be made, and contact native code.
var handleTouchEnd = function(event) {
if(!singleTouchGesture) {
return;
}

var touch = event.changedTouches[0];

var relativeDistanceX = Math.abs(((touch.screenX % availWidth) - startX) / availWidth);
var relativeDistanceY = Math.abs(((touch.screenY % availHeight) - startY) / availHeight);
var touchDistance = Math.max(relativeDistanceX, relativeDistanceY);

var scrollWidth = document.scrollWidth;
var screenWidth = availWidth;
var tapAreaWidth = availWidth * 0.2;

if(touchDistance < 0.01) {
var position = (touch.screenX % availWidth) / availWidth;
if (position <= 0.2) {
console.log("LeftTapped");
} else if(position >= 0.8) {
console.log("RightTapped");
} else {
console.log("CenterTapped");
Android.centerTapped();
}
event.stopPropagation();
event.preventDefault();
return;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Module: r2-navigator-kotlin
* Developers: Aferdita Muriqi
*
* Copyright (c) 2019. Readium Foundation. All rights reserved.
* Use of this source code is governed by a BSD-style license which is detailed in the
* LICENSE file present in the project repository where this source code is maintained.
*/

package org.readium.r2.navigator

import android.content.SharedPreferences
import android.view.View
import org.readium.r2.navigator.pager.R2ViewPager
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Publication

interface R2ActivityListener {

val publication: Publication
val preferences: SharedPreferences
val publicationIdentifier: String
val resourcePager: R2ViewPager?
get() = null
val allowToggleActionBar: Boolean
get() = true

fun toggleActionBar() {}
fun toggleActionBar(v: View? = null) {}
fun storeProgression(locations: Locations?) {}
fun nextResource(smoothScroll: Boolean) {}
fun previousResource(smoothScroll: Boolean) {}
fun nextResource(v: View? = null) {}
fun previousResource(v: View? = null) {}
fun onPageChanged(pageIndex: Int, totalPages: Int, url: String) {}
fun onPageEnded(end: Boolean) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ import org.readium.r2.shared.Publication
import kotlin.coroutines.CoroutineContext


class R2CbzActivity : AppCompatActivity(), CoroutineScope {
class R2CbzActivity : AppCompatActivity(), CoroutineScope, R2ActivityListener {
/**
* Context of this scope.
*/
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main

private lateinit var preferences: SharedPreferences
lateinit var resourcePager: R2ViewPager
override lateinit var preferences: SharedPreferences
override lateinit var resourcePager: R2ViewPager
var resources = arrayListOf<String>()

private lateinit var publicationPath: String
private lateinit var publication: Publication
override lateinit var publication: Publication
private lateinit var cbzName: String
private lateinit var publicationIdentifier: String
override lateinit var publicationIdentifier: String

var allowToggleActionBar = true
override var allowToggleActionBar = true

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -54,7 +54,7 @@ class R2CbzActivity : AppCompatActivity(), CoroutineScope {
cbzName = intent.getStringExtra("cbzName")
publicationIdentifier = publication.metadata.identifier
title = publication.metadata.title

for (link in publication.pageList) {
resources.add(link.href.toString())
}
Expand Down Expand Up @@ -96,7 +96,7 @@ class R2CbzActivity : AppCompatActivity(), CoroutineScope {
preferences.edit().putInt("$publicationIdentifier-document", documentIndex).apply()
}

fun nextResource(v: View? = null) {
override fun nextResource(v: View?) {
launch {
if (layoutDirectionIsRTL()) {
// The view has RTL layout
Expand All @@ -108,7 +108,7 @@ class R2CbzActivity : AppCompatActivity(), CoroutineScope {
}
}

fun previousResource(v: View? = null) {
override fun previousResource(v: View?) {
launch {
if (layoutDirectionIsRTL()) {
// The view has RTL layout
Expand All @@ -121,7 +121,7 @@ class R2CbzActivity : AppCompatActivity(), CoroutineScope {
}
}

fun toggleActionBar(v: View? = null) {
override fun toggleActionBar(v: View?) {
if (allowToggleActionBar) {
launch {
if (supportActionBar!!.isShowing) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Module: r2-navigator-kotlin
* Developers: Aferdita Muriqi
*
* Copyright (c) 2018. Readium Foundation. All rights reserved.
* Use of this source code is governed by a BSD-style license which is detailed in the
* LICENSE file present in the project repository where this source code is maintained.
*/

package org.readium.r2.navigator

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import androidx.webkit.WebViewClientCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.readium.r2.navigator.pager.R2BasicWebView
import org.readium.r2.shared.Publication
import kotlin.coroutines.CoroutineContext


open class R2DiViNaActivity : AppCompatActivity(), CoroutineScope, R2ActivityListener {

/**
* Context of this scope.
*/
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main

override lateinit var preferences: SharedPreferences
override lateinit var publication: Publication
override lateinit var publicationIdentifier: String

lateinit var divinaWebView: R2BasicWebView
private lateinit var publicationPath: String
private lateinit var zipName: String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_r2_divina)

preferences = getSharedPreferences("org.readium.r2.settings", Context.MODE_PRIVATE)
divinaWebView = findViewById(R.id.divinaWebView)
divinaWebView.activity = this
divinaWebView.listener = this

publicationPath = intent.getStringExtra("publicationPath")
publication = intent.getSerializableExtra("publication") as Publication
zipName = intent.getStringExtra("zipName")

publicationIdentifier = publication.metadata.identifier
title = publication.metadata.title

toggleActionBar()

// Set up divinaWebView to enable JavaScript and access to local URLs
divinaWebView.getSettings().setJavaScriptEnabled(true)
divinaWebView.getSettings().setAllowFileAccess(true)
divinaWebView.getSettings().setAllowFileAccessFromFileURLs(true)
divinaWebView.webViewClient = object : WebViewClientCompat() {

override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
// Define the JS toggleMenu function that will call Android's toggleActionBar
// divinaWebView.evaluateJavascript("window.androidObj = function AndroidClass(){};", null)
// divinaWebView.evaluateJavascript("window.androidObj.toggleMenu = function() { Android.toggleMenu() };", null)

// Now launch the DiViNa player for the folderPath = publicationPath
divinaWebView.evaluateJavascript("if (player) { player.openDiViNaFromPath('${publicationPath}'); };", null)
}
}
divinaWebView.loadUrl("file:///android_asset/divina/divinaPlayer.html")
divinaWebView.addJavascriptInterface(divinaWebView, "Android")

}

override fun toggleActionBar() {
launch {
if (supportActionBar!!.isShowing) {
divinaWebView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
or View.SYSTEM_UI_FLAG_IMMERSIVE)
} else {
divinaWebView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
}
}
}

override fun onDestroy() {
super.onDestroy()
divinaWebView.evaluateJavascript("if (player) { player.destroy(); };", null)
}
}

Loading

0 comments on commit 90476b5

Please sign in to comment.