Commit f0a5c1e3 authored by Alexander Ivanov's avatar Alexander Ivanov

Fix line breaks.

parent f3f19141
...@@ -12,36 +12,36 @@ ...@@ -12,36 +12,36 @@
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data; package com.xabber.android.data;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
/** /**
* Interface for registered database tables. * Interface for registered database tables.
* *
* @author alexander.ivanov * @author alexander.ivanov
* *
*/ */
public interface DatabaseTable { public interface DatabaseTable {
/** /**
* Called on create database. * Called on create database.
* *
* @param db * @param db
*/ */
void create(SQLiteDatabase db); void create(SQLiteDatabase db);
/** /**
* Called on database migration. * Called on database migration.
* *
* @param db * @param db
* @param toVersion * @param toVersion
*/ */
void migrate(SQLiteDatabase db, int toVersion); void migrate(SQLiteDatabase db, int toVersion);
/** /**
* Called on clear database request. * Called on clear database request.
*/ */
void clear(); void clear();
} }
...@@ -12,21 +12,21 @@ ...@@ -12,21 +12,21 @@
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data; package com.xabber.android.data;
/** /**
* Listener for database migration. * Listener for database migration.
* *
* @author alexander.ivanov * @author alexander.ivanov
* *
*/ */
public interface OnMigrationListener extends BaseManagerInterface { public interface OnMigrationListener extends BaseManagerInterface {
/** /**
* Called on database migration for each intermediate versions. * Called on database migration for each intermediate versions.
* *
* @param toVersion * @param toVersion
*/ */
void onMigrate(int toVersion); void onMigrate(int toVersion);
} }
...@@ -12,64 +12,64 @@ ...@@ -12,64 +12,64 @@
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data.account; package com.xabber.android.data.account;
public class SavedStatus implements Comparable<SavedStatus> { public class SavedStatus implements Comparable<SavedStatus> {
private final StatusMode statusMode; private final StatusMode statusMode;
private final String statusText; private final String statusText;
public SavedStatus(StatusMode statusMode, String statusText) { public SavedStatus(StatusMode statusMode, String statusText) {
super(); super();
this.statusMode = statusMode; this.statusMode = statusMode;
this.statusText = statusText; this.statusText = statusText;
} }
public StatusMode getStatusMode() { public StatusMode getStatusMode() {
return statusMode; return statusMode;
} }
public String getStatusText() { public String getStatusText() {
return statusText; return statusText;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result result = prime * result
+ ((statusMode == null) ? 0 : statusMode.hashCode()); + ((statusMode == null) ? 0 : statusMode.hashCode());
result = prime * result result = prime * result
+ ((statusText == null) ? 0 : statusText.hashCode()); + ((statusText == null) ? 0 : statusText.hashCode());
return result; return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (obj == null) if (obj == null)
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
SavedStatus other = (SavedStatus) obj; SavedStatus other = (SavedStatus) obj;
if (statusMode != other.statusMode) if (statusMode != other.statusMode)
return false; return false;
if (statusText == null) { if (statusText == null) {
if (other.statusText != null) if (other.statusText != null)
return false; return false;
} else if (!statusText.equals(other.statusText)) } else if (!statusText.equals(other.statusText))
return false; return false;
return true; return true;
} }
@Override @Override
public int compareTo(SavedStatus another) { public int compareTo(SavedStatus another) {
int result = statusMode.compareTo(another.statusMode); int result = statusMode.compareTo(another.statusMode);
if (result != 0) if (result != 0)
return result; return result;
return statusText.compareToIgnoreCase(another.statusText); return statusText.compareToIgnoreCase(another.statusText);
} }
} }
...@@ -12,25 +12,25 @@ ...@@ -12,25 +12,25 @@
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data.entity; package com.xabber.android.data.entity;
import android.database.Cursor; import android.database.Cursor;
/** /**
* Table with entity related information. * Table with entity related information.
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public abstract class AbstractEntityTable extends AbstractAccountTable { public abstract class AbstractEntityTable extends AbstractAccountTable {
public static interface Fields extends AbstractAccountTable.Fields { public static interface Fields extends AbstractAccountTable.Fields {
public static final String USER = "user"; public static final String USER = "user";
} }
public static String getUser(Cursor cursor) { public static String getUser(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.USER)); return cursor.getString(cursor.getColumnIndex(Fields.USER));
} }
} }
\ No newline at end of file
/** /**
* Copyright (c) 2013, Redsolution LTD. All rights reserved. * Copyright (c) 2013, Redsolution LTD. All rights reserved.
* *
* This file is part of Xabber project; you can redistribute it and/or * This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3. * modify it under the terms of the GNU General Public License, Version 3.
* *
* Xabber is distributed in the hope that it will be useful, but * Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of * WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data.extension.avatar; package com.xabber.android.data.extension.avatar;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement; import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import com.xabber.android.data.AbstractTable; import com.xabber.android.data.AbstractTable;
import com.xabber.android.data.DatabaseManager; import com.xabber.android.data.DatabaseManager;
/** /**
* Storage with avatar hashes for the users. * Storage with avatar hashes for the users.
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
class AvatarTable extends AbstractTable { class AvatarTable extends AbstractTable {
private static final class Fields implements BaseColumns { private static final class Fields implements BaseColumns {
private Fields() { private Fields() {
} }
public static final String USER = "user"; public static final String USER = "user";
public static final String HASH = "hash"; public static final String HASH = "hash";
} }
private static final String NAME = "avatars"; private static final String NAME = "avatars";
private static final String[] PROJECTION = new String[] { Fields.USER, private static final String[] PROJECTION = new String[] { Fields.USER,
Fields.HASH, }; Fields.HASH, };
private final DatabaseManager databaseManager; private final DatabaseManager databaseManager;
private SQLiteStatement writeStatement; private SQLiteStatement writeStatement;
private final Object writeLock; private final Object writeLock;
private final static AvatarTable instance; private final static AvatarTable instance;
static { static {
instance = new AvatarTable(DatabaseManager.getInstance()); instance = new AvatarTable(DatabaseManager.getInstance());
DatabaseManager.getInstance().addTable(instance); DatabaseManager.getInstance().addTable(instance);
} }
public static AvatarTable getInstance() { public static AvatarTable getInstance() {
return instance; return instance;
} }
private AvatarTable(DatabaseManager databaseManager) { private AvatarTable(DatabaseManager databaseManager) {
this.databaseManager = databaseManager; this.databaseManager = databaseManager;
writeStatement = null; writeStatement = null;
writeLock = new Object(); writeLock = new Object();
} }
@Override @Override
public void create(SQLiteDatabase db) { public void create(SQLiteDatabase db) {
String sql = "CREATE TABLE " + NAME + " (" + Fields.USER String sql = "CREATE TABLE " + NAME + " (" + Fields.USER
+ " TEXT PRIMARY KEY," + Fields.HASH + " TEXT);"; + " TEXT PRIMARY KEY," + Fields.HASH + " TEXT);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
} }
@Override @Override
public void migrate(SQLiteDatabase db, int toVersion) { public void migrate(SQLiteDatabase db, int toVersion) {
super.migrate(db, toVersion); super.migrate(db, toVersion);
switch (toVersion) { switch (toVersion) {
case 2: case 2:
String sql = "CREATE TABLE avatars (" + "user TEXT PRIMARY KEY," String sql = "CREATE TABLE avatars (" + "user TEXT PRIMARY KEY,"
+ "hash TEXT);"; + "hash TEXT);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
break; break;
default: default:
break; break;
} }
} }
/** /**
* Saves avatar's hash for user. * Saves avatar's hash for user.
* *
* @param bareAddress * @param bareAddress
* @param hash * @param hash
*/ */
void write(String bareAddress, String hash) { void write(String bareAddress, String hash) {
synchronized (writeLock) { synchronized (writeLock) {
if (writeStatement == null) { if (writeStatement == null) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
writeStatement = db.compileStatement("INSERT OR REPLACE INTO " writeStatement = db.compileStatement("INSERT OR REPLACE INTO "
+ NAME + " (" + Fields.USER + ", " + Fields.HASH + NAME + " (" + Fields.USER + ", " + Fields.HASH
+ ") VALUES (?, ?);"); + ") VALUES (?, ?);");
} }
writeStatement.bindString(1, bareAddress); writeStatement.bindString(1, bareAddress);
if (hash == null) if (hash == null)
writeStatement.bindNull(2); writeStatement.bindNull(2);
else else
writeStatement.bindString(2, hash); writeStatement.bindString(2, hash);
writeStatement.execute(); writeStatement.execute();
} }
} }
@Override @Override
protected String getTableName() { protected String getTableName() {
return NAME; return NAME;
} }
@Override @Override
protected String[] getProjection() { protected String[] getProjection() {
return PROJECTION; return PROJECTION;
} }
static String getUser(Cursor cursor) { static String getUser(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.USER)); return cursor.getString(cursor.getColumnIndex(Fields.USER));
} }
static String getHash(Cursor cursor) { static String getHash(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.HASH)); return cursor.getString(cursor.getColumnIndex(Fields.HASH));
} }
} }
\ No newline at end of file
...@@ -12,132 +12,132 @@ ...@@ -12,132 +12,132 @@
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data.extension.otr; package com.xabber.android.data.extension.otr;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement; import android.database.sqlite.SQLiteStatement;
import com.xabber.android.data.DatabaseManager; import com.xabber.android.data.DatabaseManager;
import com.xabber.android.data.entity.AbstractEntityTable; import com.xabber.android.data.entity.AbstractEntityTable;
/** /**
* Storage with OTR finger prints and trusted level. * Storage with OTR finger prints and trusted level.
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
class OTRTable extends AbstractEntityTable { class OTRTable extends AbstractEntityTable {
private static final class Fields implements AbstractEntityTable.Fields { private static final class Fields implements AbstractEntityTable.Fields {
private Fields() { private Fields() {
} }
public static final String USER = "user"; public static final String USER = "user";
public static final String FINGERPRINT = "fingerprint"; public static final String FINGERPRINT = "fingerprint";
public static final String VERIFIED = "verified"; public static final String VERIFIED = "verified";
} }
private static final String NAME = "otr"; private static final String NAME = "otr";
private static final String[] PROJECTION = new String[] { Fields.ACCOUNT, private static final String[] PROJECTION = new String[] { Fields.ACCOUNT,
Fields.USER, Fields.FINGERPRINT, Fields.VERIFIED, }; Fields.USER, Fields.FINGERPRINT, Fields.VERIFIED, };
private final DatabaseManager databaseManager; private final DatabaseManager databaseManager;
private SQLiteStatement writeStatement; private SQLiteStatement writeStatement;
private final Object writeLock; private final Object writeLock;
private final static OTRTable instance; private final static OTRTable instance;
static { static {
instance = new OTRTable(DatabaseManager.getInstance()); instance = new OTRTable(DatabaseManager.getInstance());
DatabaseManager.getInstance().addTable(instance); DatabaseManager.getInstance().addTable(instance);
} }
public static OTRTable getInstance() { public static OTRTable getInstance() {
return instance; return instance;
} }
private OTRTable(DatabaseManager databaseManager) { private OTRTable(DatabaseManager databaseManager) {
this.databaseManager = databaseManager; this.databaseManager = databaseManager;
writeStatement = null; writeStatement = null;
writeLock = new Object(); writeLock = new Object();
} }
@Override @Override
public void create(SQLiteDatabase db) { public void create(SQLiteDatabase db) {
String sql; String sql;
sql = "CREATE TABLE " + NAME + " (" + Fields.ACCOUNT + " TEXT," sql = "CREATE TABLE " + NAME + " (" + Fields.ACCOUNT + " TEXT,"
+ Fields.USER + " TEXT," + Fields.FINGERPRINT + " TEXT," + Fields.USER + " TEXT," + Fields.FINGERPRINT + " TEXT,"
+ Fields.VERIFIED + " INTEGER);"; + Fields.VERIFIED + " INTEGER);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "CREATE UNIQUE INDEX " + NAME + "_list ON " + NAME + " (" sql = "CREATE UNIQUE INDEX " + NAME + "_list ON " + NAME + " ("
+ Fields.ACCOUNT + ", " + Fields.USER + ", " + Fields.ACCOUNT + ", " + Fields.USER + ", "
+ Fields.FINGERPRINT + ");"; + Fields.FINGERPRINT + ");";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
} }
@Override @Override
public void migrate(SQLiteDatabase db, int toVersion) { public void migrate(SQLiteDatabase db, int toVersion) {
super.migrate(db, toVersion); super.migrate(db, toVersion);
String sql; String sql;
switch (toVersion) { switch (toVersion) {
case 54: case 54:
sql = "CREATE TABLE otr (" + "account TEXT," + "user TEXT," sql = "CREATE TABLE otr (" + "account TEXT," + "user TEXT,"
+ "fingerprint TEXT," + "verified INTEGER);"; + "fingerprint TEXT," + "verified INTEGER);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "CREATE UNIQUE INDEX otr_list ON otr (account, user);"; sql = "CREATE UNIQUE INDEX otr_list ON otr (account, user);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
break; break;
case 56: case 56:
sql = "DROP INDEX otr_list;"; sql = "DROP INDEX otr_list;";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "CREATE UNIQUE INDEX otr_list ON otr (account, user, fingerprint);"; sql = "CREATE UNIQUE INDEX otr_list ON otr (account, user, fingerprint);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
default: default:
break; break;
} }
} }
void write(String account, String user, String fingerprint, boolean verified) { void write(String account, String user, String fingerprint, boolean verified) {
synchronized (writeLock) { synchronized (writeLock) {
if (writeStatement == null) { if (writeStatement == null) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
writeStatement = db.compileStatement("INSERT OR REPLACE INTO " writeStatement = db.compileStatement("INSERT OR REPLACE INTO "
+ NAME + " (" + Fields.ACCOUNT + ", " + Fields.USER + NAME + " (" + Fields.ACCOUNT + ", " + Fields.USER
+ ", " + Fields.FINGERPRINT + ", " + Fields.VERIFIED + ", " + Fields.FINGERPRINT + ", " + Fields.VERIFIED
+ ") VALUES (?, ?, ?, ?);"); + ") VALUES (?, ?, ?, ?);");
} }
writeStatement.bindString(1, account); writeStatement.bindString(1, account);
writeStatement.bindString(2, user); writeStatement.bindString(2, user);
writeStatement.bindString(3, fingerprint); writeStatement.bindString(3, fingerprint);
writeStatement.bindLong(4, verified ? 1 : 0); writeStatement.bindLong(4, verified ? 1 : 0);
writeStatement.execute(); writeStatement.execute();
} }
} }
void remove(String account, String user) { void remove(String account, String user) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
db.delete(NAME, Fields.ACCOUNT + " = ? AND " + Fields.USER + " = ?", db.delete(NAME, Fields.ACCOUNT + " = ? AND " + Fields.USER + " = ?",
new String[] { account, user }); new String[] { account, user });
} }
@Override @Override
protected String getTableName() { protected String getTableName() {
return NAME; return NAME;
} }
@Override @Override
protected String[] getProjection() { protected String[] getProjection() {
return PROJECTION; return PROJECTION;
} }
static String getFingerprint(Cursor cursor) { static String getFingerprint(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.FINGERPRINT)); return cursor.getString(cursor.getColumnIndex(Fields.FINGERPRINT));
} }
static boolean isVerified(Cursor cursor) { static boolean isVerified(Cursor cursor) {
return cursor.getLong(cursor.getColumnIndex(Fields.VERIFIED)) != 0; return cursor.getLong(cursor.getColumnIndex(Fields.VERIFIED)) != 0;
} }
} }
\ No newline at end of file
...@@ -12,172 +12,172 @@ ...@@ -12,172 +12,172 @@
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data.message.phrase; package com.xabber.android.data.message.phrase;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.net.Uri; import android.net.Uri;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import com.xabber.android.data.AbstractTable; import com.xabber.android.data.AbstractTable;
import com.xabber.android.data.DatabaseManager; import com.xabber.android.data.DatabaseManager;
/** /**
* Storage with phrase notification settings. * Storage with phrase notification settings.
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
class PhraseTable extends AbstractTable { class PhraseTable extends AbstractTable {
private static final class Fields implements BaseColumns { private static final class Fields implements BaseColumns {
private Fields() { private Fields() {
} }
/** /**
* Text pattern. * Text pattern.
*/ */
public static final String VALUE = "value"; public static final String VALUE = "value";
/** /**
* JID pattern. * JID pattern.
*/ */
public static final String USER = "user"; public static final String USER = "user";
/** /**
* Contact list group pattern. * Contact list group pattern.
*/ */
public static final String GROUP = "_group"; public static final String GROUP = "_group";
/** /**
* Whether text should be processed as regexp. * Whether text should be processed as regexp.
*/ */
public static final String REGEXP = "regexp"; public static final String REGEXP = "regexp";
/** /**
* Used sound. * Used sound.
*/ */
public static final String SOUND = "sound"; public static final String SOUND = "sound";
} }
private static final String NAME = "phrase"; private static final String NAME = "phrase";
private static final String[] PROJECTION = new String[] { Fields._ID, private static final String[] PROJECTION = new String[] { Fields._ID,
Fields.VALUE, Fields.USER, Fields.GROUP, Fields.REGEXP, Fields.VALUE, Fields.USER, Fields.GROUP, Fields.REGEXP,
Fields.SOUND, }; Fields.SOUND, };
private final DatabaseManager databaseManager; private final DatabaseManager databaseManager;
private final static PhraseTable instance; private final static PhraseTable instance;
static { static {
instance = new PhraseTable(DatabaseManager.getInstance()); instance = new PhraseTable(DatabaseManager.getInstance());
DatabaseManager.getInstance().addTable(instance); DatabaseManager.getInstance().addTable(instance);
} }
public static PhraseTable getInstance() { public static PhraseTable getInstance() {
return instance; return instance;
} }
private PhraseTable(DatabaseManager databaseManager) { private PhraseTable(DatabaseManager databaseManager) {
this.databaseManager = databaseManager; this.databaseManager = databaseManager;
} }
@Override @Override
public void create(SQLiteDatabase db) { public void create(SQLiteDatabase db) {
String sql; String sql;
sql = "CREATE TABLE " + NAME + " (" + Fields._ID sql = "CREATE TABLE " + NAME + " (" + Fields._ID
+ " INTEGER PRIMARY KEY," + Fields.VALUE + " TEXT," + " INTEGER PRIMARY KEY," + Fields.VALUE + " TEXT,"
+ Fields.USER + " TEXT," + Fields.GROUP + " TEXT," + Fields.USER + " TEXT," + Fields.GROUP + " TEXT,"
+ Fields.REGEXP + " INTEGER," + Fields.SOUND + " TEXT);"; + Fields.REGEXP + " INTEGER," + Fields.SOUND + " TEXT);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
} }
@Override @Override
public void migrate(SQLiteDatabase db, int toVersion) { public void migrate(SQLiteDatabase db, int toVersion) {
super.migrate(db, toVersion); super.migrate(db, toVersion);
String sql; String sql;
switch (toVersion) { switch (toVersion) {
case 63: case 63:
sql = "CREATE TABLE phrase (_id INTEGER PRIMARY KEY," sql = "CREATE TABLE phrase (_id INTEGER PRIMARY KEY,"
+ "value TEXT," + "regexp INTEGER," + "sound TEXT);"; + "value TEXT," + "regexp INTEGER," + "sound TEXT);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
break; break;
case 64: case 64:
sql = "ALTER TABLE phrase ADD COLUMN user TEXT;"; sql = "ALTER TABLE phrase ADD COLUMN user TEXT;";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "UPDATE phrase SET user = \"\";"; sql = "UPDATE phrase SET user = \"\";";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "ALTER TABLE phrase ADD COLUMN _group TEXT;"; sql = "ALTER TABLE phrase ADD COLUMN _group TEXT;";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "UPDATE phrase SET _group = \"\";"; sql = "UPDATE phrase SET _group = \"\";";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
break; break;
default: default:
break; break;
} }
} }
long write(Long id, String value, String user, String group, long write(Long id, String value, String user, String group,
boolean regexp, Uri sound) { boolean regexp, Uri sound) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(Fields.VALUE, value); values.put(Fields.VALUE, value);
values.put(Fields.USER, user); values.put(Fields.USER, user);
values.put(Fields.GROUP, group); values.put(Fields.GROUP, group);
values.put(Fields.REGEXP, regexp ? 1 : 0); values.put(Fields.REGEXP, regexp ? 1 : 0);
values.put(Fields.SOUND, sound.toString()); values.put(Fields.SOUND, sound.toString());
if (id == null) if (id == null)
return db.insert(NAME, Fields.VALUE, values); return db.insert(NAME, Fields.VALUE, values);
db.update(NAME, values, Fields._ID + " = ?", db.update(NAME, values, Fields._ID + " = ?",
new String[] { String.valueOf(id) }); new String[] { String.valueOf(id) });
return id; return id;
} }
void remove(long id) { void remove(long id) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
db.delete(NAME, Fields._ID + " = ?", db.delete(NAME, Fields._ID + " = ?",
new String[] { String.valueOf(id) }); new String[] { String.valueOf(id) });
} }
@Override @Override
protected String getTableName() { protected String getTableName() {
return NAME; return NAME;
} }
@Override @Override
protected String[] getProjection() { protected String[] getProjection() {
return PROJECTION; return PROJECTION;
} }
@Override @Override
protected String getListOrder() { protected String getListOrder() {
return Fields._ID; return Fields._ID;
} }
static long getId(Cursor cursor) { static long getId(Cursor cursor) {
return cursor.getLong(cursor.getColumnIndex(Fields._ID)); return cursor.getLong(cursor.getColumnIndex(Fields._ID));
} }
static String getValue(Cursor cursor) { static String getValue(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.VALUE)); return cursor.getString(cursor.getColumnIndex(Fields.VALUE));
} }
static String getUser(Cursor cursor) { static String getUser(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.USER)); return cursor.getString(cursor.getColumnIndex(Fields.USER));
} }
static String getGroup(Cursor cursor) { static String getGroup(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.GROUP)); return cursor.getString(cursor.getColumnIndex(Fields.GROUP));
} }
static boolean isRegexp(Cursor cursor) { static boolean isRegexp(Cursor cursor) {
return cursor.getLong(cursor.getColumnIndex(Fields.REGEXP)) != 0; return cursor.getLong(cursor.getColumnIndex(Fields.REGEXP)) != 0;
} }
static Uri getSound(Cursor cursor) { static Uri getSound(Cursor cursor) {
return Uri.parse(cursor.getString(cursor.getColumnIndex(Fields.SOUND))); return Uri.parse(cursor.getString(cursor.getColumnIndex(Fields.SOUND)));
} }
} }
\ No newline at end of file
/** /**
* Copyright (c) 2013, Redsolution LTD. All rights reserved. * Copyright (c) 2013, Redsolution LTD. All rights reserved.
* *
* This file is part of Xabber project; you can redistribute it and/or * This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3. * modify it under the terms of the GNU General Public License, Version 3.
* *
* Xabber is distributed in the hope that it will be useful, but * Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of * WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License, * You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/. * along with this program. If not, see http://www.gnu.org/licenses/.
*/ */
package com.xabber.android.data.notification; package com.xabber.android.data.notification;
import java.util.Date; import java.util.Date;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement; import android.database.sqlite.SQLiteStatement;
import com.xabber.android.data.DatabaseManager; import com.xabber.android.data.DatabaseManager;
import com.xabber.android.data.entity.AbstractEntityTable; import com.xabber.android.data.entity.AbstractEntityTable;
/** /**
* Storage with notifications. * Storage with notifications.
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
class NotificationTable extends AbstractEntityTable { class NotificationTable extends AbstractEntityTable {
private static final class Fields implements AbstractEntityTable.Fields { private static final class Fields implements AbstractEntityTable.Fields {
private Fields() { private Fields() {
} }
/** /**
* Text message. * Text message.
*/ */
public static final String TEXT = "text"; public static final String TEXT = "text";
/** /**
* Time when last message was received. * Time when last message was received.
*/ */
public static final String TIMESTAMP = "timestamp"; public static final String TIMESTAMP = "timestamp";
/** /**
* Count of not notified messages. * Count of not notified messages.
*/ */
public static final String COUNT = "count"; public static final String COUNT = "count";
} }
private static final String NAME = "notifications"; private static final String NAME = "notifications";
private static final String[] PROJECTION = new String[] { Fields.ACCOUNT, private static final String[] PROJECTION = new String[] { Fields.ACCOUNT,
Fields.USER, Fields.TEXT, Fields.TIMESTAMP, Fields.COUNT }; Fields.USER, Fields.TEXT, Fields.TIMESTAMP, Fields.COUNT };
private final DatabaseManager databaseManager; private final DatabaseManager databaseManager;
private SQLiteStatement writeStatement; private SQLiteStatement writeStatement;
private final Object writeLock; private final Object writeLock;
private final static NotificationTable instance; private final static NotificationTable instance;
static { static {
instance = new NotificationTable(DatabaseManager.getInstance()); instance = new NotificationTable(DatabaseManager.getInstance());
DatabaseManager.getInstance().addTable(instance); DatabaseManager.getInstance().addTable(instance);
} }
public static NotificationTable getInstance() { public static NotificationTable getInstance() {
return instance; return instance;
} }
private NotificationTable(DatabaseManager databaseManager) { private NotificationTable(DatabaseManager databaseManager) {
this.databaseManager = databaseManager; this.databaseManager = databaseManager;
writeStatement = null; writeStatement = null;
writeLock = new Object(); writeLock = new Object();
} }
@Override @Override
public void create(SQLiteDatabase db) { public void create(SQLiteDatabase db) {
String sql; String sql;
sql = "CREATE TABLE " + NAME + " (" + Fields.ACCOUNT + " TEXT," sql = "CREATE TABLE " + NAME + " (" + Fields.ACCOUNT + " TEXT,"
+ Fields.USER + " TEXT," + Fields.TEXT + " TEXT," + Fields.USER + " TEXT," + Fields.TEXT + " TEXT,"
+ Fields.TIMESTAMP + " INTEGER," + Fields.COUNT + " INTEGER);"; + Fields.TIMESTAMP + " INTEGER," + Fields.COUNT + " INTEGER);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "CREATE UNIQUE INDEX " + NAME + "_list ON " + NAME + " (" sql = "CREATE UNIQUE INDEX " + NAME + "_list ON " + NAME + " ("
+ Fields.ACCOUNT + ", " + Fields.USER + " )"; + Fields.ACCOUNT + ", " + Fields.USER + " )";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
} }
@Override @Override
public void migrate(SQLiteDatabase db, int toVersion) { public void migrate(SQLiteDatabase db, int toVersion) {
super.migrate(db, toVersion); super.migrate(db, toVersion);
String sql; String sql;
switch (toVersion) { switch (toVersion) {
case 60: case 60:
sql = "CREATE TABLE notifications (" + "account TEXT," sql = "CREATE TABLE notifications (" + "account TEXT,"
+ "user TEXT," + "text TEXT," + "timestamp INTEGER," + "user TEXT," + "text TEXT," + "timestamp INTEGER,"
+ "count INTEGER);"; + "count INTEGER);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
sql = "CREATE UNIQUE INDEX notifications_list ON notifications (account, user);"; sql = "CREATE UNIQUE INDEX notifications_list ON notifications (account, user);";
DatabaseManager.execSQL(db, sql); DatabaseManager.execSQL(db, sql);
break; break;
default: default:
break; break;
} }
} }
void write(String account, String user, String text, Date timeStamp, void write(String account, String user, String text, Date timeStamp,
int count) { int count) {
synchronized (writeLock) { synchronized (writeLock) {
if (writeStatement == null) { if (writeStatement == null) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
writeStatement = db.compileStatement("INSERT OR REPLACE INTO " writeStatement = db.compileStatement("INSERT OR REPLACE INTO "
+ NAME + " (" + Fields.ACCOUNT + ", " + Fields.USER + NAME + " (" + Fields.ACCOUNT + ", " + Fields.USER
+ ", " + Fields.TEXT + ", " + Fields.TIMESTAMP + ", " + ", " + Fields.TEXT + ", " + Fields.TIMESTAMP + ", "
+ Fields.COUNT + ") VALUES " + "(?, ?, ?, ?, ?);"); + Fields.COUNT + ") VALUES " + "(?, ?, ?, ?, ?);");
} }
writeStatement.bindString(1, account); writeStatement.bindString(1, account);
writeStatement.bindString(2, user); writeStatement.bindString(2, user);
writeStatement.bindString(3, text); writeStatement.bindString(3, text);
writeStatement.bindLong(4, timeStamp.getTime()); writeStatement.bindLong(4, timeStamp.getTime());
writeStatement.bindLong(5, count); writeStatement.bindLong(5, count);
writeStatement.execute(); writeStatement.execute();
} }
} }
public void remove(String account, String user) { public void remove(String account, String user) {
SQLiteDatabase db = databaseManager.getWritableDatabase(); SQLiteDatabase db = databaseManager.getWritableDatabase();
db.delete(NAME, Fields.ACCOUNT + " = ? AND " + Fields.USER + " = ?", db.delete(NAME, Fields.ACCOUNT + " = ? AND " + Fields.USER + " = ?",
new String[] { account, user }); new String[] { account, user });
} }
@Override @Override
protected String getTableName() { protected String getTableName() {
return NAME; return NAME;
} }
@Override @Override
protected String[] getProjection() { protected String[] getProjection() {
return PROJECTION; return PROJECTION;
} }
@Override @Override
protected String getListOrder() { protected String getListOrder() {
return Fields.TIMESTAMP; return Fields.TIMESTAMP;
} }
static String getText(Cursor cursor) { static String getText(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(Fields.TEXT)); return cursor.getString(cursor.getColumnIndex(Fields.TEXT));
} }
static Date getTimeStamp(Cursor cursor) { static Date getTimeStamp(Cursor cursor) {
return new Date(cursor.getLong(cursor.getColumnIndex(Fields.TIMESTAMP))); return new Date(cursor.getLong(cursor.getColumnIndex(Fields.TIMESTAMP)));
} }
static int getCount(Cursor cursor) { static int getCount(Cursor cursor) {
return cursor.getInt(cursor.getColumnIndex(Fields.COUNT)); return cursor.getInt(cursor.getColumnIndex(Fields.COUNT));
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment