<commit_before>/**
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 * 
 * This code is distributed under the MIT License. Please see LICENSE.txt
 * for more details.
 */

package com.pheide.trainose;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TextView;

public class Routes extends ListActivity {
	
	private static final String TAG = "Routes";
	
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_TIMETABLES = 1;
	
	private static final int NEW_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private static final int SYNC_ID = Menu.FIRST + 2;
    private static final int ABOUT_ID = Menu.FIRST + 3;
    
	RoutesDbAdapter mRoutesDbAdapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.routes_list);
        mRoutesDbAdapter = new RoutesDbAdapter(this);
        mRoutesDbAdapter.open();
        populateList();
        registerForContextMenu(getListView());
    }
    
    private void populateList() {
        Cursor routesCursor = mRoutesDbAdapter.fetchAll();
        startManagingCursor(routesCursor);

        String[] from = new String[]{RoutesDbAdapter.KEY_SOURCE, RoutesDbAdapter.KEY_DESTINATION};
        int[] to = new int[]{R.id.source, R.id.destination};
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.routes_row, routesCursor, from, to);
        setListAdapter(notes);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        showTimetables(id);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, NEW_ID, 0, R.string.optmenu_new_route);
        menu.add(0, SYNC_ID, 0, R.string.optmenu_sync_all);
        menu.add(0, ABOUT_ID, 0, R.string.optmenu_about);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case NEW_ID:
                newRoute();
                return true;
            case SYNC_ID:
                syncAllRoutes();
                return true;
            case ABOUT_ID:
                showAboutPage();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.ctxmenu_delete_route);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mRoutesDbAdapter.delete(info.id);
                TimetablesDbAdapter timetablesDbAdapter = new TimetablesDbAdapter(this);
                timetablesDbAdapter.open();
                timetablesDbAdapter.deleteByRoute(info.id);
                
                populateList();
                return true;
        }
        return super.onContextItemSelected(item);
    }
    
    private void showTimetables(long routeId) {
    	Intent i = new Intent(this, Timetables.class);
        i.putExtra(RoutesDbAdapter.KEY_ROWID, routeId);
        startActivityForResult(i, ACTIVITY_TIMETABLES);
    }
    
    private void newRoute() {
    	Intent i = new Intent(this, RouteEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    private void showAboutPage() { //TODO put these in some kind of "view"
    	Dialog dialog = new Dialog(this);

    	dialog.setContentView(R.layout.about_dialog);<commit_msg>Closing dbAdapter that was left open<commit_after>/**
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 * 
 * This code is distributed under the MIT License. Please see LICENSE.txt
 * for more details.
 */

package com.pheide.trainose;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TextView;

public class Routes extends ListActivity {
	
	private static final String TAG = "Routes";
	
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_TIMETABLES = 1;
	
	private static final int NEW_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private static final int SYNC_ID = Menu.FIRST + 2;
    private static final int ABOUT_ID = Menu.FIRST + 3;
    
	RoutesDbAdapter mRoutesDbAdapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.routes_list);
        mRoutesDbAdapter = new RoutesDbAdapter(this);
        mRoutesDbAdapter.open();
        populateList();
        registerForContextMenu(getListView());
    }
    
    private void populateList() {
        Cursor routesCursor = mRoutesDbAdapter.fetchAll();
        startManagingCursor(routesCursor);

        String[] from = new String[]{RoutesDbAdapter.KEY_SOURCE, RoutesDbAdapter.KEY_DESTINATION};
        int[] to = new int[]{R.id.source, R.id.destination};
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.routes_row, routesCursor, from, to);
        setListAdapter(notes);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        showTimetables(id);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, NEW_ID, 0, R.string.optmenu_new_route);
        menu.add(0, SYNC_ID, 0, R.string.optmenu_sync_all);
        menu.add(0, ABOUT_ID, 0, R.string.optmenu_about);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case NEW_ID:
                newRoute();
                return true;
            case SYNC_ID:
                syncAllRoutes();
                return true;
            case ABOUT_ID:
                showAboutPage();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.ctxmenu_delete_route);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
            	//TODO move to model of some sort?
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mRoutesDbAdapter.delete(info.id);
                TimetablesDbAdapter timetablesDbAdapter = new TimetablesDbAdapter(this);
                timetablesDbAdapter.open();
                timetablesDbAdapter.deleteByRoute(info.id);
                timetablesDbAdapter.close();
                
                populateList();
                return true;
        }
        return super.onContextItemSelected(item);
    }
    
    private void showTimetables(long routeId) {
    	Intent i = new Intent(this, Timetables.class);
        i.putExtra(RoutesDbAdapter.KEY_ROWID, routeId);
        startActivityForResult(i, ACTIVITY_TIMETABLES);
    }
    
    private void newRoute() {
    	Intent i = new Intent(this, RouteEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    private void showAboutPage() { //TODO put these in some kind of "view"
    	Dialog dialog = new Dialog(this);

    	dialog.setContentView(R.layout.about_dialog);
{'fraction_non_alphanumeric': 0.08943873179091688, 'fraction_numerical': 0.003641816623821765, 'mean_word_length': 2.867854183927092, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '912030', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2626, 'n_words': 612}