EasyRider.java :  » App » easyrider » com » postpc » easyrider » Android Open Source

Android Open Source » App » easyrider 
easyrider » com » postpc » easyrider » EasyRider.java
package com.postpc.easyrider;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.postpc.easyrider.Trip.TripManager;


public class EasyRider extends ListActivity {
  
  private final int RIDE_RETURN_CODE=1;
  final String LOADING_STRING = "Loading contacts, please wait...";
  final String RIDE_IN_PROGRESS_TITLE = "Ride in progress";
  final String TERMINATE_RIDE = "Continue current ride?";
  final String CONT = "Yes";
  final String TERM = "Terminate ride";
  
  
    /** Called when the activity is first created. */
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] mainMenuText = getResources().getStringArray(R.array.main_menu_options);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.main_menu, mainMenuText));
      
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        if (TripManager.isServiceRunning())
          startMapRideView();
        
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
      // TODO Auto-generated method stub
      super.onNewIntent(intent);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      
             
      switch (position) {
        case 0: // Take a ride
          if (TripManager.isServiceRunning())
            {              
              onRideInProgress();
            }
          else
          {
            startActivityForResult(new Intent(this, com.postpc.easyrider.Ride.class), RIDE_RETURN_CODE);
          }
          break;
        case 1: // Options
          startActivity(new Intent(this, com.postpc.easyrider.PreferencesMenu.class));          
          break;
        case 2: // Hall of Shame
          startActivity(new Intent(this, com.postpc.easyrider.HallOfShame.class));          
          break;
        case 3: // Exit
          Toast.makeText(this.getBaseContext(), R.string.goodbye_string, Toast.LENGTH_SHORT).show();
          finish();
          break;
      }
      
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
      if (requestCode == RIDE_RETURN_CODE)
      {
        if (resultCode == RESULT_OK)
          if (TripManager.isServiceRunning())
          {
            startMapRideView();
          }
          else
          {
            // Debug message
            Log.d(com.postpc.easyrider.EasyRider.class.getName(), "Service is not running.\ntalk to Inger.");            
          }
      }
      
    }
    
    protected void onRideInProgress()
    {
      
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      
      builder.setTitle(RIDE_IN_PROGRESS_TITLE)
          .setMessage(TERMINATE_RIDE)
             .setCancelable(false)
             .setPositiveButton(CONT, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     startMapRideView();                   
                 }
             })
             .setNegativeButton(TERM, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {                   
                   dialog.cancel();
                   EasyRider.this.stopService(new Intent(EasyRider.this , com.postpc.easyrider.Trip.TripManager.class));
                 }
             });
      
      AlertDialog alert = builder.create();
      alert.show();
      
           
    }
    
    protected void startMapRideView()
    {
      Intent rideView = new Intent(this, MapRideView.class);
      rideView.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
       rideView.putExtra(MapRideView.INTENT_RIDER_LOCATION_EXTRA_KEY, TripManager.getRiderLocation());
       startActivity(rideView);  
    }
    

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.