Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import timber.log.Timber
* Created by chiragw15 on 9/7/17.
*/
@Suppress("UNUSED_PARAMETER", "DEPRECATION")
class ChatActivity : AppCompatActivity(), IChatView {
class ChatActivity : AppCompatActivity(), IChatView, ChatCallback {

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

@SuppressLint("RestrictedApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
Expand Down Expand Up @@ -316,7 +317,7 @@ class ChatActivity : AppCompatActivity(), IChatView {

rv_chat_feed.layoutManager = linearLayoutManager
rv_chat_feed.setHasFixedSize(false)
recyclerAdapter = ChatFeedRecyclerAdapter(this, chatMessageDatabaseList, true)
recyclerAdapter = ChatFeedRecyclerAdapter(this, chatMessageDatabaseList, true, this)
rv_chat_feed.adapter = recyclerAdapter

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

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

// Replies user with Speech
@SuppressLint("RestrictedApi")
override fun voiceReply(reply: String, language: String) {
searchChat.visibility = View.VISIBLE
fabsetting.visibility = View.VISIBLE
Expand Down Expand Up @@ -631,12 +634,20 @@ class ChatActivity : AppCompatActivity(), IChatView {
chatPresenter.checkPreferences()
}

override fun playVideo(videoId: String) {
override fun playVideo(videoId: String?) {
Timber.d(videoId)
youtubeVid.playYoutubeVid(videoId)
var bundle = Bundle()
bundle.putString("video", videoId)
var youtubeFragment = YoutubeFragment.newInstance()
youtubeFragment.arguments = bundle
supportFragmentManager.beginTransaction().add(R.id.coordinator_layout, youtubeFragment, "youtube").commit()
}

companion object {
val ALARM = "ALARM"
}

override fun onYoutubeClicked(videoId: String?) {
playVideo(videoId)
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.fossasia.susi.ai.chat

interface ChatCallback {
fun onYoutubeClicked(videoId: String?)
}
3 changes: 1 addition & 2 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class ChatPresenter(context: Context, private val chatView: IChatView?) :
var id: Long = 0
var identifier: String = ""
var tableItem: TableItem? = null
private val youtubeVid: IYoutubeVid = YoutubeVid(context)
private var textToSpeech: TextToSpeech? = null
private val context: Context = context
private lateinit var handler: Handler
Expand Down Expand Up @@ -503,7 +502,7 @@ class ChatPresenter(context: Context, private val chatView: IChatView?) :
} else if (parseSusiHelper.actionType == Constant.VIDEOPLAY || parseSusiHelper.actionType == Constant.AUDIOPLAY) {
// Play youtube video
identifier = parseSusiHelper.identifier
youtubeVid.playYoutubeVid(identifier)
chatView?.playVideo(identifier)
} else if (parseSusiHelper.actionType == Constant.ANSWER && (PrefManager.checkSpeechOutputPref() && check || PrefManager.checkSpeechAlwaysPref())) {
setMessage = parseSusiHelper.answer
try {
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/chat/YoutubeFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.fossasia.susi.ai.chat

import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTransaction
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerSupportFragment
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.listeners.FullscreenListener

class YoutubeFragment : Fragment() {
private var API_KEY: String? = null
private var m_RootView: View? = null
private var m_StopButton: View? = null
var YPlayer: YouTubePlayer? = null
private var VIDEO: String = ""

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var rootView = inflater.inflate(R.layout.fragment_youtube, container, false)
m_RootView = rootView
m_StopButton = m_RootView?.findViewById(R.id.youtube_stop_btn)
var bundle = activity?.packageManager?.getApplicationInfo(activity?.applicationContext?.packageName, PackageManager.GET_META_DATA)?.metaData
API_KEY = bundle?.getString("com.google.android.youtube.API_KEY")
if (arguments != null) {
VIDEO = arguments!!.getString("video")
}
m_StopButton?.setOnClickListener({
YPlayer?.release()
activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
})
initYoutube()
return rootView
}

private fun initYoutube() {
val youTubePlayerFragment: YouTubePlayerSupportFragment =
YouTubePlayerSupportFragment.newInstance()
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
transaction.add(R.id.youtube_fragment, youTubePlayerFragment as Fragment)
transaction.commit()
youTubePlayerFragment.initialize(API_KEY, object : YouTubePlayer.OnInitializedListener {

override fun onInitializationSuccess(
provider: YouTubePlayer.Provider?,
player: YouTubePlayer,
wasRestored: Boolean
) {
if (!wasRestored) {
YPlayer = player
YPlayer?.loadVideo(VIDEO)
YPlayer?.setOnFullscreenListener(FullscreenListener(activity, this@YoutubeFragment))
YPlayer?.play()
}
}

override fun onInitializationFailure(
arg0: YouTubePlayer.Provider?,
arg1: YouTubeInitializationResult?
) {
}
})
}
companion object {
fun newInstance() = YoutubeFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.realm.RealmRecyclerViewAdapter
import io.realm.RealmResults
import java.util.ArrayList
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.ChatCallback
import org.fossasia.susi.ai.chat.adapters.viewholders.ChatViewHolder
import org.fossasia.susi.ai.chat.adapters.viewholders.DateViewHolder
import org.fossasia.susi.ai.chat.adapters.viewholders.ImageViewHolder
Expand All @@ -43,7 +44,8 @@ import org.fossasia.susi.ai.helper.ConstraintsHelper
class ChatFeedRecyclerAdapter(
private val currContext: Context,
data: OrderedRealmCollection<ChatMessage>?,
autoUpdate: Boolean
autoUpdate: Boolean,
private val chatCallback: ChatCallback
) : RealmRecyclerViewAdapter<ChatMessage, RecyclerView.ViewHolder>(data, autoUpdate),
MessageViewHolder.ClickListener {
private var realm: Realm? = null
Expand Down Expand Up @@ -163,11 +165,11 @@ class ChatFeedRecyclerAdapter(
STOP -> return nullHolder
AUDIOPLAY -> {
view = inflater.inflate(R.layout.youtube_video, viewGroup, false)
return YoutubeVideoViewHolder(view, clickListener)
return YoutubeVideoViewHolder(view, clickListener, chatCallback)
}
VIDEOPLAY -> {
view = inflater.inflate(R.layout.youtube_video, viewGroup, false)
return YoutubeVideoViewHolder(view, clickListener)
return YoutubeVideoViewHolder(view, clickListener, chatCallback)
}
IMAGE -> {
view = inflater.inflate(R.layout.image_holder, viewGroup, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.widget.ImageView
import com.squareup.picasso.Picasso
import kotterknife.bindView
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.YoutubeVid
import org.fossasia.susi.ai.chat.ChatCallback
import org.fossasia.susi.ai.data.model.ChatMessage
import timber.log.Timber

Expand All @@ -15,7 +15,8 @@ import timber.log.Timber
*/
class YoutubeVideoViewHolder(
view: View,
clickListener: MessageViewHolder.ClickListener
clickListener: MessageViewHolder.ClickListener,
private val callback: ChatCallback
) :
MessageViewHolder(view, clickListener) {

Expand All @@ -41,8 +42,8 @@ class YoutubeVideoViewHolder(
Timber.e(e)
}
}

val youtubeVid = YoutubeVid(itemView.context)
playButton.setOnClickListener { videoId?.let { id -> youtubeVid.playYoutubeVid(id) } }
playButton.setOnClickListener {
callback.onYoutubeClicked(videoId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ interface IChatView {
fun promptSpeechInput()
fun finishActivity()
fun stopMic()
fun playVideo(videoId: String)
fun playVideo(videoId: String?)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.fossasia.susi.ai.chat.listeners

import android.content.pm.ActivityInfo
import android.support.v4.app.FragmentActivity
import com.google.android.youtube.player.YouTubePlayer
import org.fossasia.susi.ai.chat.YoutubeFragment

class FullscreenListener : YouTubePlayer.OnFullscreenListener {
private var mContext: FragmentActivity? = null
private var mFragment: YoutubeFragment? = null

constructor(context: FragmentActivity?, fragment: YoutubeFragment?) {
mContext = context
mFragment = fragment
}

override fun onFullscreen(p0: Boolean) {
if (!p0) {
mContext?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/res/layout/fragment_youtube.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="org.fossasia.susi.ai.chat.YoutubeFragment">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/close"
android:textColor="@color/md_black_1000"
android:layout_alignParentRight="true"
android:background="@color/translucent_black"
android:id="@+id/youtube_stop_btn"
android:layout_below="@id/youtube_fragment"/>
<FrameLayout
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:layout_width="match_parent"
android:id="@+id/youtube_fragment"
android:layout_height="wrap_content"/>

</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,5 @@

<!-- Shimmer background-->
<color name="shimmer_background">#dddddd</color>
<color name="translucent_black">#80000000</color>
</resources>
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
<string name="device_setting_up">Setting up your device</string>
<string name="susi_ai_hotspot" translatable="false">SUSI.AI</string>
<string name="enter_wifi_password">Enter Wi-Fi password</string>

<!-- Device Setup Screen -->
<string name="connect">1. Connect</string>
<string name="wifi">2. Wi-Fi</string>
Expand Down Expand Up @@ -599,4 +599,8 @@
<string name="alarm_query_executed">Executed planned task</string>
<string name="set_plan_action">Plan action has been set</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="close">X</string>

</resources>