org.melato.bus.android.activity.ScheduleActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.melato.bus.android.activity.ScheduleActivity.java

Source

/*-------------------------------------------------------------------------
 * Copyright (c) 2012,2013,2014 Alex Athanasopoulos.  All Rights Reserved.
 * alex@melato.org
 *-------------------------------------------------------------------------
 * This file is part of Athens Next Bus
 *
 * Athens Next Bus 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.
 *
 * Athens Next Bus 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 Athens Next Bus.  If not, see <http://www.gnu.org/licenses/>.
 *-------------------------------------------------------------------------
 */
package org.melato.bus.android.activity;

import java.util.Date;
import java.util.List;

import org.melato.android.app.HelpActivity;
import org.melato.android.menu.Menus;
import org.melato.bus.android.Info;
import org.melato.bus.android.R;
import org.melato.bus.android.activity.ExceptionActivity.ExceptionSpecifier;
import org.melato.bus.client.TimeOfDay;
import org.melato.bus.client.TimeOfDayList;
import org.melato.bus.model.Agency;
import org.melato.bus.model.DaySchedule;
import org.melato.bus.model.RStop;
import org.melato.bus.model.RouteException;
import org.melato.bus.model.RouteId;
import org.melato.bus.model.Schedule;
import org.melato.bus.model.ScheduleId;
import org.melato.bus.model.Stop;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Displays the schedule for a route
 * @author Alex Athanasopoulos
 *
 */
public class ScheduleActivity extends FragmentActivity implements OnItemClickListener, OnClickListener {
    public static final String KEY_SCHEDULE_ID = "scheduleId";
    public static final int REQUEST_SCHEDULE = 0;
    protected BusActivities activities;
    private Schedule schedule;
    private Date currentTime = new Date();
    private DaySchedule daySchedule;
    private String stopName;
    private int timeOffset;
    private ScheduleAdapter scheduleAdapter;
    private RStop rstop;

    protected String getScheduleName() {
        if (daySchedule == null)
            return "";
        return ScheduleUtilities.getScheduleName(this, daySchedule.getScheduleId());
    }

    private void setStopInfo(RStop rstop) {
        Stop[] stops = Info.routeManager(this).getStops(rstop.getRouteId());
        Stop stop = rstop.getStop();
        if (stop == null || stop.getIndex() > 0 && stop.getSecondsFromStart() == 0) {
            if (stops.length > 0) {
                stop = stops[0];
            }
        }
        if (stop != null) {
            stopName = stop.getName();
            timeOffset = stop.getSecondsFromStart();
        }
    }

    private void setSchedule(ScheduleId scheduleId) {
        if (scheduleId == null) {
            scheduleId = Info.getStickyScheduleId();
        } else {
            Info.setStickyScheduleId(scheduleId);
        }
        if (scheduleId != null) {
            daySchedule = schedule.getSchedule(scheduleId);
        } else {
            daySchedule = schedule.getSchedule(currentTime);
            if (daySchedule != null) {
                scheduleId = daySchedule.getScheduleId();
            }
        }
        String scheduleText = getScheduleName();
        IntentHelper helper = new IntentHelper(this);
        String title = helper.getRoute().getFullTitle();
        if (stopName != null) {
            scheduleText += " - " + stopName;
            if (timeOffset > 0) {
                scheduleText += " (+" + Schedule.formatDuration(timeOffset) + ")";
            }
        }
        String comment = schedule.getComment();
        if (comment != null) {
            scheduleText += "\n" + comment;
        }
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(scheduleText);
        setTitle(title);
        ListView listView = (ListView) findViewById(R.id.listView);

        if (daySchedule != null) {
            TimeOfDayList times = new TimeOfDayList(daySchedule, currentTime);
            times.setTimeOffset(timeOffset);
            List<RouteException> exceptions = schedule.getExceptions(daySchedule.getScheduleId());
            times.setExceptions(exceptions);
            scheduleAdapter = new ScheduleAdapter(times);
            listView.setAdapter(scheduleAdapter);
            listView.setOnItemClickListener(this);
            int pos = times.getDefaultPosition();
            if (pos >= 0) {
                if (pos > 0)
                    pos--;
                listView.setSelection(pos);
            }
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activities = new BusActivities(this);
        IntentHelper helper = new IntentHelper(this);
        rstop = helper.getRStop();
        if (rstop == null)
            return;
        setContentView(R.layout.schedule);
        setStopInfo(rstop);
        schedule = activities.getRouteManager().getSchedule(rstop.getRouteId());
        ScheduleId scheduleId = (ScheduleId) getIntent().getSerializableExtra(KEY_SCHEDULE_ID);
        setSchedule(scheduleId);
        Menus.addIcons(this, (LinearLayout) findViewById(R.id.icons), R.menu.schedule_menu, this);
    }

    static class TextColor {
        int text;
        int background;

        TextColor(Context context, int textId, int backgroundId) {
            text = context.getResources().getColor(textId);
            background = context.getResources().getColor(backgroundId);
        }

        public void apply(TextView view) {
            view.setBackgroundColor(background);
            view.setTextColor(text);
        }
    }

    class ScheduleAdapter extends ArrayAdapter<TimeOfDay> {
        TimeOfDayList times;
        int currentPosition;
        TextColor normalColor;
        TextColor selectedColor;

        public ScheduleAdapter(TimeOfDayList times) {
            super(ScheduleActivity.this, R.layout.list_item, times);
            this.times = times;
            currentPosition = times.getDefaultPosition();
            selectedColor = new TextColor(ScheduleActivity.this, R.color.list_highlighted_text,
                    R.color.list_highlighted_background);
            if (times.hasOffset()) {
                normalColor = new TextColor(ScheduleActivity.this, R.color.stop_text, R.color.stop_background);
            } else {
                normalColor = new TextColor(ScheduleActivity.this, R.color.list_text, R.color.list_background);

            }
        }

        boolean hasException(int position) {
            return times.hasException(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = (TextView) super.getView(position, convertView, parent);
            if (position == currentPosition) {
                selectedColor.apply(view);
            } else {
                normalColor.apply(view);
            }
            return view;
        }
    }

    private void updateAgency(MenuItem browse) {
        if (browse != null) {
            Agency agency = Info.routeManager(this).getAgency(activities.getRouteId());
            Drawable drawable = Info.getAgencyIcon(this, agency);
            if (drawable != null) {
                browse.setIcon(drawable);
            }
            if (agency.getLabel() != null) {
                browse.setTitle(agency.getLabel());
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        Menus.inflate(inflater, R.menu.schedule_menu, menu);
        updateAgency(menu.findItem(R.id.browse));
        HelpActivity.addItem(menu, this, Help.SCHEDULE);
        return true;
    }

    private void selectSchedule() {
        Intent intent = new Intent(this, SchedulesActivity.class);
        new IntentHelper(intent).putRStop(rstop);
        startActivityForResult(intent, REQUEST_SCHEDULE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SCHEDULE && resultCode == RESULT_OK) {
            ScheduleId scheduleId = (ScheduleId) data.getSerializableExtra(KEY_SCHEDULE_ID);
            setSchedule(scheduleId);
        }
    }

    private boolean onItemSelected(int itemId) {
        switch (itemId) {
        case R.id.all_schedules:
            selectSchedule();
            return true;
        default:
            return new StopActions(this).showRStop(rstop, itemId);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return onItemSelected(item.getItemId());
    }

    @Override
    public void onClick(View v) {
        onItemSelected(v.getId());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (scheduleAdapter.hasException(position)) {
            RouteId routeId = activities.getRouteId();
            ScheduleId scheduleId = daySchedule.getScheduleId();
            int time = daySchedule.getTimes()[position];
            ExceptionSpecifier exc = new ExceptionSpecifier(routeId, scheduleId, time);
            ExceptionActivity.showExceptions(this, exc);
        }
    }
}