-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Showing
52 changed files
with
752 additions
and
121 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
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
86 changes: 86 additions & 0 deletions
86
app/src/main/java/com/zionhuang/music/ui/fragments/WebViewFragment.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,86 @@ | ||
package com.zionhuang.music.ui.fragments | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import android.view.View | ||
import android.webkit.CookieManager | ||
import android.webkit.JavascriptInterface | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.core.content.edit | ||
import com.zionhuang.innertube.YouTube | ||
import com.zionhuang.music.constants.Constants.ACCOUNT_EMAIL | ||
import com.zionhuang.music.constants.Constants.ACCOUNT_NAME | ||
import com.zionhuang.music.constants.Constants.INNERTUBE_COOKIE | ||
import com.zionhuang.music.constants.Constants.VISITOR_DATA | ||
import com.zionhuang.music.databinding.FragmentWebviewBinding | ||
import com.zionhuang.music.extensions.sharedPreferences | ||
import com.zionhuang.music.ui.fragments.base.BindingFragment | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
|
||
class WebViewFragment : BindingFragment<FragmentWebviewBinding>() { | ||
override fun getViewBinding() = FragmentWebviewBinding.inflate(layoutInflater) | ||
|
||
@OptIn(DelicateCoroutinesApi::class) | ||
private val webViewClient = object : WebViewClient() { | ||
override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) { | ||
if (url.startsWith("https://music.youtube.com")) { | ||
val cookies = CookieManager.getInstance().getCookie(url) | ||
if (sharedPreferences.getString(INNERTUBE_COOKIE, null) != cookies) { | ||
sharedPreferences.edit { | ||
putString(INNERTUBE_COOKIE, cookies) | ||
} | ||
GlobalScope.launch { | ||
YouTube.getAccountInfo().onSuccess { | ||
sharedPreferences.edit { | ||
putString(ACCOUNT_NAME, it?.name) | ||
putString(ACCOUNT_EMAIL, it?.email) | ||
} | ||
}.onFailure { | ||
it.printStackTrace() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onPageFinished(view: WebView, url: String?) { | ||
binding.webview.loadUrl("javascript:Android.onRetrieveVisitorData(window.yt.config_.VISITOR_DATA)") | ||
} | ||
} | ||
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
binding.webview.apply { | ||
if (savedInstanceState != null) { | ||
restoreState(savedInstanceState) | ||
} else { | ||
loadUrl("https://accounts.google.com/ServiceLogin?ltmpl=music&service=youtube&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26next%3Dhttps%253A%252F%252Fmusic.youtube.com%252F") | ||
} | ||
webViewClient = this@WebViewFragment.webViewClient | ||
settings.apply { | ||
javaScriptEnabled = true | ||
setSupportZoom(true) | ||
builtInZoomControls = true | ||
} | ||
addJavascriptInterface(this@WebViewFragment, "Android") | ||
} | ||
} | ||
|
||
@JavascriptInterface | ||
fun onRetrieveVisitorData(visitorData: String?) { | ||
if (visitorData != null && sharedPreferences.getString(VISITOR_DATA, null) != visitorData) { | ||
sharedPreferences.edit { | ||
putString(VISITOR_DATA, visitorData) | ||
} | ||
} | ||
} | ||
|
||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
binding.webview.saveState(outState) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="?colorControlNormal" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z" /> | ||
</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
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,11 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:tint="?colorControlNormal" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:fillType="evenOdd" | ||
android:pathData="M21,1L3,1c-1.1,0 -2,0.9 -2,2v18c0,1.1 0.9,2 2,2h18c1.1,0 2,-0.9 2,-2L23,3c0,-1.1 -0.9,-2 -2,-2zM10.59,9.17L5.41,4 4,5.41l5.17,5.17 1.42,-1.41zM14.5,4l2.04,2.04L4,18.59 5.41,20 17.96,7.46 20,9.5L20,4h-5.5zM14.83,13.41l-1.41,1.41 3.13,3.13L14.5,20L20,20v-5.5l-2.04,2.04 -3.13,-3.13z" /> | ||
</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,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<WebView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/webview" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> |
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
Oops, something went wrong.