Android Open Source - wigle-wifi-wardriving-badfork Abstract Background Task






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;
/*from  w  w  w  .ja va2  s.  c o  m*/
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import net.wigle.wigleandroid.DatabaseHelper;
import net.wigle.wigleandroid.ListActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;

public abstract class AbstractBackgroundTask extends Thread implements AlertSettable {
  private static final int THREAD_PRIORITY = Process.THREAD_PRIORITY_BACKGROUND;
  
  protected Context context;
  protected final DatabaseHelper dbHelper;
  
  private final Handler handler;  
  private final AtomicBoolean interrupt = new AtomicBoolean( false );
  private final Object lock = new Object();  
  private final String name;  
  private ProgressDialog pd;
  private AlertDialog ad;
  private int lastSentPercent = -1;
  
  public AbstractBackgroundTask( final Context context, final DatabaseHelper dbHelper, final String name ) { 
    if ( context == null ) {
      throw new IllegalArgumentException( "context is null" );
    }
    if ( dbHelper == null ) {
      throw new IllegalArgumentException( "dbHelper is null" );
    }
    if ( name == null ) {
      throw new IllegalArgumentException( "name is null" );
    }
    
    this.context = context;
    this.dbHelper = dbHelper;
    this.name = name;
    
    createProgressDialog( context );
    
    this.handler = new BackgroundGuiHandler(context, lock, pd, this);
  }
  
  public final void clearProgressDialog() {
    pd = null;
  }  
  
  public final void run() {
    // set thread name
    setName( name + "-" + getName() );
    
    try {
      ListActivity.info( "setting file export thread priority (-20 highest, 19 lowest) to: " + THREAD_PRIORITY );
      Process.setThreadPriority( THREAD_PRIORITY );
      
      subRun();      
    }
    catch ( InterruptedException ex ) {
      ListActivity.info( name + " interrupted: " + ex );
    }
    catch ( final Exception ex ) {
      dbHelper.deathDialog(name, ex);
    }    
  }
  
  protected final void sendPercentTimesTen(final int percentDone, final Bundle bundle) {
    // only send up to 1000 times
    if ( percentDone > lastSentPercent && percentDone >= 0 ) {
      sendBundledMessage( BackgroundGuiHandler.WRITING_PERCENT_START + percentDone, bundle );
      lastSentPercent = percentDone;
    }
  }

  protected final void sendBundledMessage(final int what, final Bundle bundle) {    
    final Message msg = new Message();
    msg.what = what;
    msg.setData(bundle);
    handler.sendMessage(msg);
  }  
  
  protected abstract void subRun() throws IOException, InterruptedException;
  
  /** interrupt this upload */
  public final void setInterrupted() {
    interrupt.set( true );
  }  
  
  protected final boolean wasInterrupted() {
    return interrupt.get();
  }
  
  public final Handler getHandler() {
    return handler;
  }
  
  private void createProgressDialog(final Context context) {
    // make an interruptable progress dialog
    pd = ProgressDialog.show( context, context.getString(Status.WRITING.getTitle()), 
        context.getString(Status.WRITING.getMessage()), true, true,
      new OnCancelListener(){ 
        @Override
        public void onCancel( DialogInterface di ) {
          interrupt.set(true);
        }
      });
  }
  
  public final void setAlertDialog(final AlertDialog ad) {
    this.ad = ad;
  }   
  
  public final void setContext( final Context context ) {
    synchronized ( lock ) {
      this.context = context;
      
      if ( pd != null && pd.isShowing() ) {
        try {
          pd.dismiss();
        }
        catch ( Exception ex ) {
          // guess it wasn't there anyways
          ListActivity.info( "exception dismissing progress dialog: " + ex );
        }
        createProgressDialog( context );
      }
      
      if ( ad != null && ad.isShowing() ) {
        try {
          ad.dismiss();
        }
        catch ( Exception ex ) {
          // guess it wasn't there anyways
          ListActivity.info( "exception dismissing alert dialog: " + ex );
        }
      }
    }
  }
  
  protected final String getUsername() {
    final SharedPreferences prefs = context.getSharedPreferences( ListActivity.SHARED_PREFS, 0);
    String username = prefs.getString( ListActivity.PREF_USERNAME, "" );
    if ( prefs.getBoolean( ListActivity.PREF_BE_ANONYMOUS, false) ) {
      username = ListActivity.ANONYMOUS;
    }
    return username;
  }
  
  protected final String getPassword() {
    final SharedPreferences prefs = context.getSharedPreferences( ListActivity.SHARED_PREFS, 0);
    String password = prefs.getString( ListActivity.PREF_PASSWORD, "" );
    
    if ( prefs.getBoolean( ListActivity.PREF_BE_ANONYMOUS, false) ) {
      password = "";
    }
    return password;
  }
   
  /**
   * @return null if ok, else an error status
   */
  protected final Status validateUserPass(final String username, final String password) {
    Status status = null;
    if ( "".equals( username ) ) {
      // TODO: error
      ListActivity.error( "username not defined" );
      status = Status.BAD_USERNAME;
    }
    else if ( "".equals( password ) && ! ListActivity.ANONYMOUS.equals( username.toLowerCase() ) ) {
      // TODO: error
      ListActivity.error( "password not defined and username isn't 'anonymous'" );
      status = Status.BAD_PASSWORD;
    }
    
    return status;
  }
  
}




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