<commit_before>package ph.pakete.view;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.eggheadgames.siren.Siren;
import com.eggheadgames.siren.SirenAlertType;
import com.eggheadgames.siren.SirenVersionCheckType;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import hotchemi.android.rate.AppRate;
import hotchemi.android.rate.OnClickButtonListener;
import ph.pakete.BackHandledFragment;
import ph.pakete.PackagesAdapter;
import ph.pakete.PaketeApplication;
import ph.pakete.R;
import ph.pakete.model.MixpanelHelper;
import ph.pakete.model.Package;
import ph.pakete.viewmodel.ItemPackageViewModel;
import ph.pakete.viewmodel.PackagesViewModel;
import rx.subjects.ReplaySubject;

public class MainActivity extends AppCompatActivity implements BackHandledFragment.BackHandlerInterface {

    private PackagesViewModel viewModel;
    private BackHandledFragment selectedFragment;
    private Boolean exit = false;
    private SwipeRefreshLayout swipeContainer;
    private static final String SIREN_JSON_URL = "http://pakete.ph/siren/version.json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new PackagesViewModel(this);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        if (fab != null) {
            fab.setOnClickListener(v -> showCouriersFragment());
        }
        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        if (swipeContainer != null) {
            swipeContainer.setOnRefreshListener(() -> {
                viewModel.refreshPackages();
                swipeContainer.setRefreshing(false);
            });
        }

        setupRecyclerView((RecyclerView) findViewById(R.id.packages_recycler_view));
        setupAdBannerView();
        viewModel.packages.subscribe(replaySubjects -> {
            TextView emptyText = (TextView) findViewById(R.id.empty_text);
            if (emptyText != null) {
                if (replaySubjects.size() == 0) {
                    emptyText.setVisibility(View.VISIBLE);
                } else {
                    emptyText.setVisibility(View.INVISIBLE);
                }
            }
        });

        // track mixpanel
        MixpanelHelper.getMixpanel(this).track("Packages View");
        // listen for broadcast
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver,
                new IntentFilter("removeAds"));
        // app rater
        setupAppRater();
        // check for update. we force users to update right now
        Siren siren = Siren.getInstance(getApplicationContext());
        siren.setMajorUpdateAlertType(SirenAlertType.FORCE);
        siren.setMinorUpdateAlertType(SirenAlertType.FORCE);
        siren.setPatchUpdateAlertType(SirenAlertType.FORCE);
        siren.checkVersion(this, SirenVersionCheckType.DAILY, SIREN_JSON_URL);
    }

    @Override
    protected void onDestroy() {
        // send all mixpanel events
        MixpanelHelper.getMixpanel(this).flush();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_packages, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if(selectedFragment == null || !selectedFragment.onBackPressed()) {
            // Selected fragment did not consume the back press event.
            if (exit) {
                finish(); // finish activity
            } else {
                Toast.makeText(this, "Press Back again to Exit.",
                        Toast.LENGTH_SHORT).show();
                exit = true;
                new Handler().postDelayed(() -> exit = false, 3 * 1000);
            }
        }
    }<commit_msg>force refresh packages if app will enter foreground<commit_after>package ph.pakete.view;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.eggheadgames.siren.Siren;
import com.eggheadgames.siren.SirenAlertType;
import com.eggheadgames.siren.SirenVersionCheckType;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import hotchemi.android.rate.AppRate;
import hotchemi.android.rate.OnClickButtonListener;
import ph.pakete.BackHandledFragment;
import ph.pakete.PackagesAdapter;
import ph.pakete.PaketeApplication;
import ph.pakete.R;
import ph.pakete.model.MixpanelHelper;
import ph.pakete.model.Package;
import ph.pakete.viewmodel.ItemPackageViewModel;
import ph.pakete.viewmodel.PackagesViewModel;
import rx.subjects.ReplaySubject;

public class MainActivity extends AppCompatActivity implements BackHandledFragment.BackHandlerInterface {

    private PackagesViewModel viewModel;
    private BackHandledFragment selectedFragment;
    private Boolean exit = false;
    private SwipeRefreshLayout swipeContainer;
    private static final String SIREN_JSON_URL = "http://pakete.ph/siren/version.json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new PackagesViewModel(this);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        if (fab != null) {
            fab.setOnClickListener(v -> showCouriersFragment());
        }
        swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        if (swipeContainer != null) {
            swipeContainer.setOnRefreshListener(() -> {
                viewModel.refreshPackages();
                swipeContainer.setRefreshing(false);
            });
        }

        setupRecyclerView((RecyclerView) findViewById(R.id.packages_recycler_view));
        setupAdBannerView();
        viewModel.packages.subscribe(replaySubjects -> {
            TextView emptyText = (TextView) findViewById(R.id.empty_text);
            if (emptyText != null) {
                if (replaySubjects.size() == 0) {
                    emptyText.setVisibility(View.VISIBLE);
                } else {
                    emptyText.setVisibility(View.INVISIBLE);
                }
            }
        });

        // track mixpanel
        MixpanelHelper.getMixpanel(this).track("Packages View");
        // listen for broadcast
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver,
                new IntentFilter("removeAds"));
        // app rater
        setupAppRater();
        // check for update. we force users to update right now
        Siren siren = Siren.getInstance(getApplicationContext());
        siren.setMajorUpdateAlertType(SirenAlertType.FORCE);
        siren.setMinorUpdateAlertType(SirenAlertType.FORCE);
        siren.setPatchUpdateAlertType(SirenAlertType.FORCE);
        siren.checkVersion(this, SirenVersionCheckType.DAILY, SIREN_JSON_URL);
    }

    @Override
    protected void onDestroy() {
        // send all mixpanel events
        MixpanelHelper.getMixpanel(this).flush();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_packages, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // force refresh packages
        viewModel.refreshPackages();
    }

    @Override
    public void onBackPressed() {
        if(selectedFragment == null || !selectedFragment.onBackPressed()) {
            // Selected fragment did not consume the back press event.
            if (exit) {
                finish(); // finish activity
            } else {
                Toast.makeText(this, "Press Back again to Exit.",
                        Toast.LENGTH_SHORT).show();
                exit = true;
                new Handler().postDelayed(() -> exit = false, 3 * 1000);
            }
        }
    }
{'fraction_non_alphanumeric': 0.09330646574382337, 'fraction_numerical': 0.0024531277378657787, 'mean_word_length': 2.9402830514325164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2361927', 'n_tokens_mistral': 3227, 'n_tokens_neox': 3055, 'n_words': 638}