safedispatch-mobile/libSafeMobile/src/main/java/com/safemobile/database/DatabaseCommunication.java

640 lines
20 KiB
Java

package com.safemobile.database;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import com.safemobile.lib.Contact;
import com.safemobile.lib.Recording;
import com.safemobile.lib.SM;
import com.safemobile.lib.SMS;
import com.safemobile.lib.Vehicle;
import com.safemobile.lib.radio.Channel;
import com.safemobile.lib.radio.Zone;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.util.Log;
public class DatabaseCommunication {
/* Database */
private SQLiteDatabase database;
private DataBaseHelper myDbHelper;
private boolean first = false, READONLY = false;;
private Context context;
public DatabaseCommunication(Context context)
{
this.context = context;
}
// open database Connection
public void openConnection()
{
// start database connection
myDbHelper = new DataBaseHelper(context);
try
{
myDbHelper.createDataBase();
Log.d("CONNECT", "Succes connection");
}
catch (IOException ioe)
{
throw new Error("Unable to create database");
}
// end database connection
myDbHelper = new DataBaseHelper(context);
try {
if(database!=null)
database.close();
database = myDbHelper.openDataBase(READONLY);
Log.d("CONNECT", "Opened Connection");
}catch(SQLException sqle){
first = true;
//throw sqle;
}
if(first)
{
try {
if(database!=null)
database.close();
database = myDbHelper.openDataBase(READONLY);
Log.d("CONNECT", "Opened Connection");
}catch(SQLException sqle){
first = false;
//throw sqle;
}
}
}
// close database Connection
public void closeConnection()
{
database.close();
}
public boolean isOpen()
{
if(database!=null)
return database.isOpen();
else
return false;
}
// get all vehicles
public ArrayList<Vehicle> getAllVehicle()
{
ArrayList<Vehicle> allVehicles = new ArrayList<Vehicle>();
// build iterator
Cursor cursor = null;
if(database == null)
Log.d("NULL", "Database is null");
else
{
cursor = database.rawQuery("SELECT * from vehicle",new String [] {});
Log.d("CURSOR", "cursor created");
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
//old = new Vehicle(int lp, String name, long driver_id, int time_route, int GPS_reporting_interval, int is_stolen)
// new = Vehicle(int sc_id, String imei, int lp, String name, long driver_id, int time_route, int GPS_reporting_interval, int is_stolen)
allVehicles.add(new Vehicle(0, "imei", cursor.getInt(1), cursor.getString(2), cursor.getInt(3), cursor.getInt(4), cursor.getInt(5), cursor.getInt(6)));
// move to next record
cursor.moveToNext();
}
cursor.close();
}
allVehicles.add(new Vehicle(0, "imei", 1, "Demo103", 89, 0, 0, 0));
allVehicles.add(new Vehicle(0, "imei", 1, "Demo104", 101, 0, 0, 0));
allVehicles.add(new Vehicle(0, "imei", 1, "Demo105", 95, 0, 0, 0));
allVehicles.add(new Vehicle(0, "imei", 1, "Demo106", 118, 0, 0, 0));
allVehicles.add(new Vehicle(0, "imei", 1, "Demo107", 111, 0, 0, 0));
return allVehicles;
}
// get all users
public ArrayList<SMS> getAllSMS()
{
ArrayList<SMS> allSMS = new ArrayList<SMS>();
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
cursor = database.rawQuery("SELECT _id, timeGMT, imei_sour, imei_dest, mess, status from SMS",new String [] {});
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
SMS sms = new SMS();//cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5), cursor.getInt(6));
sms.idx =cursor.getInt(0);
sms.timeGMT = cursor.getInt(1);
sms.sc_id_sour = cursor.getInt(2);
sms.sc_id_dest = cursor.getInt(3);
sms.mess = cursor.getString(4);
sms.status = cursor.getInt(5);
sms.seq_idx = "";
allSMS.add(sms);
// move to next record
cursor.moveToNext();
}
cursor.close();
}
return allSMS;
}
public ArrayList<SMS> getSMS_4Imei(long imei)
{
ArrayList<SMS> allSMS = new ArrayList<SMS>();
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
cursor = database.rawQuery("SELECT _id, timeGMT, imei_sour, imei_dest, mess, status from SMS where imei_sour ="+imei+" OR imei_dest ="+imei+" ORDER BY timeGMT ASC",new String [] {});
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
SMS sms = new SMS();
sms.idx =cursor.getInt(0);
sms.timeGMT = cursor.getInt(1);
sms.sc_id_sour = cursor.getInt(2);
sms.sc_id_dest = cursor.getInt(3);
sms.mess = cursor.getString(4);
sms.status = cursor.getInt(5);
sms.seq_idx = "";
allSMS.add(sms);
// move to next record
cursor.moveToNext();
}
cursor.close();
}
return allSMS;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public ArrayList<SMS> getAllLastSMS()
{
ArrayList<SMS> allLastSMS = new ArrayList<SMS>();
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
ArrayList<Integer> imeis= new ArrayList<Integer>();
cursor = database.rawQuery("SELECT B.imei FROM (SELECT DISTINCT imei_sour AS imei FROM SMS WHERE imei_sour>0 UNION SELECT DISTINCT imei_dest AS imei FROM SMS WHERE imei_dest>0) AS B",new String [] {});
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
/*
Integer imei_sour =cursor.getInt(0);
Integer imei_dest = cursor.getInt(1);
if(imei_sour != 0)
if(!imeis.contains(imei_sour))
{
imeis.add(imei_sour);
}
if(imei_dest != 0)
if(!imeis.contains(imei_dest))
{
imeis.add(imei_dest);
}
*/
imeis.add(cursor.getInt(0));
// move to next record
cursor.moveToNext();
}
cursor.close();
for(Integer imei:imeis)
{
ArrayList<SMS> smss = getSMS_4Imei(imei);
if(smss.size() > 0)
allLastSMS.add(smss.get(smss.size() - 1));
}
Collections.sort(allLastSMS, new Comparator(){
public int compare(Object o1, Object o2) {
SMS sms1 = (SMS) o1;
SMS sms2 = (SMS) o2;
if(sms1.timeGMT < sms2.timeGMT)
return -1;
else if (sms1.timeGMT > sms2.timeGMT)
return 1;
else
return 0;
// return sms1.timeGMT.compareToIgnoreCase(p2.getFirstName());
}
});
}
return allLastSMS;
}
public boolean insertSMS(SMS sms)
{
long id = -1;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
ContentValues values = new ContentValues();
values.put("timeGMT", sms.timeGMT);
values.put("imei_sour", sms.sc_id_sour);
values.put("imei_dest", sms.sc_id_dest);
values.put("mess", sms.mess);
values.put("status", sms.status);
id = database.insert("SMS", "", values); //.rawQuery("SELECT imei_sour, imei_dest from SMS",new String [] {});
SM.Debug("DBQuery","Database Insert result: " + id);
}
//INSERT into SMS (timeGMT, imei_sour, imei_dest, mess, status) VALUES( 1324016412, 0, 101, 'two', 1)
return (id == -1)? false: true;
}
// get all users
public ArrayList<Recording> getAllRecordings()
{
ArrayList<Recording> allRecordings = new ArrayList<Recording>();
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
cursor = database.rawQuery("SELECT _id, id_sour, id_dest, date, duration, filename, type from Recordings order by _id desc",new String [] {});
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
Recording rec = new Recording();
rec.id =cursor.getInt(0);
rec.sourceRadioID = cursor.getInt(1);
rec.destinationRadioID = cursor.getInt(2);
rec.date = cursor.getInt(3);
rec.duration = cursor.getInt(4);
rec.filename = cursor.getString(5);
rec.type = cursor.getInt(6);
allRecordings.add(rec);
// move to next record
cursor.moveToNext();
}
cursor.close();
}
return allRecordings;
}
// get all users
public Recording getLastRecording()
{
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
cursor = database.rawQuery("SELECT _id, id_sour, id_dest, date, duration, filename, type from Recordings order by _id desc",new String [] {});
cursor.moveToFirst();
if(cursor.getCount() > 0)
{
Recording rec = new Recording();
rec.id =cursor.getInt(0);
rec.sourceRadioID = cursor.getInt(1);
rec.destinationRadioID = cursor.getInt(2);
rec.date = cursor.getInt(3);
rec.duration = cursor.getInt(4);
rec.filename = cursor.getString(5);
rec.type = cursor.getInt(6);
return rec;
}
}
return null;
}
public long insertRecording(Recording rec)
{
long id = -1;
if(database == null)
SM.Debug("Database is null");
else
{
ContentValues values = new ContentValues();
values.put("id_sour", rec.sourceRadioID);
values.put("id_dest", rec.destinationRadioID);
values.put("date", rec.date);
values.put("duration", rec.duration);
values.put("filename", rec.filename);
values.put("type", rec.type);
id = database.insert("Recordings", "", values); //.rawQuery("SELECT imei_sour, imei_dest from SMS",new String [] {});
SM.Debug("DBQuery","Database Insert result: " + id);
}
//INSERT into SMS (timeGMT, imei_sour, imei_dest, mess, status) VALUES( 1324016412, 0, 101, 'two', 1)
return id;
}
public boolean removeRecording(Recording rec)
{
long id = -1;
if(database == null)
SM.Debug("Database is null");
else
{
ContentValues values = new ContentValues();
values.put("id_sour", rec.sourceRadioID);
values.put("id_dest", rec.destinationRadioID);
values.put("date", rec.date);
values.put("duration", rec.duration);
values.put("filename", rec.filename);
values.put("type", rec.type);
id = database.delete("Recordings", "_id=" + rec.id, null);
SM.Debug("DBQuery","Database Remove result: " + id);
}
//INSERT into SMS (timeGMT, imei_sour, imei_dest, mess, status) VALUES( 1324016412, 0, 101, 'two', 1)
return (id == -1)? false: true;
}
// get all Contacts
public ArrayList<Contact> getAllContacts(boolean TRBO)
{
ArrayList<Contact> allContacts = new ArrayList<Contact>();
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
try
{
cursor = database.rawQuery("SELECT _id, call_id, name, call_type from contacts order by call_id asc",new String [] {});
if(cursor!=null)
{
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
Contact contact = new Contact();
contact.id = cursor.getInt(1);
contact.name = cursor.getString(2);
String type = cursor.getString(3);
if(type.equals("Private Call"))
contact.contactType = Contact.PRIVATE;
else if (type.equals("Group Call"))
contact.contactType = Contact.GROUP;
allContacts.add(contact);
// move to next record
cursor.moveToNext();
}
cursor.close();
}
}
catch(Exception ex)
{
// table doesn't exist so I have to create and populate it
createContactsTable();
populateContactsTable(TRBO);
}
}
return allContacts;
}
// get all Contacts
public ArrayList<Zone> getAllZones(boolean TRBO)
{
ArrayList<Zone> allZones = new ArrayList<Zone>();
Cursor cursor = null;
if(database == null)
SM.Debug("DBQuery","Database is null");
else
{
try
{
cursor = database.rawQuery("SELECT _id, position, name, type, zone_id from zonechannel order by zone_id asc, type desc",new String [] {});
if(cursor!=null)
{
cursor.moveToFirst();
ArrayList<Channel> listChannels = new ArrayList<Channel>();
while (cursor.isAfterLast() == false)
{
// add zone to list
if(cursor.getString(3).equals("zone"))
{
Zone z = new Zone();
z.channelList = new ArrayList<Channel>();
z.dbID = cursor.getInt(1);
z.ZoneName = cursor.getString(2);
z.id = cursor.getInt(4);
// add channels to zone
z.channelList = listChannels;
// clear channels list
listChannels = new ArrayList<Channel>();
// add zone
allZones.add(z);
}
else
{
// add channel to corresponding list
Channel c = new Channel();
c.id = cursor.getInt(1);
c.chName = cursor.getString(2);
c.id = cursor.getInt(1);
// add channel to zone if exists or add it to the list
for(Zone zone: allZones)
{
// if channel is in this zone
if(zone.id == cursor.getInt(4))
{
// create list if not exists
if(zone.channelList == null)
zone.channelList = new ArrayList<Channel>();
// add channel to list;
zone.channelList.add(c);
}
}
}
// move to next record
cursor.moveToNext();
}
cursor.close();
}
}
catch(Exception ex)
{
// table doesn't exist so I have to create and populate it
createZoneChannelTable();
populateZoneChannelTable(TRBO);
}
}
return allZones;
}
public void writeConfigFileZonesAndChannels(ArrayList<Zone> zones) {
// delete all values from zonechannel table
database.execSQL("DELETE FROM zonechannel where 1=1");
for(Zone zone:zones) {
// insert zone into database
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (" + zone.id + ",'" + zone.ZoneName + "'," +
"'zone'" + "," + zone.id + ")");
// add channels for current zone
for(Channel ch: zone.channelList) {
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (" + ch.id + ",'" + ch.chName + "'," +
"'channel'" + "," + ch.id + ")");
}
}
}
public void writeConfigFileContacts(ArrayList<Contact> contacts) {
// delete all values from contacts table
database.execSQL("DELETE FROM contacts where 1=1");
for(Contact contact: contacts) {
// insert contact into database
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (" + contact.id + ",'" + contact.name +"','" +
contact.contactType + "')");
}
}
public void createZoneChannelTable()
{
// create table
String sqlDataStore = "create table if not exists " +
"zonechannel" + " ("+ BaseColumns._ID + " integer primary key autoincrement,"
+ "position" + " INTEGER ,"
+ "name" + " text not null,"
+ "type" + " text not null,"
+ "zone_id" + " INTEGER not null)";
database.execSQL(sqlDataStore);
}
public void populateZoneChannelTable(boolean TRBO)
{
// populate table
if(TRBO)
{
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (1,'Zone1','zone',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (1,'Channel1','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (2,'Channel2','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (3,'Channel3','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (4,'Channel4','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (5,'Channel5','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (6,'Channel6','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (3,'Zone2','zone',3)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (1,'Ch5[Z2]','channel',3)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (2,'Ch6[Z2]','channel',3)");
}
else
{
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (1,'Zone1','zone',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (456,'DMO TG1','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (457,'DMO TG2','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (458,'DMO TG3','channel',1)");
database.execSQL("INSERT INTO zonechannel (position, name, type, zone_id) VALUES (459,'DMO TG4','channel',1)");
}
}
public void createContactsTable()
{
// create table
String sqlDataStore = "create table if not exists " +
"contacts" + " ("+ BaseColumns._ID + " integer primary key autoincrement,"
+ "call_id" + " INTEGER ,"
+ "name" + " text not null,"
+ "call_type" + " text not null)";
database.execSQL(sqlDataStore);
}
public void populateContactsTable(boolean TRBO)
{
if(TRBO)
{
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (101,'UNIT101','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (102,'UNIT102','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (103,'UNIT103','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (104,'BASE104','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (114,'BASE114','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (124,'BASE124','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (1,'Call1','Group Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (2,'Call2','Group Call')");
}
else
{
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (101,'UNIT101','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (102,'UNIT102','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (103,'UNIT103','Private Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (456,'DMO TG1','Group Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (457,'DMO TG2','Group Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (458,'DMO TG3','Group Call')");
database.execSQL("INSERT INTO contacts (call_id, name, call_type) VALUES (459,'DMO TG4','Group Call')");
}
}
public long insertContact(Contact contact)
{
long id = -1;
if(database == null)
SM.Debug("Database is null");
else
{
try
{
ContentValues values = new ContentValues();
values.put("call_id", contact.id);
values.put("name", contact.name);
values.put("call_type", contact.contactType);
id = database.insert("contacts", "", values); //.rawQuery("SELECT imei_sour, imei_dest from SMS",new String [] {});
SM.Debug("DBQuery","Database Insert result: " + id);
}
catch(Exception ex)
{
SM.Exception("DB[Insert Contact]","Could not insert into DB");
}
}
//INSERT into SMS (timeGMT, imei_sour, imei_dest, mess, status) VALUES( 1324016412, 0, 101, 'two', 1)
return id;
}
}