<commit_before>package io.jari.dumpert.activities;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import io.jari.dumpert.R;
import io.jari.dumpert.api.Login;
import io.jari.dumpert.dialogs.LoginDialog;
import io.jari.dumpert.fragments.*;

// username set to dummy to ensure the login functionality is not used.

public class MainActivity extends BaseActivity implements
        NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG                   = "DMA";
    private static final long   DRAWER_CLOSE_DELAY_MS = 250;

    public SharedPreferences preferences;

    private        FragmentManager manager;
    private        DrawerLayout    drawer;
    private static SharedPreferences credentials;
    private static NavigationView  navigationView;
    private static TextView        loginAction;
    private        int             navItemID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MainActivity.credentials = getSharedPreferences("dumpert", 0);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        manager = getFragmentManager();
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        if(getSupportActionBar() == null) setSupportActionBar(toolbar);

        navItemID = R.id.nav_new;
        navigationView = (NavigationView) findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.getMenu().findItem(navItemID).setChecked(true);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.open_drawer, R.string.close_drawer);

        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigate(navItemID);

        loginAction = (TextView) navigationView.getHeaderView(0).findViewById(R.id.login_action);
        loginAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag().equals("Login")) {
                    new LoginDialog().show(manager, "Login");
                    notifyAccountChanged();
                } else {
                    if(Login.logout(MainActivity.this)) {
                        notifyAccountChanged();
                    } else {
                        Toast.makeText(MainActivity.this, R.string.error_could_not_logout,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        notifyAccountChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();

        navigate(navItemID);
        notifyAccountChanged();
    }

    @Override
    public void onBackPressed() {
        if(drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(final MenuItem item) {
        item.setChecked(true);
        navItemID = item.getItemId();

        drawer.closeDrawer(GravityCompat.START);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                navigate(item.getItemId());
            }
        }, DRAWER_CLOSE_DELAY_MS);

        return true;
    }

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

        SearchView searchView = (SearchView) menu.findItem(R.id.nav_search).getActionView();

        searchView.setIconified(false);
        searchView.clearFocus();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent = new Intent(MainActivity.this, SearchResultsActivity.class);
                intent.setAction(Intent.ACTION_SEARCH);
                intent.putExtra(SearchManager.QUERY, query);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }


        });

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.nav_settings) {
            Intent settings = new Intent(MainActivity.this, PreferencesActivity.class);
            this.startActivity(settings);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void navigate(int itemID) {
        FragmentTransaction transaction = manager.beginTransaction();
        int title = R.string.app_name;

        switch(itemID) {
            case R.id.nav_new:
                title = R.string.nav_new;
                transaction.replace(R.id.rootView, new NewFragment());
                break;
            case R.id.nav_top:
                title = R.string.nav_top;
                transaction.replace(R.id.rootView, new TopFragment());
                break;
            case R.id.nav_images:
                title = R.string.nav_images;
                transaction.replace(R.id.rootView, new ImageFragment());
                break;
            case R.id.nav_videos:
                title = R.string.nav_videos;
                transaction.replace(R.id.rootView, new VideoFragment());
                break;
            case R.id.nav_audio:
                title = R.string.nav_audio;<commit_msg>cleaned up a bit<commit_after>package io.jari.dumpert.activities;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import io.jari.dumpert.R;
import io.jari.dumpert.api.Login;
import io.jari.dumpert.dialogs.LoginDialog;
import io.jari.dumpert.fragments.*;

// username set to dummy to ensure the login functionality is not used.

public class MainActivity extends BaseActivity implements
        NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG                   = "DMA";
    private static final long   DRAWER_CLOSE_DELAY_MS = 250;

    public SharedPreferences preferences;

    private        FragmentManager   manager;
    private        DrawerLayout      drawer;
    private static SharedPreferences credentials;
    private static NavigationView    navigationView;
    private static TextView          loginAction;
    private        int               navItemID = R.id.nav_new;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUI();
        navigate(navItemID);
    }

    @Override
    protected void onPause() {
        drawer = null;
        credentials = null;
        navigationView = null;
        loginAction = null;

        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

        initUI();
    }

    @Override
    public void onBackPressed() {
        if(drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(final MenuItem item) {
        item.setChecked(true);
        navItemID = item.getItemId();

        drawer.closeDrawer(GravityCompat.START);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                navigate(item.getItemId());
            }
        }, DRAWER_CLOSE_DELAY_MS);

        return true;
    }

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

        SearchView searchView = (SearchView) menu.findItem(R.id.nav_search).getActionView();

        searchView.setIconified(false);
        searchView.clearFocus();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent = new Intent(MainActivity.this, SearchResultsActivity.class);
                intent.setAction(Intent.ACTION_SEARCH);
                intent.putExtra(SearchManager.QUERY, query);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }


        });

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.nav_settings) {
            Intent settings = new Intent(MainActivity.this, PreferencesActivity.class);
            this.startActivity(settings);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initUI() {
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        manager = getFragmentManager();
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        credentials = getSharedPreferences("dumpert", 0);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        if(getSupportActionBar() == null) setSupportActionBar(toolbar);

        navigationView = (NavigationView) findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.getMenu().findItem(navItemID).setChecked(true);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.open_drawer, R.string.close_drawer);

        drawer.setDrawerListener(toggle);
        toggle.syncState();

        loginAction = (TextView) navigationView.getHeaderView(0).findViewById(R.id.login_action);
        loginAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag().equals("Login")) {
                    new LoginDialog().show(manager, "Login");
                    notifyAccountChanged();
                } else {
                    if(Login.logout(MainActivity.this)) {
                        notifyAccountChanged();
                    } else {
                        Toast.makeText(MainActivity.this, R.string.error_could_not_logout,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        notifyAccountChanged();
    }

    private void navigate(int itemID) {
        FragmentTransaction transaction = manager.beginTransaction();
        int title = R.string.app_name;

        switch(itemID) {
            case R.id.nav_new:
                title = R.string.nav_new;
                transaction.replace(R.id.rootView, new NewFragment());
                break;
            case R.id.nav_top:
                title = R.string.nav_top;
                transaction.replace(R.id.rootView, new TopFragment());
                break;
            case R.id.nav_images:
                title = R.string.nav_images;
                transaction.replace(R.id.rootView, new ImageFragment());
                break;
            case R.id.nav_videos:
                title = R.string.nav_videos;
                transaction.replace(R.id.rootView, new VideoFragment());
                break;
            case R.id.nav_audio:
                title = R.string.nav_audio;
{'fraction_non_alphanumeric': 0.090751619219762, 'fraction_numerical': 0.0015062509414068383, 'mean_word_length': 2.241152062484745, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6000133', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3419, 'n_words': 659}