package com.jiahaoliuliu.youtubeandroidintegration;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private static final String DEVELOPER_KEY = "AIzaSyC4tPvQqN08CQ6a0FudUubzOKWqOiql-mQ";

    private YouTubePlayerFragment mYoutubePlayerFragment;
    private TextView simpleInfoTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        simpleInfoTextView = (TextView) findViewById(R.id.simpleInfoTextView);
        mYoutubePlayerFragment = (YouTubePlayerFragment)getFragmentManager().findFragmentById(R.id.youtube_fragment);
        mYoutubePlayerFragment.initialize(DEVELOPER_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        if (!wasRestored) {
            youTubePlayer.cueVideo("nCgQDjiotG0");

            youTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                @Override
                public void onFullscreen(boolean isFullScreen) {
                    simpleInfoTextView.setVisibility(isFullScreen? View.GONE : View.VISIBLE);
                }
            });
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError()) {
            youTubeInitializationResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(getString(R.string.error_player), youTubeInitializationResult.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            mYoutubePlayerFragment.initialize(DEVELOPER_KEY, this);
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

@@ -659,47 +659,8 @@
  = %22
-AIzaSyC4tPvQqN08CQ6a0FudUubzOKWqOiql-mQ
 %22;%0A%0A
@@ -757,16 +757,136 @@
 extView;
+%0A    private YouTubePlayer mYouTubePlayer;%0A%0A    // Check if it was full screen or not%0A    private boolean mIsFullScreen;
 %0A%0A    @O
@@ -1482,33 +1482,34 @@
 ) %7B%0A            
-y
+mY
 ouTubePlayer.cue
@@ -1508,34 +1508,25 @@
 ayer
-.cueVideo(%22nCgQDjiotG0%22);%0A
+ = youTubePlayer;
 %0A   
@@ -1530,25 +1530,26 @@
             
-y
+mY
 ouTubePlayer
@@ -1821,23 +1821,126 @@
     
-%7D%0A            %7D
+    mIsFullScreen = isFullScreen;%0A                %7D%0A            %7D);%0A%0A            mYouTubePlayer.cueVideo(%22nCgQDjiotG0%22
 );%0A 
@@ -2885,11 +2885,205 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onBackPressed() %7B%0A        if (mIsFullScreen) %7B%0A            mYouTubePlayer.setFullscreen(false);%0A            return;%0A        %7D%0A        super.onBackPressed();%0A    %7D
 %0A%7D%0A

Handle correctly the back button