11 package com.freerdp.freerdpcore.services;
13 import android.content.ContentValues;
14 import android.content.Context;
15 import android.database.Cursor;
16 import android.database.sqlite.SQLiteDatabase;
17 import android.database.sqlite.SQLiteOpenHelper;
18 import android.provider.BaseColumns;
19 import android.util.Log;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
27 public static final String ID = BaseColumns._ID;
28 private static final int DB_VERSION = 10;
29 private static final String DB_BACKUP_PREFIX =
"temp_";
30 private static final String DB_NAME =
"bookmarks.db";
31 static final String DB_TABLE_BOOKMARK =
"tbl_manual_bookmarks";
32 static final String DB_TABLE_SCREEN =
"tbl_screen_settings";
33 static final String DB_TABLE_PERFORMANCE =
"tbl_performance_flags";
34 private static final String[] DB_TABLES = { DB_TABLE_BOOKMARK, DB_TABLE_SCREEN,
35 DB_TABLE_PERFORMANCE };
37 static final String DB_KEY_SCREEN_COLORS =
"colors";
38 static final String DB_KEY_SCREEN_RESOLUTION =
"resolution";
39 static final String DB_KEY_SCREEN_WIDTH =
"width";
40 static final String DB_KEY_SCREEN_HEIGHT =
"height";
42 static final String DB_KEY_SCREEN_SETTINGS =
"screen_settings";
43 static final String DB_KEY_SCREEN_SETTINGS_3G =
"screen_3g";
44 static final String DB_KEY_PERFORMANCE_FLAGS =
"performance_flags";
45 static final String DB_KEY_PERFORMANCE_FLAGS_3G =
"performance_3g";
47 static final String DB_KEY_PERFORMANCE_RFX =
"perf_remotefx";
48 static final String DB_KEY_PERFORMANCE_GFX =
"perf_gfx";
49 static final String DB_KEY_PERFORMANCE_H264 =
"perf_gfx_h264";
50 static final String DB_KEY_PERFORMANCE_WALLPAPER =
"perf_wallpaper";
51 static final String DB_KEY_PERFORMANCE_THEME =
"perf_theming";
52 static final String DB_KEY_PERFORMANCE_DRAG =
"perf_full_window_drag";
53 static final String DB_KEY_PERFORMANCE_MENU_ANIMATIONS =
"perf_menu_animations";
54 static final String DB_KEY_PERFORMANCE_FONTS =
"perf_font_smoothing";
55 static final String DB_KEY_PERFORMANCE_COMPOSITION =
"perf_desktop_composition";
57 static final String DB_KEY_BOOKMARK_LABEL =
"label";
58 static final String DB_KEY_BOOKMARK_HOSTNAME =
"hostname";
59 static final String DB_KEY_BOOKMARK_USERNAME =
"username";
60 static final String DB_KEY_BOOKMARK_PASSWORD =
"password";
61 static final String DB_KEY_BOOKMARK_DOMAIN =
"domain";
62 static final String DB_KEY_BOOKMARK_PORT =
"port";
64 static final String DB_KEY_BOOKMARK_REDIRECT_SDCARD =
"redirect_sdcard";
65 static final String DB_KEY_BOOKMARK_REDIRECT_SOUND =
"redirect_sound";
66 static final String DB_KEY_BOOKMARK_REDIRECT_MICROPHONE =
"redirect_microphone";
67 static final String DB_KEY_BOOKMARK_SECURITY =
"security";
68 static final String DB_KEY_BOOKMARK_REMOTE_PROGRAM =
"remote_program";
69 static final String DB_KEY_BOOKMARK_WORK_DIR =
"work_dir";
70 static final String DB_KEY_BOOKMARK_ASYNC_CHANNEL =
"async_channel";
71 static final String DB_KEY_BOOKMARK_ASYNC_UPDATE =
"async_update";
72 static final String DB_KEY_BOOKMARK_CONSOLE_MODE =
"console_mode";
73 static final String DB_KEY_BOOKMARK_DEBUG_LEVEL =
"debug_level";
75 static final String DB_KEY_BOOKMARK_GW_ENABLE =
"enable_gateway_settings";
76 static final String DB_KEY_BOOKMARK_GW_HOSTNAME =
"gateway_hostname";
77 static final String DB_KEY_BOOKMARK_GW_PORT =
"gateway_port";
78 static final String DB_KEY_BOOKMARK_GW_USERNAME =
"gateway_username";
79 static final String DB_KEY_BOOKMARK_GW_PASSWORD =
"gateway_password";
80 static final String DB_KEY_BOOKMARK_GW_DOMAIN =
"gateway_domain";
81 static final String DB_KEY_BOOKMARK_3G_ENABLE =
"enable_3g_settings";
85 super(context, DB_NAME,
null, DB_VERSION);
88 private static List<String> GetColumns(SQLiteDatabase db, String tableName)
90 List<String> ar =
null;
91 try (Cursor c = db.rawQuery(
"SELECT * FROM " + tableName +
" LIMIT 1",
null))
95 ar =
new ArrayList<>(Arrays.asList(c.getColumnNames()));
100 Log.v(tableName, e.getMessage(), e);
106 private static String joinStrings(List<String> list, String delim)
108 StringBuilder buf =
new StringBuilder();
109 int num = list.size();
110 for (
int i = 0; i < num; i++)
114 buf.append((String)list.get(i));
116 return buf.toString();
119 private void backupTables(SQLiteDatabase db)
121 for (String table : DB_TABLES)
123 final String tmpTable = DB_BACKUP_PREFIX + table;
124 final String query =
"ALTER TABLE '" + table +
"' RENAME TO '" + tmpTable +
"'";
136 private void dropOldTables(SQLiteDatabase db)
138 for (String table : DB_TABLES)
140 final String tmpTable = DB_BACKUP_PREFIX + table;
141 final String query =
"DROP TABLE IF EXISTS '" + tmpTable +
"'";
146 private void createDB(SQLiteDatabase db)
148 final String sqlScreenSettings =
149 "CREATE TABLE IF NOT EXISTS " + DB_TABLE_SCREEN +
" (" + ID +
" INTEGER PRIMARY KEY, " +
150 DB_KEY_SCREEN_COLORS +
" INTEGER DEFAULT 16, " + DB_KEY_SCREEN_RESOLUTION +
151 " INTEGER DEFAULT 0, " + DB_KEY_SCREEN_WIDTH +
", " + DB_KEY_SCREEN_HEIGHT +
");";
153 db.execSQL(sqlScreenSettings);
155 final String sqlPerformanceFlags =
156 "CREATE TABLE IF NOT EXISTS " + DB_TABLE_PERFORMANCE +
" (" + ID +
157 " INTEGER PRIMARY KEY, " + DB_KEY_PERFORMANCE_RFX +
" INTEGER, " +
158 DB_KEY_PERFORMANCE_GFX +
" INTEGER, " + DB_KEY_PERFORMANCE_H264 +
" INTEGER, " +
159 DB_KEY_PERFORMANCE_WALLPAPER +
" INTEGER, " + DB_KEY_PERFORMANCE_THEME +
" INTEGER, " +
160 DB_KEY_PERFORMANCE_DRAG +
" INTEGER, " + DB_KEY_PERFORMANCE_MENU_ANIMATIONS +
161 " INTEGER, " + DB_KEY_PERFORMANCE_FONTS +
" INTEGER, " +
162 DB_KEY_PERFORMANCE_COMPOSITION +
" INTEGER);";
164 db.execSQL(sqlPerformanceFlags);
166 final String sqlManualBookmarks = getManualBookmarksCreationString();
167 db.execSQL(sqlManualBookmarks);
170 private void upgradeTables(SQLiteDatabase db)
172 for (String table : DB_TABLES)
174 final String tmpTable = DB_BACKUP_PREFIX + table;
176 final List<String> newColumns = GetColumns(db, table);
177 List<String> columns = GetColumns(db, tmpTable);
181 columns.retainAll(newColumns);
184 final String cols = joinStrings(columns,
",");
185 final String query = String.format(
"INSERT INTO %s (%s) SELECT %s from '%s'", table,
186 cols, cols, tmpTable);
192 private void downgradeTables(SQLiteDatabase db)
194 for (String table : DB_TABLES)
196 final String tmpTable = DB_BACKUP_PREFIX + table;
198 List<String> oldColumns = GetColumns(db, table);
199 final List<String> columns = GetColumns(db, tmpTable);
201 if (oldColumns !=
null)
203 oldColumns.retainAll(columns);
206 final String cols = joinStrings(oldColumns,
",");
207 final String query = String.format(
"INSERT INTO %s (%s) SELECT %s from '%s'", table,
208 cols, cols, tmpTable);
214 private List<String> getTableNames(SQLiteDatabase db)
216 final String query =
"SELECT name FROM sqlite_master WHERE type='table'";
217 Cursor cursor = db.rawQuery(query,
null);
218 List<String> list =
new ArrayList<>();
221 if (cursor.moveToFirst() && (cursor.getCount() > 0))
223 while (!cursor.isAfterLast())
225 final String name = cursor.getString(cursor.getColumnIndex(
"name"));
239 private void insertDefault(SQLiteDatabase db)
241 ContentValues screenValues =
new ContentValues();
242 screenValues.put(DB_KEY_SCREEN_COLORS, 32);
243 screenValues.put(DB_KEY_SCREEN_RESOLUTION, 1);
244 screenValues.put(DB_KEY_SCREEN_WIDTH, 1024);
245 screenValues.put(DB_KEY_SCREEN_HEIGHT, 768);
247 final long idScreen = db.insert(DB_TABLE_SCREEN,
null, screenValues);
248 final long idScreen3g = db.insert(DB_TABLE_SCREEN,
null, screenValues);
250 ContentValues performanceValues =
new ContentValues();
251 performanceValues.put(DB_KEY_PERFORMANCE_RFX, 1);
252 performanceValues.put(DB_KEY_PERFORMANCE_GFX, 1);
253 performanceValues.put(DB_KEY_PERFORMANCE_H264, 0);
254 performanceValues.put(DB_KEY_PERFORMANCE_WALLPAPER, 0);
255 performanceValues.put(DB_KEY_PERFORMANCE_THEME, 0);
256 performanceValues.put(DB_KEY_PERFORMANCE_DRAG, 0);
257 performanceValues.put(DB_KEY_PERFORMANCE_MENU_ANIMATIONS, 0);
258 performanceValues.put(DB_KEY_PERFORMANCE_FONTS, 0);
259 performanceValues.put(DB_KEY_PERFORMANCE_COMPOSITION, 0);
261 final long idPerformance = db.insert(DB_TABLE_PERFORMANCE,
null, performanceValues);
262 final long idPerformance3g = db.insert(DB_TABLE_PERFORMANCE,
null, performanceValues);
264 ContentValues bookmarkValues =
new ContentValues();
265 bookmarkValues.put(DB_KEY_BOOKMARK_LABEL,
"Test Server");
266 bookmarkValues.put(DB_KEY_BOOKMARK_HOSTNAME,
"testservice.afreerdp.com");
267 bookmarkValues.put(DB_KEY_BOOKMARK_USERNAME,
"");
268 bookmarkValues.put(DB_KEY_BOOKMARK_PASSWORD,
"");
269 bookmarkValues.put(DB_KEY_BOOKMARK_DOMAIN,
"");
270 bookmarkValues.put(DB_KEY_BOOKMARK_PORT,
"3389");
272 bookmarkValues.put(DB_KEY_SCREEN_SETTINGS, idScreen);
273 bookmarkValues.put(DB_KEY_SCREEN_SETTINGS_3G, idScreen3g);
274 bookmarkValues.put(DB_KEY_PERFORMANCE_FLAGS, idPerformance);
275 bookmarkValues.put(DB_KEY_PERFORMANCE_FLAGS_3G, idPerformance3g);
277 bookmarkValues.put(DB_KEY_BOOKMARK_REDIRECT_SDCARD, 0);
278 bookmarkValues.put(DB_KEY_BOOKMARK_REDIRECT_SOUND, 0);
279 bookmarkValues.put(DB_KEY_BOOKMARK_REDIRECT_MICROPHONE, 0);
280 bookmarkValues.put(DB_KEY_BOOKMARK_SECURITY, 0);
281 bookmarkValues.put(DB_KEY_BOOKMARK_REMOTE_PROGRAM,
"");
282 bookmarkValues.put(DB_KEY_BOOKMARK_WORK_DIR,
"");
283 bookmarkValues.put(DB_KEY_BOOKMARK_ASYNC_CHANNEL, 1);
284 bookmarkValues.put(DB_KEY_BOOKMARK_ASYNC_UPDATE, 1);
285 bookmarkValues.put(DB_KEY_BOOKMARK_CONSOLE_MODE, 0);
286 bookmarkValues.put(DB_KEY_BOOKMARK_DEBUG_LEVEL,
"INFO");
288 db.insert(DB_TABLE_BOOKMARK,
null, bookmarkValues);
291 @Override
public void onCreate(SQLiteDatabase db)
297 private String getManualBookmarksCreationString()
299 return (
"CREATE TABLE IF NOT EXISTS " + DB_TABLE_BOOKMARK +
" (" + ID +
300 " INTEGER PRIMARY KEY, " + DB_KEY_BOOKMARK_LABEL +
" TEXT NOT NULL, " +
301 DB_KEY_BOOKMARK_HOSTNAME +
" TEXT NOT NULL, " + DB_KEY_BOOKMARK_USERNAME +
302 " TEXT NOT NULL, " + DB_KEY_BOOKMARK_PASSWORD +
" TEXT, " + DB_KEY_BOOKMARK_DOMAIN +
303 " TEXT, " + DB_KEY_BOOKMARK_PORT +
" TEXT, " + DB_KEY_SCREEN_SETTINGS +
304 " INTEGER NOT NULL, " + DB_KEY_PERFORMANCE_FLAGS +
" INTEGER NOT NULL, "
306 + DB_KEY_BOOKMARK_GW_ENABLE +
" INTEGER DEFAULT 0, " + DB_KEY_BOOKMARK_GW_HOSTNAME +
307 " TEXT, " + DB_KEY_BOOKMARK_GW_PORT +
" INTEGER DEFAULT 443, " +
308 DB_KEY_BOOKMARK_GW_USERNAME +
" TEXT, " + DB_KEY_BOOKMARK_GW_PASSWORD +
" TEXT, " +
309 DB_KEY_BOOKMARK_GW_DOMAIN +
" TEXT, "
311 + DB_KEY_BOOKMARK_3G_ENABLE +
" INTEGER DEFAULT 0, " + DB_KEY_SCREEN_SETTINGS_3G +
312 " INTEGER NOT NULL, " + DB_KEY_PERFORMANCE_FLAGS_3G +
" INTEGER NOT NULL, " +
313 DB_KEY_BOOKMARK_REDIRECT_SDCARD +
" INTEGER DEFAULT 0, " +
314 DB_KEY_BOOKMARK_REDIRECT_SOUND +
" INTEGER DEFAULT 0, " +
315 DB_KEY_BOOKMARK_REDIRECT_MICROPHONE +
" INTEGER DEFAULT 0, " +
316 DB_KEY_BOOKMARK_SECURITY +
" INTEGER, " + DB_KEY_BOOKMARK_REMOTE_PROGRAM +
317 " TEXT, " + DB_KEY_BOOKMARK_WORK_DIR +
" TEXT, " + DB_KEY_BOOKMARK_ASYNC_CHANNEL +
318 " INTEGER DEFAULT 0, " + DB_KEY_BOOKMARK_ASYNC_UPDATE +
" INTEGER DEFAULT 0, " +
319 DB_KEY_BOOKMARK_CONSOLE_MODE +
" INTEGER, " + DB_KEY_BOOKMARK_DEBUG_LEVEL +
320 " TEXT DEFAULT 'INFO', "
322 +
"FOREIGN KEY(" + DB_KEY_SCREEN_SETTINGS +
") REFERENCES " + DB_TABLE_SCREEN +
324 +
"FOREIGN KEY(" + DB_KEY_PERFORMANCE_FLAGS +
") REFERENCES " +
325 DB_TABLE_PERFORMANCE +
"(" + ID +
"), "
326 +
"FOREIGN KEY(" + DB_KEY_SCREEN_SETTINGS_3G +
") REFERENCES " + DB_TABLE_SCREEN +
328 +
"FOREIGN KEY(" + DB_KEY_PERFORMANCE_FLAGS_3G +
") REFERENCES " +
329 DB_TABLE_PERFORMANCE +
"(" + ID +
") "
334 private void recreateDB(SQLiteDatabase db)
336 for (String table : DB_TABLES)
338 final String query =
"DROP TABLE IF EXISTS '" + table +
"'";
344 private void upgradeDB(SQLiteDatabase db)
346 db.beginTransaction();
355 db.setTransactionSuccessful();
364 private void downgradeDB(SQLiteDatabase db)
366 db.beginTransaction();
375 db.setTransactionSuccessful();
386 @Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion)
408 @Override
public void onDowngrade(SQLiteDatabase db,
int oldVersion,
int newVersion)