edu.csh.coursebrowser.SectionInfoActivity.java Source code

Java tutorial

Introduction

Here is the source code for edu.csh.coursebrowser.SectionInfoActivity.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Andrew Hanes
 *
 * 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 edu.csh.coursebrowser;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import shared.AlertError;
import shared.Section;

import java.util.ArrayList;

public class SectionInfoActivity extends FragmentActivity implements ActionBar.TabListener {

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current tab position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    static String args;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_section_info);
        Bundle args = this.getIntent().getExtras();

        this.args = args.getString("args");
        this.setTitle(args.getString("title"));
        Log.v("Sections", args.getString("args"));

        // Set up the action bar to show tabs.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.

        actionBar.addTab(actionBar.newTab().setText("All").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Non-Full").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Full").setTabListener(this));

    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current tab position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current tab position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_section_info, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation
            // .html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, show the tab contents in the
        // container view.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            int num = getArguments().getInt(ARG_SECTION_NUMBER);
            ArrayList<String> all = new ArrayList<String>();
            ArrayList<String> full = new ArrayList<String>();
            ArrayList<String> empty = new ArrayList<String>();

            ListView ALL, FULL, EMPTY;

            ALL = new ListView(container.getContext());
            FULL = new ListView(container.getContext());
            EMPTY = new ListView(container.getContext());

            ALL.setAdapter(
                    new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1, all));
            FULL.setAdapter(
                    new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1, full));
            EMPTY.setAdapter(
                    new ArrayAdapter<String>(container.getContext(), android.R.layout.simple_list_item_1, empty));

            try {
                JSONObject jso = new JSONObject(SectionInfoActivity.args);
                Log.v("Sections:", "Created jso");
                JSONArray jsa = new JSONArray(jso.getString("sections"));
                Log.v("Sections:", "Created jsa");
                for (int i = 0; i < jsa.length(); ++i) {
                    Log.v("Sections:", jsa.getString(i));
                    JSONObject sec = new JSONObject(jsa.getString(i));
                    String course, department, section, instructor, id, type, maxenroll, curenroll;
                    course = sec.getString("course");
                    department = sec.getString("department");
                    section = sec.getString("section");
                    instructor = sec.getString("instructor");
                    id = sec.getString("id");
                    type = sec.getString("type");
                    maxenroll = sec.getString("maxenroll");
                    curenroll = sec.getString("curenroll");
                    Section tmp = new Section(course, department, section, instructor, id, type, maxenroll,
                            curenroll, sec.getString("times"));

                    all.add(tmp.toString());
                    int max = Integer.parseInt(maxenroll);
                    int cur = Integer.parseInt(curenroll);
                    Log.v("Enroll", max + ", " + cur);
                    if (max <= cur) {
                        full.add(tmp.toString());
                    } else {
                        empty.add(tmp.toString());
                    }

                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                new AlertError(container.getContext(), "Invalid JSON!");

                e.printStackTrace();
            }
            TextView tv = new TextView(container.getContext());
            LinearLayout l = new LinearLayout(container.getContext());
            l.setGravity(Gravity.CENTER);
            tv.setText("No Sections Found!");
            tv.setPadding(10, 10, 10, 10);
            l.addView(tv);
            switch (num) {
            case 1:
                if (all.size() == 0) {
                    return l;
                }
                return ALL;
            case 2:
                if (empty.size() == 0) {
                    return l;
                }
                return EMPTY;
            case 3:
                if (full.size() == 0) {
                    return l;
                }
                return FULL;
            }
            return ALL;
        }

    }
}