Skip to content

Commit

Permalink
feat: New YT Player, 0.9.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Oct 28, 2018
1 parent bbb2c94 commit 628dac5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<activity android:name=".ui.modules.input.link.edit.LinkCommentEditActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name=".ui.modules.DeepLinkActivity"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateUnchanged|adjustResize">
<intent-filter>
Expand Down Expand Up @@ -142,6 +143,7 @@
</activity>
<activity android:name=".ui.modules.embedview.YoutubeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="sensor"
android:theme="@style/FullscreenActivityTheme"/>

<receiver android:name=".ui.modules.notifications.notificationsservice.WykopNotificationsBroadcastReceiver">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.feelfreelinux.wykopmobilny.ui.modules

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.github.feelfreelinux.wykopmobilny.utils.wykop_link_handler.WykopLinkHandler
Expand All @@ -9,6 +10,8 @@ class DeepLinkActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val url = intent.dataString!!
val activityToOpen = WykopLinkHandler.getLinkIntent(url, this)
activityToOpen?.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(WykopLinkHandler.getLinkIntent(url, this))
finish()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.feelfreelinux.wykopmobilny.ui.modules.embedview
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import com.google.android.youtube.player.YouTubeBaseActivity
import com.google.android.youtube.player.YouTubeInitializationResult
Expand All @@ -11,27 +12,76 @@ import io.github.feelfreelinux.wykopmobilny.GOOGLE_KEY
import io.github.feelfreelinux.wykopmobilny.R
import kotlinx.android.synthetic.main.activity_youtubeplayer.*
import java.util.regex.Pattern

class YoutubeActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListener {
import android.content.pm.ActivityInfo
import android.os.Build
import android.annotation.SuppressLint
import android.R.attr.orientation
import android.app.Activity
import android.content.res.Configuration
import com.google.android.youtube.player.YouTubeStandalonePlayer.createVideoIntent
private val youTubeUrlRegEx = "^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))/"
private val videoIdRegex = arrayOf("\\?vi?=([^&]*)", "watch\\?.*v=([^&]*)", "(?:embed|vi?)/([^/?]*)", "^([A-Za-z0-9_\\-]*)")

class YoutubeActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListener, YouTubePlayer.OnFullscreenListener {
override fun onFullscreen(fullScreen: Boolean) {
if (fullScreen) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
} else {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
}
}

companion object {
const val REQUEST_CODE_INITIALIZATION_ERROR = 172
const val EXTRA_URL = "URLEXTRA"

fun createIntent(context: Context, url: String) =
Intent(context, YoutubeActivity::class.java).apply {
putExtra(EXTRA_URL, url)
(createVideoIntent(context as Activity, GOOGLE_KEY, extractVideoIdFromUrl(url.replace("m.", "")), 0, true, false))


private fun extractVideoIdFromUrl(url: String): String? {
val youTubeLinkWithoutProtocolAndDomain = youTubeLinkWithoutProtocolAndDomain(url)

for (regex in videoIdRegex) {
val compiledPattern = Pattern.compile(regex)
val matcher = compiledPattern.matcher(youTubeLinkWithoutProtocolAndDomain)

if (matcher.find()) {
return matcher.group(1)
}
}

return null
}


fun youTubeLinkWithoutProtocolAndDomain(fullurl: String): String {
val url = fullurl.replace("m.", "")
val compiledPattern = Pattern.compile(youTubeUrlRegEx)
val matcher = compiledPattern.matcher(url)

return if (matcher.find()) {
url.replace(matcher.group(), "")
} else url
}
}

private val youTubeUrlRegEx = "^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))/"
private val videoIdRegex = arrayOf("\\?vi?=([^&]*)", "watch\\?.*v=([^&]*)", "(?:embed|vi?)/([^/?]*)", "^([A-Za-z0-9_\\-]*)")
val autoRotation by lazy {
Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
}


private val extraURL by lazy { intent.getStringExtra(EXTRA_URL) }
var player: YouTubePlayer? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_youtubeplayer)
youtubePlayer.initialize(GOOGLE_KEY, this)
/* //youtubePlayer.initialize(GOOGLE_KEY, this)
startActivity(createVideoIntent(this, GOOGLE_KEY, extractVideoIdFromUrl(extraURL.replace("m.", "")), 0, true, true))
finish()*/
}

override fun onInitializationFailure(provider: YouTubePlayer.Provider?, result: YouTubeInitializationResult?) {
Expand All @@ -46,40 +96,36 @@ class YoutubeActivity : YouTubeBaseActivity(), YouTubePlayer.OnInitializedListen

override fun onInitializationSuccess(provider: YouTubePlayer.Provider?, player: YouTubePlayer?, restored: Boolean) {
if (!restored) {
this.player = player
player?.prepare()
player?.loadVideo(extractVideoIdFromUrl(extraURL.replace("m.", "")))
}
}

fun YouTubePlayer.prepare() {
fullscreenControlFlags = YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION or YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE or
YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
setOnFullscreenListener(this@YoutubeActivity)
if (autoRotation) {
addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
or YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
or YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE
or YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT)
} else {
addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
or YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
or YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT)
}
setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT)
}

private fun extractVideoIdFromUrl(url: String): String? {
val youTubeLinkWithoutProtocolAndDomain = youTubeLinkWithoutProtocolAndDomain(url)

for (regex in videoIdRegex) {
val compiledPattern = Pattern.compile(regex)
val matcher = compiledPattern.matcher(youTubeLinkWithoutProtocolAndDomain)
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

if (matcher.find()) {
return matcher.group(1)
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.player?.setFullscreen(true)
}
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
this.player?.setFullscreen(false)
}

return null
}

private fun youTubeLinkWithoutProtocolAndDomain(fullurl: String): String {
val url = fullurl.replace("m.", "")
val compiledPattern = Pattern.compile(youTubeUrlRegEx)
val matcher = compiledPattern.matcher(url)

return if (matcher.find()) {
url.replace(matcher.group(), "")
} else url
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ allprojects {
project.ext {
versionMajor = 0
versionMinor = 9
versionPatch = 6
versionBuild = 3
versionPatch = 7
versionBuild = 0
}
}

Expand Down

0 comments on commit 628dac5

Please sign in to comment.