Android Open Source - rfcx-guardian-android Alert Db






From Project

Back to project page rfcx-guardian-android.

License

The source code is released under:

Apache License

If you think the Android project rfcx-guardian-android 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 org.rfcx.guardian.database;
/*from  w  w w.j a v  a2s.c o m*/
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.rfcx.guardian.utility.DateTimeUtils;

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

public class AlertDb {

  private static final String NULL_EXC = "Exception thrown, but exception itself is null.";
  
  public AlertDb(Context context) {
    this.dbAlert = new DbAlert(context);
  }
  
  private static final String TAG = AlertDb.class.getSimpleName();
  public DateTimeUtils dateTimeUtils = new DateTimeUtils();
  static final int VERSION = 1;
  static final String DATABASE = "alert";
  static final String C_CREATED_AT = "created_at";
  static final String C_VALUE = "value";
  static final String C_MESSAGE = "message";
  private static final String[] ALL_COLUMNS = new String[] { C_CREATED_AT, C_VALUE, C_MESSAGE };
  
  
  private String createColumnString(String tableName) {
    StringBuilder sbOut = new StringBuilder();
    sbOut.append("CREATE TABLE ").append(tableName).append("(").append(C_CREATED_AT).append(" DATETIME");
    sbOut.append(", "+C_VALUE+" TEXT");
    sbOut.append(", "+C_MESSAGE+" TEXT");
    return sbOut.append(")").toString();
  }
  
  public class DbAlert {
    private String TABLE = "alert";
    class DbHelper extends SQLiteOpenHelper {
      public DbHelper(Context context) {
        super(context, DATABASE+"-"+TABLE+".db", null, VERSION);
      }
      @Override
      public void onCreate(SQLiteDatabase db) {
        try {
          db.execSQL(createColumnString(TABLE));
        } catch (SQLException e) { Log.e(TAG,(e!=null) ? e.getMessage() : NULL_EXC); }
      }
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try { db.execSQL("DROP TABLE IF EXISTS " + TABLE); onCreate(db);
        } catch (SQLException e) { Log.e(TAG,(e!=null) ? e.getMessage() : NULL_EXC); }
      }
    }
    final DbHelper dbHelper;
    public DbAlert(Context context) {
      this.dbHelper = new DbHelper(context);
    }
    public void close() {
      this.dbHelper.close();
    }
    public void insert(String value, String message) {
      ContentValues values = new ContentValues();
      values.put(C_CREATED_AT, (new DateTimeUtils()).getDateTime());
      values.put(C_VALUE, value);
      values.put(C_MESSAGE, message);
      SQLiteDatabase db = this.dbHelper.getWritableDatabase();
      try {
        db.insertWithOnConflict(TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
      } finally {
        db.close();
      }
    }
    public List<String[]> getAllAlerts() {
      SQLiteDatabase db = this.dbHelper.getWritableDatabase();
      ArrayList<String[]> list = new ArrayList<String[]>();
      try { Cursor cursor = db.query(TABLE, ALL_COLUMNS, null, null, null, null, null, null);
        if (cursor.getCount() > 0) {
          try { if (cursor.moveToFirst()) { do { list.add(new String[] { cursor.getString(0), cursor.getString(1), cursor.getString(2) });
          } while (cursor.moveToNext()); } } finally { cursor.close(); } }
      } catch (Exception e) { Log.e(TAG,(e!=null) ? e.getMessage() : NULL_EXC); } finally { db.close(); }
      return list;
    }
    public void clearAlertsBefore(Date date) {
      SQLiteDatabase db = this.dbHelper.getWritableDatabase();
      try { db.execSQL("DELETE FROM "+TABLE+" WHERE "+C_CREATED_AT+"<='"+(new DateTimeUtils()).getDateTime(date)+"'");
      } finally { db.close(); }
    }
    
    public String getSerializedAlerts() {
      List<String[]> alertList = getAllAlerts();
      String[] alertArray = new String[alertList.size()];
      for (int i = 0; i < alertList.size(); i++) {
        alertArray[i] = TextUtils.join("|", alertList.get(i));
      }
      return (alertList.size() > 0) ? TextUtils.join("$", alertArray) : "";
    }
  }
  public final DbAlert dbAlert;
}




Java Source Code List

net.sourceforge.javaFlacEncoder.ArrayRecycler.java
net.sourceforge.javaFlacEncoder.BlockEncodeRequest.java
net.sourceforge.javaFlacEncoder.BlockThreadManager.java
net.sourceforge.javaFlacEncoder.CRC16.java
net.sourceforge.javaFlacEncoder.CRC8.java
net.sourceforge.javaFlacEncoder.ChannelData.java
net.sourceforge.javaFlacEncoder.EncodedElement_32.java
net.sourceforge.javaFlacEncoder.EncodedElement.java
net.sourceforge.javaFlacEncoder.EncodingConfiguration.java
net.sourceforge.javaFlacEncoder.FLACEncoder.java
net.sourceforge.javaFlacEncoder.FLACFileOutputStream.java
net.sourceforge.javaFlacEncoder.FLACOutputStream.java
net.sourceforge.javaFlacEncoder.FLACStreamController.java
net.sourceforge.javaFlacEncoder.FLACStreamIdentifier.java
net.sourceforge.javaFlacEncoder.FLACStreamOutputStream.java
net.sourceforge.javaFlacEncoder.FLAC_ConsoleFileEncoder.java
net.sourceforge.javaFlacEncoder.FLAC_FileEncoder.java
net.sourceforge.javaFlacEncoder.FLAC_MD5.java
net.sourceforge.javaFlacEncoder.FrameHeader.java
net.sourceforge.javaFlacEncoder.FrameThread.java
net.sourceforge.javaFlacEncoder.Frame.java
net.sourceforge.javaFlacEncoder.LPC.java
net.sourceforge.javaFlacEncoder.MetadataBlockHeader.java
net.sourceforge.javaFlacEncoder.MetadataBlockStreamInfo.java
net.sourceforge.javaFlacEncoder.RiceEncoder.java
net.sourceforge.javaFlacEncoder.StreamConfiguration.java
net.sourceforge.javaFlacEncoder.Subframe_Constant.java
net.sourceforge.javaFlacEncoder.Subframe_Fixed.java
net.sourceforge.javaFlacEncoder.Subframe_LPC.java
net.sourceforge.javaFlacEncoder.Subframe_Verbatim.java
net.sourceforge.javaFlacEncoder.Subframe.java
net.sourceforge.javaFlacEncoder.UTF8Modified.java
org.rfcx.guardian.RfcxGuardianPrefs.java
org.rfcx.guardian.RfcxGuardian.java
org.rfcx.guardian.activity.MainActivity.java
org.rfcx.guardian.activity.PrefsActivity.java
org.rfcx.guardian.api.ApiCore.java
org.rfcx.guardian.audio.AudioCore.java
org.rfcx.guardian.database.AlertDb.java
org.rfcx.guardian.database.AudioDb.java
org.rfcx.guardian.database.DeviceStateDb.java
org.rfcx.guardian.database.SmsDb.java
org.rfcx.guardian.device.AirplaneMode.java
org.rfcx.guardian.device.CpuUsage.java
org.rfcx.guardian.device.DeviceState.java
org.rfcx.guardian.intentservice.ApiCheckInTriggerIntentService.java
org.rfcx.guardian.intentservice.AudioEncodeIntentService.java
org.rfcx.guardian.intentservice.ServiceMonitorIntentService.java
org.rfcx.guardian.receiver.AirplaneModeReceiver.java
org.rfcx.guardian.receiver.BootReceiver.java
org.rfcx.guardian.receiver.ConnectivityReceiver.java
org.rfcx.guardian.receiver.SmsReceiver.java
org.rfcx.guardian.service.ApiCheckInService.java
org.rfcx.guardian.service.AudioCaptureService.java
org.rfcx.guardian.service.CarrierCodeService.java
org.rfcx.guardian.service.DeviceStateService.java
org.rfcx.guardian.telecom.CarrierInteraction.java
org.rfcx.guardian.utility.DateTimeUtils.java
org.rfcx.guardian.utility.DeviceGuid.java
org.rfcx.guardian.utility.ExtAudioRecorderModified.java
org.rfcx.guardian.utility.FileUtils.java
org.rfcx.guardian.utility.HttpGet.java
org.rfcx.guardian.utility.HttpPostMultipart.java
org.rfcx.guardian.utility.TimeOfDay.java