Android Open Source - wigle-wifi-wardriving-badfork Http Downloader






From Project

Back to project page wigle-wifi-wardriving-badfork.

License

The source code is released under:

/* * Copyright (c) 2010-2012, Andrew Carra, Robert Hagemann, Hugh Kennedy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permi...

If you think the Android project wigle-wifi-wardriving-badfork 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 net.wigle.wigleandroid.background;
/*ww w . ja v a 2s .  c  om*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

import net.wigle.wigleandroid.DatabaseHelper;
import net.wigle.wigleandroid.ListActivity;
import net.wigle.wigleandroid.Network;
import net.wigle.wigleandroid.NetworkType;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;

public class HttpDownloader extends AbstractBackgroundTask {
  private final FileUploaderListener listener;
  
  public HttpDownloader( final Context context, final DatabaseHelper dbHelper, 
      final FileUploaderListener listener ) {
    
    super(context, dbHelper, "HttpDL");
    this.listener = listener;
  }
  
  protected void subRun() throws IOException, InterruptedException {
    Status status = Status.UNKNOWN;
    final Bundle bundle = new Bundle();
    try {
      final String username = getUsername();
      final String password = getPassword();
      status = validateUserPass( username, password );      
      if ( status == null ) {
        status = doDownload( username, password, bundle );
      }
      
    }
    catch ( final InterruptedException ex ) {
      ListActivity.info("Download Interrupted: " + ex);
    }      
    catch ( final Exception ex ) {
      ex.printStackTrace();
      ListActivity.error( "ex problem: " + ex, ex );
      ListActivity.writeError( this, ex, context );
      status = Status.EXCEPTION;
      bundle.putString( BackgroundGuiHandler.ERROR, "ex problem: " + ex );
    }
    finally {
      // tell the listener
      listener.transferComplete();
    }
    
    if ( status == null ) {
      status = Status.FAIL;
    }

    // tell the gui thread
    sendBundledMessage( status.ordinal(), bundle );
  }
   
  private Status doDownload( final String username, final String password, final Bundle bundle ) 
      throws IOException, InterruptedException {
    
    final boolean setBoundary = false;
    HttpURLConnection conn = HttpFileUploader.connect( 
        ListActivity.OBSERVED_URL, context.getResources(), setBoundary );
    
    // Send POST output.
    final DataOutputStream printout = new DataOutputStream (conn.getOutputStream ());
    String content = "observer=" + URLEncoder.encode ( username ) +
        "&password=" + URLEncoder.encode ( password );
    printout.writeBytes( content );
    printout.flush();
    printout.close();
    
    // get response data
    DataInputStream input = new DataInputStream ( HttpFileUploader.getInputStream( conn ) );
    insertObserved( input );
    return Status.WRITE_SUCCESS;
  }
  
  private void insertObserved( final DataInputStream reader ) 
      throws IOException, InterruptedException {
    
    final Bundle bundle = new Bundle();
    String line = null;
    int lineCount = 0;
    int totalCount = -1;
    final String COUNT_TAG = "count=";
    
    while ( (line = reader.readLine()) != null ) {
      if ( wasInterrupted() ) {
        throw new InterruptedException( "we were interrupted" );
      }
      
      if ( totalCount < 0 ) {
        if ( ! line.startsWith(COUNT_TAG) ) {
          continue;
        }
        else {
          totalCount = Integer.parseInt(line.substring(COUNT_TAG.length()));
          ListActivity.info("observed totalCount: " + totalCount);
        }
      }
      if ( line.length() != 12 || line.startsWith( "<" ) ) {
        continue;
      }
      
      // re-colon
      StringBuilder builder = new StringBuilder(15);
      for ( int i = 0; i < 12; i++ ) {
        builder.append(line.charAt(i));
        if ( i < 11 && (i%2) == 1 ) {
          builder.append(":");
        }
      }
      
      final String bssid = builder.toString();
      // ListActivity.info("line: " + line + " bssid: " + bssid);
      
      // do the insert      
      final String ssid = "";
      final int frequency = 0;
      final String capabilities = "";
      final int level = 0;
      final Network network = new Network(bssid, ssid, frequency, capabilities, level, NetworkType.WIFI);
      final Location location = new Location("wigle");
      final boolean newForRun = true;
      dbHelper.blockingAddObservation( network, location, newForRun );
      
      lineCount++;
      if ( (lineCount % 1000) == 0 ) {
        ListActivity.info("lineCount: " + lineCount + " of " + totalCount );
      }
      
      // update UI
      if ( totalCount == 0 ) {
        totalCount = 1;
      }
      final int percentDone = (int)((lineCount * 1000) / totalCount);
      sendPercentTimesTen( percentDone, bundle );      
    }        
  }
  
}




Java Source Code List

net.wigle.wigleandroid.ConcurrentLinkedHashMap.java
net.wigle.wigleandroid.DBException.java
net.wigle.wigleandroid.DBResultActivity.java
net.wigle.wigleandroid.DashboardActivity.java
net.wigle.wigleandroid.DataActivity.java
net.wigle.wigleandroid.DatabaseHelper.java
net.wigle.wigleandroid.ErrorReportActivity.java
net.wigle.wigleandroid.LatLon.java
net.wigle.wigleandroid.ListActivity.java
net.wigle.wigleandroid.MainActivity.java
net.wigle.wigleandroid.MappingActivity.java
net.wigle.wigleandroid.NetworkActivity.java
net.wigle.wigleandroid.NetworkListAdapter.java
net.wigle.wigleandroid.NetworkType.java
net.wigle.wigleandroid.Network.java
net.wigle.wigleandroid.OpenStreetMapViewWrapper.java
net.wigle.wigleandroid.Pair.java
net.wigle.wigleandroid.QueryArgs.java
net.wigle.wigleandroid.QueryThread.java
net.wigle.wigleandroid.SSLConfigurator.java
net.wigle.wigleandroid.SettingsActivity.java
net.wigle.wigleandroid.SpeechActivity.java
net.wigle.wigleandroid.TTS.java
net.wigle.wigleandroid.WigleAndroid.java
net.wigle.wigleandroid.WigleService.java
net.wigle.wigleandroid.WigleUncaughtExceptionHandler.java
net.wigle.wigleandroid.background.AbstractBackgroundTask.java
net.wigle.wigleandroid.background.AlertSettable.java
net.wigle.wigleandroid.background.BackgroundGuiHandler.java
net.wigle.wigleandroid.background.FileUploaderListener.java
net.wigle.wigleandroid.background.FileUploaderTask.java
net.wigle.wigleandroid.background.HttpDownloader.java
net.wigle.wigleandroid.background.HttpFileUploader.java
net.wigle.wigleandroid.background.KmlWriter.java
net.wigle.wigleandroid.background.Status.java
net.wigle.wigleandroid.listener.BatteryLevelReceiver.java
net.wigle.wigleandroid.listener.GPSListener.java
net.wigle.wigleandroid.listener.PhoneState7.java
net.wigle.wigleandroid.listener.PhoneStateFactory.java
net.wigle.wigleandroid.listener.PhoneState.java
net.wigle.wigleandroid.listener.SsidSpeaker.java
net.wigle.wigleandroid.listener.WifiReceiver.java