<commit_before>package com.leavjenn.hews.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
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.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.firebase.client.Firebase;
import com.leavjenn.hews.Constants;
import com.leavjenn.hews.R;
import com.leavjenn.hews.listener.OnRecyclerViewCreateListener;
import com.leavjenn.hews.misc.ChromeCustomTabsHelper;
import com.leavjenn.hews.misc.SharedPrefsManager;
import com.leavjenn.hews.Utils;
import com.leavjenn.hews.model.Post;
import com.leavjenn.hews.network.DataManager;
import com.leavjenn.hews.ui.adapter.PostAdapter;
import com.leavjenn.hews.ui.widget.AlwaysShowDialogSpinner;
import com.leavjenn.hews.ui.widget.DateRangeDialogFragment;
import com.leavjenn.hews.ui.widget.FeedbackDialogFragment;
import com.leavjenn.hews.ui.widget.FloatingScrollDownButton;
import com.leavjenn.hews.ui.widget.LoginDialogFragment;
import com.leavjenn.hews.ui.widget.PopupFloatingWindow;

import org.parceler.Parcels;

import java.util.Calendar;
import java.util.TimeZone;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;


public class MainActivity extends AppCompatActivity implements PostAdapter.OnItemClickListener,
        SharedPreferences.OnSharedPreferenceChangeListener, OnRecyclerViewCreateListener,
        AppBarLayout.OnOffsetChangedListener {

    private static final int DRAWER_CLOSE_DELAY_MS = 250;
    private static final String STATE_DRAWER_SELECTED_ITEM = "state_drawer_selected_item";
    private static final String STATE_POPULAR_DATE_RANGE = "state_popular_date_range";
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private View drawerHeader;
    private LinearLayout layoutLogin;
    private TextView tvLoginName;
    private ImageView ivExpander;
    private boolean isLoginMenuExpanded;
    private int mDrawerSelectedItem;
    private final Handler mDrawerActionHandler = new Handler();
    private ActionBarDrawerToggle mDrawerToggle;
    private AppBarLayout mAppbar;
    private Toolbar toolbar;
    private AlwaysShowDialogSpinner mSpinnerDateRange;
    private Spinner mSpinnerSortOrder;
    private PopupFloatingWindow mWindow;
    private SearchView mSearchView;
    private MenuItem mSearchItem;
    private boolean isSearchKeywordSubmitted;
    private FloatingScrollDownButton mFab;
    private String mStoryType, mStoryTypeSpec;
    private SharedPreferences prefs;
    private DataManager mDataManager;
    private CompositeSubscription mCompositeSubscription;
    private ChromeCustomTabsHelper mChromeCustomTabsHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set theme
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        String theme = SharedPrefsManager.getTheme(prefs);
        if (theme.equals(SharedPrefsManager.THEME_SEPIA)) {
            setTheme(R.style.AppTheme_Sepia);
        }
        if (theme.equals(SharedPrefsManager.THEME_DARK)) {
            setTheme(R.style.AppTheme_Dark);
        }
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);
        // setup Toolbar
        mAppbar = (AppBarLayout) findViewById(R.id.appbar);
        mAppbar.addOnOffsetChangedListener(this);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        // init spinner
        mSpinnerDateRange = (AlwaysShowDialogSpinner) findViewById(R.id.spinner_time_range);
        mSpinnerSortOrder = (Spinner) findViewById(R.id.spinner_sort_order);
        //setup drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        if (mNavigationView != null) {
            setupDrawerContent(mNavigationView);
        }
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer,
                R.string.close_drawer);
        // TODO inflate drawerHeader due to a bug:
        // https://code.google.com/p/android/issues/detail?id=190226
        drawerHeader = getLayoutInflater().inflate(R.layout.drawer_header, null);
        mNavigationView.addHeaderView(drawerHeader);
        layoutLogin = (LinearLayout) drawerHeader.findViewById(R.id.layout_login);
        tvLoginName = (TextView) drawerHeader.findViewById(R.id.tv_account);
        updateLoginName();
        layoutLogin = (LinearLayout) findViewById(R.id.layout_login);
        layoutLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickLogin();
            }
        });
        ivExpander = (ImageView) findViewById(R.id.iv_expander);

        mFab = (FloatingScrollDownButton) findViewById(R.id.fab);

        mWindow = new PopupFloatingWindow(this, mAppbar);

        if (getFragmentManager().findFragmentById(R.id.container) == null) {
            Log.i("act oncreate", "null frag");
            mStoryType = Constants.STORY_TYPE_TOP_URL;
            mStoryTypeSpec = Constants.STORY_TYPE_TOP_URL;
            PostFragment postFragment = PostFragment.newInstance(mStoryType, mStoryTypeSpec);
            getFragmentManager().beginTransaction().add(R.id.container, postFragment).commit();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //init mCompositeSubscription here due to onCreate() will not be called
//        when theme changed (call recreate())
        mCompositeSubscription = new CompositeSubscription();
        mDataManager = new DataManager();
        if (SharedPrefsManager.getIsOpenLinkInApp(prefs, this)
                && ChromeCustomTabsHelper.getPackageNameToUse(this) != null) {
            mChromeCustomTabsHelper = new ChromeCustomTabsHelper();
            mChromeCustomTabsHelper.bindCustomTabsService(this);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mDrawerSelectedItem = savedInstanceState.getInt(STATE_DRAWER_SELECTED_ITEM, 0);
        Menu menu = mNavigationView.getMenu();
        //mDrawerSelectedItem + 2 to skip login and logout
        menu.getItem(mDrawerSelectedItem + 2).setChecked(true);
        //TODO bug: item including login part
        Log.i("mDrawerSelectedItem", String.valueOf(mDrawerSelectedItem));
        if (mDrawerSelectedItem == 4) { // popular
            Log.i("mDrawerSelectedItem", "DateRange");<commit_msg>fix (wrong parameter): pass correct story type when instantiate PostFragment.<commit_after>package com.leavjenn.hews.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
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.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.firebase.client.Firebase;
import com.leavjenn.hews.Constants;
import com.leavjenn.hews.R;
import com.leavjenn.hews.listener.OnRecyclerViewCreateListener;
import com.leavjenn.hews.misc.ChromeCustomTabsHelper;
import com.leavjenn.hews.misc.SharedPrefsManager;
import com.leavjenn.hews.Utils;
import com.leavjenn.hews.model.Post;
import com.leavjenn.hews.network.DataManager;
import com.leavjenn.hews.ui.adapter.PostAdapter;
import com.leavjenn.hews.ui.widget.AlwaysShowDialogSpinner;
import com.leavjenn.hews.ui.widget.DateRangeDialogFragment;
import com.leavjenn.hews.ui.widget.FeedbackDialogFragment;
import com.leavjenn.hews.ui.widget.FloatingScrollDownButton;
import com.leavjenn.hews.ui.widget.LoginDialogFragment;
import com.leavjenn.hews.ui.widget.PopupFloatingWindow;

import org.parceler.Parcels;

import java.util.Calendar;
import java.util.TimeZone;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;


public class MainActivity extends AppCompatActivity implements PostAdapter.OnItemClickListener,
        SharedPreferences.OnSharedPreferenceChangeListener, OnRecyclerViewCreateListener,
        AppBarLayout.OnOffsetChangedListener {

    private static final int DRAWER_CLOSE_DELAY_MS = 250;
    private static final String STATE_DRAWER_SELECTED_ITEM = "state_drawer_selected_item";
    private static final String STATE_POPULAR_DATE_RANGE = "state_popular_date_range";
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private View drawerHeader;
    private LinearLayout layoutLogin;
    private TextView tvLoginName;
    private ImageView ivExpander;
    private boolean isLoginMenuExpanded;
    private int mDrawerSelectedItem;
    private final Handler mDrawerActionHandler = new Handler();
    private ActionBarDrawerToggle mDrawerToggle;
    private AppBarLayout mAppbar;
    private Toolbar toolbar;
    private AlwaysShowDialogSpinner mSpinnerDateRange;
    private Spinner mSpinnerSortOrder;
    private PopupFloatingWindow mWindow;
    private SearchView mSearchView;
    private MenuItem mSearchItem;
    private boolean isSearchKeywordSubmitted;
    private FloatingScrollDownButton mFab;
    private String mStoryType, mStoryTypeSpec;
    private SharedPreferences prefs;
    private DataManager mDataManager;
    private CompositeSubscription mCompositeSubscription;
    private ChromeCustomTabsHelper mChromeCustomTabsHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set theme
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        String theme = SharedPrefsManager.getTheme(prefs);
        if (theme.equals(SharedPrefsManager.THEME_SEPIA)) {
            setTheme(R.style.AppTheme_Sepia);
        }
        if (theme.equals(SharedPrefsManager.THEME_DARK)) {
            setTheme(R.style.AppTheme_Dark);
        }
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);
        // setup Toolbar
        mAppbar = (AppBarLayout) findViewById(R.id.appbar);
        mAppbar.addOnOffsetChangedListener(this);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        // init spinner
        mSpinnerDateRange = (AlwaysShowDialogSpinner) findViewById(R.id.spinner_time_range);
        mSpinnerSortOrder = (Spinner) findViewById(R.id.spinner_sort_order);
        //setup drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        if (mNavigationView != null) {
            setupDrawerContent(mNavigationView);
        }
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer,
                R.string.close_drawer);
        // TODO inflate drawerHeader due to a bug:
        // https://code.google.com/p/android/issues/detail?id=190226
        drawerHeader = getLayoutInflater().inflate(R.layout.drawer_header, null);
        mNavigationView.addHeaderView(drawerHeader);
        layoutLogin = (LinearLayout) drawerHeader.findViewById(R.id.layout_login);
        tvLoginName = (TextView) drawerHeader.findViewById(R.id.tv_account);
        updateLoginName();
        layoutLogin = (LinearLayout) findViewById(R.id.layout_login);
        layoutLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickLogin();
            }
        });
        ivExpander = (ImageView) findViewById(R.id.iv_expander);

        mFab = (FloatingScrollDownButton) findViewById(R.id.fab);

        mWindow = new PopupFloatingWindow(this, mAppbar);

        if (getFragmentManager().findFragmentById(R.id.container) == null) {
            Log.i("act oncreate", "null frag");
            mStoryType = Constants.TYPE_STORY;
            mStoryTypeSpec = Constants.STORY_TYPE_TOP_URL;
            PostFragment postFragment = PostFragment.newInstance(mStoryType, mStoryTypeSpec);
            getFragmentManager().beginTransaction().add(R.id.container, postFragment).commit();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //init mCompositeSubscription here due to onCreate() will not be called
//        when theme changed (call recreate())
        mCompositeSubscription = new CompositeSubscription();
        mDataManager = new DataManager();
        if (SharedPrefsManager.getIsOpenLinkInApp(prefs, this)
                && ChromeCustomTabsHelper.getPackageNameToUse(this) != null) {
            mChromeCustomTabsHelper = new ChromeCustomTabsHelper();
            mChromeCustomTabsHelper.bindCustomTabsService(this);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mDrawerSelectedItem = savedInstanceState.getInt(STATE_DRAWER_SELECTED_ITEM, 0);
        Menu menu = mNavigationView.getMenu();
        //mDrawerSelectedItem + 2 to skip login and logout
        menu.getItem(mDrawerSelectedItem + 2).setChecked(true);
        //TODO bug: item including login part
        Log.i("mDrawerSelectedItem", String.valueOf(mDrawerSelectedItem));
        if (mDrawerSelectedItem == 4) { // popular
            Log.i("mDrawerSelectedItem", "DateRange");
{'fraction_non_alphanumeric': 0.08606856309263311, 'fraction_numerical': 0.002674446875759786, 'mean_word_length': 3.966193782070631, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3135950', 'n_tokens_mistral': 4780, 'n_tokens_neox': 4653, 'n_words': 848}