Android Open Source - GeoFencingDemo Sql Lite Db Helper






From Project

Back to project page GeoFencingDemo.

License

The source code is released under:

MIT License

If you think the Android project GeoFencingDemo 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.ehc.GeoFencingDemo;
//from   www  .j  ava  2 s.com
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.location.Address;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;

public class SqlLiteDbHelper extends SQLiteOpenHelper {

  private Context context;
  private SQLiteDatabase database;
  public static final int DATABASE_VERSION = 1;
  public static final String DATABASE_NAME = "GeoFencingDemo.db";
  public static final String TABLE_NAME = "geo_fencing";


  public SqlLiteDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
    database = getWritableDatabase();
  }

  public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "create table " + TABLE_NAME + "(location_address varchar2(20),location_sublocality varchar2(20)," +
        "location_locality varchar2(20),location_country varchar2(20),location_latitude varchar2(20),location_longitude varchar2(20)," +
        "location_timestamp date,front_image BLOB,back_image BLOB)";
    db.execSQL(CREATE_TABLE);
  }

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

  }

  public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
  }

  public void insertRecord(Address address, Bitmap frontImage, Bitmap backImage) {
    try {
      deleteIfMoreRecords();
      ContentValues values = new ContentValues();
      values.put("location_address", address.getAddressLine(0));
      values.put("location_sublocality", address.getSubLocality());
      values.put("location_locality", address.getLocality());
      values.put("location_country", address.getCountryName());
      values.put("location_latitude", address.getLatitude());
      values.put("location_longitude", address.getLongitude());
      values.put("location_timestamp", String.valueOf(new Date()).replace("GMT+05:30", ""));
      values.put("front_image", getBitmapAsByteArray(frontImage));
      values.put("back_image", getBitmapAsByteArray(backImage));
      database.insert("geo_fencing", null, values);
      Log.d("record Inserted:", "success");
    } catch (Exception e) {
      e.printStackTrace();
      Log.d("record Inserted:", "failed");
    }
  }

  private void deleteIfMoreRecords() {
    Cursor dbCursor = database.rawQuery("select count(*) from " + TABLE_NAME, null);
    if (dbCursor.moveToNext() && !dbCursor.isAfterLast()) {
      int count = dbCursor.getInt(0);
      if (count >= 5) {
        deleteFirstRecord();
      }
    }
  }

  public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 50, outputStream);

    Log.d("image size:", "" + outputStream.size());
    return outputStream.toByteArray();
  }


  public LinkedList<GeoFencingDTO> getRecords() {
    try {
      LinkedList<GeoFencingDTO> locations = new LinkedList<>();
      Cursor dbCursor = database.rawQuery("select * from " + TABLE_NAME, null);
      while (dbCursor.moveToNext() && !dbCursor.isAfterLast()) {
        GeoFencingDTO dto = new GeoFencingDTO();
        dto.populateFields(dbCursor);
        locations.add(dto);
      }
      dbCursor.close();
      return locations;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }


  public GeoFencingDTO getRecord(String timeStamp) {
    GeoFencingDTO dto = new GeoFencingDTO();
    Cursor dbCursor = database.rawQuery("select * from " + TABLE_NAME + " where location_timestamp='" + timeStamp + "'", null);
    if (dbCursor.moveToNext() && !dbCursor.isAfterLast()) {
      try {
        dto.populateFields(dbCursor);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return dto;

  }

  public void deleteFirstRecord() {
    database.execSQL("delete from " + TABLE_NAME + " where rowid=(select min(rowid) from " + TABLE_NAME + ")");
  }

}




Java Source Code List

com.ehc.GeoFencingDemo.CameraPreview.java
com.ehc.GeoFencingDemo.FinalActivity.java
com.ehc.GeoFencingDemo.FirstStepActivity.java
com.ehc.GeoFencingDemo.GeoFencingActivity.java
com.ehc.GeoFencingDemo.GeoFencingDTO.java
com.ehc.GeoFencingDemo.HomeActivity.java
com.ehc.GeoFencingDemo.LocationActivity.java
com.ehc.GeoFencingDemo.LocationDetailsActivity.java
com.ehc.GeoFencingDemo.LoginActivity.java
com.ehc.GeoFencingDemo.SecondStepActivity.java
com.ehc.GeoFencingDemo.SqlLiteDbHelper.java
com.ehc.GeoFencingDemo.ThirdStepActivity.java