net.callmeike.android.mojowire.ui.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for net.callmeike.android.mojowire.ui.MainActivity.java

Source

/* $Id: $
   Copyright 2012, G. Blake Meike
    
   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 net.callmeike.android.mojowire.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;

import net.callmeike.android.mojowire.R;

/**
 *
 * @version $Revision: $
 * @author <a href="mailto:blake.meike@gmail.com">G. Blake Meike</a>
 */
public class MainActivity extends Activity
        implements PodcastFragment.PodcastDrawerCallbacks, EpisodeFragment.EpisodeCallbacks {
    private PodcastFragment podcastFragment;
    private CharSequence title;

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

        setContentView(R.layout.activity_main);

        title = getTitle();

        podcastFragment = (PodcastFragment) getFragmentManager().findFragmentById(R.id.podcasts);

        podcastFragment.setUp(R.id.podcasts, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (podcastFragment.isDrawerOpen()) {
            return super.onCreateOptionsMenu(menu);
        }

        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case R.id.action_settings:
            break;

        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

    @Override
    public void onPodcastSelected(int id, String episodeTitle) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, EpisodeFragment.newInstance(id, episodeTitle))
                .commit();
    }

    @Override
    public void onAddPodcast() {
        Intent i = new Intent(this, PodcastDetailActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);
    }

    @Override
    public void onSectionAttached(String podcastTitle) {
        title = podcastTitle;
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(title);
    }
}