Android Open Source - wigle-wifi-wardriving-badfork Main Activity






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;
//ww  w .j  av  a 2s.  c o m
import java.io.File;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost;

public final class MainActivity extends TabActivity {
  static final String TAB_LIST = "list";
  static final String TAB_MAP = "map";
  static final String TAB_DASH = "dash";
  static final String TAB_DATA = "data";
  static final Locale ORIG_LOCALE = Locale.getDefault();
  
  private static MainActivity mainActivity;
  private ListActivity listActivity;
  private boolean screenLocked = false;
  private PowerManager.WakeLock wakeLock;
  
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set language
    setLocale( this );  
    setContentView(R.layout.main);
    
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
    if ( wakeLock.isHeld() ) {
      wakeLock.release();
    }
    
    mainActivity = this;
    
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ListActivity.class);
    spec = tabHost.newTabSpec( TAB_LIST ).setIndicator(getString(R.string.tab_list),
                   getResources().getDrawable(android.R.drawable.ic_menu_sort_by_size))
                  .setContent(intent);
    tabHost.addTab(spec);
    
    intent = new Intent().setClass(this, MappingActivity.class);
    spec = tabHost.newTabSpec( TAB_MAP ).setIndicator(getString(R.string.tab_map),
                  getResources().getDrawable(android.R.drawable.ic_menu_mapmode))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, DashboardActivity.class);
    spec = tabHost.newTabSpec( TAB_DASH ).setIndicator(getString(R.string.tab_dash),
                  getResources().getDrawable(android.R.drawable.ic_menu_view))
                  .setContent(intent);
    tabHost.addTab(spec);
    
    intent = new Intent().setClass(this, DataActivity.class);
    spec = tabHost.newTabSpec( TAB_DATA ).setIndicator(getString(R.string.tab_data),
                  getResources().getDrawable(android.R.drawable.ic_menu_search))
                  .setContent(intent);
    tabHost.addTab(spec);
    
    tabHost.setCurrentTabByTag( TAB_LIST );
  }
  
  // be careful with this
  public static MainActivity getMainActivity() {
    return mainActivity;
  }
  
  /**
   * switch to the specified tab using the activity's parent, if possible
   * @param activity
   * @param tab
   */
  static void switchTab( Activity activity, String tab ) {
    final Activity parent = activity.getParent();
    if ( parent != null && parent instanceof TabActivity ) {
      ((TabActivity) parent).getTabHost().setCurrentTabByTag( tab );
    }
  }
  
  static void setLockScreen( Activity activity, boolean lockScreen ) {
    final Activity parent = activity.getParent();
    if ( parent != null && parent instanceof MainActivity ) {
      MainActivity main = (MainActivity) parent;
      main.setLockScreen( lockScreen );
    }
  }
  
  static boolean isScreenLocked( Activity activity ) {
    final Activity parent = activity.getParent();
    if ( parent != null && parent instanceof MainActivity ) {
      MainActivity main = (MainActivity) parent;
      return main.screenLocked;
    }
    return false;
  }
  
  private void setLockScreen( boolean lockScreen ) {
    this.screenLocked = lockScreen;
    if ( lockScreen ) {
      if ( ! wakeLock.isHeld() ) {
        ListActivity.info("acquire wake lock");
        wakeLock.acquire();
      }
    }
    else if ( wakeLock.isHeld() ) {
      ListActivity.info("release wake lock");
      wakeLock.release();
    }
  }
  
  public static interface Doer {
    public void execute();
  }
  
  static void createConfirmation( final Activity activity, final String message, final Doer doer ) {
    final AlertDialog.Builder builder = new AlertDialog.Builder( activity );
    builder.setCancelable( true );
    builder.setTitle( "Confirmation" );
    builder.setMessage( message );
    AlertDialog ad = builder.create();
    // ok
    ad.setButton( DialogInterface.BUTTON_POSITIVE, activity.getString(R.string.ok), new DialogInterface.OnClickListener() {
      public void onClick( final DialogInterface dialog, final int which ) {
        try {
          dialog.dismiss();
          doer.execute();
        }
        catch ( Exception ex ) {
          // guess it wasn't there anyways
          ListActivity.info( "exception dismissing alert dialog: " + ex );
        }
        return;
      } }); 
    
    // cancel
    ad.setButton( DialogInterface.BUTTON_NEGATIVE, activity.getString(R.string.cancel), new DialogInterface.OnClickListener() {
      public void onClick( final DialogInterface dialog, final int which ) {
        try {
          dialog.dismiss();
        }
        catch ( Exception ex ) {
          // guess it wasn't there anyways
          ListActivity.info( "exception dismissing alert dialog: " + ex );
        }
        return;
      } }); 
    
    try {
      ad.show();
    }
    catch ( WindowManager.BadTokenException ex ) {
      ListActivity.info( "exception showing dialog, view probably changed: " + ex, ex );
    }
  }
  
  public static CheckBox prefSetCheckBox( final Context context, final Dialog dialog, final int id, 
      final String pref, final boolean def ) {
    
    final SharedPreferences prefs = context.getSharedPreferences( ListActivity.SHARED_PREFS, 0);
    final CheckBox checkbox = (CheckBox) dialog.findViewById( id );
    checkbox.setChecked( prefs.getBoolean( pref, def ) );
    return checkbox;
  }
  
  public static CheckBox prefSetCheckBox( final Activity activity, final int id, final String pref, final boolean def ) {
    final SharedPreferences prefs = activity.getSharedPreferences( ListActivity.SHARED_PREFS, 0);
    final CheckBox checkbox = (CheckBox) activity.findViewById( id );
    checkbox.setChecked( prefs.getBoolean( pref, def ) );
    return checkbox;
  }
  
  public static CheckBox prefBackedCheckBox( final Activity activity, final int id, final String pref, final boolean def ) {
    final SharedPreferences prefs = activity.getSharedPreferences( ListActivity.SHARED_PREFS, 0);
    final Editor editor = prefs.edit();
    final CheckBox checkbox = prefSetCheckBox( activity, id, pref, def );
    checkbox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
      public void onCheckedChanged( final CompoundButton buttonView, final boolean isChecked ) {             
            editor.putBoolean( pref, isChecked );
            editor.commit();
        }
    });
    
    return checkbox;
  }
  
  static MainActivity getMainActivity( Activity activity ) {
    final Activity parent = activity.getParent();    
    if ( parent != null && parent instanceof MainActivity ) {
      return (MainActivity) parent;
    }
    return null;
  }
  
  /** safely get the canonical path, as this call throws exceptions on some devices */
  public static String safeFilePath( final File file ) {
    String retval = null;
    try {
      retval = file.getCanonicalPath();
    }
    catch ( Exception ex ) {
      // ignore
    }
    
    if ( retval == null ) {
      retval = file.getAbsolutePath();
    }
    return retval;
  }
  
  /**
   * For now, we need a handle to the list activity to FINISH HIM.
   * Eventually the MainActivity might handle all the services and listeners and things
   * @param listActivity
   */
  public void setListActivity( final ListActivity listActivity ) {
    this.listActivity = listActivity;
  }
  
  public static void finishListActivity( final Activity activity ) {
    MainActivity main = getMainActivity( activity );
    if ( main != null ) {
      main.finishListActivity();
    }
  }
  
  public static ListActivity getListActivity( final Activity activity ) {
    MainActivity main = getMainActivity( activity );
    if ( main != null ) {
      return main.listActivity;
    }
    return null;
  }
  
  public void finishListActivity() {
    if ( listActivity != null ) {
      listActivity.finish();
    }
  }
  
  @Override
  public void onDestroy() {
    ListActivity.info( "MAIN: destroy." );
    super.onDestroy();
  }
  
  @Override
  public void onPause() {
    ListActivity.info( "MAIN: pause." );
    super.onPause();
    
    // deal with wake lock
    if ( wakeLock.isHeld() ) {
      ListActivity.info("release wake lock");
      wakeLock.release();
    }
  }
  
  @Override
  public void onResume() {
    ListActivity.info( "MAIN: resume." );
    super.onResume();
    
    // deal with wake lock
    if ( ! wakeLock.isHeld() && screenLocked ) {
      ListActivity.info("acquire wake lock");
      wakeLock.acquire();
    }
  }
  
  @Override
  public void onConfigurationChanged( final Configuration newConfig ) {
    ListActivity.info( "MAIN: config changed" );
    setLocale( this, newConfig );
    super.onConfigurationChanged( newConfig );        
  }
  
  @Override
  public void onStart() {
    ListActivity.info( "MAIN: start." );
    super.onStart();
  }
  
  @Override
  public void onStop() {
    ListActivity.info( "MAIN: stop." );
    super.onStop();
  }
  
  @Override
  public void onRestart() {
    ListActivity.info( "MAIN: restart." );
    super.onRestart();
  }
  
  @Override
  public void finish() {
    ListActivity.info( "MAIN: finish." );
    super.finish();
  }
  
  public static Throwable getBaseThrowable(final Throwable throwable) {
    Throwable retval = throwable;
    while ( retval.getCause() != null ) {
      retval = retval.getCause();
    }
    return retval;
  }
  
  public static String getBaseErrorMessage(Throwable throwable, final boolean withNewLine) {
    throwable = MainActivity.getBaseThrowable( throwable );
    final String newline = withNewLine ? "\n" : " ";
    return throwable.getClass().getSimpleName() + ":" + newline + throwable.getMessage();
  }
  
  public static void setLocale( final Activity activity ) {
    final Context context = activity.getBaseContext();
    final Configuration config = context.getResources().getConfiguration();
    setLocale( activity, config );
  }
  
  public static void setLocale( final Activity activity, final Configuration config ) {
    final Context context = activity.getBaseContext();
    final SharedPreferences prefs = context.getSharedPreferences( ListActivity.SHARED_PREFS, 0 );
    final String lang = prefs.getString( ListActivity.PREF_LANGUAGE, "" );
    final String current = config.locale.getLanguage();
    ListActivity.info("current lang: " + current + " new lang: " + lang);
    Locale newLocale = null;
    if (! "".equals(lang) && ! current.equals(lang) && lang != null) {
      newLocale = new Locale(lang);
    }
    else if ("".equals(lang) && ORIG_LOCALE != null && ! current.equals(ORIG_LOCALE.getLanguage()) ) {
      newLocale = ORIG_LOCALE;
    }
    
    if ( newLocale != null ) {
      Locale.setDefault(newLocale);
      config.locale = newLocale;
      ListActivity.info("setting locale: " + newLocale);
      context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    }          
  }
}




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