<commit_before>package com.sferadev.danacast.activities;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.github.sv244.torrentstream.StreamStatus;
import com.github.sv244.torrentstream.Torrent;
import com.github.sv244.torrentstream.TorrentOptions;
import com.github.sv244.torrentstream.TorrentStream;
import com.github.sv244.torrentstream.listeners.TorrentListener;
import com.google.android.libraries.cast.companionlibrary.cast.BaseCastManager;
import com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager;
import com.google.android.libraries.cast.companionlibrary.widgets.MiniController;
import com.instabug.wrapper.support.activity.InstabugAppCompatActivity;
import com.sferadev.danacast.R;
import com.sferadev.danacast.helpers.Category;
import com.sferadev.danacast.helpers.Constants;
import com.sferadev.danacast.helpers.Provider;
import com.sferadev.danacast.models.EntryModel;
import com.sferadev.danacast.utils.ContentUtils;
import com.sferadev.danacast.utils.NetworkUtils;
import com.sferadev.danacast.utils.PreferenceUtils;
import com.sferadev.danacast.utils.UpdateUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends InstabugAppCompatActivity implements AdapterView.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener, TorrentListener {

    private VideoCastManager mCastManager;
    private MiniController mMini;
    private SwipeRefreshLayout mRefresh;
    private SearchView searchView;

    private ArrayList<ArrayList<EntryModel>> mContent = new ArrayList<>();
    private ArrayAdapter mAdapter;

    private String LAST_CONTENT;

    private TorrentStream mTorrentStream;
    private ProgressDialog torrentProgressDialog;

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

        mCastManager = VideoCastManager.getInstance();
        mCastManager.reconnectSessionIfPossible();
        mMini = (MiniController) findViewById(R.id.miniController);
        mCastManager.addMiniController(mMini);
        mCastManager.setCastControllerImmersive(false);

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

        mRefresh = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);

        mRefresh.setColorSchemeResources(R.color.colorAccent);
        mRefresh.setOnRefreshListener(this);

        ListView mListView = (ListView) findViewById(R.id.listview);

        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                EntryModel entry = mContent.get(mContent.size() - 1).get(position);
                if (entry.getLink() != null) {
                    ContentUtils.addToClipboard(MainActivity.this, entry.getLink());
                }
                return false;
            }
        });

        handleIntent(getIntent());

        List<String> items = new ArrayList<>();
        for (EntryModel object : mContent.get(mContent.size() - 1))
            items.add(object.getTitle());
        mAdapter = new ArrayAdapter<>(
                this,
                R.layout.list_row,
                R.id.drawer_text,
                items);
        mListView.setAdapter(mAdapter);

        TorrentOptions torrentOptions = new TorrentOptions();
        ContentUtils.mFilesPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS) + File.separator + "DanaCast" + File.separator;
        torrentOptions.setSaveLocation(ContentUtils.mFilesPath + "TorrentCache");
        torrentOptions.setRemoveFilesAfterStop(true);

        mTorrentStream = TorrentStream.init(torrentOptions);
        mTorrentStream.addListener(this);

        UpdateUtils.checkUpdates(this);

        ContentUtils.removeLocalFiles("TorrentCache");
    }

    @Override
    public void onResume() {
        mCastManager = VideoCastManager.getInstance();
        if (null != mCastManager) {
            mCastManager.incrementUiCounter();
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        mCastManager.decrementUiCounter();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (null != mCastManager) {
            mMini.removeOnMiniControllerChangedListener(mCastManager);
            mCastManager.removeMiniController(mMini);
        }
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }

    @Override
    public void onRefresh() {
        // Handle the refresh of mRefresh
        mRefresh.setRefreshing(true);
        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(false);
                mContent.add(Category.getCategories());
                updateListview();
            }
        }, 2500);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
        MenuItem searchItem = menu.findItem(R.id.action_search);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = null;
        if (searchItem != null) searchView = (SearchView) searchItem.getActionView();<commit_msg>Dana: Don't go home on refresh<commit_after>package com.sferadev.danacast.activities;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.github.sv244.torrentstream.StreamStatus;
import com.github.sv244.torrentstream.Torrent;
import com.github.sv244.torrentstream.TorrentOptions;
import com.github.sv244.torrentstream.TorrentStream;
import com.github.sv244.torrentstream.listeners.TorrentListener;
import com.google.android.libraries.cast.companionlibrary.cast.BaseCastManager;
import com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager;
import com.google.android.libraries.cast.companionlibrary.widgets.MiniController;
import com.instabug.wrapper.support.activity.InstabugAppCompatActivity;
import com.sferadev.danacast.R;
import com.sferadev.danacast.helpers.Category;
import com.sferadev.danacast.helpers.Constants;
import com.sferadev.danacast.helpers.Provider;
import com.sferadev.danacast.models.EntryModel;
import com.sferadev.danacast.utils.ContentUtils;
import com.sferadev.danacast.utils.NetworkUtils;
import com.sferadev.danacast.utils.PreferenceUtils;
import com.sferadev.danacast.utils.UpdateUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends InstabugAppCompatActivity implements AdapterView.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener, TorrentListener {

    private VideoCastManager mCastManager;
    private MiniController mMini;
    private SwipeRefreshLayout mRefresh;
    private SearchView searchView;

    private ArrayList<ArrayList<EntryModel>> mContent = new ArrayList<>();
    private ArrayAdapter mAdapter;

    private String LAST_CONTENT;

    private TorrentStream mTorrentStream;
    private ProgressDialog torrentProgressDialog;

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

        mCastManager = VideoCastManager.getInstance();
        mCastManager.reconnectSessionIfPossible();
        mMini = (MiniController) findViewById(R.id.miniController);
        mCastManager.addMiniController(mMini);
        mCastManager.setCastControllerImmersive(false);

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

        mRefresh = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);

        mRefresh.setColorSchemeResources(R.color.colorAccent);
        mRefresh.setOnRefreshListener(this);

        ListView mListView = (ListView) findViewById(R.id.listview);

        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                EntryModel entry = mContent.get(mContent.size() - 1).get(position);
                if (entry.getLink() != null) {
                    ContentUtils.addToClipboard(MainActivity.this, entry.getLink());
                }
                return false;
            }
        });

        handleIntent(getIntent());

        List<String> items = new ArrayList<>();
        for (EntryModel object : mContent.get(mContent.size() - 1))
            items.add(object.getTitle());
        mAdapter = new ArrayAdapter<>(
                this,
                R.layout.list_row,
                R.id.drawer_text,
                items);
        mListView.setAdapter(mAdapter);

        TorrentOptions torrentOptions = new TorrentOptions();
        ContentUtils.mFilesPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS) + File.separator + "DanaCast" + File.separator;
        torrentOptions.setSaveLocation(ContentUtils.mFilesPath + "TorrentCache");
        torrentOptions.setRemoveFilesAfterStop(true);

        mTorrentStream = TorrentStream.init(torrentOptions);
        mTorrentStream.addListener(this);

        UpdateUtils.checkUpdates(this);

        ContentUtils.removeLocalFiles("TorrentCache");
    }

    @Override
    public void onResume() {
        mCastManager = VideoCastManager.getInstance();
        if (null != mCastManager) {
            mCastManager.incrementUiCounter();
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        mCastManager.decrementUiCounter();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (null != mCastManager) {
            mMini.removeOnMiniControllerChangedListener(mCastManager);
            mCastManager.removeMiniController(mMini);
        }
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }

    @Override
    public void onRefresh() {
        // Handle the refresh of mRefresh
        mRefresh.setRefreshing(true);
        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(false);
                //mContent.add(Category.getCategories());
                updateListview();
            }
        }, 2500);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
        MenuItem searchItem = menu.findItem(R.id.action_search);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = null;
        if (searchItem != null) searchView = (SearchView) searchItem.getActionView();
{'fraction_non_alphanumeric': 0.0941814989224998, 'fraction_numerical': 0.003831111820576263, 'mean_word_length': 3.3177119228118537, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3020807', 'n_tokens_mistral': 3573, 'n_tokens_neox': 3413, 'n_words': 578}