org.yuzhiqiang.itsmine.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.yuzhiqiang.itsmine.MainActivity.java

Source

/*
 * Copyright (c) 2014 Yu Zhiqiang
 * Open source license: The MIT License
 */

package org.yuzhiqiang.itsmine;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks,
        IdTagDialogFragment.NoticeDialogListener, ItemListFragment.ItemListCursorProvider {

    private final String ALL_ITEMS = "ALL_ITEMS";
    private final String TOP_LEVEL_ITEMS = "TOP_LEVEL_ITEMS";

    // Used to store the last screen title. For use in {@link #restoreActionBar()}.
    // Fragment managing the behaviors, interactions and presentation of the navigation drawer.
    private NavigationDrawerFragment mNavigationDrawerFragment;

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

        DatabaseHelper.init(getApplicationContext());

        mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
                .findFragmentById(R.id.navigation_drawer);

        // 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
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        switch (position) {
        case 0:
            fragmentTransaction.replace(R.id.container, new ItemListFragment(), ALL_ITEMS);
            break;
        case 1:
            fragmentTransaction.replace(R.id.container, new ItemListFragment(), TOP_LEVEL_ITEMS);
            break;
        case 2:
            fragmentTransaction.replace(R.id.container, new CategoryFragment());
            break;
        case 3:
            fragmentTransaction.replace(R.id.container, new SettingsFragment());
            break;
        }
        fragmentTransaction.commit();
    }

    public void restoreActionBar() {
        final ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setTitle(getTitle());
        }
    }

    @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.main, menu);
            restoreActionBar();

            // Get the SearchView and set the searchable configuration
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
            // Assumes current activity is the searchable activity
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

            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();
        if (id == R.id.action_input_id_tag) {
            IdTagDialogFragment newFragment = new IdTagDialogFragment();
            newFragment.show(getFragmentManager(), null);
            return true;
        }
        if (id == R.id.action_scan) {
            QRcodeScan.startScan(this);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String itemIdTag = QRcodeScan.processResult(requestCode, resultCode, data);
        if (itemIdTag != null)
            ItemActivity.start(this, itemIdTag);
        else
            super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public Bundle returnBundleToDialog(String fragmentTag) {
        Bundle bundle = new Bundle();
        bundle.putString(IdTagDialogFragment.TITLE, getString(R.string.title_dialog_id_tag_enter));
        return bundle;
    }

    @Override
    public void onDialogPositiveClick(String itemIdTag, String fragmentTag) {
        ItemActivity.start(this, itemIdTag);
    }

    @Override
    public Cursor returnCursorToList(String fragmentTag) {
        switch (fragmentTag) {
        case ALL_ITEMS:
            return DatabaseHelper.listAllItems();
        case TOP_LEVEL_ITEMS:
            return DatabaseHelper.listTopLevelItems();
        }
        throw new IllegalStateException();
    }

    @Override
    public String returnTitleToList(String fragmentTag) {
        switch (fragmentTag) {
        case ALL_ITEMS:
            return getString(R.string.title_section_all_items);
        case TOP_LEVEL_ITEMS:
            return getString(R.string.title_section_top_level_items);
        }
        throw new IllegalStateException();
    }
}