Settings.java :  » Weather » fhj-ws2010-itm08-weather-app » at » fhj » itm » weather » Android Open Source

Android Open Source » Weather » fhj ws2010 itm08 weather app 
fhj ws2010 itm08 weather app » at » fhj » itm » weather » Settings.java
package at.fhj.itm.weather;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class is used to save and persist the settings. These values are:
 * <ul>
 * <li>startWithResult...flag saying that the application should startup with
 * the weather page</li>
 * <li>gpsLoadAtStart...flag saying that the gps sensor is used</li>
 * <li>gpsReloading...flag saying that the gps sensor should reload when the
 * location has changed</li>
 * <li>gpsMinDistance...integer value defining the distance the user has to move
 * to activate a gps-called weather-reload</li>
 * <li>home...String representing an address to be saved for quick access</li>
 * </ul>
 * 
 * The setter will only cause a local change. The method {@link #saveValues()}
 * has to be called to persist the values.
 * 
 * 
 * @author Christian Tassler & Andreas Reibenbacher
 * 
 */
public class Settings extends SQLiteOpenHelper {

  private static final String CREATE_TABLE_SETTINGS = "CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, gpsLoadAtStart BOOLEAN, gpsReloading BOOLEAN, gpsMinDistance INTEGER, startWithResult BOOLEAN, home VARCHAR);";
  private static final String DROP_TABLE_SETTINGS = "DROP TABLE IF EXISTS settings;";

  public Settings(Context context) {
    super(context, "weatherApp.db", null, 2);
    loadValues();
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_SETTINGS);
    db.execSQL("INSERT INTO settings (`gpsLoadAtStart`, `gpsReloading`, `gpsMinDistance`, `startWithResult`, `home`) VALUES ('"
        + gpsLoadAtStart
        + "', '"
        + gpsReloading
        + "', '"
        + gpsMinDistance
        + "', '"
        + startWithResult
        + "', '"
        + home
        + "'); ");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE_SETTINGS);
    this.onCreate(db);
  }

  private boolean gpsLoadAtStart = true; // init the gps at start
  private boolean gpsReloading = true; // update the geodata regularly
  private int gpsMinDistance = 1000; // update only when moved x meter
  private boolean startWithResult = true; // start up with result
  private String home = ""; // one address can be stored for quick access

  /**
   * Loads the values from the database
   */
  private void loadValues() {
    String[] columns = { "gpsLoadAtStart", "gpsReloading",
        "gpsMinDistance", "startWithResult", "home" };
    Cursor c = this.getReadableDatabase().query("settings", columns, null,
        null, null, null, null);
    c.moveToLast();

    setGpsLoadAtStart(c.getInt(0) == 1);
    setGpsReloading(c.getInt(1) == 1);
    setGPSMinDistance(c.getInt(2));
    setStartWithResult(c.getInt(3) == 1);
    setHome(c.getString(4));
    c.close();
    Log.i("WeatherApp", "settings loaded " + isGpsLoadAtStart()
        + isGpsReloading() + getGPSMinDistance() + isStartWithResult());
  }

  /**
   * Persists the values to the database.
   */
  public void saveValues() {
    ContentValues cv = new ContentValues();
    cv.put("gpsLoadAtStart", isGpsLoadAtStart());
    cv.put("gpsReloading", isGpsReloading());
    cv.put("gpsMinDistance", getGPSMinDistance());
    cv.put("startWithResult", isStartWithResult());
    cv.put("home", getHome());
    SQLiteDatabase connection = this.getWritableDatabase();
    connection.insert("settings", null, cv);
    connection.close();
    cv.clear();
    
    Log.i("WeatherApp", "settings saved " + isGpsLoadAtStart()
        + isGpsReloading() + getGPSMinDistance() + isStartWithResult());
  }

  public int getGPSMinDistance() {
    return this.gpsMinDistance;
  }

  public void setGPSMinDistance(int gpsMinDistance) {
    this.gpsMinDistance = gpsMinDistance;
  }

  public void setGpsReloading(boolean gpsReload) {
    this.gpsReloading = gpsReload;
  }

  public boolean isGpsReloading() {
    return gpsReloading;
  }

  public void setGpsLoadAtStart(boolean gpsLoadatStart) {
    this.gpsLoadAtStart = gpsLoadatStart;
  }

  public boolean isGpsLoadAtStart() {
    return gpsLoadAtStart;
  }

  public void setStartWithResult(boolean startWithResult) {
    this.startWithResult = startWithResult;
  }

  public boolean isStartWithResult() {
    return this.startWithResult;
  }

  public void setHome(String location) {
    this.home = location;
  }

  public String getHome() {
    return this.home;
  }

  @Override
  public String toString() {
    return "Settings [gpsLoadAtStart=" + gpsLoadAtStart + ", gpsReloading="
        + gpsReloading + ", gpsMinDistance=" + gpsMinDistance
        + ", startWithResult=" + startWithResult + ", home=" + home
        + "]";
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.