Android Open Source - wigle-wifi-wardriving-badfork Query Thread






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;
//from  w ww . j av  a2 s .  co  m
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class QueryThread extends Thread {
  private final BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>();
  private final AtomicBoolean done = new AtomicBoolean( false );
  private final DatabaseHelper dbHelper;
  
  public interface ResultHandler {
    public void handleRow( Cursor cursor );
    public void complete();
  }
  public static class Request {
    private final String sql;
    private final ResultHandler handler;
    
    public Request( final String sql, final ResultHandler handler ) {
      if ( sql == null ) {
        throw new IllegalArgumentException( "sql is null" );
      }
      if ( handler == null ) {
        throw new IllegalArgumentException( "handler is null" );
      }
      this.sql = sql;
      this.handler = handler;
    }
  }
  
  public QueryThread( final DatabaseHelper dbHelper ) {
    this.dbHelper = dbHelper;
    setName( "query-" + getName() );
  }
  
  public void setDone() {
    done.set( true );
  }
  
  public void addToQueue( final Request request ) {
    try {
      queue.put( request );
    }
    catch ( InterruptedException ex ) {
      ListActivity.info( getName() + " interrupted" );
    }
  }
  
  public void run() {
    while ( ! done.get() ) {
      try {
        final Request request = queue.take();
        // if(true) throw new DBException("meh", new SQLiteException("meat puppets"));
        if ( request != null ) {
          final SQLiteDatabase db = dbHelper.getDB();
          if ( db != null ) {
            final Cursor cursor = db.rawQuery( request.sql, null );
            while ( cursor.moveToNext() ) {
              request.handler.handleRow( cursor );
            }
            request.handler.complete();
            cursor.close();
          }
        }
      }
      catch ( InterruptedException ex ) {
        ListActivity.info( getName() + " interrupted" );
      }
      catch ( DBException ex ) {
        dbHelper.deathDialog("query thread", ex);        
      }
    }
  }
  
}




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