-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e8050a
commit a6c8431
Showing
19 changed files
with
411 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
app/src/main/java/ru/BShakhovsky/Piano_Transcription/Web/WebActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
@file:Suppress("PackageName") | ||
|
||
package ru.BShakhovsky.Piano_Transcription.Web | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.github.kittinunf.fuel.core.Response | ||
import com.github.kittinunf.fuel.httpGet | ||
import com.github.kittinunf.result.Result | ||
import com.google.android.material.snackbar.Snackbar | ||
import com.google.gson.Gson | ||
import com.google.gson.JsonSyntaxException | ||
|
||
import kotlinx.android.synthetic.main.activity_web.download | ||
import kotlinx.android.synthetic.main.activity_web.goBack | ||
import kotlinx.android.synthetic.main.activity_web.goForward | ||
import kotlinx.android.synthetic.main.activity_web.goHome | ||
|
||
import kotlinx.android.synthetic.main.content_web.web | ||
import kotlinx.android.synthetic.main.content_web.webText | ||
|
||
import ru.BShakhovsky.Piano_Transcription.DebugMode | ||
import ru.BShakhovsky.Piano_Transcription.R | ||
import java.net.URLDecoder | ||
|
||
class WebActivity : AppCompatActivity(), View.OnClickListener { | ||
|
||
data class AdaptiveFormat( | ||
val url: String, val mimeType: String, @Suppress("SpellCheckingInspection") val itag: Int, | ||
val averageBitrate: Int, val audioSampleRate: String, val contentsLength: String | ||
) | ||
|
||
data class Thumbnail(val url: String, val width: String, val height: String) | ||
data class Thumbnails(val thumbnails: List<Thumbnail>) | ||
|
||
data class StreamingData(val adaptiveFormats: List<AdaptiveFormat>) | ||
data class PlayabilityStatus(val status: String) | ||
data class VideoDetails(val title: String, val author: String, val thumbnail: Thumbnails) | ||
|
||
data class PlayerResponse( | ||
val streamingData: StreamingData?, | ||
val playabilityStatus: PlayabilityStatus, val videoDetails: VideoDetails | ||
) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_web) | ||
|
||
with(web) { | ||
@SuppressLint("SetJavaScriptEnabled") | ||
settings.javaScriptEnabled = true | ||
|
||
savedInstanceState?.let { | ||
web.restoreState(it) | ||
return@with | ||
} | ||
|
||
with(intent.extras) { | ||
(if (this == null) "https://youtu.be" | ||
else get(Intent.EXTRA_TEXT).toString().also { | ||
if (!intent.hasExtra(Intent.EXTRA_SUBJECT)) Snackbar.make( | ||
web, getString(R.string.notUrl, it), Snackbar.LENGTH_LONG | ||
).show() | ||
}).also { | ||
webViewClient = WebClient(it, webText) | ||
loadUrl(it) | ||
} | ||
} | ||
} | ||
|
||
arrayOf(goHome, goBack, goForward, download).forEach { it.setOnClickListener(this) } | ||
} | ||
|
||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
web.saveState(outState) | ||
} | ||
|
||
override fun onRestoreInstanceState(savedInstanceState: Bundle) { | ||
super.onRestoreInstanceState(savedInstanceState) | ||
web.restoreState(savedInstanceState) | ||
} | ||
|
||
override fun onClick(view: View?) { | ||
DebugMode.assertArgument(view != null) | ||
with(web) { | ||
when (view?.id) { | ||
R.id.goHome -> super.onBackPressed() | ||
R.id.goBack -> if (canGoBack()) goBack() | ||
R.id.goForward -> if (canGoForward()) goForward() | ||
R.id.download -> tryDownload() | ||
else -> DebugMode.assertState(false) | ||
} | ||
} | ||
} | ||
|
||
override fun onBackPressed() { | ||
with(web) { if (canGoBack()) goBack() else super.onBackPressed() } | ||
} | ||
|
||
private fun tryDownload() { | ||
DebugMode.assertState(web.url != null) | ||
web.url?.let { url -> | ||
@Suppress("RegExpAnonymousGroup") when { | ||
Regex("""^https?:https://(www\.)?(m\.)?youtube\.com""").containsMatchIn(url) -> | ||
Regex("""v=[\w\-]{11}""").find(url)?.value?.substring(2) | ||
@Suppress("SpellCheckingInspection") | ||
Regex("""^https?:https://(www\.)?(m\.)?youtu.be/""").containsMatchIn(url) -> | ||
@Suppress("SpellCheckingInspection") | ||
Regex("""youtu.be/[\w\-]{11}""").find(url)?.value?.substring(9) | ||
else -> null | ||
}.also { id -> | ||
if (id == null) | ||
Snackbar.make(web, R.string.noLink, Snackbar.LENGTH_LONG).show() | ||
else "https://www.youtube.com/get_video_info?video_id=$id" | ||
.httpGet().response { _, response, result -> | ||
when (result) { | ||
is Result.Failure -> { | ||
Snackbar.make(web, R.string.linkFailed, Snackbar.LENGTH_LONG).show() | ||
return@response | ||
} | ||
is Result.Success -> startDownload(response) | ||
else -> DebugMode.assertState(false) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun startDownload(response: Response) { | ||
try { | ||
var playerResponse = "" | ||
for (p in response.toString().split('&')) | ||
p.split('=').also { if (it[0] == "player_response") playerResponse = it[1] } | ||
val parsedJson = Gson().fromJson( | ||
URLDecoder.decode(playerResponse, "UTF-8"), PlayerResponse::class.java | ||
) | ||
if (parsedJson == null) { | ||
Snackbar.make(web, R.string.copyrightProtected, Snackbar.LENGTH_LONG).show() | ||
return | ||
} | ||
// if (parsedJson.playabilityStatus.status == "UNPLAYABLE") { | ||
if (parsedJson.streamingData == null) { | ||
Snackbar.make(web, R.string.copyrightProtected, Snackbar.LENGTH_LONG).show() | ||
return | ||
} | ||
|
||
var maxITag = 0 | ||
var bestQualityAudioFormat: AdaptiveFormat? = null | ||
for (adaptiveFormat in parsedJson.streamingData.adaptiveFormats) | ||
if ((adaptiveFormat.mimeType.startsWith("audio")) | ||
and (adaptiveFormat.itag >= maxITag) | ||
) { | ||
maxITag = adaptiveFormat.itag | ||
bestQualityAudioFormat = adaptiveFormat | ||
} | ||
|
||
Snackbar.make( | ||
// Uri.parse(bestQualityAudioFormat.url).toString() | ||
web, bestQualityAudioFormat?.url | ||
?: getString(R.string.noAudioStream), Snackbar.LENGTH_LONG | ||
).show() | ||
} catch (e: JsonSyntaxException) { | ||
Snackbar.make(web, R.string.notJson, Snackbar.LENGTH_LONG).show() | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/ru/BShakhovsky/Piano_Transcription/Web/WebClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@file:Suppress("PackageName") | ||
|
||
package ru.BShakhovsky.Piano_Transcription.Web | ||
|
||
import android.view.View | ||
|
||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import android.webkit.WebResourceRequest | ||
|
||
import android.widget.TextView | ||
|
||
import ru.BShakhovsky.Piano_Transcription.DebugMode | ||
import ru.BShakhovsky.Piano_Transcription.R | ||
|
||
class WebClient(private var requestedUrl: String, private var webText: TextView) : WebViewClient() { | ||
|
||
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { | ||
DebugMode.assertArgument((view != null) and (request != null)) | ||
request?.url.toString().also { | ||
requestedUrl = it | ||
view?.loadUrl(it) | ||
} | ||
return super.shouldOverrideUrlLoading(view, request) | ||
} | ||
|
||
override fun onPageFinished(view: WebView?, url: String?) { | ||
super.onPageFinished(view, url) | ||
DebugMode.assertArgument((view != null) and (url != null)) | ||
if (url == "about:blank") with(webText) { | ||
visibility = View.VISIBLE | ||
text = context.getString(R.string.aboutBlank, requestedUrl) | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<vector xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
android:width="24dp" android:height="24dp" | ||
android:viewportWidth="24" android:viewportHeight="24"> | ||
<!--suppress LongLine --> | ||
<path android:fillColor="#FF000000" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<vector xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
android:width="24dp" android:height="24dp" | ||
android:viewportWidth="24" android:viewportHeight="24"> | ||
<!--suppress LongLine --> | ||
<path android:fillColor="#FF000000" android:pathData="M10.09,15.59L11.5,17l5,-5 -5,-5 -1.41,1.41L12.67,11H3v2h9.67l-2.58,2.59zM19,3H5c-1.11,0 -2,0.9 -2,2v4h2V5h14v14H5v-4H3v4c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<vector xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
android:width="24dp" android:height="24dp" | ||
android:viewportWidth="24" android:viewportHeight="24"> | ||
<!--suppress LongLine --> | ||
<path android:fillColor="#FF000000" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM17,13l-5,5 -5,-5h3V9h4v4h3z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<vector xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
android:width="24dp" android:height="24dp" | ||
android:viewportWidth="24" android:viewportHeight="24"> | ||
<!--suppress LongLine --> | ||
<path android:fillColor="#FF000000" android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
android:color="@color/colorAccent" /> | ||
|
||
<corners android:radius="24dp" /> | ||
|
||
</shape> |
Oops, something went wrong.