@Override
  protected void onDestroy() {
    super.onDestroy();

    YouTubeUtility.markVideoAsViewed(this, mVideoId);

    if (mQueryYouTubeTask != null) {
      mQueryYouTubeTask.cancel(true);
    }

    if (mVideoView != null) {
      mVideoView.stopPlayback();
    }

    // clear the flag that keeps the screen ON
    getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    this.mQueryYouTubeTask = null;
    this.mVideoView = null;
  }
    @Override
    protected Uri doInBackground(YouTubeId... pParams) {
      String lUriStr = null;
      String lYouTubeFmtQuality =
          "17"; // 3gpp medium quality, which should be fast enough to view over EDGE connection
      String lYouTubeVideoId = null;

      if (isCancelled()) return null;

      try {

        publishProgress(new ProgressUpdateInfo(mMsgDetect));

        WifiManager lWifiManager =
            (WifiManager) OpenYouTubePlayerActivity.this.getSystemService(Context.WIFI_SERVICE);
        TelephonyManager lTelephonyManager =
            (TelephonyManager)
                OpenYouTubePlayerActivity.this.getSystemService(Context.TELEPHONY_SERVICE);

        ////////////////////////////
        // if we have a fast connection (wifi or 3g), then we'll get a high quality YouTube video
        if ((lWifiManager.isWifiEnabled()
                && lWifiManager.getConnectionInfo() != null
                && lWifiManager.getConnectionInfo().getIpAddress() != 0)
            || ((lTelephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS
                    ||

                    /* icky... using literals to make backwards compatible with 1.5 and 1.6 */
                    lTelephonyManager.getNetworkType() == 9 /*HSUPA*/
                    || lTelephonyManager.getNetworkType() == 10 /*HSPA*/
                    || lTelephonyManager.getNetworkType() == 8 /*HSDPA*/
                    || lTelephonyManager.getNetworkType() == 5 /*EVDO_0*/
                    || lTelephonyManager.getNetworkType() == 6 /*EVDO A*/)
                && lTelephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)) {
          lYouTubeFmtQuality = "18";
        }

        ///////////////////////////////////
        // if the intent is to show a playlist, get the latest video id from the playlist, otherwise
        // the video
        // id was explicitly declared.
        if (pParams[0] instanceof PlaylistId) {
          publishProgress(new ProgressUpdateInfo(mMsgPlaylist));
          lYouTubeVideoId = YouTubeUtility.queryLatestPlaylistVideo((PlaylistId) pParams[0]);
        } else if (pParams[0] instanceof VideoId) {
          lYouTubeVideoId = pParams[0].getId();
        }

        mVideoId = lYouTubeVideoId;
        // Log.e(this.getClass().getSimpleName(), "The lYouTubeVideoId is "+lYouTubeVideoId);
        publishProgress(new ProgressUpdateInfo(mMsgToken));

        if (isCancelled()) return null;

        ////////////////////////////////////
        // calculate the actual URL of the video, encoded with proper YouTube token
        lUriStr = YouTubeUtility.calculateYouTubeUrl(lYouTubeFmtQuality, true, lYouTubeVideoId);
        Log.e(this.getClass().getSimpleName(), "The lUriStr is " + lUriStr);

        if (isCancelled()) return null;

        if (lYouTubeFmtQuality.equals("17")) {
          publishProgress(new ProgressUpdateInfo(mMsgLowBand));
        } else {
          publishProgress(new ProgressUpdateInfo(mMsgHiBand));
        }

      } catch (Exception e) {
        Log.e(
            this.getClass().getSimpleName(),
            "Error occurred while retrieving information from YouTube." + e.getMessage(),
            e);
      }

      if (lUriStr != null) {
        return Uri.parse(lUriStr);
      } else {
        return null;
      }
    }