cat.wuyingren.whatsannoy.activities.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for cat.wuyingren.whatsannoy.activities.MainActivity.java

Source

/** Copyright (c) 2013-2014 Jordi Lpez <@wuyingren> & <@aniol>
 *
 * MIT License:
 *
 * 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 cat.wuyingren.whatsannoy.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.DialogFragment;
import org.holoeverywhere.widget.DrawerLayout;

import cat.wuyingren.whatsannoy.R;
import cat.wuyingren.whatsannoy.fragments.NavigationDrawerFragment;
import cat.wuyingren.whatsannoy.fragments.NowFragment;
import cat.wuyingren.whatsannoy.fragments.RandomFragment;
import cat.wuyingren.whatsannoy.fragments.ScheduleFragment;
import cat.wuyingren.whatsannoy.fragments.TimePickerFragment;
import cat.wuyingren.whatsannoy.utils.SystemUtils;

public class MainActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks, TimePickerFragment.OnDBChangedListener {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    private int sdkVer = 1;
    private Context context = this;

    private NowFragment nowFragment;
    private ScheduleFragment scheduleFragment;
    private RandomFragment randomFragment;

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

        // Configure fragment references
        sdkVer = SystemUtils.getSdkInt();
        nowFragment = NowFragment.newInstance();
        scheduleFragment = ScheduleFragment.newInstance(sdkVer);
        randomFragment = RandomFragment.newInstance();

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
                .findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        /*fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();*/
        switch (position) {
        case 0:
            if (nowFragment == null) {
                nowFragment = NowFragment.newInstance();
            }
            fragmentManager.beginTransaction().replace(R.id.container, nowFragment).commit();
            break;
        case 1:

            fragmentManager.beginTransaction().replace(R.id.container, scheduleFragment).commit();
            break;
        case 2:
            fragmentManager.beginTransaction().replace(R.id.container, randomFragment).commit();
            break;
        }
        onSectionAttached(position); // update title
    }

    public void onSectionAttached(int number) {
        switch (number) {
        case 0:
            mTitle = getString(R.string.title_section1);
            break;
        case 1:
            mTitle = getString(R.string.title_section2);
            break;
        case 2:
            mTitle = getString(R.string.title_section3);
            break;
        }
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.global, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
        case R.id.action_settings:
            openSettings();
            return true;
        case R.id.action_new:
            scheduleNew();
            return true;
        }
        /*if (id == R.id.action_settings) {
        openSettings();
        return true;
        }*/
        return super.onOptionsItemSelected(item);
    }

    private void scheduleNew() {
        DialogFragment df = new TimePickerFragment();
        df.show(getSupportFragmentManager(), "timePicker");
    }

    private void openSettings() {
        Intent i = new Intent(context, SettingsActivity.class);
        //overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        startActivity(i);
    }

    @Override
    public void onDBChanged() {

        scheduleFragment.updateDB();
    }
}