Android Open Source - salaya-tram-map Main Activity






From Project

Back to project page salaya-tram-map.

License

The source code is released under:

Unless otherwise noted, this software is Copyright 2011 Pawit Pornkitprasan and is licensed under the Simplified BSD License. Copyright 2011 Pawit Pornkitprasan. All rights reserved. Red...

If you think the Android project salaya-tram-map 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 org.dyndns.pawitp.salayatrammap;
//  w  w w .jav  a  2 s  . com
import org.dyndns.pawitp.salayatrammap.map.MapView;
import org.dyndns.pawitp.salayatrammap.map.NoStopMatchedException;
import org.dyndns.pawitp.salayatrammap.map.TramDbHelper;
import org.dyndns.pawitp.salayatrammap.schedule.NoMoreTramException;
import org.dyndns.pawitp.salayatrammap.schedule.TramCarSchedule;
import org.dyndns.pawitp.salayatrammap.schedule.TramException;
import org.dyndns.pawitp.salayatrammap.schedule.TramsSchedule;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
  
  static final String TAG = "MainActivity";
  
  private TramsSchedule mTramsSchedule;
  private Handler mHandler;
  private TramDbHelper mDbHelper;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
    
    mTramsSchedule = new TramsSchedule(this);
    mHandler = new Handler();
    mDbHelper = new TramDbHelper(this);
    mDbHelper.open();
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
      ((MapView) findViewById(R.id.mapView)).showStopInfo(Integer.valueOf(intent.getDataString()));
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      try {
        String query = intent.getStringExtra(SearchManager.QUERY);
        int id = mDbHelper.getFirstSearchResult(query);
        ((MapView) findViewById(R.id.mapView)).showStopInfo(id);
      } catch (NoStopMatchedException e) {
        Toast.makeText(this, R.string.no_match, Toast.LENGTH_SHORT).show();
      }
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    
    runnableUpdateTramsTime.run();
  }
  
  
  @Override
  protected void onPause() {
    super.onPause();
    
    mHandler.removeCallbacks(runnableUpdateTramsTime);
  }

  @Override
  public void onBackPressed() {
    MapView mapView = (MapView) findViewById(R.id.mapView);
    if (mapView.isShowingStopInfo()) {
      mapView.hideStopInfo();
    }
    else {
      super.onBackPressed();
    }
  }
  
  public void holidayToggle(View view) {
    mTramsSchedule.setHoliday(!mTramsSchedule.isHoliday());
    
    mHandler.removeCallbacks(runnableUpdateTramsTime);
    runnableUpdateTramsTime.run();
    
    // Update widget
    Intent i = new Intent(MiscWidgetBroadcastReceiver.UPDATE_WIDGET_IF_ENABLED_INTENT);
    sendBroadcast(i);
  }
  
  public void btnSearchOnClick(View view) {
    onSearchRequested();
  }
  
  private void updateTramTime(int tramId, int idLeft, int idNext) {
    TramCarSchedule schedule = mTramsSchedule.getSchedule(tramId);
    TextView txtLeft = (TextView) findViewById(idLeft);
    TextView txtNext = (TextView) findViewById(idNext);
    
    try {
      txtNext.setText(Utils.formatTime(this, schedule.getNextTram()));
    } catch (NoMoreTramException e) {
      txtNext.setText(R.string.no_tram);
    }
    
    try {
      txtLeft.setText(Utils.formatTime(this, schedule.getLastTram()));
    } catch (TramException e) {
      txtLeft.setText(R.string.no_tram);
    }
  }
  
  private Runnable runnableUpdateTramsTime = new Runnable() {
    
    public void run() {
      Log.v(TAG, "Updating trams time");
      
      mTramsSchedule.updateSchedules();
      
      updateTramTime(TramsSchedule.TRAM_GREEN, R.id.txtTramLeftGreen, R.id.txtTramNextGreen);
      updateTramTime(TramsSchedule.TRAM_BLUE, R.id.txtTramLeftBlue, R.id.txtTramNextBlue);
      updateTramTime(TramsSchedule.TRAM_RED, R.id.txtTramLeftRed, R.id.txtTramNextRed);
      
      // Update "Holiday" label
      TextView lblHoliday = (TextView) findViewById(R.id.lblScheduleType);
      lblHoliday.setText(mTramsSchedule.isHoliday() ? R.string.holiday_schedule : R.string.normal_schedule);
      
      long updateTime = mTramsSchedule.getNextUpdateTime();
      mHandler.postDelayed(runnableUpdateTramsTime, updateTime);
    }
    
  };
}




Java Source Code List

org.dyndns.pawitp.salayatrammap.MainActivity.java
org.dyndns.pawitp.salayatrammap.MiscWidgetBroadcastReceiver.java
org.dyndns.pawitp.salayatrammap.TramScheduleWidgetProvider.java
org.dyndns.pawitp.salayatrammap.Utils.java
org.dyndns.pawitp.salayatrammap.map.MapView.java
org.dyndns.pawitp.salayatrammap.map.NoStopMatchedException.java
org.dyndns.pawitp.salayatrammap.map.SearchSuggestionProvider.java
org.dyndns.pawitp.salayatrammap.map.TramDbHelper.java
org.dyndns.pawitp.salayatrammap.map.Zoomer.java
org.dyndns.pawitp.salayatrammap.schedule.NoMoreTramException.java
org.dyndns.pawitp.salayatrammap.schedule.NoTramLeftException.java
org.dyndns.pawitp.salayatrammap.schedule.TramCarSchedule.java
org.dyndns.pawitp.salayatrammap.schedule.TramException.java
org.dyndns.pawitp.salayatrammap.schedule.TramsSchedule.java