Android Open Source - Gazetti_Newspaper_Reader Home Screen Fragment






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.homescreen;
/*from  www. j a  v a  2  s.  com*/
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.*;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import com.crashlytics.android.Crashlytics;
import com.flaviofaria.kenburnsview.KenBurnsView;
import com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.appearance.simple.SwingBottomInAnimationAdapter;
import com.nineoldandroids.view.ViewHelper;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import in.sahildave.gazetti.R;
import in.sahildave.gazetti.homescreen.adapter.CellModel;
import in.sahildave.gazetti.homescreen.adapter.GridAdapter;
import in.sahildave.gazetti.news_activities.WebsiteListActivity;
import in.sahildave.gazetti.util.BitmapTransform;
import in.sahildave.gazetti.util.GazettiEnums.Category;
import in.sahildave.gazetti.util.GazettiEnums.Newspapers;
import in.sahildave.gazetti.util.NewsCatFileUtil;
import in.sahildave.gazetti.util.UserPrefUtil;

import java.util.List;
import java.util.Random;

public class HomeScreenFragment extends Fragment {
    private Callbacks activityCallback;
    private GridView gridview;
    private List<CellModel> cellList;
    private GridAdapter adapter;

    private String LOG_TAG = HomeScreenFragment.class.getName();

    private boolean phoneMode;
    private View actionBarCustomView;
    private ImageView phoneBackgroundImage;
    private KenBurnsView kenBurnsView;
    private Activity activity;

    public HomeScreenFragment() {

    }

    public void refreshCellGrid() {
        if (NewsCatFileUtil.getInstance(getActivity()).isUserPrefChanged()) {
            NewsCatFileUtil.getInstance(getActivity()).setUserPrefChanged(false);
            if(cellList!=null){
                cellList.clear();
            }
            setupCellGrid();
        }
    }

    public interface Callbacks {
        public void showAddNewCellDialog(List<CellModel> cellList, GridAdapter adapter);

        public void showEditCellDialog(int position, String newspaper, String category, List<CellModel> cellList,
                                       GridAdapter adapter);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            activityCallback = (Callbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement ToolbarListener");
        }

        this.activity = activity;
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.homescreen_fragment, container, false);

        ActionBar actionBar = ((ActionBarActivity) activity).getSupportActionBar();
        actionBarCustomView = actionBar.getCustomView();

        gridview = (GridView) rootView.findViewById(R.id.gridview);
        phoneBackgroundImage = (ImageView) rootView.findViewById(R.id.phone_homescreen_background);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setupImageBackground(view);
        setupCellGrid();
        registerForContextMenu(gridview);
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                if (position == (cellList.size() - 1)) {
                    activityCallback.showAddNewCellDialog(cellList, adapter);
                } else {
                    CellModel clickedObject = cellList.get(position);

                    String npId = clickedObject.getNewspaperId();
                    String catId = clickedObject.getCategoryId();
                    String npName = clickedObject.getNewspaperTitle();
                    String catName = clickedObject.getCategoryTitle();

                    npId = String.valueOf(Integer.parseInt(npId) + 1);

                    Intent headlinesIntent = new Intent(getActivity(), WebsiteListActivity.class);
                    headlinesIntent.putExtra("npId", npId);
                    headlinesIntent.putExtra("catId", catId);
                    headlinesIntent.putExtra("npName", npName);
                    headlinesIntent.putExtra("catName", catName);
                    startActivity(headlinesIntent);
                }

            }
        });

        if (phoneMode) {

            gridview.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    if (firstVisibleItem != 0) {
                        return;
                    }
                    if (null != gridview.getChildAt(0)) {

                        int actionBarTopMargin = gridview.getChildAt(0).getTop() - actionBarCustomView.getHeight();

                        if (actionBarTopMargin < ((-1) * actionBarCustomView.getHeight())) {
                            actionBarTopMargin = ((-1) * actionBarCustomView.getHeight());
                        }
                        ViewHelper.setTranslationY(actionBarCustomView, actionBarTopMargin);

                    }
                }

            });
        }

    }

    private void setupCellGrid() {
//        Log.d(LOG_TAG, "Setting up cell grid");
        cellList = UserPrefUtil.getInstance(getActivity()).getUserPrefCellList();
        putAddNewCellInList();

        adapter = new GridAdapter(getActivity(), cellList);

        SwingBottomInAnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(adapter);
        AlphaInAnimationAdapter animAdapterMultiple = new AlphaInAnimationAdapter(animAdapter);
        animAdapterMultiple.setAbsListView(gridview);

        gridview.setAdapter(animAdapterMultiple);
    }

    private void putAddNewCellInList() {
        if (cellList.size() > 0) {
            CellModel modelObject = cellList.get(cellList.size() - 1);
            if (!modelObject.getNewspaperImage().equals("add_new")) {
                cellList.add(new CellModel(Newspapers.ADD_NEW, Category.ADD_NEW));
            }
        } else {
            cellList.add(new CellModel(Newspapers.ADD_NEW, Category.ADD_NEW));
        }
    }

    private void setupImageBackground(View view) {

        kenBurnsView = (KenBurnsView) view.findViewById(R.id.kenBurnsView_Background);
        phoneMode = (kenBurnsView == null);

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

        try {
            new AsyncTask<Void, Void, Integer>(){
                @Override
                protected Integer doInBackground(Void... params) {
                    if (phoneMode) {
                        return getPhoneBackground();
                    } else {
                        return getTabletBackground();
                    }
                }

                @Override
                protected void onPostExecute(Integer resID) {

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

                    if(phoneMode){
                        requestCreator.into(phoneBackgroundImage);
                    } else {
                        requestCreator.into(kenBurnsView);
                    }

                }
            }.execute();
        } catch (Exception e) {
            Crashlytics.logException(e);
        }
    }

    private int getTabletBackground() {
        Random rand = new Random();
        int n = rand.nextInt(3);
        String backgroundImageUri = "land_" + n;

        int resID = getResources().getIdentifier(backgroundImageUri, "drawable", getActivity().getPackageName());
        if (resID == 0) {
            resID = getResources().getIdentifier("land_0", "drawable", getActivity().getPackageName());
        }
//        Log.d(LOG_TAG, "returning "+resID+" for "+n);
        return resID;
    }

    private int getPhoneBackground() {
        // get a random image, if null then get image_0
        Random rand = new Random();
        int n = rand.nextInt(3);
        String backgroundImageUri = "port_" + n;
        int resID = getResources().getIdentifier(backgroundImageUri, "drawable", getActivity().getPackageName());
        if (resID == 0) {
            resID = getResources().getIdentifier("port_0", "drawable", getActivity().getPackageName());
        }
//        Log.d(LOG_TAG, "returning "+resID+" for "+n);
        return resID;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.gridview_context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        int position = info.position;
        String newspaper = cellList.get(position).getNewspaperImage();
        String category = cellList.get(position).getCategoryTitle();

        switch (item.getItemId()) {
            case R.id.edit:
                if (position == (cellList.size() - 1)) {
                    Toast.makeText(getActivity(), "Cannot Edit", Toast.LENGTH_SHORT).show();
                    return true;
                }
                activityCallback.showEditCellDialog(position, newspaper, category, cellList, adapter);
                return true;
            case R.id.delete:
                if (position == (cellList.size() - 1)) {
                    Toast.makeText(getActivity(), "Cannot Delete", Toast.LENGTH_SHORT).show();
                    return true;
                }
                UserPrefUtil.getInstance(getActivity()).deleteUserPref(cellList.get(position));
                cellList.remove(position);
                adapter.notifyDataSetChanged();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
}




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