Android Open Source - AerisAndroidLibrary My Places Db






From Project

Back to project page AerisAndroidLibrary.

License

The source code is released under:

Apache License

If you think the Android project AerisAndroidLibrary 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.example.db;
/*  www  .  j  av a  2 s  .  c o m*/
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import com.example.demoaerisproject.BaseApplication;
import com.example.demoaerisproject.R;
import com.example.preference.PrefManager;
import com.hamweather.aeris.communication.parameter.PlaceParameter;

public class MyPlacesDb extends SQLiteOpenHelper {

  public static final String DATABASE_NAME = "AerisPlaces.db";
  public static final int DB_VERSION = 1;

  private Context context;

  public MyPlacesDb(Context context) {
    super(context, DATABASE_NAME, null, DB_VERSION);
    this.context = context;
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
   * .SQLiteDatabase)
   */
  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL(MyPlaceTable.CREATE_TABLE_STMT);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  }

  public static final class MyPlaceTable implements
      android.provider.BaseColumns {
    /** The name of this table */
    public static final String TABLE = "aeris_places";

    static final String CREATE_TABLE_STMT = "CREATE TABLE "
        + MyPlaceTable.TABLE + "(" + " " + BaseColumns._ID + " "
        + PlacesColumns._ID_TYPE + "," + " " + PlacesColumns.NAME + " "
        + PlacesColumns.NAME_TYPE + ", " + PlacesColumns.STATE + " "
        + PlacesColumns.STATE_TYPE + ", " + PlacesColumns.COUNTRY + " "
        + PlacesColumns.COUNTRY_TYPE + ", " + PlacesColumns.LATITUDE
        + " " + PlacesColumns.LATITUDE_TYPE + ", "
        + PlacesColumns.LONGITUDE + " " + PlacesColumns.LONGITUDE_TYPE
        + ", " + PlacesColumns.MY_CITY
        + " " + PlacesColumns.MY_CITY_TYPE + ");";
  }

  public static class PlacesColumns {
    static final String _ID_TYPE = "INTEGER PRIMARY KEY AUTOINCREMENT";
    public static final String NAME = "name";
    static final String NAME_TYPE = "TEXT NOT NULL";
    public static final String STATE = "state";
    static final String STATE_TYPE = "TEXT";
    public static final String COUNTRY = "country";
    static final String COUNTRY_TYPE = "TEXT";
    public static final String MY_CITY = "mycity";
    static final String MY_CITY_TYPE = "BOOLEAN";
    public static final String LATITUDE = "latitude";
    static final String LATITUDE_TYPE = "REAL";
    public static final String LONGITUDE = "longitude";
    static final String LONGITUDE_TYPE = "REAL";

  }

  public long insertPlaces(String name, String state, String country,
      double lat, double lon,
      boolean myPlace) {
    SQLiteDatabase sqldb = getWritableDatabase();
    ContentValues args = new ContentValues();
    args.put(PlacesColumns.NAME, name);
    if (state != null)
      args.put(PlacesColumns.STATE, state);
    if (country != null)
      args.put(PlacesColumns.COUNTRY, country);
    args.put(PlacesColumns.LATITUDE, lat);
    args.put(PlacesColumns.LONGITUDE, lon);
    args.put(PlacesColumns.MY_CITY, myPlace);
    Cursor cursor = sqldb.query(MyPlaceTable.TABLE,
        new String[] { PlacesColumns.NAME }, PlacesColumns.NAME
            + "=? AND " + PlacesColumns.COUNTRY + "=?",
        new String[] { name, country }, null, null, null);
    long id = -1;
    if (cursor != null && cursor.moveToFirst()) {
      id = sqldb.update(MyPlaceTable.TABLE, args, PlacesColumns.NAME
          + "=? AND " + PlacesColumns.COUNTRY + "=?", new String[] {
          name, country });
    } else {
      id = sqldb.insert(MyPlaceTable.TABLE, null, args);
    }
    cursor.close();
    if (myPlace) {
      ContentValues changeArgs = new ContentValues();
      changeArgs.put(PlacesColumns.MY_CITY, false);
      // get all rows, update them to false minus the correct one.
      sqldb.update(MyPlaceTable.TABLE, changeArgs, PlacesColumns.NAME
          + "!=? OR " + PlacesColumns.COUNTRY + "!=?", new String[] {
          name, country });
      BaseApplication.enableNotificationService(context, PrefManager
.getBoolPreference(context,
              context.getString(R.string.pref_ntf_enabled)));
      MyPlacesSubject.getInstance().notifyObservers(null);
    }
    return id;
  }

  public boolean deletePlace(MyPlace place) {

    if (place != null && place.name != null && place.country != null) {
      SQLiteDatabase sqldb = getWritableDatabase();
      int affected = sqldb.delete(MyPlaceTable.TABLE, PlacesColumns.NAME
          + "=? AND " + PlacesColumns.COUNTRY + "=?", new String[] {
          place.name, place.country });
      return affected > 0;
    }
    return false;
  }

  public Cursor getMyPlaceCursor() {
    SQLiteDatabase sqldb = getWritableDatabase();
    Cursor cursor = sqldb.query(MyPlaceTable.TABLE, null,
        PlacesColumns.MY_CITY + "=1", null, null, null, null);
    return cursor;
  }

  public PlaceParameter getMyPlaceParameter() {
    Cursor cursor = getMyPlaceCursor();
    if (cursor != null && cursor.moveToFirst()) {
      MyPlace place = new MyPlace();
      place.name = cursor.getString(cursor
          .getColumnIndex(PlacesColumns.NAME));
      place.state = cursor.getString(cursor
          .getColumnIndex(PlacesColumns.STATE));
      place.country = cursor.getString(cursor
          .getColumnIndex(PlacesColumns.COUNTRY));
      cursor.close();
      if (place.state != null) {
        return new PlaceParameter(String.format("%s,%s,%s", place.name,
            place.state, place.country));
      } else {
        return new PlaceParameter(String.format("%s,%s", place.name,
            place.country));
      }

    }
    return null;
  }

  public MyPlace getMyPlace() {
    Cursor cursor = getMyPlaceCursor();
    if (cursor != null && cursor.moveToFirst()) {
      MyPlace place = new MyPlace();
      place.name = cursor.getString(cursor
          .getColumnIndex(PlacesColumns.NAME));
      place.state = cursor.getString(cursor
          .getColumnIndex(PlacesColumns.STATE));
      place.country = cursor.getString(cursor
          .getColumnIndex(PlacesColumns.COUNTRY));
      place.latitude = cursor.getDouble(cursor
          .getColumnIndex(PlacesColumns.LATITUDE));
      place.longitude = cursor.getDouble(cursor
          .getColumnIndex(PlacesColumns.LONGITUDE));
      cursor.close();
      return place;
    }
    return null;
  }

  public List<MyPlace> getPlaces() {
    SQLiteDatabase sqldb = getWritableDatabase();
    Cursor cursor = sqldb.query(MyPlaceTable.TABLE, null, null, null, null,
        null, null);
    List<MyPlace> retval = new ArrayList<MyPlace>();
    if (cursor != null) {
      while (cursor.moveToNext()) {
        MyPlace place = new MyPlace();
        place.name = cursor.getString(cursor
            .getColumnIndex(PlacesColumns.NAME));
        place.state = cursor.getString(cursor
            .getColumnIndex(PlacesColumns.STATE));
        place.country = cursor.getString(cursor
            .getColumnIndex(PlacesColumns.COUNTRY));
        place.myLoc = cursor.getInt(cursor
            .getColumnIndex(PlacesColumns.MY_CITY)) == 1;
        place.latitude = cursor.getDouble(cursor
            .getColumnIndex(PlacesColumns.LATITUDE));
        place.longitude = cursor.getDouble(cursor
            .getColumnIndex(PlacesColumns.LONGITUDE));
        retval.add(place);
      }
      cursor.close();
    }
    return retval;
  }

}




Java Source Code List

aeris.AerisUnusedStub.java
com.example.customendpoint.CustomEndpointAdapter.java
com.example.customendpoint.CustomMoonModel.java
com.example.customendpoint.CustomSunModel.java
com.example.customendpoint.CustomSunmoonFragment.java
com.example.customendpoint.CustomSunmoonHolder.java
com.example.customendpoint.CustomSunmoonModel.java
com.example.customendpoint.CustomSunmoonResponse.java
com.example.db.MyLocLoader.java
com.example.db.MyPlace.java
com.example.db.MyPlacesDb.java
com.example.db.MyPlacesSubject.java
com.example.db.SimpleCursorLoader.java
com.example.demoaerisproject.AerisDialog.java
com.example.demoaerisproject.AerisNotification.java
com.example.demoaerisproject.BaseApplication.java
com.example.demoaerisproject.DrawerActivity.java
com.example.demoaerisproject.LocationSearchActivity.java
com.example.demoaerisproject.MyLocsActivity.java
com.example.demoaerisproject.SettingsActivity.java
com.example.edithelp.EditTextEnterListener.java
com.example.edithelp.EnterPressedDelegate.java
com.example.fragment.AerisFragment.java
com.example.fragment.AerisPrefFragment.java
com.example.fragment.ExtForecastFragment.java
com.example.fragment.HeadlessFragment.java
com.example.fragment.MapFragment.java
com.example.fragment.NearbyObsFragment.java
com.example.fragment.ObservationFragment.java
com.example.fragment.OverviewFragment.java
com.example.fragment.RecentObsFragment.java
com.example.fragment.RefreshInterface.java
com.example.fragment.SplashFragment.java
com.example.fragment.WeekendFragment.java
com.example.listview.AdapterHolder.java
com.example.listview.DayNightPeriod.java
com.example.listview.ForecastAdapter.java
com.example.listview.ForecastItemHolder.java
com.example.listview.IndexMonitorAdapter.java
com.example.listview.ListAdapter.java
com.example.listview.ObservationAdapter.java
com.example.listview.ObservationItemHolder.java
com.example.listview.ObservationPeriodAdapter.java
com.example.listview.PlacesAdapter.java
com.example.listview.PlacesItemHolder.java
com.example.listview.RecentObsHolder.java
com.example.listview.WeekendAdapter.java
com.example.listview.WeekendItemHolder.java
com.example.menudrawer.HomeFragment.java
com.example.menudrawer.NavDrawerItem.java
com.example.menudrawer.NavDrawerListAdapter.java
com.example.preference.PrefManager.java
com.example.service.NotificationService.java
com.example.service.ScreenOnReceiver.java
com.example.service.ScreenOnService.java
com.example.util.FormatUtil.java
com.example.view.DayNightView.java
com.example.view.SmallForecastView.java
com.example.view.TemperatureInfoData.java
com.example.view.TemperatureWindowAdapter.java
com.example.view.TwoPartView.java