com.cblabs.wardrobe.activities.FeedActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.cblabs.wardrobe.activities.FeedActivity.java

Source

/*
 * Copyright 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.cblabs.wardrobe.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.os.Build;
import android.util.TypedValue;
import android.app.Activity;
import android.graphics.Color;

import com.astuetz.PagerSlidingTabStrip;
import com.cblabs.wardrobe.R;
import com.cblabs.wardrobe.fragments.BuzzFragment;
import com.cblabs.wardrobe.fragments.FeedFragment;
import com.cblabs.wardrobe.fragments.FriendsFragment;
import com.cblabs.wardrobe.fragments.MyProfileFragment;
import com.cblabs.wardrobe.utils.GeneralFunctions;
import com.cblabs.wardrobe.utils.PrefsManager;
import com.cblabs.wardrobe.utils.WardrobeConstants;
import com.cblabs.wardrobe.web.pojos.PojoUserDetails;
import com.facebook.Session;
import com.facebook.SessionState;
import com.google.gson.Gson;

public class FeedActivity extends FragmentActivity implements View.OnClickListener {

    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    private ViewPager mViewPager;
    private PojoUserDetails data;
    private PrefsManager mPrefsManager;
    private ImageView settings;

    //Create the feed activity
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Session.getActiveSession() != null)
            Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_feed);

        //Initialize vies and listeners
        initializeViews();
        addClickListeners();

        //Initialize preference manager
        mPrefsManager = new PrefsManager(this);
        data = mPrefsManager.getLoginData();
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(mViewPager);
        tabs.setBackgroundResource(android.R.color.white);
        tabs.setDividerColorResource(android.R.color.transparent);
        tabs.setIndicatorColorResource(R.color.transparent);
        tabs.setIndicatorHeight((int) getResources().getDimension(R.dimen.tab_ribbon_height));

        tabs.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

            }
        });

        mViewPager.setCurrentItem(getIntent().getIntExtra(WardrobeConstants.INTENT_KEY_FEED_MAIN, 0));

    }

    private void initializeViews() {
        settings = (ImageView) findViewById(R.id.settings);
    }

    private void addClickListeners() {
        settings.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.settings:
            startActivity(new Intent(this, SettingsActivity.class));
            break;
        }
    }

    public class AppSectionsPagerAdapter extends FragmentPagerAdapter
            implements PagerSlidingTabStrip.IconTabProvider {

        //private String[] titles = {"PUBLIC", "PRIVATE", "FRIENDS", "BUZZ", "PROFILE"};
        private final int[] ICONS = { R.drawable.selector_tab_public, R.drawable.selector_tab_private,
                R.drawable.selector_tab_buzz, R.drawable.selector_tab_profile };

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
            case 0: {
                Fragment fragment = new FeedFragment();
                Bundle args = new Bundle();
                args.putInt(WardrobeConstants.FRAGMENT_DATA_KEY, i + 1);
                fragment.setArguments(args);
                return fragment;
            }
            case 1: {
                Fragment fragment = new FeedFragment();
                Bundle args = new Bundle();
                args.putInt(WardrobeConstants.FRAGMENT_DATA_KEY, i + 1);
                fragment.setArguments(args);
                return fragment;
            }
            case 2: {
                Fragment fragment = new BuzzFragment();
                return fragment;
            }
            case 3: {
                Fragment fragment = new MyProfileFragment();
                Bundle args = new Bundle();
                try {
                    args.putString(WardrobeConstants.FRAGMENT_DATA_KEY, new Gson().toJson(data.getProfile()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fragment.setArguments(args);
                return fragment;
            }
            default: {
                return null;
            }
            }
        }

        @Override
        public int getCount() {
            return ICONS.length;
        }

        @Override
        public int getPageIconResId(int position) {
            return ICONS[position];
        }

    }

    public static void setStatusBarColor(Activity activity, int statusBarColor) {

    }

}