Commit 27f542c7 authored by Grigory Fedorov's avatar Grigory Fedorov

For #344 Database version increased to 67, migration for "Show message text in...

For #344 Database version increased to 67, migration for "Show message text in notification" chat option added.
 If chat option was same to global, it set to global, if it was different, it set to different.
parent e9a90014
...@@ -14,12 +14,6 @@ ...@@ -14,12 +14,6 @@
*/ */
package com.xabber.android.data; package com.xabber.android.data;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import android.database.DatabaseUtils; import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
...@@ -27,6 +21,12 @@ import android.database.sqlite.SQLiteOpenHelper; ...@@ -27,6 +21,12 @@ import android.database.sqlite.SQLiteOpenHelper;
import com.xabber.android.data.entity.AbstractAccountTable; import com.xabber.android.data.entity.AbstractAccountTable;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
/** /**
* Helps to open, create, and upgrade the database file. * Helps to open, create, and upgrade the database file.
* <p/> * <p/>
...@@ -38,13 +38,10 @@ public class DatabaseManager extends SQLiteOpenHelper implements ...@@ -38,13 +38,10 @@ public class DatabaseManager extends SQLiteOpenHelper implements
OnLoadListener, OnClearListener { OnLoadListener, OnClearListener {
private static final String DATABASE_NAME = "xabber.db"; private static final String DATABASE_NAME = "xabber.db";
private static final int DATABASE_VERSION = 66; private static final int DATABASE_VERSION = 67;
private static final SQLiteException DOWNGRAD_EXCEPTION = new SQLiteException( private static final SQLiteException DOWNGRAD_EXCEPTION = new SQLiteException(
"Database file was deleted"); "Database file was deleted");
private final ArrayList<DatabaseTable> registeredTables;
private final static DatabaseManager instance; private final static DatabaseManager instance;
static { static {
...@@ -52,15 +49,94 @@ public class DatabaseManager extends SQLiteOpenHelper implements ...@@ -52,15 +49,94 @@ public class DatabaseManager extends SQLiteOpenHelper implements
Application.getInstance().addManager(instance); Application.getInstance().addManager(instance);
} }
public static DatabaseManager getInstance() { private final ArrayList<DatabaseTable> registeredTables;
return instance;
}
private DatabaseManager() { private DatabaseManager() {
super(Application.getInstance(), DATABASE_NAME, null, DATABASE_VERSION); super(Application.getInstance(), DATABASE_NAME, null, DATABASE_VERSION);
registeredTables = new ArrayList<DatabaseTable>(); registeredTables = new ArrayList<DatabaseTable>();
} }
public static DatabaseManager getInstance() {
return instance;
}
/**
* Builds IN statement for specified collection of values.
*
* @param <T>
* @param column
* @param values
* @return "column IN (value1, ... valueN)" or
* "(column IS NULL AND column IS NOT NULL)" if ids is empty.
*/
public static <T> String in(String column, Collection<T> values) {
if (values.isEmpty())
return new StringBuilder("(").append(column)
.append(" IS NULL AND ").append(column)
.append(" IS NOT NULL)").toString();
StringBuilder builder = new StringBuilder(column);
builder.append(" IN (");
Iterator<T> iterator = values.iterator();
while (iterator.hasNext()) {
T value = iterator.next();
if (value instanceof String)
builder.append(DatabaseUtils.sqlEscapeString((String) value));
else
builder.append(value.toString());
if (iterator.hasNext())
builder.append(",");
}
builder.append(")");
return builder.toString();
}
public static void execSQL(SQLiteDatabase db, String sql) {
LogManager.iString(DatabaseManager.class.getName(), sql);
db.execSQL(sql);
}
public static void dropTable(SQLiteDatabase db, String table) {
execSQL(db, "DROP TABLE IF EXISTS " + table + ";");
}
public static void renameTable(SQLiteDatabase db, String table,
String newTable) {
execSQL(db, "ALTER TABLE " + table + " RENAME TO " + newTable + ";");
}
public static String commaSeparatedFromCollection(Collection<String> strings) {
StringBuilder builder = new StringBuilder();
for (String value : strings) {
if (builder.length() > 0)
builder.append(",");
builder.append(value.replace("\\", "\\\\").replace(",", "\\,"));
}
return builder.toString();
}
public static Collection<String> collectionFromCommaSeparated(String value) {
Collection<String> collection = new ArrayList<String>();
boolean escape = false;
StringBuilder builder = new StringBuilder();
for (int index = 0; index < value.length(); index++) {
char chr = value.charAt(index);
if (!escape) {
if (chr == '\\') {
escape = true;
continue;
} else if (chr == ',') {
collection.add(builder.toString());
builder = new StringBuilder();
continue;
}
}
escape = false;
builder.append(chr);
}
collection.add(builder.toString());
return Collections.unmodifiableCollection(collection);
}
/** /**
* Register new table. * Register new table.
* *
...@@ -146,81 +222,4 @@ public class DatabaseManager extends SQLiteOpenHelper implements ...@@ -146,81 +222,4 @@ public class DatabaseManager extends SQLiteOpenHelper implements
((AbstractAccountTable) table).removeAccount(account); ((AbstractAccountTable) table).removeAccount(account);
} }
/**
* Builds IN statement for specified collection of values.
*
* @param <T>
* @param column
* @param values
* @return "column IN (value1, ... valueN)" or
* "(column IS NULL AND column IS NOT NULL)" if ids is empty.
*/
public static <T> String in(String column, Collection<T> values) {
if (values.isEmpty())
return new StringBuilder("(").append(column)
.append(" IS NULL AND ").append(column)
.append(" IS NOT NULL)").toString();
StringBuilder builder = new StringBuilder(column);
builder.append(" IN (");
Iterator<T> iterator = values.iterator();
while (iterator.hasNext()) {
T value = iterator.next();
if (value instanceof String)
builder.append(DatabaseUtils.sqlEscapeString((String) value));
else
builder.append(value.toString());
if (iterator.hasNext())
builder.append(",");
}
builder.append(")");
return builder.toString();
}
public static void execSQL(SQLiteDatabase db, String sql) {
LogManager.iString(DatabaseManager.class.getName(), sql);
db.execSQL(sql);
}
public static void dropTable(SQLiteDatabase db, String table) {
execSQL(db, "DROP TABLE IF EXISTS " + table + ";");
}
public static void renameTable(SQLiteDatabase db, String table,
String newTable) {
execSQL(db, "ALTER TABLE " + table + " RENAME TO " + newTable + ";");
}
public static String commaSeparatedFromCollection(Collection<String> strings) {
StringBuilder builder = new StringBuilder();
for (String value : strings) {
if (builder.length() > 0)
builder.append(",");
builder.append(value.replace("\\", "\\\\").replace(",", "\\,"));
}
return builder.toString();
}
public static Collection<String> collectionFromCommaSeparated(String value) {
Collection<String> collection = new ArrayList<String>();
boolean escape = false;
StringBuilder builder = new StringBuilder();
for (int index = 0; index < value.length(); index++) {
char chr = value.charAt(index);
if (!escape) {
if (chr == '\\') {
escape = true;
continue;
} else if (chr == ',') {
collection.add(builder.toString());
builder = new StringBuilder();
continue;
}
}
escape = false;
builder.append(chr);
}
collection.add(builder.toString());
return Collections.unmodifiableCollection(collection);
}
} }
...@@ -19,6 +19,7 @@ import android.database.sqlite.SQLiteDatabase; ...@@ -19,6 +19,7 @@ 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.SettingsManager;
/** /**
* Storage with settings to show text in notification for each chat. * Storage with settings to show text in notification for each chat.
...@@ -70,6 +71,26 @@ class ShowTextTable extends AbstractChatPropertyTable<ShowMessageTextInNotificat ...@@ -70,6 +71,26 @@ class ShowTextTable extends AbstractChatPropertyTable<ShowMessageTextInNotificat
case 52: case 52:
initialMigrate(db, "chat_show_text", "INTEGER"); initialMigrate(db, "chat_show_text", "INTEGER");
break; break;
case 67:
int trueMigrationValue;
int falseMigrationValue;
if (SettingsManager.eventsShowText()) {
trueMigrationValue = ShowMessageTextInNotification.default_settings.ordinal();
falseMigrationValue = ShowMessageTextInNotification.hide.ordinal();
} else {
trueMigrationValue = ShowMessageTextInNotification.show.ordinal();
falseMigrationValue = ShowMessageTextInNotification.default_settings.ordinal();
}
String sql = "UPDATE " + NAME
+ " SET " + Fields.VALUE + " = CASE WHEN (" + Fields.VALUE + "=1) THEN "
+ trueMigrationValue + " ELSE " + falseMigrationValue + " END;";
DatabaseManager.execSQL(db, sql);
default: default:
break; break;
} }
......
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