<commit_before>package com.nilhcem.inaccessible.memory.ui.game;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Toast;

import com.nilhcem.inaccessible.memory.BuildConfig;
import com.nilhcem.inaccessible.memory.R;
import com.nilhcem.inaccessible.memory.core.Game;
import com.nilhcem.inaccessible.memory.ui.base.BaseActivity;

import org.joda.time.DateTime;
import org.joda.time.Duration;

import java.util.Arrays;
import java.util.Random;

import icepick.Icicle;

import static com.nilhcem.inaccessible.memory.core.Game.FlippedStatus;
import static com.nilhcem.inaccessible.memory.core.Game.FlippedStatus.INVALID_PAIR;
import static com.nilhcem.inaccessible.memory.core.Game.FlippedStatus.PAIR_FOUND;

public class GameActivity extends BaseActivity implements CardView.OnCardFlippedListener {

    public static final String EXTRA_NB_CARDS = "nb_cards";

    @Icicle Game mGame;
    @Icicle String[] mCheersMessages;
    @Icicle DateTime mStarted;

    private int mNbCards;
    private ViewGroup mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNbCards = getIntent().getIntExtra(EXTRA_NB_CARDS, 0);

        if (savedInstanceState == null) {
            mGame = new Game(Arrays.copyOfRange(getResources().getStringArray(R.array.animals_array), 0, mNbCards));
            mCheersMessages = getResources().getStringArray(R.array.cheers_array);
            mStarted = new DateTime();
        }
        mLayout = createLayout(mGame);
        setContentView(mLayout);
    }

    @Override
    public void onCardFlipped(int position) {
        try {
            FlippedStatus flippedStatus = mGame.flipCard(position);
            if (flippedStatus.equals(PAIR_FOUND)) {
                announceMessage(mCheersMessages[new Random().nextInt(mCheersMessages.length)]);
            } else if (flippedStatus.equals(INVALID_PAIR)) {
                announceMessage(getString(R.string.pair_invalid));
            }
        } catch (UnsupportedOperationException e) {
            announceMessage(getString(R.string.card_already_flipped_error, position + 1));
        }

        updateCardsContentDescriptions();
        checkGameOver();
    }

    private ViewGroup createLayout(Game game) {
        GameLayout layout = new GameLayout(this);
        int nbCards = game.getNbCards();
        for (int i = 0; i < nbCards; i++) {
            layout.addView(new CardView(this, game.getCardAt(i), i, this));
        }
        return layout;
    }

    private void announceMessage(String error) {
        if (BuildConfig.DEBUG) {
            Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
        } else {
            mLayout.announceForAccessibility(error);
        }
    }

    private void updateCardsContentDescriptions() {
        final int count = mLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            ((CardView) mLayout.getChildAt(i)).updateContentDescription();
        }
    }

    private void checkGameOver() {
        if (mGame.isOver()) {
            announceMessage(getString(R.string.game_over, mNbCards * 2, new Duration(mStarted, new DateTime()).getStandardSeconds()));
            finish();
        }
    }
}<commit_msg>Add possibility to post a delayed announce message<commit_after>package com.nilhcem.inaccessible.memory.ui.game;

import android.os.Bundle;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.Toast;

import com.nilhcem.inaccessible.memory.BuildConfig;
import com.nilhcem.inaccessible.memory.R;
import com.nilhcem.inaccessible.memory.core.Game;
import com.nilhcem.inaccessible.memory.ui.base.BaseActivity;

import org.joda.time.DateTime;
import org.joda.time.Duration;

import java.util.Arrays;
import java.util.Random;

import icepick.Icicle;

import static com.nilhcem.inaccessible.memory.core.Game.FlippedStatus;
import static com.nilhcem.inaccessible.memory.core.Game.FlippedStatus.INVALID_PAIR;
import static com.nilhcem.inaccessible.memory.core.Game.FlippedStatus.PAIR_FOUND;

public class GameActivity extends BaseActivity implements CardView.OnCardFlippedListener {

    public static final String EXTRA_NB_CARDS = "nb_cards";
    private static final int ONE_SECOND = 1000;

    @Icicle Game mGame;
    @Icicle String[] mCheersMessages;
    @Icicle DateTime mStarted;

    private int mNbCards;
    private ViewGroup mLayout;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNbCards = getIntent().getIntExtra(EXTRA_NB_CARDS, 0);

        if (savedInstanceState == null) {
            mGame = new Game(Arrays.copyOfRange(getResources().getStringArray(R.array.animals_array), 0, mNbCards));
            mCheersMessages = getResources().getStringArray(R.array.cheers_array);
            mStarted = new DateTime();
        }
        mLayout = createLayout(mGame);
        setContentView(mLayout);
    }

    @Override
    public void onCardFlipped(int position) {
        try {
            FlippedStatus flippedStatus = mGame.flipCard(position);
            if (flippedStatus.equals(PAIR_FOUND)) {
                announceMessage(mCheersMessages[new Random().nextInt(mCheersMessages.length)], true);
            } else if (flippedStatus.equals(INVALID_PAIR)) {
                announceMessage(getString(R.string.pair_invalid), true);
            }
        } catch (UnsupportedOperationException e) {
            announceMessage(getString(R.string.card_already_flipped_error, position + 1));
        }

        updateCardsContentDescriptions();
        checkGameOver();
    }

    private ViewGroup createLayout(Game game) {
        GameLayout layout = new GameLayout(this);
        int nbCards = game.getNbCards();
        for (int i = 0; i < nbCards; i++) {
            layout.addView(new CardView(this, game.getCardAt(i), i, this));
        }
        return layout;
    }

    private void announceMessage(String message) {
        announceMessage(message, false);
    }

    private void announceMessage(final String message, boolean withDelay) {
        if (BuildConfig.DEBUG) {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        } else {
            mHandler.postDelayed(new Runnable() {
                public void run() {
                    mLayout.announceForAccessibility(message);
                }
            }, withDelay ? ONE_SECOND : 0);
        }
    }

    private void updateCardsContentDescriptions() {
        final int count = mLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            ((CardView) mLayout.getChildAt(i)).updateContentDescription();
        }
    }

    private void checkGameOver() {
        if (mGame.isOver()) {
            announceMessage(getString(R.string.game_over, mNbCards * 2, new Duration(mStarted, new DateTime()).getStandardSeconds()));
            finish();
        }
    }
}
{'fraction_non_alphanumeric': 0.10271428571428572, 'fraction_numerical': 0.0024285714285714284, 'mean_word_length': 2.959841628959276, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4742752', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2070, 'n_words': 395}