use flavors for youtube player api

This commit is contained in:
David Luhmer 2017-06-20 15:48:54 +02:00
parent 8856d2fbac
commit ccba687340
11 changed files with 147 additions and 50 deletions

View file

@ -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'
@ -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'

View file

@ -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();
}
});
}
}

View file

@ -75,12 +75,12 @@ public class YoutubePlaybackService extends PlaybackService {
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) {
@ -88,7 +88,7 @@ public class YoutubePlaybackService extends PlaybackService {
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);

View file

@ -51,7 +51,7 @@ import de.luhmer.owncloudnewsreader.view.PodcastSlidingUpPanelLayout;
* 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.
*

View file

@ -3,7 +3,6 @@ package de.luhmer.owncloudnewsreader;
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;
@ -28,9 +27,6 @@ import android.widget.FrameLayout;
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;
@ -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;
@ -277,6 +271,7 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
}
boolean currentlyPlaying = false;
boolean showedYoutubeFeatureNotAvailableDialog = false;
boolean videoViewInitialized = false;
boolean isVideoViewVisible = true;
@ -325,39 +320,38 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
eventBus.post(new RegisterVideoOutput(surfaceView, rlVideoPodcastSurfaceWrapper));
togglePodcastVideoViewAnimation();
}
} else if(podcast.getVideoType() == PlaybackService.VideoType.YouTube){
if(!videoViewInitialized) {
isVideoViewVisible = true;
videoViewInitialized = true;
rlVideoPodcastSurfaceWrapper.removeAllViews();
} else if(podcast.getVideoType() == PlaybackService.VideoType.YouTube) {
if(BuildConfig.FLAVOR.equals("extra")) {
if (!videoViewInitialized) {
isVideoViewVisible = true;
videoViewInitialized = true;
rlVideoPodcastSurfaceWrapper.removeAllViews();
rlVideoPodcastSurfaceWrapper.setVisibility(View.VISIBLE);
rlVideoPodcastSurfaceWrapper.setVisibility(View.VISIBLE);
togglePodcastVideoViewAnimation();
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));
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;

View file

@ -65,7 +65,7 @@ public class SyncIntervalSelectorActivity extends AppCompatActivity {
@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) {

View file

@ -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;
}

View file

@ -188,7 +188,9 @@ public class PodcastNotification {
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);
@ -198,7 +200,7 @@ public class PodcastNotification {
.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());

View file

@ -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>

View file

@ -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
}
}

View file

@ -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) { }
}