com.mysmallcornerstudios.tempus.ui.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.mysmallcornerstudios.tempus.ui.MainActivity.java

Source

/*
 * Copyright 2013 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.mysmallcornerstudios.tempus.ui;

import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.GravityCompat;
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.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.mysmallcornerstudios.tempus.R;
import com.mysmallcornerstudios.tempus.dal.AlarmProvider;
import com.mysmallcornerstudios.tempus.dal.AlarmSQLiteHelper;
import com.mysmallcornerstudios.tempus.dal.TimerProvider;
import com.mysmallcornerstudios.tempus.dal.TimerSQLiteHelper;
import com.mysmallcornerstudios.tempus.ui.util.MenuActions;

import static com.mysmallcornerstudios.tempus.dal.AlarmSQLiteHelper.TIME;
import static com.mysmallcornerstudios.tempus.dal.TimerSQLiteHelper.ALARM_COUNT;
import static com.mysmallcornerstudios.tempus.dal.TimerSQLiteHelper.DURATION;
import static com.mysmallcornerstudios.tempus.dal.TimerSQLiteHelper.ID;
import static com.mysmallcornerstudios.tempus.dal.TimerSQLiteHelper.NAME;

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private DrawerLayout drawerLayout;
    private ListView timerList;
    private ActionBarDrawerToggle drawerToggle;
    private SimpleCursorAdapter timerCursorAdapter;

    private CharSequence drawerTitle;
    private CharSequence title;

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

        title = drawerTitle = getTitle();

        getSupportLoaderManager().initLoader(TIMER_LOADER, savedInstanceState, this);

        final String[] timerFromColumns = { ID, NAME, DURATION, ALARM_COUNT };
        final int[] timerToViews = { R.id.timer_row_id, R.id.timer_name, R.id.timer_duration,
                R.id.timer_alarm_count };

        timerCursorAdapter = new SimpleCursorAdapter(this, R.layout.drawer_list_item, null, timerFromColumns,
                timerToViews, 0);
        timerList = (ListView) findViewById(R.id.left_drawer);
        timerList.setAdapter(timerCursorAdapter);
        timerList.setOnItemClickListener(new DrawerItemClickListener());

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        drawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
                drawerLayout, /* DrawerLayout object */
                R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open, /* "open drawer" description for accessibility */
                R.string.drawer_close /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(title);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(drawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        if (savedInstanceState == null) {
            //            selectItem(0, 0, "Tempus", "", 0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.timer, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = drawerLayout.isDrawerOpen(timerList);
        menu.findItem(R.id.add_timer).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.add_timer:
            MenuActions.openAddTimerDialog(getSupportFragmentManager());
            break;
        case R.id.menu_settings:
            break;
        default:
            return super.onOptionsItemSelected(item);
        }

        return false;
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Long timerId = Long.parseLong(((TextView) view.findViewById(R.id.timer_row_id)).getText().toString());
            String timerName = (((TextView) view.findViewById(R.id.timer_name)).getText()).toString();
            String duration = (((TextView) view.findViewById(R.id.timer_duration)).getText()).toString();
            int alarmCount = Integer
                    .parseInt(((TextView) view.findViewById(R.id.timer_alarm_count)).getText().toString());

            selectItem(position, timerId, timerName, duration, alarmCount);
        }
    }

    private void selectItem(int position, long timerId, String timerName, String duration, int alarmCount) {

        final Bundle args = new Bundle();
        args.putLong(ID, timerId);
        args.putString(NAME, timerName);
        args.putString(DURATION, duration);
        args.putInt(ALARM_COUNT, alarmCount);

        final Fragment fragment = new TimerFragment();
        fragment.setArguments(args);

        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        timerList.setItemChecked(position, true);
        setTitle(timerName);
        drawerLayout.closeDrawer(timerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        this.title = title;
        getActionBar().setTitle(this.title);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) {

        CursorLoader cursorLoader = null;

        switch (loaderId) {
        case TIMER_LOADER:
            cursorLoader = new CursorLoader(this, TimerProvider.CONTENT_URI, TimerSQLiteHelper.COLUMNS, null, null,
                    null);
            break;
        }

        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

        switch (loader.getId()) {
        case TIMER_LOADER:
            timerCursorAdapter.changeCursor(cursor);
            break;
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

        switch (loader.getId()) {
        case TIMER_LOADER:
            timerCursorAdapter.changeCursor(null);
            break;
        }
    }

    private static final int TIMER_LOADER = 0x00;
}