com.cardio3g.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.cardio3g.MainActivity.java

Source

package com.cardio3g;

import android.Manifest;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Messenger;
import android.os.SystemClock;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.android.vending.expansion.zipfile.ZipResourceFile;
import com.cardio3g.expansion.DownloaderService;
import com.cardio3g.fragment.BuiltInProgramsFragment;
import com.cardio3g.fragment.CustomProgramFragment;
import com.cardio3g.fragment.HealthyLivingFragment;
import com.cardio3g.fragment.MuscleIsolationFragment;
import com.cardio3g.fragment.PersonalTrainerFragment;
import com.cardio3g.fragment.SportsTrainingFragment;
import com.crashlytics.android.Crashlytics;
import com.google.android.vending.expansion.downloader.DownloadProgressInfo;
import com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;
import com.google.android.vending.expansion.downloader.DownloaderServiceMarshaller;
import com.google.android.vending.expansion.downloader.Helpers;
import com.google.android.vending.expansion.downloader.IDownloaderClient;
import com.google.android.vending.expansion.downloader.IDownloaderService;
import com.google.android.vending.expansion.downloader.IStub;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.zip.CRC32;

import io.fabric.sdk.android.Fabric;
import io.fabric.sdk.android.services.concurrency.AsyncTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IDownloaderClient {

    Button btnBuiltIn, btnMuscleIsolation, btnCustomProgram, btnSportsTraining, btnhealthyLiving,
            btnPersonalTrainer, btnVideos, btnImages, btnCardio;
    FrameLayout frameLayout;
    VideoView videoView;
    boolean boolBuilt = false, boolMuscle = false, boolCustom = false, boolSports = false, boolHealthy = false,
            boolPersonal = false;
    MediaController mediaController;
    private IStub mDownloaderClientStub;
    private int mState;
    private boolean mCancelValidation;
    private IDownloaderService mRemoteService;
    RelativeLayout mDownloadViewGroup, mainLayout;
    ProgressBar mDownloadProgressBar;
    TextView mProgressPercentTextView;
    public static final int MULTIPLE_PERMISSIONS = 100; // code you want.
    String[] permissions = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Fabric.with(this, new Crashlytics());
        setContentView(R.layout.activity_main);
        mDownloadViewGroup = (RelativeLayout) findViewById(R.id.downloadViewGroup);
        mDownloadProgressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
        mProgressPercentTextView = (TextView) findViewById(R.id.downloadProgressPercentTextView);
        mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
        mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, DownloaderService.class);
        if (!expansionFilesDelivered()) {

            try {
                Intent launchIntent = MainActivity.this.getIntent();
                Intent intentToLaunchThisActivityFromNotification = new Intent(MainActivity.this,
                        MainActivity.this.getClass());
                intentToLaunchThisActivityFromNotification
                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());

                if (launchIntent.getCategories() != null) {
                    for (String category : launchIntent.getCategories()) {
                        intentToLaunchThisActivityFromNotification.addCategory(category);
                    }
                }

                // Build PendingIntent used to open this activity from
                // Notification
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
                        intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);
                // Request to start the download
                int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent,
                        DownloaderService.class);

                if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
                    // The DownloaderService has started downloading the files, show progress
                    initializeDownloadUI();
                    return;
                } // otherwise, download not needed so we fall through to the app
            } catch (PackageManager.NameNotFoundException e) {
                Log.e("EXPANSION ERROR", "Cannot find package!", e);
            }
        } else {
            validateXAPKZipFiles();
        }

        btnBuiltIn = (Button) findViewById(R.id.button_builtin);
        btnMuscleIsolation = (Button) findViewById(R.id.button_muscle);
        btnCustomProgram = (Button) findViewById(R.id.button_custom);
        btnSportsTraining = (Button) findViewById(R.id.button_sports);
        btnhealthyLiving = (Button) findViewById(R.id.button_healthy);
        btnPersonalTrainer = (Button) findViewById(R.id.button_personal);
        btnVideos = (Button) findViewById(R.id.button_exercise_videos);
        btnCardio = (Button) findViewById(R.id.button_visit);
        btnImages = (Button) findViewById(R.id.button_exercise_photos);
        frameLayout = (FrameLayout) findViewById(R.id.frame);
        videoView = (VideoView) findViewById(R.id.video_view);
        btnBuiltIn.setOnClickListener(this);
        btnMuscleIsolation.setOnClickListener(this);
        btnCustomProgram.setOnClickListener(this);
        btnSportsTraining.setOnClickListener(this);
        btnhealthyLiving.setOnClickListener(this);
        btnPersonalTrainer.setOnClickListener(this);
        btnVideos.setOnClickListener(this);
        btnCardio.setOnClickListener(this);
        btnImages.setOnClickListener(this);

        mediaController = new MediaController(MainActivity.this);
        checkPermission();
    }

    @Override
    public void onClick(View view) {
        if (view == btnBuiltIn) {
            if (boolBuilt) {
                boolBuilt = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_down_in, R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                }
            } else {
                boolBuilt = true;
                removeAllFragments(getSupportFragmentManager());

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_in);
                    fragmentTransaction.replace(R.id.frame, new BuiltInProgramsFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(R.id.frame, new BuiltInProgramsFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                }
            }
        }
        if (view == btnMuscleIsolation) {
            if (boolMuscle) {
                boolMuscle = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_down_in, R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                }
            } else {
                boolMuscle = true;
                removeAllFragments(getSupportFragmentManager());

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new MuscleIsolationFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(R.id.frame, new MuscleIsolationFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            }
        }
        if (view == btnCustomProgram) {
            if (boolCustom) {
                boolCustom = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_down_in, R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();

                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                }
            } else {
                boolCustom = true;
                removeAllFragments(getSupportFragmentManager());

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new CustomProgramFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(R.id.frame, new CustomProgramFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            }
        }
        if (view == btnSportsTraining) {
            if (boolSports) {
                boolSports = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_down_in, R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                }
            } else {
                boolSports = true;
                removeAllFragments(getSupportFragmentManager());

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new SportsTrainingFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    //fragmentTransaction.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new SportsTrainingFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                }
            }
        }

        if (view == btnhealthyLiving) {
            if (boolHealthy) {
                boolHealthy = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_down_in, R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    //fragmentTransaction.setCustomAnimations(R.anim.push_down_in,R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                }
            } else {
                boolHealthy = true;

                removeAllFragments(getSupportFragmentManager());

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new HealthyLivingFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    //fragmentTransaction.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new HealthyLivingFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            }
        }
        if (view == btnPersonalTrainer) {
            if (boolPersonal) {
                boolPersonal = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_down_in, R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));
                    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    //fragmentTransaction.setCustomAnimations(R.anim.push_down_in,R.anim.push_down_in);
                    if (f != null)
                        fragmentTransaction.remove(f);
                    fragmentTransaction.commit();

                }
            } else {
                boolPersonal = true;

                removeAllFragments(getSupportFragmentManager());

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonselected, null));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white, null));

                    removeAllFragments(getSupportFragmentManager());

                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
                    fragmentTransaction.replace(R.id.frame, new PersonalTrainerFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                } else {

                    btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                    btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonselected));
                    btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                    btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                    btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                    btnImages.setTextColor(getResources().getColor(android.R.color.white));

                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(R.id.frame, new PersonalTrainerFragment());
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();

                }
            }
        }

        if (view == btnVideos) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtnclickd, null));
                btnVideos.setTextColor(getResources().getColor(android.R.color.black, null));
                btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                Intent intent = new Intent(MainActivity.this, AllImagesVideosActivity.class);
                intent.putExtra("type", "video");
                startActivity(intent);
            } else {
                btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtnclickd));
                btnVideos.setTextColor(getResources().getColor(android.R.color.black));
                btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                btnImages.setTextColor(getResources().getColor(android.R.color.white));

                Intent intent = new Intent(MainActivity.this, AllImagesVideosActivity.class);
                intent.putExtra("type", "video");
                startActivity(intent);
            }
        }
        if (view == btnCardio) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtnclickd, null));
                btnCardio.setTextColor(getResources().getColor(android.R.color.black, null));
                btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                btnImages.setTextColor(getResources().getColor(android.R.color.white, null));
                try {
                    startActivity(new Intent(MainActivity.this, Visit3GCardioActivity.class));
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtnclickd));
                btnCardio.setTextColor(getResources().getColor(android.R.color.black));
                btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                btnImages.setTextColor(getResources().getColor(android.R.color.white));

                try {

                    startActivity(new Intent(MainActivity.this, Visit3GCardioActivity.class));
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (view == btnImages) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtnclickd, null));
                btnImages.setTextColor(getResources().getColor(android.R.color.black, null));
                btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected, null));
                btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                btnVideos.setTextColor(getResources().getColor(android.R.color.white, null));
                btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn, null));
                btnCardio.setTextColor(getResources().getColor(android.R.color.white, null));
                Intent intent = new Intent(MainActivity.this, AllImagesVideosActivity.class);
                intent.putExtra("type", "image");
                startActivity(intent);

            } else {
                btnImages.setBackground(getResources().getDrawable(R.drawable.smallbtnclickd));
                btnImages.setTextColor(getResources().getColor(android.R.color.black));
                btnBuiltIn.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnMuscleIsolation.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnCustomProgram.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnSportsTraining.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnhealthyLiving.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnPersonalTrainer.setBackground(getResources().getDrawable(R.drawable.buttonunselected));
                btnVideos.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                btnVideos.setTextColor(getResources().getColor(android.R.color.white));
                btnCardio.setBackground(getResources().getDrawable(R.drawable.smallbtn));
                btnCardio.setTextColor(getResources().getColor(android.R.color.white));
                Intent intent = new Intent(MainActivity.this, AllImagesVideosActivity.class);
                intent.putExtra("type", "image");
                startActivity(intent);

            }
        }

    }

    private static void removeAllFragments(FragmentManager fragmentManager) {
        while (fragmentManager.getBackStackEntryCount() > 0) {
            fragmentManager.popBackStackImmediate();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (videoView != null) {
            if (videoView.isPlaying()) {
                videoView.resume();
            }

        }
    }

    @Override
    public void onRestart() {
        super.onRestart();
        if (videoView != null) {
            if (videoView.isPlaying()) {
                videoView.resume();
            }

        }
    }

    private static class XAPKFile {
        public final boolean mIsMain;
        public final int mFileVersion;
        public final long mFileSize;

        XAPKFile(boolean isMain, int fileVersion, long fileSize) {
            mIsMain = isMain;
            mFileVersion = fileVersion;
            mFileSize = fileSize;
        }
    }

    private static final XAPKFile[] xAPKS = { new XAPKFile(true, // true signifies a main file
            11, // the version of the APK that the file was uploaded against
            349868864L // the length of the file in bytes
            ) };
    static private final float SMOOTHING_FACTOR = 0.005f;

    /**
     * Connect the stub to our service on start.
     */
    @Override
    protected void onStart() {
        if (null != mDownloaderClientStub) {
            mDownloaderClientStub.connect(this);
        }
        super.onStart();
    }

    /**
     * Disconnect the stub from our service on stop
     */
    @Override
    protected void onStop() {
        if (null != mDownloaderClientStub) {
            mDownloaderClientStub.disconnect(this);
        }
        finish();
        if (videoView != null) {
            if (videoView.isPlaying()) {
                videoView.pause();
            }

        }
        super.onStop();
    }

    /**
     * Critical implementation detail. In onServiceConnected we create the
     * remote service and marshaler. This is how we pass the client information
     * back to the service so the client can be properly notified of changes. We
     * must do this every time we reconnect to the service.
     */
    @Override
    public void onServiceConnected(Messenger m) {
        mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
        mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
    }

    /**
     * The download state should trigger changes in the UI --- it may be useful
     * to show the state as being indeterminate at times. This sample can be
     * considered a guideline.
     */
    @Override
    public void onDownloadStateChanged(int newState) {
        setState(newState);
        boolean showDashboard = true;
        boolean showCellMessage = false;
        boolean paused;
        boolean indeterminate;
        switch (newState) {
        case IDownloaderClient.STATE_IDLE:
            // STATE_IDLE means the service is listening, so it's
            // safe to start making calls via mRemoteService.
            paused = false;
            indeterminate = true;
            break;
        case IDownloaderClient.STATE_CONNECTING:
        case IDownloaderClient.STATE_FETCHING_URL:
            showDashboard = true;
            paused = false;
            indeterminate = true;
            break;
        case IDownloaderClient.STATE_DOWNLOADING:
            paused = false;
            showDashboard = true;
            indeterminate = false;
            break;

        case IDownloaderClient.STATE_FAILED_CANCELED:
        case IDownloaderClient.STATE_FAILED:
        case IDownloaderClient.STATE_FAILED_FETCHING_URL:
        case IDownloaderClient.STATE_FAILED_UNLICENSED:
            paused = true;
            showDashboard = false;
            indeterminate = false;
            break;
        case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
        case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
            showDashboard = false;
            paused = true;
            indeterminate = false;
            showCellMessage = true;
            break;

        case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
            paused = true;
            indeterminate = false;
            break;
        case IDownloaderClient.STATE_PAUSED_ROAMING:
        case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
            paused = true;
            indeterminate = false;
            break;
        case IDownloaderClient.STATE_COMPLETED:
            showDashboard = false;
            paused = false;
            indeterminate = false;
            validateXAPKZipFiles();
            return;
        default:
            paused = true;
            indeterminate = true;
            showDashboard = true;
        }
        int newDashboardVisibility = showDashboard ? View.VISIBLE : View.GONE;
        if (mDownloadViewGroup.getVisibility() != newDashboardVisibility) {
            mDownloadViewGroup.setVisibility(newDashboardVisibility);
        }
        mDownloadProgressBar.setIndeterminate(indeterminate);
    }

    /**
     * Sets the state of the various controls based on the progressinfo object
     * sent from the downloader service.
     */
    @Override
    public void onDownloadProgress(DownloadProgressInfo progress) {
        mDownloadProgressBar.setMax((int) (progress.mOverallTotal >> 8));
        mDownloadProgressBar.setProgress((int) (progress.mOverallProgress >> 8));
        mProgressPercentTextView
                .setText(Long.toString(progress.mOverallProgress * 100 / progress.mOverallTotal) + "%");
    }

    /**
     * Go through each of the Expansion APK files and open each as a zip file.
     * Calculate the CRC for each file and return false if any fail to match.
     *
     * @return true if XAPKZipFile is successful
     */
    void validateXAPKZipFiles() {
        AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {

            @Override
            protected void onPreExecute() {
                mDownloadViewGroup.setVisibility(View.VISIBLE);
                super.onPreExecute();
            }

            @Override
            protected Boolean doInBackground(Object... params) {
                for (XAPKFile xf : xAPKS) {
                    String fileName = Helpers.getExpansionAPKFileName(MainActivity.this, xf.mIsMain,
                            xf.mFileVersion);
                    if (!Helpers.doesFileExist(MainActivity.this, fileName, xf.mFileSize, false))
                        return false;
                    fileName = Helpers.generateSaveFileName(MainActivity.this, fileName);
                    ZipResourceFile zrf;
                    byte[] buf = new byte[1024 * 256];
                    try {
                        zrf = new ZipResourceFile(fileName);
                        ZipResourceFile.ZipEntryRO[] entries = zrf.getAllEntries();
                        /**
                         * First calculate the total compressed length
                         */
                        long totalCompressedLength = 0;
                        for (ZipResourceFile.ZipEntryRO entry : entries) {
                            totalCompressedLength += entry.mCompressedLength;
                        }
                        float averageVerifySpeed = 0;
                        long totalBytesRemaining = totalCompressedLength;
                        long timeRemaining;
                        /**
                         * Then calculate a CRC for every file in the Zip file,
                         * comparing it to what is stored in the Zip directory.
                         * Note that for compressed Zip files we must extract
                         * the contents to do this comparison.
                         */
                        for (ZipResourceFile.ZipEntryRO entry : entries) {
                            if (-1 != entry.mCRC32) {
                                long length = entry.mUncompressedLength;
                                CRC32 crc = new CRC32();
                                DataInputStream dis = null;
                                try {
                                    dis = new DataInputStream(zrf.getInputStream(entry.mFileName));

                                    long startTime = SystemClock.uptimeMillis();
                                    while (length > 0) {
                                        int seek = (int) (length > buf.length ? buf.length : length);
                                        dis.readFully(buf, 0, seek);
                                        crc.update(buf, 0, seek);
                                        length -= seek;
                                        long currentTime = SystemClock.uptimeMillis();
                                        long timePassed = currentTime - startTime;
                                        if (timePassed > 0) {
                                            float currentSpeedSample = (float) seek / (float) timePassed;
                                            if (0 != averageVerifySpeed) {
                                                averageVerifySpeed = SMOOTHING_FACTOR * currentSpeedSample
                                                        + (1 - SMOOTHING_FACTOR) * averageVerifySpeed;
                                            } else {
                                                averageVerifySpeed = currentSpeedSample;
                                            }
                                            totalBytesRemaining -= seek;
                                            timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
                                            this.publishProgress(new DownloadProgressInfo(totalCompressedLength,
                                                    totalCompressedLength - totalBytesRemaining, timeRemaining,
                                                    averageVerifySpeed));
                                        }
                                        startTime = currentTime;
                                        if (mCancelValidation)
                                            return true;
                                    }
                                    if (crc.getValue() != entry.mCRC32) {
                                        Log.e("EXPANSION ERROR",
                                                "CRC does not match for entry: " + entry.mFileName);
                                        Log.e("EXPANSION ERROR", "In file: " + entry.getZipFileName());
                                        return false;
                                    }
                                } finally {
                                    if (null != dis) {
                                        dis.close();
                                    }
                                }
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        return false;
                    }
                }
                return true;
            }

            @Override
            protected void onProgressUpdate(DownloadProgressInfo... values) {
                onDownloadProgress(values[0]);
                super.onProgressUpdate(values);
            }

            @Override
            protected void onPostExecute(Boolean result) {
                if (result) {
                    mDownloadViewGroup.setVisibility(View.GONE);
                } else {
                    mDownloadViewGroup.setVisibility(View.VISIBLE);
                }
                super.onPostExecute(result);
            }

        };
        validationTask.execute(new Object());
    }

    boolean expansionFilesDelivered() {
        for (XAPKFile xf : xAPKS) {
            String fileName = Helpers.getExpansionAPKFileName(this, xf.mIsMain, xf.mFileVersion);
            if (!Helpers.doesFileExist(this, fileName, xf.mFileSize, false))
                return false;
        }
        return true;
    }

    private void setState(int newState) {
        if (mState != newState) {
            mState = newState;
        }
    }

    @Override
    protected void onDestroy() {
        this.mCancelValidation = true;
        super.onDestroy();
    }

    public void initializeDownloadUI() {

        mDownloadViewGroup.setVisibility(View.VISIBLE);
        mainLayout.setVisibility(View.GONE);

    }

    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                + ContextCompat.checkSelfPermission(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    || ActivityCompat.shouldShowRequestPermissionRationale(this,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE }, MULTIPLE_PERMISSIONS);
                }

            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE }, MULTIPLE_PERMISSIONS);
                }
            }
        } else {
            startVideo();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) {
        case MULTIPLE_PERMISSIONS:
            if (grantResults.length > 0) {
                boolean cameraPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                boolean readExternalFile = grantResults[0] == PackageManager.PERMISSION_GRANTED;

                if (cameraPermission && readExternalFile) {
                    startVideo();
                }
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE }, MULTIPLE_PERMISSIONS);
                }
            }

        }
    }

    public void startVideo() {
        try {
            videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/home_video"));
            videoView.requestFocus();
        } catch (Exception e) {
            e.printStackTrace();
        }
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaController.setAnchorView(videoView);
                videoView.start();
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/home_video"));
                videoView.requestFocus();
                videoView.start();
            }
        });
    }
}