mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-04-08 18:55:09 +02:00
2013-04-25 Mark Holland <mark@mark-holland.me.uk>
* src/pandroid/agent/Core.java: Changed get and update database values methods to support new DataBaseHandler. * src/pandroid/agent/DataBaseHandler.java: Complete rewrite. * src/pandroid/agent/PandroidAgent.java: Removed initiate database, database now supplied with build. * src/pandroid/agent/PandroidAgentListener.java: changed SimIDReport to simIDReport to reflect database. * assets/database/pandroid.zip: zipped sqlite3 database containing all default values. * libs/android-sqlite-asset-helper.jar: library to facilitate database operations. Only temporarily being used. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@8052 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
parent
4475671c59
commit
edff3417ed
pandora_agents/android
@ -1,3 +1,22 @@
|
||||
2013-04-25 Mark Holland <mark@mark-holland.me.uk>
|
||||
|
||||
* src/pandroid/agent/Core.java: Changed get and update
|
||||
database values methods to support new DataBaseHandler.
|
||||
|
||||
* src/pandroid/agent/DataBaseHandler.java: Complete rewrite.
|
||||
|
||||
* src/pandroid/agent/PandroidAgent.java: Removed initiate
|
||||
database, database now supplied with build.
|
||||
|
||||
* src/pandroid/agent/PandroidAgentListener.java: changed
|
||||
SimIDReport to simIDReport to reflect database.
|
||||
|
||||
* assets/database/pandroid.zip: zipped sqlite3
|
||||
database containing all default values.
|
||||
|
||||
* libs/android-sqlite-asset-helper.jar: library to facilitate
|
||||
database operations. Only temporarily being used.
|
||||
|
||||
2013-04-23 Mark Holland <mark@mark-holland.me.uk>
|
||||
|
||||
* src/pandroid/agent/Core.java: Migrated from shared
|
||||
|
BIN
pandora_agents/android/assets/databases/pandroid.zip
Normal file
BIN
pandora_agents/android/assets/databases/pandroid.zip
Normal file
Binary file not shown.
BIN
pandora_agents/android/libs/android-sqlite-asset-helper.jar
Normal file
BIN
pandora_agents/android/libs/android-sqlite-asset-helper.jar
Normal file
Binary file not shown.
@ -14,14 +14,10 @@
|
||||
|
||||
package pandroid.agent;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class Core {
|
||||
|
||||
@ -371,7 +367,7 @@ public class Core {
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
//Initialize database
|
||||
public static void initDatabase(Context context){
|
||||
if (con == null) {
|
||||
@ -451,10 +447,10 @@ public class Core {
|
||||
db.addValue(dh);
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
//Updates a given row "name" with a "value"
|
||||
public static void updateValue(Context context, String name, String value){
|
||||
db = new DataBaseHandler(con);
|
||||
public static synchronized void updateValue(Context context, String name, String value){
|
||||
db = new DataBaseHandler(con, "pandroid", null, 1);
|
||||
//Retrieve id of row to update
|
||||
int id = getDataHandler(con, name).get_id();
|
||||
|
||||
@ -464,15 +460,15 @@ public class Core {
|
||||
}
|
||||
|
||||
//Returns the DataHandler object of the given row "name"
|
||||
public static DataHandler getDataHandler(Context context, String name){
|
||||
db = new DataBaseHandler(con);
|
||||
public static synchronized DataHandler getDataHandler(Context context, String name){
|
||||
db = new DataBaseHandler(con, "pandroid", null, 1);
|
||||
|
||||
return db.getValue(name);
|
||||
}
|
||||
|
||||
//Returns the value of the given row "name"
|
||||
public static String getValue(Context context, String name){
|
||||
db = new DataBaseHandler(con);
|
||||
public static synchronized String getValue(Context context, String name){
|
||||
db = new DataBaseHandler(con, "pandroid", null, 1);
|
||||
|
||||
return db.getValue(name).get_value();
|
||||
|
||||
|
@ -1,165 +1,479 @@
|
||||
package pandroid.agent;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteDatabase.CursorFactory;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.readystatesoftware.sqliteasset.SQLiteAssetException;
|
||||
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
|
||||
|
||||
public class DataBaseHandler extends SQLiteOpenHelper {
|
||||
|
||||
// All Static variables
|
||||
// Database Version
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
|
||||
// Database Name
|
||||
private static final String DATABASE_NAME = "PANDROID_DATA";
|
||||
|
||||
// Contacts table name
|
||||
private static final String TABLE_DATA = "data"; //"values" is a reserved sqllite word
|
||||
private static final String TAG = SQLiteAssetHelper.class.getSimpleName();
|
||||
private static final String ASSET_DB_PATH = "databases";
|
||||
|
||||
// Contacts table name
|
||||
private static final String TABLE_NAME = "PANDROID_DATA";
|
||||
|
||||
// Contacts Table Columns names
|
||||
private static final String KEY_ID = "id";
|
||||
private static final String KEY_NAME = "name";
|
||||
private static final String KEY_VALUE = "value";
|
||||
|
||||
public DataBaseHandler(Context context) {
|
||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
// Creating Tables
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_DATA + "("
|
||||
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
|
||||
+ KEY_VALUE + " TEXT" + ")";
|
||||
db.execSQL(CREATE_CONTACTS_TABLE);
|
||||
}
|
||||
|
||||
// Upgrading database
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// Drop older table if existed
|
||||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATA);
|
||||
|
||||
// Create tables again
|
||||
onCreate(db);
|
||||
}
|
||||
|
||||
/**
|
||||
* All CRUD(Create, Read, Update, Delete) Operations
|
||||
private static final String KEY_ID = "_ID";
|
||||
private static final String KEY_NAME = "NAME";
|
||||
private static final String KEY_VALUE = "VALUE";
|
||||
|
||||
|
||||
private final Context mContext;
|
||||
private final String mName;
|
||||
private final CursorFactory mFactory;
|
||||
private final int mNewVersion;
|
||||
|
||||
private SQLiteDatabase mDatabase = null;
|
||||
private boolean mIsInitializing = false;
|
||||
|
||||
private String mDatabasePath;
|
||||
private String mArchivePath;
|
||||
private String mUpgradePathFormat;
|
||||
|
||||
private int mForcedUpgradeVersion = 0;
|
||||
|
||||
/**
|
||||
* Create a helper object to create, open, and/or manage a database in
|
||||
* a specified location.
|
||||
* This method always returns very quickly. The database is not actually
|
||||
* created or opened until one of {@link #getWritableDatabase} or
|
||||
* {@link #getReadableDatabase} is called.
|
||||
*
|
||||
* @param context to use to open or create the database
|
||||
* @param name of the database file
|
||||
* @param storageDirectory to store the database file upon creation; caller must
|
||||
* ensure that the specified absolute path is available and can be written to
|
||||
* @param factory to use for creating cursor objects, or null for the default
|
||||
* @param version number of the database (starting at 1); if the database is older,
|
||||
* SQL file(s) contained within the application assets folder will be used to
|
||||
* upgrade the database
|
||||
*/
|
||||
public DataBaseHandler(Context context, String name, String storageDirectory, CursorFactory factory, int version) {
|
||||
super(context, name, factory, version);
|
||||
|
||||
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
|
||||
if (name == null) throw new IllegalArgumentException("Databse name cannot be null");
|
||||
|
||||
mContext = context;
|
||||
mName = name;
|
||||
mFactory = factory;
|
||||
mNewVersion = version;
|
||||
|
||||
mArchivePath = ASSET_DB_PATH + "/" + name + ".zip";
|
||||
if (storageDirectory != null) {
|
||||
mDatabasePath = storageDirectory;
|
||||
} else {
|
||||
mDatabasePath = context.getApplicationInfo().dataDir + "/databases";
|
||||
}
|
||||
mUpgradePathFormat = ASSET_DB_PATH + "/" + name + "_upgrade_%s-%s.sql";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a helper object to create, open, and/or manage a database in
|
||||
* the application's default private data directory.
|
||||
* This method always returns very quickly. The database is not actually
|
||||
* created or opened until one of {@link #getWritableDatabase} or
|
||||
* {@link #getReadableDatabase} is called.
|
||||
*
|
||||
* @param context to use to open or create the database
|
||||
* @param name of the database file
|
||||
* @param factory to use for creating cursor objects, or null for the default
|
||||
* @param version number of the database (starting at 1); if the database is older,
|
||||
* SQL file(s) contained within the application assets folder will be used to
|
||||
* upgrade the database
|
||||
*/
|
||||
public DataBaseHandler(Context context, String name, CursorFactory factory, int version) {
|
||||
this(context, name, null, factory, version);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create and/or open a database that will be used for reading and writing.
|
||||
* The first time this is called, the database will be extracted and copied
|
||||
* from the application's assets folder.
|
||||
*
|
||||
* <p>Once opened successfully, the database is cached, so you can
|
||||
* call this method every time you need to write to the database.
|
||||
* (Make sure to call {@link #close} when you no longer need the database.)
|
||||
* Errors such as bad permissions or a full disk may cause this method
|
||||
* to fail, but future attempts may succeed if the problem is fixed.</p>
|
||||
*
|
||||
* <p class="caution">Database upgrade may take a long time, you
|
||||
* should not call this method from the application main thread, including
|
||||
* from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
|
||||
*
|
||||
* @throws SQLiteException if the database cannot be opened for writing
|
||||
* @return a read/write database object valid until {@link #close} is called
|
||||
*/
|
||||
@Override
|
||||
public synchronized SQLiteDatabase getWritableDatabase() {
|
||||
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
|
||||
return mDatabase; // The database is already open for business
|
||||
}
|
||||
|
||||
if (mIsInitializing) {
|
||||
throw new IllegalStateException("getWritableDatabase called recursively");
|
||||
}
|
||||
|
||||
// If we have a read-only database open, someone could be using it
|
||||
// (though they shouldn't), which would cause a lock to be held on
|
||||
// the file, and our attempts to open the database read-write would
|
||||
// fail waiting for the file lock. To prevent that, we acquire the
|
||||
// lock on the read-only database, which shuts out other users.
|
||||
|
||||
boolean success = false;
|
||||
SQLiteDatabase db = null;
|
||||
//if (mDatabase != null) mDatabase.lock();
|
||||
try {
|
||||
mIsInitializing = true;
|
||||
//if (mName == null) {
|
||||
// db = SQLiteDatabase.create(null);
|
||||
//} else {
|
||||
// db = mContext.openOrCreateDatabase(mName, 0, mFactory);
|
||||
//}
|
||||
db = createOrOpenDatabase(false);
|
||||
|
||||
int version = db.getVersion();
|
||||
|
||||
// do force upgrade
|
||||
if (version != 0 && version < mForcedUpgradeVersion) {
|
||||
db = createOrOpenDatabase(true);
|
||||
db.setVersion(mNewVersion);
|
||||
version = db.getVersion();
|
||||
}
|
||||
|
||||
if (version != mNewVersion) {
|
||||
db.beginTransaction();
|
||||
try {
|
||||
if (version == 0) {
|
||||
onCreate(db);
|
||||
} else {
|
||||
if (version > mNewVersion) {
|
||||
Log.w(TAG, "Can't downgrade read-only database from version " +
|
||||
version + " to " + mNewVersion + ": " + db.getPath());
|
||||
}
|
||||
onUpgrade(db, version, mNewVersion);
|
||||
}
|
||||
db.setVersion(mNewVersion);
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
onOpen(db);
|
||||
success = true;
|
||||
return db;
|
||||
} finally {
|
||||
mIsInitializing = false;
|
||||
if (success) {
|
||||
if (mDatabase != null) {
|
||||
try { mDatabase.close(); } catch (Exception e) { }
|
||||
//mDatabase.unlock();
|
||||
}
|
||||
mDatabase = db;
|
||||
} else {
|
||||
//if (mDatabase != null) mDatabase.unlock();
|
||||
if (db != null) db.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create and/or open a database. This will be the same object returned by
|
||||
* {@link #getWritableDatabase} unless some problem, such as a full disk,
|
||||
* requires the database to be opened read-only. In that case, a read-only
|
||||
* database object will be returned. If the problem is fixed, a future call
|
||||
* to {@link #getWritableDatabase} may succeed, in which case the read-only
|
||||
* database object will be closed and the read/write object will be returned
|
||||
* in the future.
|
||||
*
|
||||
* <p class="caution">Like {@link #getWritableDatabase}, this method may
|
||||
* take a long time to return, so you should not call it from the
|
||||
* application main thread, including from
|
||||
* {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
|
||||
*
|
||||
* @throws SQLiteException if the database cannot be opened
|
||||
* @return a database object valid until {@link #getWritableDatabase}
|
||||
* or {@link #close} is called.
|
||||
*/
|
||||
@Override
|
||||
public synchronized SQLiteDatabase getReadableDatabase() {
|
||||
if (mDatabase != null && mDatabase.isOpen()) {
|
||||
return mDatabase; // The database is already open for business
|
||||
}
|
||||
|
||||
if (mIsInitializing) {
|
||||
throw new IllegalStateException("getReadableDatabase called recursively");
|
||||
}
|
||||
|
||||
try {
|
||||
return getWritableDatabase();
|
||||
} catch (SQLiteException e) {
|
||||
if (mName == null) throw e; // Can't open a temp database read-only!
|
||||
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
|
||||
}
|
||||
|
||||
SQLiteDatabase db = null;
|
||||
try {
|
||||
mIsInitializing = true;
|
||||
String path = mContext.getDatabasePath(mName).getPath();
|
||||
db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
|
||||
if (db.getVersion() != mNewVersion) {
|
||||
throw new SQLiteException("Can't upgrade read-only database from version " +
|
||||
db.getVersion() + " to " + mNewVersion + ": " + path);
|
||||
}
|
||||
|
||||
onOpen(db);
|
||||
Log.w(TAG, "Opened " + mName + " in read-only mode");
|
||||
mDatabase = db;
|
||||
return mDatabase;
|
||||
} finally {
|
||||
mIsInitializing = false;
|
||||
if (db != null && db != mDatabase) db.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close any open database object.
|
||||
*/
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (mIsInitializing) throw new IllegalStateException("Closed during initialization");
|
||||
|
||||
if (mDatabase != null && mDatabase.isOpen()) {
|
||||
mDatabase.close();
|
||||
mDatabase = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onCreate(SQLiteDatabase db) {
|
||||
// do nothing - createOrOpenDatabase() is called in
|
||||
// getWritableDatabase() to handle database creation.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
|
||||
Log.w(TAG, "Upgrading database " + mName + " from version " + oldVersion + " to " + newVersion + "...");
|
||||
|
||||
ArrayList<String> paths = new ArrayList<String>();
|
||||
getUpgradeFilePaths(oldVersion, newVersion-1, newVersion, paths);
|
||||
|
||||
if (paths.isEmpty()) {
|
||||
Log.e(TAG, "no upgrade script path from " + oldVersion + " to " + newVersion);
|
||||
throw new SQLiteAssetException("no upgrade script path from " + oldVersion + " to " + newVersion);
|
||||
}
|
||||
|
||||
Collections.sort(paths);
|
||||
for (String path : paths) {
|
||||
try {
|
||||
Log.w(TAG, "processing upgrade: " + path);
|
||||
InputStream is = mContext.getAssets().open(path);
|
||||
String sql = convertStreamToString(is);
|
||||
if (sql != null) {
|
||||
String[] cmds = sql.split(";");
|
||||
for (String cmd : cmds) {
|
||||
//Log.d(TAG, "cmd=" + cmd);
|
||||
if (cmd.trim().length() > 0) {
|
||||
db.execSQL(cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Log.w(TAG, "Successfully upgraded database " + mName + " from version " + oldVersion + " to " + newVersion);
|
||||
|
||||
}
|
||||
|
||||
public void setForcedUpgradeVersion(int version) {
|
||||
mForcedUpgradeVersion = version;
|
||||
}
|
||||
|
||||
private SQLiteDatabase createOrOpenDatabase(boolean force) throws SQLiteAssetException {
|
||||
SQLiteDatabase db = returnDatabase();
|
||||
if (db != null) {
|
||||
// database already exists
|
||||
if (force) {
|
||||
Log.w(TAG, "forcing database upgrade!");
|
||||
copyDatabaseFromAssets();
|
||||
db = returnDatabase();
|
||||
}
|
||||
return db;
|
||||
} else {
|
||||
// database does not exist, copy it from assets and return it
|
||||
copyDatabaseFromAssets();
|
||||
db = returnDatabase();
|
||||
return db;
|
||||
}
|
||||
}
|
||||
|
||||
private SQLiteDatabase returnDatabase(){
|
||||
try {
|
||||
SQLiteDatabase db = SQLiteDatabase.openDatabase(mDatabasePath + "/" + mName, mFactory, SQLiteDatabase.OPEN_READWRITE);
|
||||
Log.i(TAG, "successfully opened database " + mName);
|
||||
return db;
|
||||
} catch (SQLiteException e) {
|
||||
Log.w(TAG, "could not open database " + mName + " - " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void copyDatabaseFromAssets() throws SQLiteAssetException {
|
||||
Log.w(TAG, "copying database from assets...");
|
||||
|
||||
try {
|
||||
InputStream zipFileStream = mContext.getAssets().open(mArchivePath);
|
||||
File f = new File(mDatabasePath + "/");
|
||||
if (!f.exists()) { f.mkdir(); }
|
||||
|
||||
ZipInputStream zis = getFileFromZip(zipFileStream);
|
||||
if (zis == null) {
|
||||
throw new SQLiteAssetException("Archive is missing a SQLite database file");
|
||||
}
|
||||
writeExtractedFileToDisk(zis, new FileOutputStream(mDatabasePath + "/" + mName));
|
||||
|
||||
Log.w(TAG, "database copy complete");
|
||||
|
||||
} catch (FileNotFoundException fe) {
|
||||
SQLiteAssetException se = new SQLiteAssetException("Missing " + mArchivePath + " file in assets or target folder not writable");
|
||||
se.setStackTrace(fe.getStackTrace());
|
||||
throw se;
|
||||
} catch (IOException e) {
|
||||
SQLiteAssetException se = new SQLiteAssetException("Unable to extract " + mArchivePath + " to data directory");
|
||||
se.setStackTrace(e.getStackTrace());
|
||||
throw se;
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream getUpgradeSQLStream(int oldVersion, int newVersion) {
|
||||
String path = String.format(mUpgradePathFormat, oldVersion, newVersion);
|
||||
try {
|
||||
InputStream is = mContext.getAssets().open(path);
|
||||
return is;
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "missing database upgrade script: " + path);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void getUpgradeFilePaths(int baseVersion, int start, int end, ArrayList<String> paths) {
|
||||
|
||||
int a;
|
||||
int b;
|
||||
|
||||
InputStream is = getUpgradeSQLStream(start, end);
|
||||
if (is != null) {
|
||||
String path = String.format(mUpgradePathFormat, start, end);
|
||||
paths.add(path);
|
||||
//Log.d(TAG, "found script: " + path);
|
||||
a = start - 1;
|
||||
b = start;
|
||||
is = null;
|
||||
} else {
|
||||
a = start - 1;
|
||||
b = end;
|
||||
}
|
||||
|
||||
if (a < baseVersion) {
|
||||
return;
|
||||
} else {
|
||||
getUpgradeFilePaths(baseVersion, a, b, paths); // recursive call
|
||||
}
|
||||
}
|
||||
|
||||
private void writeExtractedFileToDisk(ZipInputStream zin, OutputStream outs) throws IOException {
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = zin.read(buffer))>0){
|
||||
outs.write(buffer, 0, length);
|
||||
}
|
||||
outs.flush();
|
||||
outs.close();
|
||||
zin.close();
|
||||
}
|
||||
|
||||
private ZipInputStream getFileFromZip(InputStream zipFileStream) throws FileNotFoundException, IOException {
|
||||
ZipInputStream zis = new ZipInputStream(zipFileStream);
|
||||
ZipEntry ze = null;
|
||||
while ((ze = zis.getNextEntry()) != null) {
|
||||
Log.w(TAG, "extracting file: '" + ze.getName() + "'...");
|
||||
return zis;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String convertStreamToString(InputStream is) {
|
||||
return new Scanner(is).useDelimiter("\\A").next();
|
||||
}
|
||||
|
||||
// // Getting single contact
|
||||
// Cursor getContact(int id) {
|
||||
// mDatabase = this.getReadableDatabase();
|
||||
//
|
||||
// return mDatabase.query(TABLE_NAME, new String[] { KEY_ID,
|
||||
// KEY_NAME, KEY_VALUE }, KEY_ID + "=?",
|
||||
// new String[] { String.valueOf(id) }, null, null, null, null);
|
||||
//
|
||||
// }
|
||||
|
||||
// Update single value
|
||||
public int updateValue(DataHandler dh){
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_NAME, dh.get_name());
|
||||
values.put(KEY_VALUE, dh.get_value());
|
||||
|
||||
// updating row
|
||||
return db.update(TABLE_NAME, values, KEY_ID + " = ?",
|
||||
new String[] { String.valueOf(dh.get_id())});
|
||||
}
|
||||
|
||||
// Adding new contact
|
||||
void addValue(DataHandler dh) {
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_NAME, dh.get_name());
|
||||
values.put(KEY_VALUE, dh.get_value());
|
||||
|
||||
// Inserting Row
|
||||
db.insert(TABLE_DATA, null, values);
|
||||
db.close(); // Closing database connection
|
||||
}
|
||||
|
||||
// Getting single contact
|
||||
// Getting single contact
|
||||
DataHandler getValue(String value) {
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
DataHandler dh = null;
|
||||
|
||||
Cursor cursor = db.query(TABLE_DATA, new String[] { KEY_ID,
|
||||
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID,
|
||||
KEY_NAME, KEY_VALUE }, KEY_NAME + "=?",
|
||||
new String[] { value }, null, null, null, null);
|
||||
if (cursor != null)
|
||||
if (cursor != null){
|
||||
cursor.moveToFirst();
|
||||
|
||||
|
||||
DataHandler dh = new DataHandler(
|
||||
dh = new DataHandler(
|
||||
cursor.getString(1), cursor.getString(2));
|
||||
// return contact
|
||||
|
||||
}
|
||||
return dh;
|
||||
}
|
||||
|
||||
// Getting all values
|
||||
public List<DataHandler> getAllValues(){
|
||||
List<DataHandler> valueList = new ArrayList<DataHandler>();
|
||||
// Select All Query
|
||||
String selectQuery = "SELECT * FROM " + TABLE_DATA;
|
||||
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
Cursor cursor = db.rawQuery(selectQuery, null);
|
||||
|
||||
// looping through all rows adding to the list
|
||||
if(cursor.moveToFirst()){
|
||||
do{
|
||||
DataHandler dh = new DataHandler();
|
||||
dh.set_id(Integer.parseInt(cursor.getString(0)));
|
||||
dh.set_name(cursor.getString(1));
|
||||
dh.set_value(cursor.getString(2));
|
||||
valueList.add(dh);
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
// return value list
|
||||
return valueList;
|
||||
}
|
||||
|
||||
// Update single value
|
||||
public int updateValue(DataHandler dh){
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_NAME, dh.get_name());
|
||||
values.put(KEY_VALUE, dh.get_value());
|
||||
|
||||
// updating row
|
||||
return db.update(TABLE_DATA, values, KEY_ID + " = ?",
|
||||
new String[] { String.valueOf(dh.get_id())});
|
||||
}
|
||||
|
||||
// Getting contacts Count
|
||||
public boolean isEmpty() {
|
||||
String countQuery = "SELECT * FROM " + TABLE_DATA;
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
Cursor cursor = db.rawQuery(countQuery, null);
|
||||
cursor.close();
|
||||
|
||||
// return count
|
||||
if(cursor.getCount() == 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -66,8 +66,8 @@ public class PandroidAgent extends TabActivity {
|
||||
File installation = new File(getApplicationContext().getFilesDir(), "INSTALLATION");
|
||||
if(!installation.exists()){
|
||||
//Create database with default values
|
||||
DataBaseHandler db = new DataBaseHandler(this);
|
||||
Core.initDatabase(this);
|
||||
//DataBaseHandler db = new DataBaseHandler(this,"pandroid", null, 1);
|
||||
|
||||
//Log.d("DATABASE",Core.db.getValue("latitude").get_value());
|
||||
Core.restartAgentListener(getApplicationContext());
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ public class PandroidAgentListener extends Service {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
private void contact(){
|
||||
Date date = new Date();
|
||||
|
||||
@ -156,73 +156,72 @@ public class PandroidAgentListener extends Service {
|
||||
|
||||
updateValues();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
private void contact(){
|
||||
|
||||
/*
|
||||
Toast toast = Toast.makeText(getApplicationContext(),
|
||||
|
||||
getString(R.string.loading),
|
||||
Toast.LENGTH_SHORT);
|
||||
toast.setGravity(Gravity.BOTTOM,0,0);
|
||||
toast.show();
|
||||
*/
|
||||
|
||||
|
||||
Date date = new Date();
|
||||
|
||||
putSharedData("PANDROID_DATA", "lastContact", Long.toString(date.getTime() / 1000), "long");
|
||||
|
||||
String lastXML = "";
|
||||
new contactTask().execute(lastXML);
|
||||
updateValues();
|
||||
|
||||
}//end contact
|
||||
|
||||
|
||||
private class contactTask extends AsyncTask<String, Void, Void>{
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(String...params) {
|
||||
|
||||
String lastXML = params[0];
|
||||
lastXML = buildXML();
|
||||
String destFileName = "";
|
||||
String agentName = getSharedData("PANDROID_DATA", "agentName", Core.defaultAgentName, "string");
|
||||
destFileName = agentName + "." + System.currentTimeMillis() + ".data";
|
||||
|
||||
writeFile(destFileName, lastXML);
|
||||
String[] tentacleData = {
|
||||
"-a",
|
||||
getSharedData("PANDROID_DATA", "serverAddr", "", "string"),
|
||||
"-p",
|
||||
Core.defaultServerPort,
|
||||
"-v",
|
||||
"/data/data/pandroid.agent/files/" + destFileName
|
||||
};
|
||||
|
||||
int tentacleRet = new tentacle_client().tentacle_client(tentacleData);
|
||||
|
||||
putSharedData("PANDROID_DATA", "lastXML", lastXML, "string");
|
||||
if(tentacleRet == 0) {
|
||||
putSharedData("PANDROID_DATA", "contactError", "0", "integer");
|
||||
// Deleting the file after send it
|
||||
// move to only delete if sent successfully
|
||||
File file = new File("/data/data/pandroid.agent/files/" + destFileName);
|
||||
file.delete();
|
||||
if (Core.helloSignal >= 1)
|
||||
Core.helloSignal = 0;
|
||||
Core.updateConf(getApplicationContext());
|
||||
}
|
||||
else{
|
||||
putSharedData("PANDROID_DATA", "contactError", "1", "integer");
|
||||
}
|
||||
return null;
|
||||
|
||||
}//end doInBackground
|
||||
}
|
||||
|
||||
// private void contact(){
|
||||
//
|
||||
// /*
|
||||
// Toast toast = Toast.makeText(getApplicationContext(),
|
||||
//
|
||||
// getString(R.string.loading),
|
||||
// Toast.LENGTH_SHORT);
|
||||
// toast.setGravity(Gravity.BOTTOM,0,0);
|
||||
// toast.show();
|
||||
// */
|
||||
//
|
||||
//
|
||||
// Date date = new Date();
|
||||
//
|
||||
// putSharedData("PANDROID_DATA", "lastContact", Long.toString(date.getTime() / 1000), "long");
|
||||
//
|
||||
// String lastXML = "";
|
||||
// new contactTask().execute(lastXML);
|
||||
// updateValues();
|
||||
//
|
||||
// }//end contact
|
||||
//
|
||||
// private class contactTask extends AsyncTask<String, Void, Void>{
|
||||
//
|
||||
// @Override
|
||||
// protected Void doInBackground(String...params) {
|
||||
//
|
||||
// String lastXML = params[0];
|
||||
// lastXML = buildXML();
|
||||
// String destFileName = "";
|
||||
// String agentName = getSharedData("PANDROID_DATA", "agentName", Core.defaultAgentName, "string");
|
||||
// destFileName = agentName + "." + System.currentTimeMillis() + ".data";
|
||||
//
|
||||
// writeFile(destFileName, lastXML);
|
||||
// String[] tentacleData = {
|
||||
// "-a",
|
||||
// getSharedData("PANDROID_DATA", "serverAddr", "", "string"),
|
||||
// "-p",
|
||||
// Core.defaultServerPort,
|
||||
// "-v",
|
||||
// "/data/data/pandroid.agent/files/" + destFileName
|
||||
// };
|
||||
//
|
||||
// int tentacleRet = new tentacle_client().tentacle_client(tentacleData);
|
||||
//
|
||||
// putSharedData("PANDROID_DATA", "lastXML", lastXML, "string");
|
||||
// if(tentacleRet == 0) {
|
||||
// putSharedData("PANDROID_DATA", "contactError", "0", "integer");
|
||||
// // Deleting the file after send it
|
||||
// // move to only delete if sent successfully
|
||||
// File file = new File("/data/data/pandroid.agent/files/" + destFileName);
|
||||
// file.delete();
|
||||
// if (Core.helloSignal >= 1)
|
||||
// Core.helloSignal = 0;
|
||||
// Core.updateConf(getApplicationContext());
|
||||
// }
|
||||
// else{
|
||||
// putSharedData("PANDROID_DATA", "contactError", "1", "integer");
|
||||
// }
|
||||
// return null;
|
||||
//
|
||||
// }//end doInBackground
|
||||
// }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// From unfinished task of buffering unsent xml files when no connection available //
|
||||
@ -304,7 +303,7 @@ public class PandroidAgentListener extends Service {
|
||||
String helloSignal = getSharedData("PANDROID_DATA", "helloSignal", ""+Core.defaultHelloSignal, "integer");
|
||||
String roaming = getSharedData("PANDROID_DATA", "roaming", ""+Core.defaultRoaming, "integer");
|
||||
|
||||
String SimIDReport = getSharedData("PANDROID_DATA", "SimIDReport", Core.defaultSimIDReport, "string");
|
||||
String simIDReport = getSharedData("PANDROID_DATA", "simIDReport", Core.defaultSimIDReport, "string");
|
||||
String DeviceUpTimeReport = getSharedData("PANDROID_DATA", "DeviceUpTimeReport", Core.defaultDeviceUpTimeReport, "string");
|
||||
String NetworkOperatorReport = getSharedData("PANDROID_DATA", "NetworkOperatorReport", Core.defaultNetworkOperatorReport, "string");
|
||||
String NetworkTypeReport = getSharedData("PANDROID_DATA", "NetworkTypeReport", Core.defaultNetworkTypeReport, "string");
|
||||
@ -362,7 +361,7 @@ public class PandroidAgentListener extends Service {
|
||||
buffer += buildmoduleXML("upTime","Total device uptime in seconds.", "generic_data", upTime);
|
||||
|
||||
if (Core.hasSim){
|
||||
if (SimIDReport.equals("enabled"))
|
||||
if (simIDReport.equals("enabled"))
|
||||
buffer += buildmoduleXML("simID", "The Sim ID.", "generic_data_string", SimID);
|
||||
if (NetworkOperatorReport.equals("enabled"))
|
||||
buffer += buildmoduleXML("networkOperator","Currently registered network operator", "generic_data_string", networkOperator);
|
||||
|
Loading…
x
Reference in New Issue
Block a user