Skip to content

Commit

Permalink
use flavors for youtube player api
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Development committed Jun 20, 2017
1 parent 8856d2f commit ccba687
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 50 deletions.
11 changes: 10 additions & 1 deletion News-Android-App/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ android {
}
}

productFlavors {
// 100% Open-Source Edition
oss {
}
// 99% Open-source edition: uses the YouTube Player Jar
extra {
}
}

lintOptions {
// translations are imported from transifex, so no need to check here
disable 'MissingTranslation'
Expand Down Expand Up @@ -114,7 +123,7 @@ dependencies {
compile project(':MaterialShowcaseView:library')
compile 'com.nbsp:library:1.02' // MaterialFilePicker

compile 'com.github.tommus:youtube-android-player-api:1.2.2'
extraCompile 'com.github.tommus:youtube-android-player-api:1.2.2'

testCompile 'org.robolectric:robolectric:3.0-rc3'
testCompile 'junit:junit:4.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.luhmer.owncloudnewsreader;

import android.app.Activity;
import android.app.FragmentTransaction;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

import org.greenrobot.eventbus.EventBus;

import de.luhmer.owncloudnewsreader.events.podcast.RegisterYoutubeOutput;

public class YoutubePlayerManager {

public static void StartYoutubePlayer(final Activity activity, int YOUTUBE_CONTENT_VIEW_ID, final EventBus eventBus, final Runnable onInitSuccess) {
YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
ft.add(YOUTUBE_CONTENT_VIEW_ID, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize("AIzaSyA2OHKWvF_hRVtPmLcwnO8yF6-iah2hjbk", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
eventBus.post(new RegisterYoutubeOutput(youTubePlayer, wasRestored));
onInitSuccess.run();
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
youTubeInitializationResult.getErrorDialog(activity, 0).show();
//Toast.makeText(activity, "Error while playing youtube video! (InitializationFailure)", Toast.LENGTH_LONG).show();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ public VideoType getVideoType() {
return VideoType.YouTube;
}

public void setYoutubePlayer(YouTubePlayer youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
youTubePlayer.setPlaybackEventListener(youtubePlaybackEventListener);
youTubePlayer.setPlayerStateChangeListener(youtubePlayerStateChangeListener);
public void setYoutubePlayer(Object youTubePlayer, boolean wasRestored) {
this.youTubePlayer = (YouTubePlayer) youTubePlayer;
this.youTubePlayer.setPlaybackEventListener(youtubePlaybackEventListener);
this.youTubePlayer.setPlayerStateChangeListener(youtubePlayerStateChangeListener);

youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
this.youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);

// Start buffering
if (!wasRestored) {
Pattern youtubeIdPattern = Pattern.compile(".*?v=([^&]*)");
Matcher matcher = youtubeIdPattern.matcher(getMediaItem().link);
if(matcher.matches()) {
String youtubeId = matcher.group(1);
youTubePlayer.cueVideo(youtubeId);
this.youTubePlayer.cueVideo(youtubeId);
} else {
Toast.makeText(context, "Cannot find youtube video id", Toast.LENGTH_LONG).show();
setStatus(Status.FAILED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link PodcastFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* to StartYoutubePlayer interaction events.
* Use the {@link PodcastFragment#newInstance} factory method to
* create an instance of this fragment.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.animation.Animator;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
Expand All @@ -28,9 +27,6 @@
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;

import org.greenrobot.eventbus.EventBus;
Expand Down Expand Up @@ -67,12 +63,10 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP

private static final String TAG = PodcastFragmentActivity.class.getCanonicalName();


@Inject SharedPreferences mPrefs;
@Inject ApiProvider mApi;
@Inject public MemorizingTrustManager mMTM;


private PodcastPlaybackService mPodcastPlaybackService;
private boolean mBound = false;
private EventBus eventBus;
Expand Down Expand Up @@ -277,6 +271,7 @@ protected void UpdatePodcastView() {
}

boolean currentlyPlaying = false;
boolean showedYoutubeFeatureNotAvailableDialog = false;

boolean videoViewInitialized = false;
boolean isVideoViewVisible = true;
Expand Down Expand Up @@ -325,39 +320,38 @@ public void onEvent(UpdatePodcastStatusEvent podcast) {
eventBus.post(new RegisterVideoOutput(surfaceView, rlVideoPodcastSurfaceWrapper));
togglePodcastVideoViewAnimation();
}
} else if(podcast.getVideoType() == PlaybackService.VideoType.YouTube){
if(!videoViewInitialized) {
isVideoViewVisible = true;
videoViewInitialized = true;
rlVideoPodcastSurfaceWrapper.removeAllViews();

rlVideoPodcastSurfaceWrapper.setVisibility(View.VISIBLE);
} else if(podcast.getVideoType() == PlaybackService.VideoType.YouTube) {
if(BuildConfig.FLAVOR.equals("extra")) {
if (!videoViewInitialized) {
isVideoViewVisible = true;
videoViewInitialized = true;
rlVideoPodcastSurfaceWrapper.removeAllViews();

togglePodcastVideoViewAnimation();
rlVideoPodcastSurfaceWrapper.setVisibility(View.VISIBLE);

final int YOUTUBE_CONTENT_VIEW_ID = 10101010;
FrameLayout frame = new FrameLayout(this);
frame.setId(YOUTUBE_CONTENT_VIEW_ID);
rlVideoPodcastSurfaceWrapper.addView(frame);
//setContentView(frame, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
togglePodcastVideoViewAnimation();

final int YOUTUBE_CONTENT_VIEW_ID = 10101010;
FrameLayout frame = new FrameLayout(this);
frame.setId(YOUTUBE_CONTENT_VIEW_ID);
rlVideoPodcastSurfaceWrapper.addView(frame);
//setContentView(frame, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));


YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(YOUTUBE_CONTENT_VIEW_ID, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize("AIzaSyA2OHKWvF_hRVtPmLcwnO8yF6-iah2hjbk", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
eventBus.post(new RegisterYoutubeOutput(youTubePlayer, wasRestored));
togglePodcastVideoViewAnimation();
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(PodcastFragmentActivity.this, "Error while playing youtube video! (InitializationFailure)", Toast.LENGTH_LONG).show();
}
});
YoutubePlayerManager.StartYoutubePlayer(this, YOUTUBE_CONTENT_VIEW_ID, eventBus, new Runnable() {
@Override
public void run() {
togglePodcastVideoViewAnimation();
}
});
}
} else if(!showedYoutubeFeatureNotAvailableDialog) {
showedYoutubeFeatureNotAvailableDialog = true;
new AlertDialog.Builder(this)
.setTitle(getString(R.string.warning))
.setMessage(R.string.dialog_feature_not_available)
.setCancelable(true)
.setPositiveButton(getString(android.R.string.ok), null)
.show();
}
} else {
isVideoViewVisible = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// automatically StartYoutubePlayer clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package de.luhmer.owncloudnewsreader.events.podcast;

import com.google.android.youtube.player.YouTubePlayer;

public class RegisterYoutubeOutput {

public RegisterYoutubeOutput(YouTubePlayer youTubePlayer, boolean wasRestored) {
public RegisterYoutubeOutput(Object youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
this.wasRestored = wasRestored;
}

public YouTubePlayer youTubePlayer;
public Object youTubePlayer; // (Type: com.google.android.youtube.player.YouTubePlayer;)
public boolean wasRestored;

}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ private void initMediaSessions() {
showImageForEmptyUri(R.drawable.default_feed_icon_light).
showImageOnFail(R.drawable.default_feed_icon_light).
build();
Bitmap bmpAlbumArt = ImageLoader.getInstance().loadImageSync(favIconUrl, displayImageOptions);

//TODO networkOnMainThreadExceptionHere!
//Bitmap bmpAlbumArt = ImageLoader.getInstance().loadImageSync(favIconUrl, displayImageOptions);



Expand All @@ -198,7 +200,7 @@ private void initMediaSessions() {
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "Test")
//.putString(MediaMetadataCompat.METADATA_KEY_TITLE, podcastItem.title)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 100)
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bmpAlbumArt)
//.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bmpAlbumArt)
/* .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher)) */
.build());
Expand Down
2 changes: 2 additions & 0 deletions News-Android-App/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
<string name="pref_title_MarkAsReadWhileScrolling">Mark as read while scrolling</string>
<string name="pref_title_OpenInBrowserDirectly">Use external browser to view articles</string>

<string name="dialog_feature_not_available">This feature is not available in this (open-source) version of this app. If you want to use this feature please download the app from the GitHub Repository or download the App from the Google PlayStore.</string>


<!-- MemorizingTrustManager -->
<string name="mtm_accept_cert">Accept Unknown Certificate?</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.luhmer.owncloudnewsreader;

import android.app.Activity;
import org.greenrobot.eventbus.EventBus;

public class YoutubePlayerManager {

public static void StartYoutubePlayer(final Activity activity, int YOUTUBE_CONTENT_VIEW_ID, final EventBus eventBus, final Runnable onInitSuccess) {
// Dummy
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package de.luhmer.owncloudnewsreader.services.podcast;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.luhmer.owncloudnewsreader.model.MediaItem;

/**
* Created by david on 31.01.17.
*/

public class YoutubePlaybackService extends PlaybackService {

public YoutubePlaybackService(Context context, PodcastStatusListener podcastStatusListener, MediaItem mediaItem) {
super(context, podcastStatusListener, mediaItem);
setStatus(Status.FAILED);
}

@Override
public void destroy() { }

@Override
public void play() { }

@Override
public void pause() { }

public void seekTo(double percent) { }
public int getCurrentDuration() {
return 0;
}

public int getTotalDuration() {
return 0;
}

@Override
public VideoType getVideoType() {
return VideoType.YouTube;
}

public void setYoutubePlayer(Object youTubePlayer, boolean wasRestored) { }
}

0 comments on commit ccba687

Please sign in to comment.