com.pickr.activities.FlickrActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.pickr.activities.FlickrActivity.java

Source

/*
 * Copyright (C) 2013 Fabien Barbero Permission is hereby granted, free of
 * charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions: The above copyright notice and this permission
 * notice shall be included in all copies or substantial portions of the
 * Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.pickr.activities;

import java.util.ArrayList;
import java.util.List;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import com.pickr.Constants;
import com.pickr.R;
import com.pickr.fragments.ContactsFragment;
import com.pickr.fragments.FavoritesFragment;
import com.pickr.fragments.FlickrFragment;
import com.pickr.fragments.GalleryFragment;
import com.pickr.fragments.PhotosetsFragment;
import com.pickr.tasks.UserInfosLoader;
import com.pickr.ui.Item;
import com.pickr.ui.adapters.DrawerListAdapter;

import org.ez.flickr.api.entities.BaseUser;
import org.ez.flickr.api.entities.Contact;
import org.ez.flickr.api.entities.Photo;
import org.ez.flickr.api.entities.Photoset;

public class FlickrActivity extends Activity implements OnItemClickListener, PhotosetsFragment.OnPhotosetListener,
        GalleryFragment.OnGalleryPhotoListener, FavoritesFragment.OnFavoritePhotoListener,
        ContactsFragment.OnContactsListener, SearchView.OnQueryTextListener {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private List<Item<FlickrFragment>> mDrawerListItems;
    private FlickrFragment mCurrentFragment;
    private DrawerListAdapter<FlickrFragment> mDrawerListAdapter;
    private BaseUser mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flickr);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.flickrDrawerLayout);
        mDrawerList = (ListView) findViewById(R.id.flickrListDrawer);
        TextView userInfosTextView = (TextView) findViewById(R.id.flickrUserInfosTextView);
        ImageView userInfosImageView = (ImageView) findViewById(R.id.flickrUserInfosImageView);

        // configure the action bar
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        // Configure the drawer
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.app_name,
                R.string.app_name);
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mUser = (BaseUser) getIntent().getSerializableExtra(Constants.PARAM_USER);
        boolean isMe = getIntent().getBooleanExtra(Constants.PARAM_IS_ME, false);

        // Load user infos
        new UserInfosLoader(mUser, userInfosTextView, userInfosImageView).execute();

        // Set the adapter for the list view
        mDrawerListItems = new ArrayList<Item<FlickrFragment>>();
        // if(isMe) {
        mDrawerListItems.add(
                new Item<FlickrFragment>(R.string.title_profile, R.drawable.ic_personal, new GalleryFragment()));
        // }
        mDrawerListItems.add(
                new Item<FlickrFragment>(R.string.title_albums, R.drawable.ic_albums, new PhotosetsFragment()));
        mDrawerListItems.add(new Item<FlickrFragment>(R.string.title_favorites, R.drawable.ic_favorites,
                new FavoritesFragment()));
        mDrawerListItems.add(
                new Item<FlickrFragment>(R.string.title_contacts, R.drawable.ic_contacts, new ContactsFragment()));

        mDrawerListAdapter = new DrawerListAdapter<FlickrFragment>(mDrawerListItems, this);
        mDrawerList.setAdapter(mDrawerListAdapter);
        mDrawerList.setOnItemClickListener(this);

        Bundle fragmentsBundle = new Bundle();
        fragmentsBundle.putSerializable(Constants.PARAM_USER, mUser);
        for (Item<FlickrFragment> item2 : mDrawerListItems) {
            item2.getValue().setArguments(fragmentsBundle);
        }

        // Show first fragment
        showFragment(mDrawerListItems.get(0));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_flickr, menu);

        SearchView searchView = (SearchView) menu.findItem(R.id.searchField).getActionView();
        searchView.setOnQueryTextListener(this);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        if (mCurrentFragment == null) {
            return false;
        }
        mCurrentFragment.searchQuery(query);
        return true;
    }

    /**
     * Show a fragment in the main view
     * 
     * @param item The fragment to display
     */
    private void showFragment(Item<FlickrFragment> item) {
        FlickrFragment fragment = item.getValue();
        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.flickrFragmentsLayout, fragment, fragment.getClass().getSimpleName());
        transaction.commit();

        getActionBar().setTitle(fragment.getTitle());

        mCurrentFragment = fragment;
        for (Item<FlickrFragment> item2 : mDrawerListItems) {
            item2.setSelected(false);
        }
        item.setSelected(true);
        mDrawerListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getId() == R.id.flickrListDrawer) {
            Item<FlickrFragment> item = mDrawerListItems.get(position);
            if (mDrawerListItems == null || !item.getValue().equals(mCurrentFragment)) {
                showFragment(item);
            }
            mDrawerLayout.closeDrawers();
        }
    }

    @Override
    public void onPhotosetSelected(Photoset set) {
        Intent intent = new Intent(this, PhotosetActivity.class);
        intent.putExtra(PhotosetActivity.PARAM_SET, set);
        startActivity(intent);
    }

    @Override
    public void onGalleryPhotoSelected(Photo photo) {
        Intent intent = new Intent(this, GalleryPagerActivity.class);
        intent.putExtra(GalleryPagerActivity.PARAM_PHOTO, photo);
        intent.putExtra(GalleryPagerActivity.PARAM_USER, mUser);
        startActivity(intent);
    }

    @Override
    public void onFavoritePhotoSelected(Photo photo) {
        Intent intent = new Intent(this, FavoritesPagerActivity.class);
        intent.putExtra(FavoritesPagerActivity.PARAM_PHOTO, photo);
        intent.putExtra(FavoritesPagerActivity.PARAM_USER, mUser);
        startActivity(intent);
    }

    @Override
    public void onContactSelected(Contact contact) {
        Intent intent = new Intent(this, FlickrActivity.class);
        intent.putExtra(Constants.PARAM_USER, contact);
        startActivity(intent);
    }

}