fr.shywim.antoinedaniel.ui.fragment.GamesFragment.java Source code

Java tutorial

Introduction

Here is the source code for fr.shywim.antoinedaniel.ui.fragment.GamesFragment.java

Source

/*
 * Copyright (c) 2015 Matthieu Harl
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package fr.shywim.antoinedaniel.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import fr.shywim.antoinedaniel.R;
import fr.shywim.antoinedaniel.ui.BlindGameActivity;
import fr.shywim.antoinedaniel.utils.UiUtils;

public class GamesFragment extends Fragment {

    Context mContext;

    ActivityCallbacksGames callbacks;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment QuizzFragment.
     */
    public static GamesFragment newInstance() {
        return new GamesFragment();
    }

    public GamesFragment() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
        ((FragmentsCallbacks) activity).onFragmentAttached(this);
        try {
            callbacks = (ActivityCallbacksGames) activity;
        } catch (ClassCastException e) {
            throw new RuntimeException("Activity must implements Callbacks!");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_games, container, false);

        UiUtils.setStatusBarBackground(root, getResources());

        // GPG Stuff
        if (!callbacks.isConnected()) {
            root.findViewById(R.id.sign_in_controll).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, R.string.gplay_not_connected, Toast.LENGTH_SHORT).show();
                }
            });
            root.findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callbacks.connect();
                }
            });
        } else {
            root.findViewById(R.id.sign_in_button).setVisibility(View.GONE);
            ((ImageView) root.findViewById(R.id.sign_in_controll))
                    .setColorFilter(getResources().getColor(R.color.primary), PorterDuff.Mode.MULTIPLY);
            root.findViewById(R.id.sign_in_controll).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, R.string.gplay_connected, Toast.LENGTH_SHORT).show();
                }
            });
        }

        // Setup blind buttons
        root.findViewById(R.id.blind_normal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendHitAnalytics("blind_normal");
                Intent intent = new Intent(getActivity(), BlindGameActivity.class);
                intent.putExtra(BlindGameActivity.ARG_DIFFICULTY, BlindGameActivity.Difficulty.NORMAL);
                getActivity().startActivityForResult(intent, BlindGameActivity.REQUEST_CODE);
            }
        });
        root.findViewById(R.id.blind_hardcore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendHitAnalytics("blind_hardcore");
                Intent intent = new Intent(getActivity(), BlindGameActivity.class);
                intent.putExtra(BlindGameActivity.ARG_DIFFICULTY, BlindGameActivity.Difficulty.HARDCORE);
                getActivity().startActivityForResult(intent, BlindGameActivity.REQUEST_CODE);
            }
        });
        root.findViewById(R.id.blind_yolo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendHitAnalytics("blind_yolo");
                Intent intent = new Intent(getActivity(), BlindGameActivity.class);
                intent.putExtra(BlindGameActivity.ARG_DIFFICULTY, BlindGameActivity.Difficulty.YOLO);
                getActivity().startActivityForResult(intent, BlindGameActivity.REQUEST_CODE);
            }
        });

        Toolbar toolbar = (Toolbar) root.findViewById(R.id.toolbar_actionbar);
        toolbar.setTitle(R.string.drawer_games);
        ((FragmentsCallbacks) mContext).setActionBarToolbar(toolbar);

        return root;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.removeGroup(R.id.sound_menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        callbacks = null;
    }

    void sendHitAnalytics(String label) {
        /*Utils.getTracker(mContext)
        .send(new HitBuilders.EventBuilder()
           .setCategory(getString(R.string.ana_cat_game))
           .setAction(getString(R.string.ana_act_play))
           .setLabel(label)
           .build()
        );*/
    }

    public interface ActivityCallbacksGames {
        public boolean isConnected();

        public void connect();

        public void showLeaderboard(int which);

        public void showAchievements();
    }
}