<commit_before>package com.abid_mujtaba.fetchheaders;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abid_mujtaba.fetchheaders.fragments.AccountFragment;
import com.abid_mujtaba.fetchheaders.interfaces.ToggleMenu;
import com.abid_mujtaba.fetchheaders.misc.ThreadPool;
import com.abid_mujtaba.fetchheaders.models.Account;
import com.abid_mujtaba.fetchheaders.models.Email;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends ActionBarActivity implements ToggleMenu, TextToSpeech.OnInitListener
{
    private LinearLayout scrollList;
    private Menu mMenu;                 // A handle to the Menu item

    private ArrayList<AccountFragment> mFragments = new ArrayList<AccountFragment>();      // Stores all fragments added to this activity

    private Handler mHandler = new Handler();           // Handler used to carry out UI actions from background threads

    private boolean fShowSeen = false;                  // Flag which control whether seen emails should be displayed or not
    private boolean fDisableMenu = false;               // A flag that indicates whether the menu should be disabled or not. We want the menu disabled when we are performing certain tasks such as fetching emails

    private String BUNDLE_FLAG_SHOW_SEEN = "BUNDLE_FLAG_SHOW_SEEN";     // Used as a key for the showSeen flag stored in the Bundle that saves state information when the activity is restarted (possibly because of screen rotation)

    private TextToSpeech mTTS;


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

        setContentView(R.layout.main);

        if (savedInstanceState != null)     // If the passed in state information bundle is non-empty we expect it to contain the saved value of fShowSeen. We also pass in a default value.
        {
            fShowSeen = savedInstanceState.getBoolean(BUNDLE_FLAG_SHOW_SEEN, false);
        }

        scrollList = (LinearLayout) findViewById(R.id.scrollList);

        if (mTTS == null)       // If onCreate is called multiple times we do NOT want to create multiple TextToSpeech objects
        {
            mTTS = new TextToSpeech(this, this);
        }

        if (Account.numberOfAccounts() > 0)             // Accounts have been specified
        {
            TextView tvEmpty = (TextView) findViewById(R.id.txtNoAccounts);     // We start by removing the No Accounts view since accounts are present
            scrollList.removeView(tvEmpty);

            FragmentManager fM = getSupportFragmentManager();
            FragmentTransaction fT = fM.beginTransaction();

            for (int ii = 0; ii < Account.numberOfAccounts(); ii++)
            {
                String tag = "TAG_" + ii;           // This is the tag we will use to get a handle on the fragment in the FragmentManager

                AccountFragment aF = (AccountFragment) fM.findFragmentByTag(tag);           // We attempt to access the fragment via the specified tag

                if (aF == null)         // This indicates that the Fragment does not exist yet so we create it. It has setRetainInstance(true) so it persists across configuration changes.
                {
                    aF = AccountFragment.newInstance(ii);

                    fT.add(R.id.scrollList, aF, tag);       // Note: The addition to the scrollList only happens when aF == null, which happens when the persistent fragment has not been created yet
                }                                           //       Since Views retain state across config changes the scrollList remembers that it has fragments added to it

                mFragments.add(aF);
            }

            fT.commit();
        }
    }


    @Override
    protected void onDestroy()
    {
        if (mTTS != null)           // mTTS needs to be properly shutdown otherwise the app will complain about a leaked service
        {
            mTTS.stop();
            mTTS.shutdown();
        }

        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_menu, menu);

        mMenu = menu;       // Store a handle to the Menu item.<commit_msg>Pausing the MainActivity (possibly by pressing the back button to exit) stops the TTS.<commit_after>package com.abid_mujtaba.fetchheaders;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abid_mujtaba.fetchheaders.fragments.AccountFragment;
import com.abid_mujtaba.fetchheaders.interfaces.ToggleMenu;
import com.abid_mujtaba.fetchheaders.misc.ThreadPool;
import com.abid_mujtaba.fetchheaders.models.Account;
import com.abid_mujtaba.fetchheaders.models.Email;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends ActionBarActivity implements ToggleMenu, TextToSpeech.OnInitListener
{
    private LinearLayout scrollList;
    private Menu mMenu;                 // A handle to the Menu item

    private ArrayList<AccountFragment> mFragments = new ArrayList<AccountFragment>();      // Stores all fragments added to this activity

    private Handler mHandler = new Handler();           // Handler used to carry out UI actions from background threads

    private boolean fShowSeen = false;                  // Flag which control whether seen emails should be displayed or not
    private boolean fDisableMenu = false;               // A flag that indicates whether the menu should be disabled or not. We want the menu disabled when we are performing certain tasks such as fetching emails

    private String BUNDLE_FLAG_SHOW_SEEN = "BUNDLE_FLAG_SHOW_SEEN";     // Used as a key for the showSeen flag stored in the Bundle that saves state information when the activity is restarted (possibly because of screen rotation)

    private TextToSpeech mTTS;


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

        setContentView(R.layout.main);

        if (savedInstanceState != null)     // If the passed in state information bundle is non-empty we expect it to contain the saved value of fShowSeen. We also pass in a default value.
        {
            fShowSeen = savedInstanceState.getBoolean(BUNDLE_FLAG_SHOW_SEEN, false);
        }

        scrollList = (LinearLayout) findViewById(R.id.scrollList);

        if (mTTS == null)       // If onCreate is called multiple times we do NOT want to create multiple TextToSpeech objects
        {
            mTTS = new TextToSpeech(this, this);
        }

        if (Account.numberOfAccounts() > 0)             // Accounts have been specified
        {
            TextView tvEmpty = (TextView) findViewById(R.id.txtNoAccounts);     // We start by removing the No Accounts view since accounts are present
            scrollList.removeView(tvEmpty);

            FragmentManager fM = getSupportFragmentManager();
            FragmentTransaction fT = fM.beginTransaction();

            for (int ii = 0; ii < Account.numberOfAccounts(); ii++)
            {
                String tag = "TAG_" + ii;           // This is the tag we will use to get a handle on the fragment in the FragmentManager

                AccountFragment aF = (AccountFragment) fM.findFragmentByTag(tag);           // We attempt to access the fragment via the specified tag

                if (aF == null)         // This indicates that the Fragment does not exist yet so we create it. It has setRetainInstance(true) so it persists across configuration changes.
                {
                    aF = AccountFragment.newInstance(ii);

                    fT.add(R.id.scrollList, aF, tag);       // Note: The addition to the scrollList only happens when aF == null, which happens when the persistent fragment has not been created yet
                }                                           //       Since Views retain state across config changes the scrollList remembers that it has fragments added to it

                mFragments.add(aF);
            }

            fT.commit();
        }
    }


    @Override
    protected void onDestroy()
    {
        if (mTTS != null)           // mTTS needs to be properly shutdown otherwise the app will complain about a leaked service
        {
            mTTS.shutdown();
        }

        super.onDestroy();
    }


    @Override
    protected void onStop()
    {
        if (mTTS != null)
        {
            mTTS.stop();        // We stop the TTS engine when the activity stops. This stops ALL current and queued TTS activity.
        }

        super.onStop();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_menu, menu);

        mMenu = menu;       // Store a handle to the Menu item.
{'fraction_non_alphanumeric': 0.06882383153569595, 'fraction_numerical': 0.001027221366204417, 'mean_word_length': 2.5148014440433215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5623214', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2431, 'n_words': 875}