Skip to content

Commit 1e9fb07

Browse files
feat: Play videos in the app #2109
Add a fragment which plays YouTube videos in the app YoutubeFragment.kt: A the fragment to play Youtube Videos FullscreenListener.kt: Listens for full screen event fragment_youtube.xml: Fragment layout Add a callback (ChatCallback) which plays youtube fragment from the ChatActivity REQUIRED: Give YouTube API key for proper functioning
1 parent ed89462 commit 1e9fb07

File tree

11 files changed

+158
-16
lines changed

11 files changed

+158
-16
lines changed

app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import timber.log.Timber
6565
* Created by chiragw15 on 9/7/17.
6666
*/
6767
@Suppress("UNUSED_PARAMETER", "DEPRECATION")
68-
class ChatActivity : AppCompatActivity(), IChatView {
68+
class ChatActivity : AppCompatActivity(), IChatView, ChatCallback {
6969

7070
private val chatPresenter: IChatPresenter by inject { parametersOf(this) }
7171
lateinit var youtubeVid: IYoutubeVid
@@ -90,6 +90,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
9090
}
9191
}
9292

93+
@SuppressLint("RestrictedApi")
9394
override fun onCreate(savedInstanceState: Bundle?) {
9495
super.onCreate(savedInstanceState)
9596
setContentView(R.layout.activity_chat)
@@ -316,7 +317,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
316317

317318
rv_chat_feed.layoutManager = linearLayoutManager
318319
rv_chat_feed.setHasFixedSize(false)
319-
recyclerAdapter = ChatFeedRecyclerAdapter(this, chatMessageDatabaseList, true)
320+
recyclerAdapter = ChatFeedRecyclerAdapter(this, chatMessageDatabaseList, true, this)
320321
rv_chat_feed.adapter = recyclerAdapter
321322

322323
rv_chat_feed.addOnLayoutChangeListener { _, _, _, _, bottom, _, _, _, oldBottom ->
@@ -364,6 +365,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
364365
}
365366

366367
// Take user's speech as input and send the message
368+
@SuppressLint("RestrictedApi")
367369
override fun promptSpeechInput() {
368370
if (recordingThread != null) {
369371
chatPresenter.stopHotwordDetection()
@@ -379,6 +381,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
379381
}
380382

381383
// Replies user with Speech
384+
@SuppressLint("RestrictedApi")
382385
override fun voiceReply(reply: String, language: String) {
383386
searchChat.visibility = View.VISIBLE
384387
fabsetting.visibility = View.VISIBLE
@@ -631,12 +634,20 @@ class ChatActivity : AppCompatActivity(), IChatView {
631634
chatPresenter.checkPreferences()
632635
}
633636

634-
override fun playVideo(videoId: String) {
637+
override fun playVideo(videoId: String?) {
635638
Timber.d(videoId)
636-
youtubeVid.playYoutubeVid(videoId)
639+
var bundle = Bundle()
640+
bundle.putString("video", videoId)
641+
var youtubeFragment = YoutubeFragment.newInstance()
642+
youtubeFragment.arguments = bundle
643+
supportFragmentManager.beginTransaction().add(R.id.coordinator_layout, youtubeFragment, "youtube").commit()
637644
}
638645

639646
companion object {
640647
val ALARM = "ALARM"
641648
}
649+
650+
override fun onYoutubeClicked(videoId: String?) {
651+
playVideo(videoId)
652+
}
642653
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.fossasia.susi.ai.chat
2+
3+
interface ChatCallback {
4+
fun onYoutubeClicked(videoId: String?)
5+
}

app/src/main/java/org/fossasia/susi/ai/chat/ChatPresenter.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class ChatPresenter(context: Context, private val chatView: IChatView?) :
6868
var id: Long = 0
6969
var identifier: String = ""
7070
var tableItem: TableItem? = null
71-
private val youtubeVid: IYoutubeVid = YoutubeVid(context)
7271
private var textToSpeech: TextToSpeech? = null
7372
private val context: Context = context
7473
private lateinit var handler: Handler
@@ -503,7 +502,7 @@ class ChatPresenter(context: Context, private val chatView: IChatView?) :
503502
} else if (parseSusiHelper.actionType == Constant.VIDEOPLAY || parseSusiHelper.actionType == Constant.AUDIOPLAY) {
504503
// Play youtube video
505504
identifier = parseSusiHelper.identifier
506-
youtubeVid.playYoutubeVid(identifier)
505+
chatView?.playVideo(identifier)
507506
} else if (parseSusiHelper.actionType == Constant.ANSWER && (PrefManager.checkSpeechOutputPref() && check || PrefManager.checkSpeechAlwaysPref())) {
508507
setMessage = parseSusiHelper.answer
509508
try {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.fossasia.susi.ai.chat
2+
3+
import android.content.pm.PackageManager
4+
import android.os.Bundle
5+
import android.support.v4.app.Fragment
6+
import android.support.v4.app.FragmentTransaction
7+
import android.view.LayoutInflater
8+
import android.view.View
9+
import android.view.ViewGroup
10+
import com.google.android.youtube.player.YouTubeInitializationResult
11+
import com.google.android.youtube.player.YouTubePlayer
12+
import com.google.android.youtube.player.YouTubePlayerSupportFragment
13+
import org.fossasia.susi.ai.R
14+
import org.fossasia.susi.ai.chat.listeners.FullscreenListener
15+
16+
class YoutubeFragment : Fragment() {
17+
private var API_KEY: String? = null
18+
private var m_RootView: View? = null
19+
private var m_StopButton: View? = null
20+
var YPlayer: YouTubePlayer? = null
21+
private var VIDEO: String = ""
22+
23+
override fun onCreateView(
24+
inflater: LayoutInflater,
25+
container: ViewGroup?,
26+
savedInstanceState: Bundle?
27+
): View? {
28+
var rootView = inflater.inflate(R.layout.fragment_youtube, container, false)
29+
m_RootView = rootView
30+
m_StopButton = m_RootView?.findViewById(R.id.youtube_stop_btn)
31+
var bundle = activity?.packageManager?.getApplicationInfo(activity?.applicationContext?.packageName, PackageManager.GET_META_DATA)?.metaData
32+
API_KEY = bundle?.getString("com.google.android.youtube.API_KEY")
33+
if (arguments != null) {
34+
VIDEO = arguments!!.getString("video")
35+
}
36+
m_StopButton?.setOnClickListener({
37+
YPlayer?.release()
38+
activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
39+
})
40+
initYoutube()
41+
return rootView
42+
}
43+
44+
private fun initYoutube() {
45+
val youTubePlayerFragment: YouTubePlayerSupportFragment =
46+
YouTubePlayerSupportFragment.newInstance()
47+
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
48+
transaction.add(R.id.youtube_fragment, youTubePlayerFragment as Fragment)
49+
transaction.commit()
50+
youTubePlayerFragment.initialize(API_KEY, object : YouTubePlayer.OnInitializedListener {
51+
52+
override fun onInitializationSuccess(
53+
provider: YouTubePlayer.Provider?,
54+
player: YouTubePlayer,
55+
wasRestored: Boolean
56+
) {
57+
if (!wasRestored) {
58+
YPlayer = player
59+
YPlayer?.loadVideo(VIDEO)
60+
YPlayer?.setOnFullscreenListener(FullscreenListener(activity, this@YoutubeFragment))
61+
YPlayer?.play()
62+
}
63+
}
64+
65+
override fun onInitializationFailure(
66+
arg0: YouTubePlayer.Provider?,
67+
arg1: YouTubeInitializationResult?
68+
) {
69+
}
70+
})
71+
}
72+
companion object {
73+
fun newInstance() = YoutubeFragment()
74+
}
75+
}

app/src/main/java/org/fossasia/susi/ai/chat/adapters/recycleradapters/ChatFeedRecyclerAdapter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.realm.RealmRecyclerViewAdapter
2121
import io.realm.RealmResults
2222
import java.util.ArrayList
2323
import org.fossasia.susi.ai.R
24+
import org.fossasia.susi.ai.chat.ChatCallback
2425
import org.fossasia.susi.ai.chat.adapters.viewholders.ChatViewHolder
2526
import org.fossasia.susi.ai.chat.adapters.viewholders.DateViewHolder
2627
import org.fossasia.susi.ai.chat.adapters.viewholders.ImageViewHolder
@@ -43,7 +44,8 @@ import org.fossasia.susi.ai.helper.ConstraintsHelper
4344
class ChatFeedRecyclerAdapter(
4445
private val currContext: Context,
4546
data: OrderedRealmCollection<ChatMessage>?,
46-
autoUpdate: Boolean
47+
autoUpdate: Boolean,
48+
private val chatCallback: ChatCallback
4749
) : RealmRecyclerViewAdapter<ChatMessage, RecyclerView.ViewHolder>(data, autoUpdate),
4850
MessageViewHolder.ClickListener {
4951
private var realm: Realm? = null
@@ -163,11 +165,11 @@ class ChatFeedRecyclerAdapter(
163165
STOP -> return nullHolder
164166
AUDIOPLAY -> {
165167
view = inflater.inflate(R.layout.youtube_video, viewGroup, false)
166-
return YoutubeVideoViewHolder(view, clickListener)
168+
return YoutubeVideoViewHolder(view, clickListener, chatCallback)
167169
}
168170
VIDEOPLAY -> {
169171
view = inflater.inflate(R.layout.youtube_video, viewGroup, false)
170-
return YoutubeVideoViewHolder(view, clickListener)
172+
return YoutubeVideoViewHolder(view, clickListener, chatCallback)
171173
}
172174
IMAGE -> {
173175
view = inflater.inflate(R.layout.image_holder, viewGroup, false)

app/src/main/java/org/fossasia/susi/ai/chat/adapters/viewholders/YoutubeVideoViewHolder.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.widget.ImageView
66
import com.squareup.picasso.Picasso
77
import kotterknife.bindView
88
import org.fossasia.susi.ai.R
9-
import org.fossasia.susi.ai.chat.YoutubeVid
9+
import org.fossasia.susi.ai.chat.ChatCallback
1010
import org.fossasia.susi.ai.data.model.ChatMessage
1111
import timber.log.Timber
1212

@@ -15,7 +15,8 @@ import timber.log.Timber
1515
*/
1616
class YoutubeVideoViewHolder(
1717
view: View,
18-
clickListener: MessageViewHolder.ClickListener
18+
clickListener: MessageViewHolder.ClickListener,
19+
private val callback: ChatCallback
1920
) :
2021
MessageViewHolder(view, clickListener) {
2122

@@ -41,8 +42,8 @@ class YoutubeVideoViewHolder(
4142
Timber.e(e)
4243
}
4344
}
44-
45-
val youtubeVid = YoutubeVid(itemView.context)
46-
playButton.setOnClickListener { videoId?.let { id -> youtubeVid.playYoutubeVid(id) } }
45+
playButton.setOnClickListener {
46+
callback.onYoutubeClicked(videoId)
47+
}
4748
}
4849
}

app/src/main/java/org/fossasia/susi/ai/chat/contract/IChatView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ interface IChatView {
2929
fun promptSpeechInput()
3030
fun finishActivity()
3131
fun stopMic()
32-
fun playVideo(videoId: String)
32+
fun playVideo(videoId: String?)
3333
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.fossasia.susi.ai.chat.listeners
2+
3+
import android.content.pm.ActivityInfo
4+
import android.support.v4.app.FragmentActivity
5+
import com.google.android.youtube.player.YouTubePlayer
6+
import org.fossasia.susi.ai.chat.YoutubeFragment
7+
8+
class FullscreenListener : YouTubePlayer.OnFullscreenListener {
9+
private var mContext: FragmentActivity? = null
10+
private var mFragment: YoutubeFragment? = null
11+
12+
constructor(context: FragmentActivity?, fragment: YoutubeFragment?) {
13+
mContext = context
14+
mFragment = fragment
15+
}
16+
17+
override fun onFullscreen(p0: Boolean) {
18+
if (!p0) {
19+
mContext?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
tools:context="org.fossasia.susi.ai.chat.YoutubeFragment">
7+
<Button
8+
android:layout_height="wrap_content"
9+
android:layout_width="wrap_content"
10+
android:text="@string/close"
11+
android:textColor="@color/md_black_1000"
12+
android:layout_alignParentRight="true"
13+
android:background="@color/translucent_black"
14+
android:id="@+id/youtube_stop_btn"
15+
android:layout_below="@id/youtube_fragment"/>
16+
<FrameLayout
17+
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
18+
android:layout_width="match_parent"
19+
android:id="@+id/youtube_fragment"
20+
android:layout_height="wrap_content"/>
21+
22+
</RelativeLayout>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,5 @@
330330

331331
<!-- Shimmer background-->
332332
<color name="shimmer_background">#dddddd</color>
333+
<color name="translucent_black">#80000000</color>
333334
</resources>

0 commit comments

Comments
 (0)