<commit_before>/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.antew.redditinpictures.library.ui;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.antew.redditinpictures.library.adapter.CursorPagerAdapter;
import com.antew.redditinpictures.library.enums.Age;
import com.antew.redditinpictures.library.enums.Category;
import com.antew.redditinpictures.library.enums.Vote;
import com.antew.redditinpictures.library.preferences.SharedPreferencesHelper;
import com.antew.redditinpictures.library.reddit.PostData;
import com.antew.redditinpictures.library.reddit.RedditLoginInformation;
import com.antew.redditinpictures.library.reddit.RedditUrl;
import com.antew.redditinpictures.library.service.RedditService;
import com.antew.redditinpictures.library.Constants;
import com.antew.redditinpictures.library.utils.Ln;
import com.antew.redditinpictures.library.utils.StringUtil;
import com.antew.redditinpictures.library.utils.Strings;
import com.antew.redditinpictures.pro.R;
import com.antew.redditinpictures.sqlite.RedditContract;
import java.util.ArrayList;
import java.util.List;

public class ImageDetailActivity extends ImageViewerActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    protected MenuItem  mUpvoteMenuItem;
    protected MenuItem  mDownvoteMenuItem;
    protected RedditUrl mRedditUrl;
    protected boolean   mRequestInProgress;
    private   String    mAfter;
    private   String    mBefore;
    private   Category  mCategory;
    private   Age       mAge;
    private   String    mSubreddit;
    private   int       mRequestedPage;
    private boolean mFirstLoad = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        displayVote();

        getSupportLoaderManager().initLoader(Constants.LOADER_REDDIT, null, this);
        getSupportLoaderManager().initLoader(Constants.LOADER_POSTS, null, this);
        // Put the current page / total pages text in the ActionBar
        updateDisplay(mPager.getCurrentItem());
    }

    public void getExtras() {
        Intent i = getIntent();

        if (i.hasExtra(Constants.EXTRA_AGE)) {
            mAge = Age.valueOf(i.getStringExtra(Constants.EXTRA_AGE));
        }

        if (i.hasExtra(Constants.EXTRA_CATEGORY)) {
            mCategory = Category.valueOf(i.getStringExtra(Constants.EXTRA_CATEGORY));
        }

        if (i.hasExtra(Constants.EXTRA_SELECTED_SUBREDDIT)) {
            mSubreddit = i.getStringExtra(Constants.EXTRA_SELECTED_SUBREDDIT);
        }

        mRequestedPage = getIntent().getIntExtra(Constants.EXTRA_IMAGE, -1);

        Ln.d("Got Extras: Age %s Category %s Subreddit %s", mAge, mCategory, mSubreddit);
    }

    public FragmentStatePagerAdapter getPagerAdapter() {
        return new CursorPagerAdapter(getSupportFragmentManager(), null);
    }

    /**
     * Update the vote
     */
    protected void updateDisplay(int position) {
        PostData p = getAdapter().getPost(position);
        if (p != null) {
            displayVote(p.getVote());
        }

        int count = getAdapter().getCount();
        if (count > 0) {
            getSupportActionBar().setTitle(++position + "/" + getAdapter().getCount() + " - " + getString(R.string.reddit_in_pictures));
        } else {
            getSupportActionBar().setTitle(getString(R.string.reddit_in_pictures));
        }
    }

    @Override
    public void reachedLastPage() {
        super.reachedLastPage();

        if (!isRequestInProgress() && mAdapter.getCount() > 0) {
            Ln.d("Reached last page, loading more images");
            setRequestInProgress(true);
            RedditService.getPosts(this, mSubreddit, mAge, mCategory, mAfter);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        displayVote();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putParcelable(Constants.EXTRA_REDDIT_URL, mRedditUrl);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        if (savedInstanceState.containsKey(Constants.EXTRA_REDDIT_URL)) {
            mRedditUrl = savedInstanceState.getParcelable(Constants.EXTRA_REDDIT_URL);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.image_detail_menu, menu);
        super.onCreateOptionsMenu(menu);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // We save the upvote and downvote icons so that we can change their icon later
        mUpvoteMenuItem = menu.findItem(R.id.upvote);
        mDownvoteMenuItem = menu.findItem(R.id.downvote);

        if (!RedditLoginInformation.isLoggedIn()) {
            mUpvoteMenuItem.setVisible(false);
            mDownvoteMenuItem.setVisible(false);
        }

        // We save the icon for locking the view pager so that we can reference
        // it when we receive a broadcast message to toggle the ViewPager lock state
        lockViewPagerItem = menu.findItem(R.id.lock_viewpager);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        int itemId = item.getItemId();
        if (itemId == R.id.upvote || itemId == R.id.downvote) {
            handleVote(item);
        }

        return true;
    }

    public void handleVote(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.upvote) {
            vote(Vote.UP, item, getAdapter().getPost(mPager.getCurrentItem()));
        } else if (itemId == R.id.downvote) {
            vote(Vote.DOWN, item, getAdapter().getPost(mPager.getCurrentItem()));
        }
    }

    /**
     * Handles updating the vote based on the action bar vote icon that was clicked, broadcasts a
     * message to have the fragment update the score.
     * <p>
     * If the user is not logged in, we return immediately.<commit_msg>Made Upvote/Downvote show when not logged in. Fixes #112<commit_after>/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.antew.redditinpictures.library.ui;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.antew.redditinpictures.library.Constants;
import com.antew.redditinpictures.library.adapter.CursorPagerAdapter;
import com.antew.redditinpictures.library.dialog.LoginDialogFragment;
import com.antew.redditinpictures.library.enums.Age;
import com.antew.redditinpictures.library.enums.Category;
import com.antew.redditinpictures.library.enums.Vote;
import com.antew.redditinpictures.library.preferences.SharedPreferencesHelper;
import com.antew.redditinpictures.library.reddit.PostData;
import com.antew.redditinpictures.library.reddit.RedditLoginInformation;
import com.antew.redditinpictures.library.reddit.RedditUrl;
import com.antew.redditinpictures.library.service.RedditService;
import com.antew.redditinpictures.library.utils.Ln;
import com.antew.redditinpictures.library.utils.StringUtil;
import com.antew.redditinpictures.library.utils.Strings;
import com.antew.redditinpictures.pro.R;
import com.antew.redditinpictures.sqlite.RedditContract;
import java.util.ArrayList;
import java.util.List;

public class ImageDetailActivity extends ImageViewerActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    protected MenuItem  mUpvoteMenuItem;
    protected MenuItem  mDownvoteMenuItem;
    protected RedditUrl mRedditUrl;
    protected boolean   mRequestInProgress;
    private   String    mAfter;
    private   String    mBefore;
    private   Category  mCategory;
    private   Age       mAge;
    private   String    mSubreddit;
    private   int       mRequestedPage;
    private boolean mFirstLoad = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        displayVote();

        getSupportLoaderManager().initLoader(Constants.LOADER_REDDIT, null, this);
        getSupportLoaderManager().initLoader(Constants.LOADER_POSTS, null, this);
        // Put the current page / total pages text in the ActionBar
        updateDisplay(mPager.getCurrentItem());
    }

    public void getExtras() {
        Intent i = getIntent();

        if (i.hasExtra(Constants.EXTRA_AGE)) {
            mAge = Age.valueOf(i.getStringExtra(Constants.EXTRA_AGE));
        }

        if (i.hasExtra(Constants.EXTRA_CATEGORY)) {
            mCategory = Category.valueOf(i.getStringExtra(Constants.EXTRA_CATEGORY));
        }

        if (i.hasExtra(Constants.EXTRA_SELECTED_SUBREDDIT)) {
            mSubreddit = i.getStringExtra(Constants.EXTRA_SELECTED_SUBREDDIT);
        }

        mRequestedPage = getIntent().getIntExtra(Constants.EXTRA_IMAGE, -1);

        Ln.d("Got Extras: Age %s Category %s Subreddit %s", mAge, mCategory, mSubreddit);
    }

    public FragmentStatePagerAdapter getPagerAdapter() {
        return new CursorPagerAdapter(getSupportFragmentManager(), null);
    }

    /**
     * Update the vote
     */
    protected void updateDisplay(int position) {
        PostData p = getAdapter().getPost(position);
        if (p != null) {
            displayVote(p.getVote());
        }

        int count = getAdapter().getCount();
        if (count > 0) {
            getSupportActionBar().setTitle(++position + "/" + getAdapter().getCount() + " - " + getString(R.string.reddit_in_pictures));
        } else {
            getSupportActionBar().setTitle(getString(R.string.reddit_in_pictures));
        }
    }

    @Override
    public void reachedLastPage() {
        super.reachedLastPage();

        if (!isRequestInProgress() && mAdapter.getCount() > 0) {
            Ln.d("Reached last page, loading more images");
            setRequestInProgress(true);
            RedditService.getPosts(this, mSubreddit, mAge, mCategory, mAfter);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        displayVote();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putParcelable(Constants.EXTRA_REDDIT_URL, mRedditUrl);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        if (savedInstanceState.containsKey(Constants.EXTRA_REDDIT_URL)) {
            mRedditUrl = savedInstanceState.getParcelable(Constants.EXTRA_REDDIT_URL);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.image_detail_menu, menu);
        super.onCreateOptionsMenu(menu);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // We save the upvote and downvote icons so that we can change their icon later
        mUpvoteMenuItem = menu.findItem(R.id.upvote);
        mDownvoteMenuItem = menu.findItem(R.id.downvote);

        // We save the icon for locking the view pager so that we can reference
        // it when we receive a broadcast message to toggle the ViewPager lock state
        lockViewPagerItem = menu.findItem(R.id.lock_viewpager);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        int itemId = item.getItemId();
        if (itemId == R.id.upvote || itemId == R.id.downvote) {
            handleVote(item);
        }

        return true;
    }

    protected void showLogin() {
        // Only needs to be shown if they aren't currently logged in.
        if (!RedditLoginInformation.isLoggedIn()) {
            LoginDialogFragment loginFragment = LoginDialogFragment.newInstance();
            loginFragment.show(getSupportFragmentManager(), Constants.DIALOG_LOGIN);
        }
    }

    public void handleVote(MenuItem item) {
        if (!RedditLoginInformation.isLoggedIn()) {
            showLogin();
        } else {
            int itemId = item.getItemId();
            if (itemId == R.id.upvote) {
                vote(Vote.UP, item, getAdapter().getPost(mPager.getCurrentItem()));
            } else if (itemId == R.id.downvote) {
                vote(Vote.DOWN, item, getAdapter().getPost(mPager.getCurrentItem()));
            }
        }
    }

    /**
     * Handles updating the vote based on the action bar vote icon that was clicked, broadcasts a
     * message to have the fragment update the score.
     * <p>
     * If the user is not logged in, we return immediately.
{'fraction_non_alphanumeric': 0.09364930182599356, 'fraction_numerical': 0.002349624060150376, 'mean_word_length': 3.1751681614349776, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3439112', 'n_tokens_mistral': 4364, 'n_tokens_neox': 4273, 'n_words': 960}