Android Open Source - android-accelerometer-gps Main Application






From Project

Back to project page android-accelerometer-gps.

License

The source code is released under:

MIT License

If you think the Android project android-accelerometer-gps 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 com.szlosek.whenmoving;
/*  w w w .  j  ava2  s  .  com*/
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class MainApplication extends Application {
  protected static MainApplication mInstance;
  public static MainService mServiceInstance;
  
  // Status and Preferences
  public static boolean trackingOn = false; // not running
  public static int prefInterval = 0;
  public static int prefTimeout = 0;
  public static double prefThreshold = 0.30;
  public static double prefThreshold2 = 10.1;
  
  // Wake Locks
  protected static PowerManager mPowerManager;
  protected static PowerManager.WakeLock mWakeLock1;
  protected static PowerManager.WakeLock mWakeLock2;
  
  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
    mServiceInstance = null;
    mPowerManager = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
  }
  @Override
  public void onLowMemory() {
  }
  
  public static void Debug(String message) {
    Log.d("WhenMoving", message);
  }
  
  public static MainApplication getInstance() {
    return mInstance;
  }
  
  public void startup() {
    wakeLock1(true);
    Debug("startup");
    Intent i = new Intent(this, MainService.class);
    startService(i);
  }
  
  // Accelerometer Wake Lock
  public static void wakeLock1(boolean up) {
    if (up) {
      mWakeLock1 = mPowerManager.newWakeLock(
        PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
        "WakeLock:Accelerometer"
      );
      mWakeLock1.acquire();
    } else {
      if (mWakeLock1 != null) {
        if (mWakeLock1.isHeld()) {
          mWakeLock1.release();
        }
        mWakeLock1 = null;
      }
    }
  }
  // GPS Wake Lock
  public static void wakeLock2(boolean up) {
    if (up) {
      mWakeLock2 = mPowerManager.newWakeLock(
        PowerManager.PARTIAL_WAKE_LOCK,
        "WakeLock:GPS"
      );
      mWakeLock2.acquire();
    } else {
      if (mWakeLock2 != null) {
        if (mWakeLock2.isHeld()) {
          mWakeLock2.release();
        }
        mWakeLock2 = null;
      }
    }
  }
  
  public static void onPreferenceChange() {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApplication.getInstance());
    //  Get latest settings, and update accordingly
    boolean newState = sp.getBoolean("pref_onoff", false); // false is off/not-running
    
    prefInterval = Integer.parseInt( sp.getString("pref_interval", "60") );
    prefTimeout = Integer.parseInt( sp.getString("pref_timeout", "30") );
    
    MainApplication.prefThreshold = Double.parseDouble( sp.getString("pref_threshold", "0.30") );
    MainApplication.prefThreshold2 = MainApplication.prefThreshold + 9.80;
    
    // If we turned off the service, handle that change
    toggleState( newState );
  }
  
  public static void toggleState(boolean newState) {
    Debug(String.format("New state: %s", (newState == true ? "on" : "off")));
    if (MainApplication.trackingOn == true) {
      if (newState == false) {
        MainApplication.trackingOn = false;
        // Graceful shutdown in progress
        Toast.makeText(getInstance(), String.format("Gracefully stopping in %ds", MainApplication.prefInterval), Toast.LENGTH_SHORT).show();
      }
    } else {
      if (newState == true) {
        // Schedule an alarm
        MainApplication.trackingOn = true;
        MainApplication.getInstance().startup();
      }
    }
  }

}




Java Source Code List

com.szlosek.whenmoving.CalibrationActivity.java
com.szlosek.whenmoving.CircularBuffer.java
com.szlosek.whenmoving.DatabaseHelper.java
com.szlosek.whenmoving.DaysActivity.java
com.szlosek.whenmoving.MainActivity.java
com.szlosek.whenmoving.MainApplication.java
com.szlosek.whenmoving.MainReceiver.java
com.szlosek.whenmoving.MainService.java
com.szlosek.whenmoving.SettingsActivity.java
com.szlosek.whenmoving.TimeoutReceiver.java