FastRoadSearchActivity.java :  » App » for-happy-date-for-android » com » AndroidC » ForHappyDate » FastRoad » Android Open Source

Android Open Source » App » for happy date for android 
for happy date for android » com » AndroidC » ForHappyDate » FastRoad » FastRoadSearchActivity.java
package com.AndroidC.ForHappyDate.FastRoad;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.AndroidC.ForHappyDate.R;

public class FastRoadSearchActivity extends ListActivity implements RadioGroup.OnCheckedChangeListener,
                                      View.OnClickListener,
                                      LocationListener {
  private RadioGroup rg;
  private String des;
  private Handler handler = new Handler();
  private ArrayList< FastRoadListData > content = new ArrayList< FastRoadListData >();
  private String src;
  private String page;
  private Hashtable srcHash, desHash;
  private FastRoadListAdapter frla;
  private int listPos;
  private String[] srcLoc;
  private String[] srcAddress;
  private String[] srcX;
  private String[] srcY;
  private FastRoadModules frm = new FastRoadModules();
  private int flag;
  private int cotentLength;
  private String desLoc;
  private String[] desAddr;
  private TextView srcText;
  private TextView next;
  private int srcPos = -1;
  private String[] desX, desY;
  private Hashtable resultHash ;
  private double lat;
  private double lng;
  private List< Address > addr;
  private String[] gpsAddr, gpsX, gpsY;
  private Hashtable gpsHash;
  private LocationManager locationManager;
  private Address address;
  private int chk;
  private ProgressDialog pd;
  private LinearLayout ly;
  private Button research;
  
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fastroadsearch);

        rg = (RadioGroup) findViewById(R.id.radiogroup);
        srcText = ( TextView ) findViewById( R.id.srcText );
        next = ( TextView ) findViewById( R.id.lastText );
        
        ly = ( LinearLayout ) findViewById( R.id.searchLinear );
        research = ( Button ) findViewById( R.id.research );
        research.setOnClickListener( new View.OnClickListener() {
          public void onClick( View v ) {
            ly.setVisibility( View.VISIBLE );
            research.setVisibility( View.GONE );
          }
        });
        
        srcText.setVisibility( View.GONE );
        next.setVisibility( View.GONE );
       
  
    
        des = getIntent().getExtras().getString( "subject" );
        desLoc = getIntent().getExtras().getString( "desAddr" );
     
         
        rg.setOnCheckedChangeListener( FastRoadSearchActivity.this );
        frla = new FastRoadListAdapter( FastRoadSearchActivity.this, R.layout.srcsearchlist, content );
        setListAdapter( frla );
        
        final Button transbtn = ( Button ) findViewById( R.id.transbtn );
        transbtn.setOnClickListener( new View.OnClickListener() {
          public void onClick( View v ) {
            if( src == null ) {
              Toast.makeText(FastRoadSearchActivity.this, " !", Toast.LENGTH_LONG ).show();
            }
            
            else if( desAddr.length == 0 ) {
              Toast.makeText(FastRoadSearchActivity.this, "  .", Toast.LENGTH_SHORT ).show();
            
            }
            else if( chk == 1 ) {
                pd = ProgressDialog.show(FastRoadSearchActivity.this, "Loading...", "/  ", true,
                          false); 
              new Thread( null, RoadInfoThread, "RoadInfoThread" ).start();
            }
            
            
            
            else if( chk == 2 ) {
                pd = ProgressDialog.show(FastRoadSearchActivity.this, "Loading...", "/  ", true,
                          false); 
              new Thread( null, gpsRoadInfoThread, "gpsRoadInfoThread" ).start();
            }
          }
        });
        
        final Button carbtn = ( Button ) findViewById( R.id.carbtn );
        carbtn.setOnClickListener( new View.OnClickListener() {
          public void onClick( View v ) {
            if( src == null ) {
              
              Toast.makeText(FastRoadSearchActivity.this, " !", Toast.LENGTH_SHORT ).show();
              
            }
            
            else if( desAddr.length == 0 ) {
              Toast.makeText(FastRoadSearchActivity.this, "  .", Toast.LENGTH_SHORT ).show();
              
            }
            
            else if( chk == 1 ) {
        
              Intent i = new Intent( FastRoadSearchActivity.this, TransOutCityActivity.class );
              Bundle b = new Bundle();
              
    
              
              b.putString( "desLoc", desAddr[0] );
              b.putString( "desX", desX[0] );
              b.putString( "desY", desY[0] );
              b.putString( "srcLoc", content.get( srcPos ).getName() );
              b.putString( "srcX", content.get( srcPos ).getSrcX() );
              b.putString( "srcY", content.get( srcPos ).getSrcY() );
              i.putExtra( "car", b );
              startActivity(i);
            }
            
            else if( chk == 2 ) {
              
              Intent i = new Intent( FastRoadSearchActivity.this, TransOutCityActivity.class );
              Bundle b = new Bundle();
              
          
              
              b.putString( "desLoc", desAddr[0] );
              b.putString( "desX", desX[0] );
              b.putString( "desY", desY[0] );
              b.putString( "srcLoc", gpsAddr[0]);
              b.putString( "srcX", gpsX[0]);
              b.putString( "srcY", gpsY[0] );
              i.putExtra( "car", b );
              startActivity(i);
            }
          }
        });
        
     ;
          
    }
  
    public void onCheckedChanged(RadioGroup group, int checkedId) {
      LinearLayout ll = ( LinearLayout ) findViewById( R.id.srcLinear );
      if( checkedId == R.id.myloc ) {
        chk = 2;
        new Thread( null, desSearchThread, "desSEarchThread" ).start();
         ll.removeAllViewsInLayout();
         ll.setVisibility( View.GONE );
         
         // gps 
         chkGpsService();
         
         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         Location location = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,    this);
         
      
         
      }
      
      if( checkedId == R.id.self ) {
        LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            Gravity.RIGHT );
        
            final EditText editText = new EditText( this );
            editText.setHint( " " );
            editText.setTextSize( 18 );
            editText.setSingleLine( true );

            final ImageButton srcbtn = new ImageButton( this );
            Resources r = getResources();
            Drawable d = r.getDrawable(android.R.drawable.ic_search_category_default);
            srcbtn.setImageDrawable(d);
            
            ll.addView( editText, lllp );
            ll.addView( srcbtn, lllp );
            ll.setVisibility( View.VISIBLE );
            
            srcbtn.setOnClickListener( new View.OnClickListener() {
               public void onClick(View v) {
                    next.setVisibility( View.VISIBLE );

                next.setOnClickListener( new View.OnClickListener() {
                  public void onClick( View v ) {
                    page = ++flag + "";
                    new Thread( null, srcSearchThread, "srcSearchThread" ).start();
                    
                  }
                });
                
                src = editText.getText().toString();
                
                if( src.matches("\\s*") ) {
                  Toast.makeText( FastRoadSearchActivity.this, " !", Toast.LENGTH_SHORT ).show();
                }
                
                else {
                  content.clear();
                  page = 1 + "";
                  flag = 1;
                  new Thread( null, srcSearchThread, "srcSearchThread" ).start();
                  new Thread( null, desSearchThread, "desSEarchThread" ).start();
                }
               }
            });
            
            
      }

    }

    public void onClick(View v) {
    }
    
    /*
     *   
     */
    private Runnable desSearchThread = new Runnable() {
      public void run() {
        desHash = frm.srcNdesDataParsing( frm.srcNdesPosSearch( desLoc, "1" ) );
            desAddr = ( String[] ) desHash.get("name");
            desX = ( String[] ) desHash.get( "x" );
            desY = ( String[] ) desHash.get( "y" );
            
          
            handler.post( desSEarchThreadGUI ); 
            }
    };
    
    /*
     *   
     */
    private Runnable gpsSearchThread = new Runnable() {
      public void run() {
        if( addr.size() != 0 ) {
        address = addr.get(0);
        src = address.getAddressLine(0).replaceFirst( "|", " " );
        
        gpsHash = frm.srcNdesDataParsing( frm.srcNdesPosSearch( src, "1" ) );
              gpsAddr = ( String[] ) gpsHash.get("name");
              gpsX = ( String[] ) gpsHash.get( "x" );
              gpsY = ( String[] ) gpsHash.get( "y" );
              
              
              handler.post( gpsSrcGUI );
              }  
 
         }
    };
    
    private Runnable gpsSrcGUI = new Runnable() {
    public void run() {
      srcText.setText( gpsAddr[0] );
      srcText.setVisibility( View.VISIBLE );
    }
    };
    
    private Runnable desSEarchThreadGUI = new Runnable() {
      public void run() {
        final TextView desText = ( TextView ) findViewById( R.id.desText );
        desText.setText( des );
      }
    };
    /*
     *   
     */
    private Runnable srcSearchThread = new Runnable() {
      public void run() {
        
      
        //  
        srcHash = frm.srcNdesDataParsing( frm.srcNdesPosSearch( src, page ) );
        
         if( srcHash != null ) {
          handler.post( srcUpdateGUI );
          
        }
        //    
        else {
          next.setVisibility( View.GONE );
        }
      }
    };
    
    private Runnable srcUpdateGUI = new Runnable() {
      public void run() {
        ly.setVisibility( View.GONE );
      research.setVisibility( View.VISIBLE );
      
        //  
        srcLoc = ( String[] ) srcHash.get( "name" );
        srcX = ( String[] ) srcHash.get( "x" );
        srcY = ( String[] ) srcHash.get( "y" );
        cotentLength = content.size();

        if( srcHash.containsKey( "address" ) ) {
          srcAddress = ( String[] ) srcHash.get( "address" );
          for( int i = 0; i < srcAddress.length; i++ ) {
            content.add( new FastRoadListData( srcLoc[i], srcAddress[i], srcX[i], srcY[i] ) );
            frla.notifyDataSetChanged();
          }
        }
        
        else {
          for( int i = 0; i < srcLoc.length; i++ ) {
            content.add( new FastRoadListData( srcLoc[i], "", srcX[i], srcY[i] ) );
            frla.notifyDataSetChanged();
          }
        }
        
        getListView().setSelection( cotentLength );
        
        if( content.isEmpty() ) {
          next.setVisibility( View.GONE );
          Toast.makeText( FastRoadSearchActivity.this, "  .", Toast.LENGTH_SHORT);
        }
        
      }
    };
    
    /*     */
  private class FastRoadListAdapter extends ArrayAdapter< FastRoadListData > {
    private ArrayList< FastRoadListData > items;

    public FastRoadListAdapter( Context context, int textViewResourceId, ArrayList< FastRoadListData > items) {  
      super(context, textViewResourceId, items);
      this.items = items; 
    } 
            
    @Override      
    public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
      if ( v == null ) {
        LayoutInflater vi = ( LayoutInflater ) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        v = vi.inflate( R.layout.srcsearchlist, null );
      }
  
      FastRoadListData frd = items.get( position ); 
      
        if ( frd != null ) {
            final TextView name = (TextView) v.findViewById(R.id.srcname);
            final TextView addr = (TextView) v.findViewById(R.id.srcaddr);    

            if( frd.getName() != null && frd.getAddr() != null ) {
              name.setText( frd.getName() );
              addr.setText( frd.getAddr() );
            }
        }
        
      return v;   
    }
  }
  
  

  @Override
  protected Dialog onCreateDialog(int id) {
    if( id == 1 ) {
      AlertDialog.Builder dialog = new AlertDialog.Builder( this );
      dialog.setTitle( " " );
      dialog.setMessage( "" );
      dialog.setPositiveButton( "", new OnClickListener() {
        public void onClick( DialogInterface dialog, int arg1 ) {
          
        }
      });
    
      dialog.setNegativeButton( "", new OnClickListener() {
        public void onClick( DialogInterface dialog, int arg1 ) {
        }
      });
      

      return dialog.create();
      
    }
    return null;
  }

  @Override
  protected void onPrepareDialog(int id, Dialog dialog) {
    AlertDialog olddialog = ( AlertDialog ) dialog;
    
    dialog.setTitle( content.get( listPos ).getName() );
    olddialog.setMessage( content.get( listPos ).getAddr() );
    
    olddialog.setButton( "", new OnClickListener() {
      public void onClick( DialogInterface dialog, int arg1 ) {
        srcText.setText( content.get( listPos ).getName() );
        srcPos = listPos;
        srcText.setVisibility( View.VISIBLE );
        chk = 1;
        
      }});
    
    olddialog.setButton2( " ", new OnClickListener() {
      public void onClick( DialogInterface dialog, int arg1 ) {
        
      }});
    
  
    

    super.onPrepareDialog(id, dialog);
  }
  
  // ListView    
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    listPos = position;
    showDialog(1);
    
  }
  
   private Runnable RoadInfoThread = new Runnable() {
        public void run() {
          /*
          if( desAddr.length == 0 ) {
            
            handler.post( new Runnable() {
              public void run() {
                Toast.makeText(FastRoadSearchActivity.this, "  .", Toast.LENGTH_SHORT).show();
                pd.dismiss();
              }
            });
            
            
          }
          
          else {*/
            resultHash = frm.transParsing(  content.get( srcPos ).getName(), 
                content.get( srcPos ).getSrcX(), 
                content.get( srcPos ).getSrcY(), 
                desAddr[0], 
                desX[0], 
                desY[0]);
            
            handler.post( RoadInfoThread2 ); 
          //}
        }
   };
   
   private Runnable gpsRoadInfoThread = new Runnable() {
        public void run() {
          resultHash = frm.transParsing( gpsAddr[0], 
                         gpsX[0], 
                         gpsY[0],
                         desAddr[0], 
                         desX[0], 
                         desY[0]);
          
          handler.post( gpsRoadInfoThread2 );
          
        }
   };
   
  
   
   private Runnable RoadInfoThread2 = new Runnable() {
        public void run() {
          //  
          if( resultHash.get("0") == null ) {
          
            Intent i = new Intent( FastRoadSearchActivity.this, TransOutCityActivity.class );
            i.putExtra( "outbus", ( Hashtable )resultHash.get( "outbus" ) );
            i.putExtra( "bus", ( Hashtable )resultHash.get( "bus" ) );
            i.putExtra( "trans", ( Hashtable )resultHash.get( "trans" ) );
            i.putExtra( "air", ( Hashtable )resultHash.get( "air" ) );
            i.putExtra( "srcLoc", content.get( srcPos ).getName() );
            i.putExtra( "srcX", content.get( srcPos ).getSrcX() );
            i.putExtra( "srcY", content.get( srcPos ).getSrcY() );
            i.putExtra( "desLoc", desAddr[0] );
            i.putExtra( "desX", desX[0] );
            i.putExtra( "desY", desY[0] );
            
            pd.dismiss();
            startActivity( i );
            
          }
          
          // 
          else {

            Intent i = new Intent( FastRoadSearchActivity.this, TransOutCityActivity.class );
            int len = resultHash.size();
            int cnt1 = 0;
            int cnt2 = 0;
            int cnt3 = 0;
            for( int j = 0; j < len; j++ ) {
                  TransportationData[] tmp = ( TransportationData[] ) resultHash.get( j + "" );
                  
                
  
                    // +
                    if( tmp[0].getItem( "pathType" ).equals( "3" ) ) {
                      cnt1++;
                    }
                    
                    // 
                    if( tmp[0].getItem( "pathType" ).equals( "2" ) ) {
                      cnt2++;
                  
                    }
                    // 
                    if( tmp[0].getItem( "pathType" ).equals( "1" ) ) {
                      cnt3++;
                    }

                }
                
            

            TransportationData[][] bus = new TransportationData[cnt2][];
            TransportationData[][] sub = new TransportationData[cnt3][];
            TransportationData[][] busNsub = new TransportationData[cnt1][];
            

              cnt1 = 0;
              cnt2 = 0;
              cnt3 = 0;
              
              for( int y = 0 ; y < len; y++ ) {  
                TransportationData[] tmp = ( TransportationData[] ) resultHash.get( y + "" );

                  if( tmp[0].getItem( "pathType" ).equals( "3" ) ) {
                    busNsub[cnt1] = tmp;
                    cnt1++;
                  }
                  
                  if( tmp[0].getItem( "pathType" ).equals( "2" ) ) {
                    bus[cnt2] = tmp;
                    cnt2++;
                  }
                  
                  if( tmp[0].getItem( "pathType" ).equals( "1" ) ) {
                    sub[cnt3] = tmp;
                    cnt3++;
                  }

              }
 
              Hashtable< String, TransportationData[][] > inLoc = new Hashtable< String, TransportationData[][] >();
              inLoc.put("bus", bus );
              inLoc.put("sub", sub );
              inLoc.put("busNsub", busNsub );
              
               MyApplication ma = (MyApplication) getApplication();
               ma.setMyClass( inLoc );
            i.putExtra("inLoc", inLoc );

            pd.dismiss();
            startActivity( i );

            
          }
      
      
        }
   };
   
   private Runnable gpsRoadInfoThread2 = new Runnable() {
        public void run() {
          Toast.makeText(FastRoadSearchActivity.this, "/ ..", Toast.LENGTH_SHORT ).show();
          
          //  
          if( resultHash.get("0") == null ) {
          
            Intent i = new Intent( FastRoadSearchActivity.this, TransOutCityActivity.class );
            i.putExtra( "outbus", ( Hashtable )resultHash.get( "outbus" ) );
            i.putExtra( "bus", ( Hashtable )resultHash.get( "bus" ) );
            i.putExtra( "trans", ( Hashtable )resultHash.get( "trans" ) );
            i.putExtra( "air", ( Hashtable )resultHash.get( "air" ) );
            i.putExtra( "srcLoc", gpsAddr[0] );
            i.putExtra( "srcX", gpsX[0] );
            i.putExtra( "srcY", gpsY[0] );
            i.putExtra( "desLoc", desAddr[0] );
            i.putExtra( "desX", desX[0] );
            i.putExtra( "desY", desY[0] );
            
            pd.dismiss();
            startActivity( i );
            
          }
          
          // 
          else {

            Intent i = new Intent( FastRoadSearchActivity.this, TransOutCityActivity.class );
            int len = resultHash.size();
            int cnt1 = 0;
            int cnt2 = 0;
            int cnt3 = 0;
            for( int j = 0; j < len; j++ ) {
                  TransportationData[] tmp = ( TransportationData[] ) resultHash.get( j + "" );
                  
                
  
                    // +
                    if( tmp[0].getItem( "pathType" ).equals( "3" ) ) {
                      cnt1++;
                    }
                    
                    // 
                    if( tmp[0].getItem( "pathType" ).equals( "2" ) ) {
                      cnt2++;
                  
                    }
                    // 
                    if( tmp[0].getItem( "pathType" ).equals( "1" ) ) {
                      cnt3++;
                    }

                }
                

            TransportationData[][] bus = new TransportationData[cnt2][];
            TransportationData[][] sub = new TransportationData[cnt3][];
            TransportationData[][] busNsub = new TransportationData[cnt1][];
            

           cnt1 = 0;
           cnt2 = 0;
           cnt3 = 0;
           
           for( int y = 0 ; y < len; y++ ) {  
             TransportationData[] tmp = ( TransportationData[] ) resultHash.get( y + "" );

               if( tmp[0].getItem( "pathType" ).equals( "3" ) ) {
                 busNsub[cnt1] = tmp;
                 cnt1++;
               }
               
               if( tmp[0].getItem( "pathType" ).equals( "2" ) ) {
                 bus[cnt2] = tmp;
                 cnt2++;
               }
               
               if( tmp[0].getItem( "pathType" ).equals( "1" ) ) {
                 sub[cnt3] = tmp;
                 cnt3++;
               }

           }
           
       
       
           Hashtable< String, TransportationData[][] > inLoc = new Hashtable< String, TransportationData[][] >();
          inLoc.put("bus", bus );
          inLoc.put("sub", sub );
          inLoc.put("bus&sub", busNsub );
          
           MyApplication ma = (MyApplication) getApplication();
           ma.setMyClass( inLoc );
          i.putExtra("inLoc", inLoc );

          pd.dismiss();
          startActivity( i );

            
          }
      
      
        }
   };
   
   /*
    * gps 
    */
   private boolean chkGpsService() {
      String gs = android.provider.Settings.Secure.getString(getContentResolver(),
      android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
      
      if (gs.indexOf("gps", 0) < 0) {
        // GPS OFF  Dialog    ..
        AlertDialog.Builder gsDialog = new AlertDialog.Builder(this);
        gsDialog.setTitle("GPS ");
        gsDialog.setMessage("GPS .\n ?");
        gsDialog.setPositiveButton("", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            // GPS  
            Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
          }
        });
        
        gsDialog.setNegativeButton("", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
          }
        });
        
        gsDialog.create().show();
        return false;
      } 
      
      else {
        return true;
      }
    }



  private Runnable reverseGeoCoderThread = new Runnable() {
    public void run() {
    
        Geocoder mGeoCoder = new Geocoder( FastRoadSearchActivity.this, Locale.KOREA );
        try {
          addr = mGeoCoder.getFromLocation( lat, lng, 1 );
      } catch( Exception e ) {}
      
      new Thread( null, gpsSearchThread, "gpsSearchThread" ).start();
    }
  };

  @Override
  public void onLocationChanged(Location location) {
    if( location != null ) {

       lat = location.getLatitude();
      lng = location.getLongitude();
      
      new Thread( null, reverseGeoCoderThread, "reverseGeoCoderThread" ).start();


   }
   
   else {
     Toast.makeText( this, "GPS  .", Toast.LENGTH_SHORT ).show();
   }
    
  }

  @Override
  public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    
  }
  
}    
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.