Search in sources :

Example 11 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project symmetric-ds by JumpMind.

the class SymmetricService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (symmetricEngine == null) {
        try {
            Log.i(TAG, "creating engine");
            String key = intent.getStringExtra(INTENTKEY_SQLITEOPENHELPER_REGISTRY_KEY);
            if (key != null) {
                SQLiteOpenHelper databaseHelper = SQLiteOpenHelperRegistry.lookup(key);
                if (databaseHelper != null) {
                    String registrationUrl = intent.getStringExtra(INTENTKEY_REGISTRATION_URL);
                    String externalId = intent.getStringExtra(INTENTKEY_EXTERNAL_ID);
                    String nodeGroupId = intent.getStringExtra(INTENTKEY_NODE_GROUP_ID);
                    Serializable passedInProps = (Serializable) intent.getSerializableExtra(INTENTKEY_PROPERTIES);
                    Properties properties = null;
                    if (passedInProps instanceof Properties) {
                        properties = (Properties) passedInProps;
                    } else if (passedInProps instanceof Map) {
                        properties = new Properties();
                        @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) passedInProps;
                        for (String propKey : map.keySet()) {
                            properties.setProperty(propKey, map.get(propKey));
                        }
                    } else {
                        properties = new Properties();
                    }
                    symmetricEngine = new AndroidSymmetricEngine(registrationUrl, externalId, nodeGroupId, properties, databaseHelper, getApplicationContext());
                    Log.i(TAG, "engine created");
                } else {
                    Log.e(TAG, "Could not find SQLiteOpenHelper in registry using " + key);
                }
            } else {
                Log.e(TAG, "Must provide valid " + INTENTKEY_SQLITEOPENHELPER_REGISTRY_KEY);
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage(), ex);
        }
    }
    if (symmetricEngine != null) {
        if (!symmetricEngine.isStarted() && !symmetricEngine.isStarting()) {
            try {
                Runnable runnable = new Runnable() {

                    public void run() {
                        try {
                            Log.i(TAG, "starting engine");
                            symmetricEngine.start();
                            Log.i(TAG, "engine started");
                        } catch (Exception ex) {
                            Log.e(TAG, ex.getMessage(), ex);
                        }
                    }
                };
                boolean startInBackground = intent.getBooleanExtra(INTENTKEY_START_IN_BACKGROUND, false);
                if (startInBackground) {
                    new Thread(runnable).start();
                } else {
                    runnable.run();
                }
            } catch (Exception ex) {
                Log.e(TAG, ex.getMessage(), ex);
            }
        }
    }
    return START_REDELIVER_INTENT;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) Serializable(java.io.Serializable) Properties(java.util.Properties) Map(java.util.Map)

Example 12 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.

the class HistoryManager method addHistoryItemDetails.

public void addHistoryItemDetails(String itemID, String itemDetails) {
    // As we're going to do an update only we don't need need to worry
    // about the preferences; if the item wasn't saved it won't be udpated
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_DETAIL_COL_PROJECTION, DBHelper.TEXT_COL + "=?", new String[] { itemID }, null, null, DBHelper.TIMESTAMP_COL + " DESC", "1");
        String oldID = null;
        String oldDetails = null;
        if (cursor.moveToNext()) {
            oldID = cursor.getString(0);
            oldDetails = cursor.getString(1);
        }
        if (oldID != null) {
            String newDetails;
            if (oldDetails == null) {
                newDetails = itemDetails;
            } else if (oldDetails.contains(itemDetails)) {
                newDetails = null;
            } else {
                newDetails = oldDetails + " : " + itemDetails;
            }
            if (newDetails != null) {
                ContentValues values = new ContentValues();
                values.put(DBHelper.DETAILS_COL, newDetails);
                db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
            }
        }
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Example 13 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.

the class HistoryManager method clearHistory.

void clearHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    try {
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME, null, null);
    } finally {
        close(null, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 14 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.

the class HistoryManager method buildHistoryItems.

public List<HistoryItem> buildHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    List<HistoryItem> items = new ArrayList<>();
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
        while (cursor.moveToNext()) {
            String text = cursor.getString(0);
            String display = cursor.getString(1);
            String format = cursor.getString(2);
            long timestamp = cursor.getLong(3);
            String details = cursor.getString(4);
            Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
            items.add(new HistoryItem(result, display, details));
        }
    } finally {
        close(cursor, db);
    }
    return items;
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Result(com.google.zxing.Result)

Example 15 with SQLiteOpenHelper

use of android.database.sqlite.SQLiteOpenHelper in project weex-example by KalicyZhou.

the class HistoryManager method hasHistoryItems.

public boolean hasHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
        cursor.moveToFirst();
        return cursor.getInt(0) > 0;
    } finally {
        close(cursor, db);
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Aggregations

SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)46 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)36 Cursor (android.database.Cursor)21 Test (org.junit.Test)14 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)9 ContentValues (android.content.ContentValues)7 Result (com.google.zxing.Result)6 SQLException (android.database.SQLException)5 SharedPreferences (android.content.SharedPreferences)3 DateFormat (java.text.DateFormat)3 ArrayList (java.util.ArrayList)3 Context (android.content.Context)2 SQLiteException (android.database.sqlite.SQLiteException)2 Changes (com.pushtorefresh.storio.sqlite.Changes)2 RawQuery (com.pushtorefresh.storio.sqlite.queries.RawQuery)2 Date (java.util.Date)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)1 InsertQuery (com.pushtorefresh.storio.sqlite.queries.InsertQuery)1 Serializable (java.io.Serializable)1