com.brandao.tictactoe.gamesettings.GameSettingsFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.brandao.tictactoe.gamesettings.GameSettingsFragment.java

Source

/*
 * Copyright (c) 2016 Skytrait
 *
 * Licensed under the Apache License, Version 2.0 (the License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * www.skytrait.com
 *
 */

package com.brandao.tictactoe.gamesettings;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatRadioButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.brandao.tictactoe.R;
import com.brandao.tictactoe.board.BoardActivity;
import com.brandao.tictactoe.extras.C;
import com.brandao.tictactoe.extras.Feedback;
import com.brandao.tictactoe.settings.Settings;

public class GameSettingsFragment extends Fragment {
    public final String TAG = "GameSettingsFragment";

    private int mGameDifficulty = -1;
    private int mWhoGoesFirst = -1;
    private int mGameType = -1;

    private EditText mPlayerOneNameInput;
    private EditText mPlayerTwoNameInput;
    private TextView mDifficultyTitleDisplay;

    private RadioGroup mSetDifficulty;
    private RadioGroup mSetFirstPlayer;

    private SharedPreferences mPrefs;

    private AppCompatRadioButton mEasy;
    private AppCompatRadioButton mMed;
    private AppCompatRadioButton mHard;

    public static GameSettingsFragment newInstance() {
        return new GameSettingsFragment();
    }

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

        mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    }

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

        mPlayerOneNameInput = (EditText) v.findViewById(R.id.player_one_name_input);
        mPlayerTwoNameInput = (EditText) v.findViewById(R.id.player_two_name_input);

        mDifficultyTitleDisplay = (TextView) v.findViewById(R.id.difficulty_title_display);
        mSetDifficulty = (RadioGroup) v.findViewById(R.id.set_difficulty_radio_group);
        mSetFirstPlayer = (RadioGroup) v.findViewById(R.id.set_first_player_radio_group);

        mEasy = (AppCompatRadioButton) v.findViewById(R.id.set_difficulty_easy);
        mMed = (AppCompatRadioButton) v.findViewById(R.id.set_difficulty_medium);
        mHard = (AppCompatRadioButton) v.findViewById(R.id.set_difficulty_hard);

        return v;
    }

    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        ColorStateList red = new ColorStateList(
                new int[][] { new int[] { -android.R.attr.state_checked },
                        new int[] { android.R.attr.state_checked } },
                new int[] {

                        Color.DKGRAY, ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_red_shadow), });

        ColorStateList green = new ColorStateList(
                new int[][] { new int[] { -android.R.attr.state_checked },
                        new int[] { android.R.attr.state_checked } },
                new int[] {

                        Color.DKGRAY, ContextCompat.getColor(getActivity(), R.color.tic_tac_toe_green_shadow), });

        mEasy.setSupportButtonTintList(red);
        mMed.setSupportButtonTintList(green);
        mHard.setSupportButtonTintList(red);

        v.findViewById(R.id.start_tic_tac_toe).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Feedback.feedback(getActivity(), R.raw.game_over_sound);

                startTicTacToe();
            }
        });

        v.findViewById(R.id.back_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Feedback.feedback(getActivity(), R.raw.blu_dot_clicked);

                NavUtils.navigateUpFromSameTask(getActivity());
            }
        });

        mSetDifficulty.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.set_difficulty_easy: {
                    mGameDifficulty = C.DIFFICULTY_EASY;

                    break;
                }
                case R.id.set_difficulty_medium: {
                    mGameDifficulty = C.DIFFICULTY_MEDIUM;

                    break;
                }
                case R.id.set_difficulty_hard: {
                    mGameDifficulty = C.DIFFICULTY_HARD;

                    break;
                }
                }
            }
        });

        mSetFirstPlayer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.set_x_first: {
                    mWhoGoesFirst = C.X_GOES_FIRST;

                    break;
                }
                case R.id.set_o_first: {
                    mWhoGoesFirst = C.O_GOES_FIRST;

                    break;
                }
                case R.id.set_x_o_random: {
                    mWhoGoesFirst = C.X_O_RANDOM;

                    break;
                }
                }
            }
        });

        setTheme(v);
        restoreAndSetPrefs(v);
    }

    @Override
    public void onPause() {
        savePrefs();

        super.onPause();
    }

    public void savePrefs() {
        SharedPreferences.Editor editor = mPrefs.edit();

        if (mGameType == C.TYPE_TWO_PLAYER) {
            editor.putString(Settings.PREF_MULTI_PLAYER_ONE_NAME, mPlayerOneNameInput.getText().toString());
            editor.putString(Settings.PREF_MULTI_PLAYER_TWO_NAME, mPlayerTwoNameInput.getText().toString());
        } else {
            editor.putString(Settings.PREF_SINGLE_PLAYER_NAME, mPlayerOneNameInput.getText().toString());
            editor.putString(Settings.PREF_COMPUTER_NAME, mPlayerTwoNameInput.getText().toString());
        }

        editor.putInt(Settings.PREF_GAME_DIFFICULTY, mGameDifficulty);
        editor.putInt(Settings.PREF_WHO_GOES_FIRST, mWhoGoesFirst);
        editor.apply();
    }

    public void setTheme(View v) {
        Typeface titleFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/DroidSans-Bold.ttf");

        if (((TextView) v.findViewById(R.id.tic)) != null) {
            ((TextView) v.findViewById(R.id.tic)).setTypeface(titleFont);
            ((TextView) v.findViewById(R.id.tac)).setTypeface(titleFont);
            ((TextView) v.findViewById(R.id.toe)).setTypeface(titleFont);
        }

        ((RadioButton) v.findViewById(R.id.set_difficulty_easy)).setTypeface(titleFont);
        ((RadioButton) v.findViewById(R.id.set_difficulty_medium)).setTypeface(titleFont);
        ((RadioButton) v.findViewById(R.id.set_difficulty_hard)).setTypeface(titleFont);

        ((TextView) v.findViewById(R.id.player_one_prompt)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.player_two_prompt)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.player_title)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.who_goes_first_title)).setTypeface(titleFont);

        ((ImageView) v.findViewById(R.id.player_one_image)).setBackgroundResource(R.drawable.ic_green_x_android);
        ((ImageView) v.findViewById(R.id.player_two_image)).setBackgroundResource(R.drawable.ic_red_o_android);

        ((RadioButton) v.findViewById(R.id.set_o_first)).setBackgroundResource(R.drawable.red_o_android_selector);
        ((RadioButton) v.findViewById(R.id.set_x_first)).setBackgroundResource(R.drawable.green_x_android_selector);
        ((RadioButton) v.findViewById(R.id.set_x_o_random)).setBackgroundResource(R.drawable.xo_selector);

        ((RadioButton) v.findViewById(R.id.set_o_first)).setButtonDrawable(R.drawable.transparent_image);
        ((RadioButton) v.findViewById(R.id.set_x_first)).setButtonDrawable(R.drawable.transparent_image);
        ((RadioButton) v.findViewById(R.id.set_x_o_random)).setButtonDrawable(R.drawable.transparent_image);

        mPlayerOneNameInput.setTypeface(titleFont);
        mPlayerTwoNameInput.setTypeface(titleFont);
        mDifficultyTitleDisplay.setTypeface(titleFont);

        ((TextView) v.findViewById(R.id.start_tic_tac_toe)).setTypeface(titleFont);
        ((TextView) v.findViewById(R.id.back_button)).setTypeface(titleFont);
    }

    public void restoreAndSetPrefs(View v) {
        mGameType = getActivity().getIntent().getIntExtra(C.TAG_INTENT_GAME_TYPE, C.TYPE_ONE_PLAYER);
        mWhoGoesFirst = mPrefs.getInt(Settings.PREF_WHO_GOES_FIRST, Settings.PREF_WHO_GOES_FIRST_DEFAULT);
        mGameDifficulty = mPrefs.getInt(Settings.PREF_GAME_DIFFICULTY, Settings.PREF_GAME_DIFFICULTY_DEFAULT);

        switch (mGameDifficulty) {
        case C.DIFFICULTY_EASY: {
            mSetDifficulty.check(R.id.set_difficulty_easy);

            break;
        }
        case C.DIFFICULTY_MEDIUM: {
            mSetDifficulty.check(R.id.set_difficulty_medium);

            break;
        }
        case C.DIFFICULTY_HARD: {
            mSetDifficulty.check(R.id.set_difficulty_hard);

            break;
        }
        }

        switch (mWhoGoesFirst) {
        case C.X_GOES_FIRST: {
            mSetFirstPlayer.check(R.id.set_x_first);

            break;
        }
        case C.O_GOES_FIRST: {
            mSetFirstPlayer.check(R.id.set_o_first);

            break;
        }
        case C.X_O_RANDOM: {
            mSetFirstPlayer.check(R.id.set_x_o_random);

            break;
        }
        }

        if (mGameType == C.TYPE_ONE_PLAYER) {
            mPlayerOneNameInput.setText(
                    mPrefs.getString(Settings.PREF_SINGLE_PLAYER_NAME, Settings.PREF_SINGLE_PLAYER_NAME_DEFAULT));
            mPlayerOneNameInput.setHint(R.string.human);

            mPlayerTwoNameInput
                    .setText(mPrefs.getString(Settings.PREF_COMPUTER_NAME, Settings.PREF_COMPUTER_NAME_DEFAULT));
            mPlayerTwoNameInput.setHint(R.string.android);

            mSetDifficulty.setVisibility(View.VISIBLE);
            mDifficultyTitleDisplay.setVisibility(View.VISIBLE);

            ((TextView) v.findViewById(R.id.player_title)).setText(getString(R.string.single_player));
            ((TextView) v.findViewById(R.id.player_one_prompt)).setText(getString(R.string.human));
            ((TextView) v.findViewById(R.id.player_two_prompt)).setText(getString(R.string.android));
        } else {
            mPlayerOneNameInput.setText(mPrefs.getString(Settings.PREF_MULTI_PLAYER_ONE_NAME,
                    Settings.PREF_MULTI_PLAYER_ONE_NAME_DEFAULT));
            mPlayerOneNameInput.setHint(R.string.player_one);

            mPlayerTwoNameInput.setText(mPrefs.getString(Settings.PREF_MULTI_PLAYER_TWO_NAME,
                    Settings.PREF_MULTI_PLAYER_TWO_NAME_DEFAULT));
            mPlayerTwoNameInput.setHint(R.string.player_two);

            mSetDifficulty.setVisibility(View.GONE);
            mDifficultyTitleDisplay.setVisibility(View.GONE);

            ((TextView) v.findViewById(R.id.player_title)).setText(getString(R.string.multi_player));
            ((TextView) v.findViewById(R.id.player_one_prompt)).setText(getString(R.string.player_one));
            ((TextView) v.findViewById(R.id.player_two_prompt)).setText(getString(R.string.player_two));
        }
    }

    public void startTicTacToe() {
        Intent i = new Intent(getActivity(), BoardActivity.class);

        i.putExtra(C.TAG_INTENT_GAME_TYPE, mGameType);
        i.putExtra(C.TAG_INTENT_GAME_DIFFICULTY, mGameDifficulty);

        if (mPlayerOneNameInput.getText().toString().length() > 0) {
            i.putExtra(C.TAG_INTENT_PLAYER_ONE_NAME, mPlayerOneNameInput.getText().toString());
        } else {
            if (mGameType == C.TYPE_TWO_PLAYER) {
                i.putExtra(C.TAG_INTENT_PLAYER_ONE_NAME, getString(R.string.player_one));
            } else {
                i.putExtra(C.TAG_INTENT_PLAYER_ONE_NAME, getString(R.string.human));
            }
        }

        if (mPlayerTwoNameInput.getText().toString().length() > 0) {
            i.putExtra(C.TAG_INTENT_PLAYER_TWO_NAME, mPlayerTwoNameInput.getText().toString());
        } else {
            if (mGameType == C.TYPE_TWO_PLAYER) {
                i.putExtra(C.TAG_INTENT_PLAYER_TWO_NAME, getString(R.string.player_two));
            } else {
                i.putExtra(C.TAG_INTENT_PLAYER_TWO_NAME, getString(R.string.android));
            }
        }

        i.putExtra(C.TAG_INTENT_WHO_GOES_FIRST, mWhoGoesFirst);

        startActivity(i);
    }
}