diff --git a/app/src/main/java/com/naman14/timber/nowplaying/BaseNowplayingFragment.java b/app/src/main/java/com/naman14/timber/nowplaying/BaseNowplayingFragment.java index 43cbf3e4a..ecacc8ad6 100644 --- a/app/src/main/java/com/naman14/timber/nowplaying/BaseNowplayingFragment.java +++ b/app/src/main/java/com/naman14/timber/nowplaying/BaseNowplayingFragment.java @@ -15,6 +15,7 @@ package com.naman14.timber.nowplaying; import android.animation.ObjectAnimator; +import android.content.Intent; import android.graphics.Bitmap; import android.graphics.PorterDuff; import android.os.AsyncTask; @@ -30,7 +31,9 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; -import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.SeekBar; @@ -221,6 +224,51 @@ public void onCreate(@Nullable Bundle savedInstanceState) { accentColor = Config.accentColor(getActivity(), ateKey); } + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + inflater.inflate(R.menu.now_playing, menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_search_videos: + pauseMusicPlayer(); + openSongSearchOnYouTubeApp(); + break; + } + return super.onOptionsItemSelected(item); + } + + private void pauseMusicPlayer() { + if (MusicPlayer.isPlaying()) { + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + MusicPlayer.playOrPause(); + if (recyclerView != null && recyclerView.getAdapter() != null) + recyclerView.getAdapter().notifyDataSetChanged(); + } + }, 250); + } + } + + private void openSongSearchOnYouTubeApp() { + Intent intent = new Intent(Intent.ACTION_SEARCH); + intent.setPackage("com.google.android.youtube"); + intent.putExtra("query", MusicPlayer.getArtistName() + " " + MusicPlayer.getTrackName()); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + } + @Override public void onPause() { super.onPause(); diff --git a/app/src/main/res/menu/now_playing.xml b/app/src/main/res/menu/now_playing.xml new file mode 100644 index 000000000..f2b19ede1 --- /dev/null +++ b/app/src/main/res/menu/now_playing.xml @@ -0,0 +1,6 @@ + +
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 96ff31595..b7c74ea22 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,4 +156,5 @@