Android Open Source - Gradulator Main Activity






From Project

Back to project page Gradulator.

License

The source code is released under:

MIT License

If you think the Android project Gradulator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
The MIT License (MIT)//from  w ww.ja v  a2  s.  c om

Copyright (c) 2014 Gannon Lawlor

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 com.gannon.gradulator;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.gannon.gradulator.CourseActivity;
import com.gannon.gradulator.R;
import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.gannon.gradulator.adapters.CourseAdapter;
import com.gannon.gradulator.database.CourseDataSource;
import com.gannon.gradulator.dialogfragments.AddCourseDialogFragment;
import com.gannon.gradulator.dialogfragments.DeleteCourseDialogFragment;
import com.gannon.gradulator.models.Course;
import com.github.amlcurran.showcaseview.targets.ActionItemTarget;

import java.sql.SQLException;
import java.util.List;


public class MainActivity extends Activity {

    public CourseDataSource courseDataSource;

    public CourseAdapter adapter;
    public int listNumber;
    public boolean edit = false;

    SharedPreferences prefs = null;
    public boolean first = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        courseDataSource = new CourseDataSource(this);

        prefs = getSharedPreferences("com.gannon.Gradulator", MODE_PRIVATE);

        ListView myListView = (ListView) findViewById(R.id.course_list);
        try {
            courseDataSource.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        List<Course> values = courseDataSource.getAllCourses();

        adapter = new CourseAdapter(this, values);
        myListView.setAdapter(adapter);

        registerForContextMenu(myListView);



        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, CourseActivity.class);
                Course course = adapter.getItem(i);
                intent.putExtra("Course", course);
                startActivity(intent);
            }
        });

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }


    }

    @Override
    protected void onResume() {
        super.onResume();

        if (prefs.getBoolean("firstrun", true)) {
            ActionItemTarget target = new ActionItemTarget(this,R.id.action_addCourse);
            new ShowcaseView.Builder(this)
                    .setTarget(target)
                    .setContentTitle("Add a Course")
                    .setContentText("Click on this button to add a Course")
                    .hideOnTouchOutside()
                    .build();
            prefs.edit().putBoolean("firstrun", false).commit();
        }


    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);
        MenuInflater inflater = getMenuInflater();
        menu.setHeaderTitle("Select an action");
        inflater.inflate(R.menu.course_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        listNumber = ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position;
        switch (item.getItemId()) {
            case R.id.button_delete:
                deleteCourse();
                return true;
            case R.id.button_edit:
                editCourse();

                return true;
            case R.id.button_share:
                Course shareCourse = adapter.getItem(listNumber);
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBody = "I got a " + shareCourse.roundGrade() + "% in " + shareCourse.getName() + "! You can keep track of your grades too with #Gradeulator";
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Look at Me!");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }




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



        return true;
    }

    @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_addCourse) {
            addCourse();
            adapter.notifyDataSetChanged();
            return true;
        }
        if (id == R.id.action_tell) {
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "I have been using Gradeulator to keep track of all my grades! You can too with http://goo.gl/2GOKDL";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Look at Me!");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void addCourse() {
        DialogFragment newFragment = new AddCourseDialogFragment();
        newFragment.show(getFragmentManager(), "addcourse");
    }

    public void deleteCourse() {
        DialogFragment newFragment = new DeleteCourseDialogFragment();
        newFragment.show(getFragmentManager(), "deletecourse");
    }

    public void editCourse() {
        edit = true;
        DialogFragment newFragment = new AddCourseDialogFragment();
        newFragment.show(getFragmentManager(), "editcourse");
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}




Java Source Code List

com.gannon.gradulator.ApplicationTest.java
com.gannon.gradulator.CourseActivity.java
com.gannon.gradulator.EvaluationActivity.java
com.gannon.gradulator.MainActivity.java
com.gannon.gradulator.adapters.CourseAdapter.java
com.gannon.gradulator.adapters.EvaluationAdapter.java
com.gannon.gradulator.database.CourseContract.java
com.gannon.gradulator.database.CourseDataSource.java
com.gannon.gradulator.database.CourseDbHelper.java
com.gannon.gradulator.database.EvaluationDataSource.java
com.gannon.gradulator.dialogfragments.AddCourseDialogFragment.java
com.gannon.gradulator.dialogfragments.DeleteCourseDialogFragment.java
com.gannon.gradulator.dialogfragments.DeleteEvaluationDialogFragment.java
com.gannon.gradulator.models.Course.java
com.gannon.gradulator.models.Evaluation.java