Android Open Source - Ascent Main Activity






From Project

Back to project page Ascent.

License

The source code is released under:

GNU General Public License

If you think the Android project Ascent 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

package be.sourcery.ascent;
//from  w w  w.  j ava 2 s.c om
/*
 * This file is part of Ascent.
 *
 *  Ascent is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Ascent is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Ascent.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Date;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends MyActivity {

    private static final int EXPORT_DATA_REQUEST = 1;
    private static final int IMPORT_DATA_REQUEST = 2;
    private static final int REPEAT_REQUEST = 3;

    private CursorAdapter adapter;
    private InternalDB db;
    private ListView listView;
    private Cursor cursor;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle(R.string.latestAscents);
        db = new InternalDB(this);
        populateList();
        update();
    }

    private void populateList() {
        cursor = db.getAscentsCursor();
        startManagingCursor(cursor);
        adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.ascent_list_item, cursor,
                new String[] {"date", "style", "route_grade", "route_name", "comment"},
                new int[] {R.id.dateCell, R.id.styleCell, R.id.gradeCell, R.id.nameCell, R.id.commentCell},
                0);
        listView = (ListView)this.findViewById(R.id.list);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View view, int position, long row) {
                long id = adapter.getItemId(position);
                Ascent ascent = db.getAscent(id);
                editAscent(ascent);
            }
        });
    }

    private void update() {
        cursor.requery();
        adapter.notifyDataSetChanged();
        TextView countView = (TextView) this.findViewById(R.id.countView);
        int count12 = db.getCountLast12Months();
        int count = db.getCountAllTime();
        countView.setText("Ascents: " + count + " - 12M: " + count12);
        TextView scoreView = (TextView) this.findViewById(R.id.scoreView);
        Date now = new Date();
        int year = now.getYear() + 1900;
        scoreView.setText("Score: " + db.getScoreLast12Months() + " - All Time: " + db.getScoreAllTime() + " - Year: " + db.getScoreForYear(year));
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_actionbar, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Ascent ascent = db.getAscent(info.id);
        switch (item.getItemId()) {
            case R.id.delete:
                db.deleteAscent(ascent);
                update();
                return true;
            case R.id.repeat:
                repeatAscent(ascent);
                update();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case (EXPORT_DATA_REQUEST) : {
                if (resultCode == Activity.RESULT_OK) {
                    int count = data.getIntExtra("count", 0);
                    Toast.makeText(this, "Exported " + count + " ascents", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Failed exporting ascents", Toast.LENGTH_SHORT).show();
                }
                break;
            }
            case (IMPORT_DATA_REQUEST) : {
                if (resultCode == Activity.RESULT_OK) {
                    int count = data.getIntExtra("count", 0);
                    Toast.makeText(this, "Imported " + count + " ascents", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Failed importing ascents", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
        update();
    }

    public void onDestroy() {
        super.onDestroy();
        db.close();
    }

    public void onResume() {
        super.onResume();
        update();
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_crags:
                cragsList();
                return true;
            case R.id.menu_import:
                importData();
                return true;
            case R.id.menu_export:
                exportData();
                return true;
            case R.id.menu_add:
                addAscent();
                return true;
            case R.id.menu_score:
                showScore();
                return true;
            case R.id.menu_top10:
                showTop10();
                return true;
            case R.id.menu_grade:
                showGrades();
                return true;
            case R.id.menu_projects:
                projectsList();
                return true;
        }
        return false;
    }

    private void showScore() {
        Intent myIntent = new Intent(this, ScoreGraphActivity.class);
        startActivity(myIntent);
    }

    private void showGrades() {
        Intent myIntent = new Intent(this, GradeGraphActivity.class);
        startActivity(myIntent);
    }

    private void showTop10() {
        Intent myIntent = new Intent(this, Top10Activity.class);
        startActivity(myIntent);
    }

    private void editAscent(Ascent ascent) {
        Intent myIntent = new Intent(this, EditAscentActivity.class);
        Bundle b = new Bundle();
        b.putLong("ascentId", ascent.getId());
        myIntent.putExtras(b);
        startActivity(myIntent);
    }

    private void importData() {
        Intent myIntent = new Intent(this, ImportDataActivity.class);
        startActivityForResult(myIntent, IMPORT_DATA_REQUEST);
    }

    private void exportData() {
        Intent myIntent = new Intent(this, ExportDataActivity.class);
        startActivityForResult(myIntent, EXPORT_DATA_REQUEST);
    }

    private void repeatAscent(Ascent ascent) {
        Intent myIntent = new Intent(this, RepeatAscentActivity.class);
        Bundle b = new Bundle();
        b.putLong("ascentId", ascent.getId());
        myIntent.putExtras(b);
        startActivityForResult(myIntent, REPEAT_REQUEST);
    }

    private void addCrag() {
        Intent myIntent = new Intent(this, AddCragActivity.class);
        startActivityForResult(myIntent, 0);
    }

    private void addAscent() {
        Intent myIntent = new Intent(this, AddAscentActivity.class);
        startActivityForResult(myIntent, 0);
    }

    private void cragsList() {
        Intent myIntent = new Intent(this, CragListActivity.class);
        startActivityForResult(myIntent, 0);
    }

    private void projectsList() {
        Intent myIntent = new Intent(this, ProjectListActivity.class);
        startActivityForResult(myIntent, 0);
    }

}




Java Source Code List

be.sourcery.ascent.AddAscentActivity.java
be.sourcery.ascent.AddCragActivity.java
be.sourcery.ascent.AddProjectActivity.java
be.sourcery.ascent.AscentApplication.java
be.sourcery.ascent.AscentProvider.java
be.sourcery.ascent.Ascent.java
be.sourcery.ascent.CragAscentsActivity.java
be.sourcery.ascent.CragListActivity.java
be.sourcery.ascent.Crag.java
be.sourcery.ascent.EditAscentActivity.java
be.sourcery.ascent.ExportDataActivity.java
be.sourcery.ascent.GradeAscentsActivity.java
be.sourcery.ascent.GradeGraphActivity.java
be.sourcery.ascent.GradeInfo.java
be.sourcery.ascent.GradeView.java
be.sourcery.ascent.ImportDataActivity.java
be.sourcery.ascent.InternalDB.java
be.sourcery.ascent.MainActivity.java
be.sourcery.ascent.MyActivity.java
be.sourcery.ascent.ProjectListActivity.java
be.sourcery.ascent.Project.java
be.sourcery.ascent.RepeatAscentActivity.java
be.sourcery.ascent.Route.java
be.sourcery.ascent.ScoreGraphActivity.java
be.sourcery.ascent.SearchAscentsActivity.java
be.sourcery.ascent.TickProjectActivity.java
be.sourcery.ascent.Top10Activity.java