Android Open Source - Gazetti_Newspaper_Reader Article Loading Callback






From Project

Back to project page Gazetti_Newspaper_Reader.

License

The source code is released under:

MIT License

If you think the Android project Gazetti_Newspaper_Reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package in.sahildave.gazetti.news_activities;
//from   w  ww  . j  a  v  a 2 s.com
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.crashlytics.android.Crashlytics;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import fr.castorflex.android.smoothprogressbar.SmoothProgressBar;
import in.sahildave.gazetti.R;
import in.sahildave.gazetti.util.BitmapTransform;

/**
 * Created by sahil on 4/10/14.
 */
public class ArticleLoadingCallback {

    // CallBacks for handling asynctask for twoPane mode

    View rootView; // ScrollView from DetailFragment
    View headerStub;

    // Header
    SmoothProgressBar detailViewProgress;
    TextView mTitleTextView;
    ImageView mMainImageView;
    String mImageURL;
    String mArticleURL;
    String titleText = "";

    // Subtitle
    RelativeLayout mSubtitleLayout;
    TextView mArticlePubDateView;

    // Body
    TextView mArticleTextView;
    String bodyText = "";

    // Footer
    LinearLayout mArticleFooter;

    LinearLayout mScrollToReadLayout;
    private boolean displayScrollToRead = false;
    private Animation slide_up;
    private Activity activity;

    public ArticleLoadingCallback(Activity activity){
        this.activity = activity;
        slide_up = AnimationUtils.loadAnimation(activity, R.anim.slide_up);
    }

    public void articleNotFound(String mArticleUrl) {

        Toast.makeText(activity, "No Reading Mode Available", Toast.LENGTH_LONG).show();
        Toast.makeText(activity, "Loading Webpage...", Toast.LENGTH_LONG).show();

        WebViewFragment webViewFragment = new WebViewFragment();
        Bundle bundle  = new Bundle();
        bundle.putString("URL", mArticleUrl);
        webViewFragment.setArguments(bundle);
        try {
            ((FragmentActivity)activity).getSupportFragmentManager().beginTransaction()
                    .replace(R.id.website_detail_container, webViewFragment, "webViewFragment").commit();
        } catch (Exception e) {
            Crashlytics.logException(e);
        }
    }


    public void onPreExecute(View rootView) {
        this.rootView = rootView;

        // initialize article views
        mArticleTextView = (TextView) rootView.findViewById(R.id.article_body);
        mArticlePubDateView = (TextView) rootView.findViewById(R.id.pubDateView);
        mSubtitleLayout = (RelativeLayout) rootView.findViewById(R.id.subtitleLayout);
        mScrollToReadLayout = (LinearLayout) rootView.findViewById(R.id.scrollToReadLayout);
        mArticleFooter = (LinearLayout) rootView.findViewById(R.id.article_footer);

        // Progress Bar
        detailViewProgress = (SmoothProgressBar) rootView.findViewById(R.id.detailViewProgressBar);
        detailViewProgress.progressiveStart();
    }

    public void setHeaderStub(View headerStub) {
        this.headerStub = headerStub;
    }

    public void onPostExecute(String[] result, String mArticlePubDate) {

        titleText = result[0];
        mImageURL = result[1];
        bodyText = result[2];

        mSubtitleLayout.setVisibility(View.VISIBLE);
        mArticleFooter.setVisibility(View.VISIBLE);
        mArticlePubDateView.setText(mArticlePubDate);
        mArticleTextView.setVisibility(View.VISIBLE);
        mArticleTextView.setText(bodyText);

        if (mImageURL == null) {
            mTitleTextView = (TextView) headerStub.findViewById(R.id.article_title);
            mTitleTextView.setText(titleText);
            detailViewProgress.progressiveStop();
        } else {
            mTitleTextView = (TextView) headerStub.findViewById(R.id.article_header_title);
            mTitleTextView.setText(titleText);

            mMainImageView = (ImageView) headerStub.findViewById(R.id.article_header_image);

            //height and width of screen
            final int MAX_HEIGHT = activity.getResources().getDisplayMetrics().heightPixels;
            final int MAX_WIDTH = activity.getResources().getDisplayMetrics().widthPixels;

            RequestCreator requestCreator = Picasso.with(activity)
                    .load(mImageURL)
                    .transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT));

            requestCreator.into(mMainImageView, new Callback() {

                @Override
                public void onSuccess() {

                    mMainImageView.getViewTreeObserver().addOnGlobalLayoutListener(
                            new ViewTreeObserver.OnGlobalLayoutListener() {

                                @SuppressWarnings("deprecation")
                                @SuppressLint("NewApi")
                                @Override
                                public void onGlobalLayout() {

                                    // Get Display metrics according to the SDK
                                    Display display = activity.getWindowManager().getDefaultDisplay();
                                    Point screen = new Point();
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                                        display.getSize(screen);
                                    } else {
                                        screen.x = display.getWidth();
                                        screen.y = display.getHeight();
                                    }

                                    // StatusBar Height
                                    int statusBarHeight = 0;
                                    int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
                                    if (resId > 0) {
                                        statusBarHeight = activity.getResources().getDimensionPixelSize(resId);
                                    }

                                    // ActionBar Height
                                    TypedValue tv = new TypedValue();
                                    int actionBarHeight = 0;
                                    if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                                        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                                                activity.getResources().getDisplayMetrics());
                                    }


                                    // Boolean to check if image+subtitle is
                                    // large enough.
                                    // If yes, then display "Scroll To Read"
                                    displayScrollToRead = (screen.y - statusBarHeight - actionBarHeight) < (mArticleTextView
                                            .getTop()) * 1.08;

                                    if (displayScrollToRead) {
                                        mScrollToReadLayout.startAnimation(slide_up);
                                        mScrollToReadLayout.setVisibility(View.VISIBLE);
                                    }

                                    // remove GlobalLayoutListener according to
                                    // SDK
                                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                                        mMainImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                    } else {
                                        mMainImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                    }

                                }

                            });

                    detailViewProgress.progressiveStop();
                }

                @Override
                public void onError() {}
            });
        }

        bodyText = null;
        titleText = null;
        mImageURL = null;
    }
}




Java Source Code List

in.sahildave.gazetti.StarterApplication.java
in.sahildave.gazetti.bookmarks.BookmarkAdapter.java
in.sahildave.gazetti.bookmarks.BookmarkDetailActivity.java
in.sahildave.gazetti.bookmarks.BookmarkDetailFragment.java
in.sahildave.gazetti.bookmarks.BookmarkListActivity.java
in.sahildave.gazetti.bookmarks.BookmarkListFragment.java
in.sahildave.gazetti.bookmarks.BookmarkLoadingCallback.java
in.sahildave.gazetti.bookmarks.sqlite.BookmarkDataSource.java
in.sahildave.gazetti.bookmarks.sqlite.BookmarkModel.java
in.sahildave.gazetti.bookmarks.sqlite.SQLiteHelper.java
in.sahildave.gazetti.homescreen.HomeScreenActivity.java
in.sahildave.gazetti.homescreen.HomeScreenFragment.java
in.sahildave.gazetti.homescreen.adapter.AddCellDialogFragment.java
in.sahildave.gazetti.homescreen.adapter.CellModel.java
in.sahildave.gazetti.homescreen.adapter.EditCellDialogFragment.java
in.sahildave.gazetti.homescreen.adapter.GridAdapter.java
in.sahildave.gazetti.homescreen.adapter.NewsCatModel.java
in.sahildave.gazetti.homescreen.newcontent.DialogNewContentExpListAdaper.java
in.sahildave.gazetti.homescreen.newcontent.DialogNewContent.java
in.sahildave.gazetti.news_activities.ArticleLoadingCallback.java
in.sahildave.gazetti.news_activities.WebViewFragment.java
in.sahildave.gazetti.news_activities.WebsiteDetailActivity.java
in.sahildave.gazetti.news_activities.WebsiteDetailFragment.java
in.sahildave.gazetti.news_activities.WebsiteListActivity.java
in.sahildave.gazetti.news_activities.WebsiteListFragment.java
in.sahildave.gazetti.news_activities.adapter.NavDrawerListAdapter.java
in.sahildave.gazetti.news_activities.adapter.NewsAdapter.java
in.sahildave.gazetti.news_activities.adapter.RobotoLight.java
in.sahildave.gazetti.news_activities.fetch.firstPost.java
in.sahildave.gazetti.news_activities.fetch.hindu.java
in.sahildave.gazetti.news_activities.fetch.indianExpressBusiness.java
in.sahildave.gazetti.news_activities.fetch.indianExpress.java
in.sahildave.gazetti.news_activities.fetch.toi.java
in.sahildave.gazetti.preference.FeedSelectFragment.java
in.sahildave.gazetti.preference.FeedSelectSettingsActivity.java
in.sahildave.gazetti.preference.LicensesActivity.java
in.sahildave.gazetti.preference.PreferenceExpListAdapter.java
in.sahildave.gazetti.preference.SettingsActivity.java
in.sahildave.gazetti.util.BitmapTransform.java
in.sahildave.gazetti.util.ConfigService.java
in.sahildave.gazetti.util.Constants.java
in.sahildave.gazetti.util.GazettiEnums.java
in.sahildave.gazetti.util.JsonHelper.java
in.sahildave.gazetti.util.NewsCatFileUtil.java
in.sahildave.gazetti.util.ShareButtonListener.java
in.sahildave.gazetti.util.UserPrefUtil.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenExpListAdapter.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenFragmentExpList.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenFragmentFirst.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenViewPagerActivity.java