FreeRDP
BookmarkBaseGateway.java
1 /*
2  Helper class to access bookmark database
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.services;
12 
13 import android.content.ContentValues;
14 import android.database.Cursor;
15 import android.database.sqlite.SQLiteDatabase;
16 import android.database.sqlite.SQLiteException;
17 import android.database.sqlite.SQLiteOpenHelper;
18 import android.database.sqlite.SQLiteQueryBuilder;
19 import android.util.Log;
20 
21 import com.freerdp.freerdpcore.domain.BookmarkBase;
22 
23 import java.util.ArrayList;
24 
25 public abstract class BookmarkBaseGateway
26 {
27  private final static String TAG = "BookmarkBaseGateway";
28  private SQLiteOpenHelper bookmarkDB;
29 
30  private static final String JOIN_PREFIX = "join_";
31  private static final String KEY_BOOKMARK_ID = "bookmarkId";
32  private static final String KEY_SCREEN_COLORS = "screenColors";
33  private static final String KEY_SCREEN_COLORS_3G = "screenColors3G";
34  private static final String KEY_SCREEN_RESOLUTION = "screenResolution";
35  private static final String KEY_SCREEN_RESOLUTION_3G = "screenResolution3G";
36  private static final String KEY_SCREEN_WIDTH = "screenWidth";
37  private static final String KEY_SCREEN_WIDTH_3G = "screenWidth3G";
38  private static final String KEY_SCREEN_HEIGHT = "screenHeight";
39  private static final String KEY_SCREEN_HEIGHT_3G = "screenHeight3G";
40 
41  private static final String KEY_PERFORMANCE_RFX = "performanceRemoteFX";
42  private static final String KEY_PERFORMANCE_RFX_3G = "performanceRemoteFX3G";
43  private static final String KEY_PERFORMANCE_GFX = "performanceGfx";
44  private static final String KEY_PERFORMANCE_GFX_3G = "performanceGfx3G";
45  private static final String KEY_PERFORMANCE_H264 = "performanceGfxH264";
46  private static final String KEY_PERFORMANCE_H264_3G = "performanceGfxH2643G";
47  private static final String KEY_PERFORMANCE_WALLPAPER = "performanceWallpaper";
48  private static final String KEY_PERFORMANCE_WALLPAPER_3G = "performanceWallpaper3G";
49  private static final String KEY_PERFORMANCE_THEME = "performanceTheming";
50  private static final String KEY_PERFORMANCE_THEME_3G = "performanceTheming3G";
51 
52  private static final String KEY_PERFORMANCE_DRAG = "performanceFullWindowDrag";
53  private static final String KEY_PERFORMANCE_DRAG_3G = "performanceFullWindowDrag3G";
54  private static final String KEY_PERFORMANCE_MENU_ANIMATIONS = "performanceMenuAnimations";
55  private static final String KEY_PERFORMANCE_MENU_ANIMATIONS_3G = "performanceMenuAnimations3G";
56  private static final String KEY_PERFORMANCE_FONTS = "performanceFontSmoothing";
57  private static final String KEY_PERFORMANCE_FONTS_3G = "performanceFontSmoothing3G";
58  private static final String KEY_PERFORMANCE_COMPOSITION = "performanceDesktopComposition";
59  private static final String KEY_PERFORMANCE_COMPOSITION_3G = "performanceDesktopComposition3G";
60 
61  public BookmarkBaseGateway(SQLiteOpenHelper bookmarkDB)
62  {
63  this.bookmarkDB = bookmarkDB;
64  }
65 
66  protected abstract BookmarkBase createBookmark();
67 
68  protected abstract String getBookmarkTableName();
69 
70  protected abstract void addBookmarkSpecificColumns(ArrayList<String> columns);
71 
72  protected abstract void addBookmarkSpecificColumns(BookmarkBase bookmark,
73  ContentValues columns);
74 
75  protected abstract void readBookmarkSpecificColumns(BookmarkBase bookmark, Cursor cursor);
76 
77  public void insert(BookmarkBase bookmark)
78  {
79  // begin transaction
80  SQLiteDatabase db = getWritableDatabase();
81  db.beginTransaction();
82 
83  long rowid;
84  ContentValues values = new ContentValues();
85  values.put(BookmarkDB.DB_KEY_BOOKMARK_LABEL, bookmark.getLabel());
86  values.put(BookmarkDB.DB_KEY_BOOKMARK_USERNAME, bookmark.getUsername());
87  values.put(BookmarkDB.DB_KEY_BOOKMARK_PASSWORD, bookmark.getPassword());
88  values.put(BookmarkDB.DB_KEY_BOOKMARK_DOMAIN, bookmark.getDomain());
89  // insert screen and performance settings
90  rowid = insertScreenSettings(db, bookmark.getScreenSettings());
91  values.put(BookmarkDB.DB_KEY_SCREEN_SETTINGS, rowid);
92  rowid = insertPerformanceFlags(db, bookmark.getPerformanceFlags());
93  values.put(BookmarkDB.DB_KEY_PERFORMANCE_FLAGS, rowid);
94 
95  // advanced settings
96  values.put(BookmarkDB.DB_KEY_BOOKMARK_3G_ENABLE,
97  bookmark.getAdvancedSettings().getEnable3GSettings());
98  // insert 3G screen and 3G performance settings
99  rowid = insertScreenSettings(db, bookmark.getAdvancedSettings().getScreen3G());
100  values.put(BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G, rowid);
101  rowid = insertPerformanceFlags(db, bookmark.getAdvancedSettings().getPerformance3G());
102  values.put(BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G, rowid);
103  values.put(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SDCARD,
104  bookmark.getAdvancedSettings().getRedirectSDCard());
105  values.put(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SOUND,
106  bookmark.getAdvancedSettings().getRedirectSound());
107  values.put(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_MICROPHONE,
108  bookmark.getAdvancedSettings().getRedirectMicrophone());
109  values.put(BookmarkDB.DB_KEY_BOOKMARK_SECURITY,
110  bookmark.getAdvancedSettings().getSecurity());
111  values.put(BookmarkDB.DB_KEY_BOOKMARK_CONSOLE_MODE,
112  bookmark.getAdvancedSettings().getConsoleMode());
113  values.put(BookmarkDB.DB_KEY_BOOKMARK_REMOTE_PROGRAM,
114  bookmark.getAdvancedSettings().getRemoteProgram());
115  values.put(BookmarkDB.DB_KEY_BOOKMARK_WORK_DIR,
116  bookmark.getAdvancedSettings().getWorkDir());
117 
118  values.put(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_CHANNEL,
119  bookmark.getDebugSettings().getAsyncChannel());
120  values.put(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_UPDATE,
121  bookmark.getDebugSettings().getAsyncUpdate());
122  values.put(BookmarkDB.DB_KEY_BOOKMARK_DEBUG_LEVEL,
123  bookmark.getDebugSettings().getDebugLevel());
124 
125  // add any special columns
126  addBookmarkSpecificColumns(bookmark, values);
127 
128  // insert bookmark and end transaction
129  db.insertOrThrow(getBookmarkTableName(), null, values);
130  db.setTransactionSuccessful();
131  db.endTransaction();
132  }
133 
134  public boolean update(BookmarkBase bookmark)
135  {
136  // start a transaction
137  SQLiteDatabase db = getWritableDatabase();
138  db.beginTransaction();
139 
140  // bookmark settings
141  ContentValues values = new ContentValues();
142  values.put(BookmarkDB.DB_KEY_BOOKMARK_LABEL, bookmark.getLabel());
143  values.put(BookmarkDB.DB_KEY_BOOKMARK_USERNAME, bookmark.getUsername());
144  values.put(BookmarkDB.DB_KEY_BOOKMARK_PASSWORD, bookmark.getPassword());
145  values.put(BookmarkDB.DB_KEY_BOOKMARK_DOMAIN, bookmark.getDomain());
146  // update screen and performance settings settings
147  updateScreenSettings(db, bookmark);
148  updatePerformanceFlags(db, bookmark);
149 
150  // advanced settings
151  values.put(BookmarkDB.DB_KEY_BOOKMARK_3G_ENABLE,
152  bookmark.getAdvancedSettings().getEnable3GSettings());
153  // update 3G screen and 3G performance settings settings
154  updateScreenSettings3G(db, bookmark);
155  updatePerformanceFlags3G(db, bookmark);
156  values.put(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SDCARD,
157  bookmark.getAdvancedSettings().getRedirectSDCard());
158  values.put(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SOUND,
159  bookmark.getAdvancedSettings().getRedirectSound());
160  values.put(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_MICROPHONE,
161  bookmark.getAdvancedSettings().getRedirectMicrophone());
162  values.put(BookmarkDB.DB_KEY_BOOKMARK_SECURITY,
163  bookmark.getAdvancedSettings().getSecurity());
164  values.put(BookmarkDB.DB_KEY_BOOKMARK_CONSOLE_MODE,
165  bookmark.getAdvancedSettings().getConsoleMode());
166  values.put(BookmarkDB.DB_KEY_BOOKMARK_REMOTE_PROGRAM,
167  bookmark.getAdvancedSettings().getRemoteProgram());
168  values.put(BookmarkDB.DB_KEY_BOOKMARK_WORK_DIR,
169  bookmark.getAdvancedSettings().getWorkDir());
170 
171  values.put(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_CHANNEL,
172  bookmark.getDebugSettings().getAsyncChannel());
173  values.put(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_UPDATE,
174  bookmark.getDebugSettings().getAsyncUpdate());
175  values.put(BookmarkDB.DB_KEY_BOOKMARK_DEBUG_LEVEL,
176  bookmark.getDebugSettings().getDebugLevel());
177 
178  addBookmarkSpecificColumns(bookmark, values);
179 
180  // update bookmark
181  boolean res = (db.update(getBookmarkTableName(), values,
182  BookmarkDB.ID + " = " + bookmark.getId(), null) == 1);
183 
184  // commit
185  db.setTransactionSuccessful();
186  db.endTransaction();
187 
188  return res;
189  }
190 
191  public void delete(long id)
192  {
193  SQLiteDatabase db = getWritableDatabase();
194  db.delete(getBookmarkTableName(), BookmarkDB.ID + " = " + id, null);
195  }
196 
197  public BookmarkBase findById(long id)
198  {
199  Cursor cursor =
200  queryBookmarks(getBookmarkTableName() + "." + BookmarkDB.ID + " = " + id, null);
201  if (cursor.getCount() == 0)
202  {
203  cursor.close();
204  return null;
205  }
206 
207  cursor.moveToFirst();
208  BookmarkBase bookmark = getBookmarkFromCursor(cursor);
209  cursor.close();
210  return bookmark;
211  }
212 
213  public BookmarkBase findByLabel(String label)
214  {
215  Cursor cursor = queryBookmarks(BookmarkDB.DB_KEY_BOOKMARK_LABEL + " = '" + label + "'",
216  BookmarkDB.DB_KEY_BOOKMARK_LABEL);
217  if (cursor.getCount() > 1)
218  Log.e(TAG, "More than one bookmark with the same label found!");
219 
220  BookmarkBase bookmark = null;
221  if (cursor.moveToFirst() && (cursor.getCount() > 0))
222  bookmark = getBookmarkFromCursor(cursor);
223 
224  cursor.close();
225  return bookmark;
226  }
227 
228  public ArrayList<BookmarkBase> findByLabelLike(String pattern)
229  {
230  Cursor cursor =
231  queryBookmarks(BookmarkDB.DB_KEY_BOOKMARK_LABEL + " LIKE '%" + pattern + "%'",
232  BookmarkDB.DB_KEY_BOOKMARK_LABEL);
233  ArrayList<BookmarkBase> bookmarks = new ArrayList<>(cursor.getCount());
234 
235  if (cursor.moveToFirst() && (cursor.getCount() > 0))
236  {
237  do
238  {
239  bookmarks.add(getBookmarkFromCursor(cursor));
240  } while (cursor.moveToNext());
241  }
242 
243  cursor.close();
244  return bookmarks;
245  }
246 
247  public ArrayList<BookmarkBase> findAll()
248  {
249  Cursor cursor = queryBookmarks(null, BookmarkDB.DB_KEY_BOOKMARK_LABEL);
250  final int count = cursor.getCount();
251  ArrayList<BookmarkBase> bookmarks = new ArrayList<>(count);
252 
253  if (cursor.moveToFirst() && (count > 0))
254  {
255  do
256  {
257  bookmarks.add(getBookmarkFromCursor(cursor));
258  } while (cursor.moveToNext());
259  }
260 
261  cursor.close();
262  return bookmarks;
263  }
264 
265  protected Cursor queryBookmarks(String whereClause, String orderBy)
266  {
267  // create tables string
268  final String ID = BookmarkDB.ID;
269  final String tables =
270  BookmarkDB.DB_TABLE_BOOKMARK + " INNER JOIN " + BookmarkDB.DB_TABLE_SCREEN + " AS " +
271  JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS + " ON " + JOIN_PREFIX +
272  BookmarkDB.DB_KEY_SCREEN_SETTINGS + "." + ID + " = " + BookmarkDB.DB_TABLE_BOOKMARK +
273  "." + BookmarkDB.DB_KEY_SCREEN_SETTINGS + " INNER JOIN " +
274  BookmarkDB.DB_TABLE_PERFORMANCE + " AS " + JOIN_PREFIX +
275  BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + " ON " + JOIN_PREFIX +
276  BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." + ID + " = " + BookmarkDB.DB_TABLE_BOOKMARK +
277  "." + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + " INNER JOIN " +
278  BookmarkDB.DB_TABLE_SCREEN + " AS " + JOIN_PREFIX +
279  BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + " ON " + JOIN_PREFIX +
280  BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + "." + ID + " = " + BookmarkDB.DB_TABLE_BOOKMARK +
281  "." + BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + " INNER JOIN " +
282  BookmarkDB.DB_TABLE_PERFORMANCE + " AS " + JOIN_PREFIX +
283  BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + " ON " + JOIN_PREFIX +
284  BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." + ID + " = " +
285  BookmarkDB.DB_TABLE_BOOKMARK + "." + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G;
286 
287  // create columns list
288  ArrayList<String> columns = new ArrayList<>();
289  addBookmarkColumns(columns);
290  addScreenSettingsColumns(columns);
291  addPerformanceFlagsColumns(columns);
292  addScreenSettings3GColumns(columns);
293  addPerformanceFlags3GColumns(columns);
294 
295  String[] cols = new String[columns.size()];
296  columns.toArray(cols);
297 
298  SQLiteDatabase db = getReadableDatabase();
299  final String query = SQLiteQueryBuilder.buildQueryString(false, tables, cols, whereClause,
300  null, null, orderBy, null);
301  return db.rawQuery(query, null);
302  }
303 
304  private void addBookmarkColumns(ArrayList<String> columns)
305  {
306  columns.add(getBookmarkTableName() + "." + BookmarkDB.ID + " " + KEY_BOOKMARK_ID);
307  columns.add(BookmarkDB.DB_KEY_BOOKMARK_LABEL);
308  columns.add(BookmarkDB.DB_KEY_BOOKMARK_USERNAME);
309  columns.add(BookmarkDB.DB_KEY_BOOKMARK_PASSWORD);
310  columns.add(BookmarkDB.DB_KEY_BOOKMARK_DOMAIN);
311 
312  // advanced settings
313  columns.add(BookmarkDB.DB_KEY_BOOKMARK_3G_ENABLE);
314  columns.add(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SDCARD);
315  columns.add(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SOUND);
316  columns.add(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_MICROPHONE);
317  columns.add(BookmarkDB.DB_KEY_BOOKMARK_SECURITY);
318  columns.add(BookmarkDB.DB_KEY_BOOKMARK_CONSOLE_MODE);
319  columns.add(BookmarkDB.DB_KEY_BOOKMARK_REMOTE_PROGRAM);
320  columns.add(BookmarkDB.DB_KEY_BOOKMARK_WORK_DIR);
321 
322  // debug settings
323  columns.add(BookmarkDB.DB_KEY_BOOKMARK_DEBUG_LEVEL);
324  columns.add(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_CHANNEL);
325  columns.add(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_UPDATE);
326 
327  addBookmarkSpecificColumns(columns);
328  }
329 
330  private void addScreenSettingsColumns(ArrayList<String> columns)
331  {
332  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS + "." +
333  BookmarkDB.DB_KEY_SCREEN_COLORS + " as " + KEY_SCREEN_COLORS);
334  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS + "." +
335  BookmarkDB.DB_KEY_SCREEN_RESOLUTION + " as " + KEY_SCREEN_RESOLUTION);
336  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS + "." +
337  BookmarkDB.DB_KEY_SCREEN_WIDTH + " as " + KEY_SCREEN_WIDTH);
338  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS + "." +
339  BookmarkDB.DB_KEY_SCREEN_HEIGHT + " as " + KEY_SCREEN_HEIGHT);
340  }
341 
342  private void addPerformanceFlagsColumns(ArrayList<String> columns)
343  {
344  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
345  BookmarkDB.DB_KEY_PERFORMANCE_RFX + " as " + KEY_PERFORMANCE_RFX);
346  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
347  BookmarkDB.DB_KEY_PERFORMANCE_GFX + " as " + KEY_PERFORMANCE_GFX);
348  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
349  BookmarkDB.DB_KEY_PERFORMANCE_H264 + " as " + KEY_PERFORMANCE_H264);
350  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
351  BookmarkDB.DB_KEY_PERFORMANCE_WALLPAPER + " as " + KEY_PERFORMANCE_WALLPAPER);
352  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
353  BookmarkDB.DB_KEY_PERFORMANCE_THEME + " as " + KEY_PERFORMANCE_THEME);
354  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
355  BookmarkDB.DB_KEY_PERFORMANCE_DRAG + " as " + KEY_PERFORMANCE_DRAG);
356  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
357  BookmarkDB.DB_KEY_PERFORMANCE_MENU_ANIMATIONS + " as " +
358  KEY_PERFORMANCE_MENU_ANIMATIONS);
359  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
360  BookmarkDB.DB_KEY_PERFORMANCE_FONTS + " as " + KEY_PERFORMANCE_FONTS);
361  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + "." +
362  BookmarkDB.DB_KEY_PERFORMANCE_COMPOSITION + " " + KEY_PERFORMANCE_COMPOSITION);
363  }
364 
365  private void addScreenSettings3GColumns(ArrayList<String> columns)
366  {
367  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + "." +
368  BookmarkDB.DB_KEY_SCREEN_COLORS + " as " + KEY_SCREEN_COLORS_3G);
369  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + "." +
370  BookmarkDB.DB_KEY_SCREEN_RESOLUTION + " as " + KEY_SCREEN_RESOLUTION_3G);
371  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + "." +
372  BookmarkDB.DB_KEY_SCREEN_WIDTH + " as " + KEY_SCREEN_WIDTH_3G);
373  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + "." +
374  BookmarkDB.DB_KEY_SCREEN_HEIGHT + " as " + KEY_SCREEN_HEIGHT_3G);
375  }
376 
377  private void addPerformanceFlags3GColumns(ArrayList<String> columns)
378  {
379  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
380  BookmarkDB.DB_KEY_PERFORMANCE_RFX + " as " + KEY_PERFORMANCE_RFX_3G);
381  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
382  BookmarkDB.DB_KEY_PERFORMANCE_GFX + " as " + KEY_PERFORMANCE_GFX_3G);
383  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
384  BookmarkDB.DB_KEY_PERFORMANCE_H264 + " as " + KEY_PERFORMANCE_H264_3G);
385  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
386  BookmarkDB.DB_KEY_PERFORMANCE_WALLPAPER + " as " +
387  KEY_PERFORMANCE_WALLPAPER_3G);
388  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
389  BookmarkDB.DB_KEY_PERFORMANCE_THEME + " as " + KEY_PERFORMANCE_THEME_3G);
390  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
391  BookmarkDB.DB_KEY_PERFORMANCE_DRAG + " as " + KEY_PERFORMANCE_DRAG_3G);
392  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
393  BookmarkDB.DB_KEY_PERFORMANCE_MENU_ANIMATIONS + " as " +
394  KEY_PERFORMANCE_MENU_ANIMATIONS_3G);
395  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
396  BookmarkDB.DB_KEY_PERFORMANCE_FONTS + " as " + KEY_PERFORMANCE_FONTS_3G);
397  columns.add(JOIN_PREFIX + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + "." +
398  BookmarkDB.DB_KEY_PERFORMANCE_COMPOSITION + " " +
399  KEY_PERFORMANCE_COMPOSITION_3G);
400  }
401 
402  protected BookmarkBase getBookmarkFromCursor(Cursor cursor)
403  {
404  BookmarkBase bookmark = createBookmark();
405  bookmark.setId(cursor.getLong(cursor.getColumnIndex(KEY_BOOKMARK_ID)));
406  bookmark.setLabel(
407  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_LABEL)));
408  bookmark.setUsername(
409  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_USERNAME)));
410  bookmark.setPassword(
411  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_PASSWORD)));
412  bookmark.setDomain(
413  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_DOMAIN)));
414  readScreenSettings(bookmark, cursor);
415  readPerformanceFlags(bookmark, cursor);
416 
417  // advanced settings
418  bookmark.getAdvancedSettings().setEnable3GSettings(
419  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_3G_ENABLE)) != 0);
420  readScreenSettings3G(bookmark, cursor);
421  readPerformanceFlags3G(bookmark, cursor);
422  bookmark.getAdvancedSettings().setRedirectSDCard(
423  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SDCARD)) != 0);
424  bookmark.getAdvancedSettings().setRedirectSound(
425  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_SOUND)));
426  bookmark.getAdvancedSettings().setRedirectMicrophone(
427  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_REDIRECT_MICROPHONE)) !=
428  0);
429  bookmark.getAdvancedSettings().setSecurity(
430  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_SECURITY)));
431  bookmark.getAdvancedSettings().setConsoleMode(
432  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_CONSOLE_MODE)) != 0);
433  bookmark.getAdvancedSettings().setRemoteProgram(
434  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_REMOTE_PROGRAM)));
435  bookmark.getAdvancedSettings().setWorkDir(
436  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_WORK_DIR)));
437 
438  bookmark.getDebugSettings().setAsyncChannel(
439  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_CHANNEL)) == 1);
440  bookmark.getDebugSettings().setAsyncUpdate(
441  cursor.getInt(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_ASYNC_UPDATE)) == 1);
442  bookmark.getDebugSettings().setDebugLevel(
443  cursor.getString(cursor.getColumnIndex(BookmarkDB.DB_KEY_BOOKMARK_DEBUG_LEVEL)));
444 
445  readBookmarkSpecificColumns(bookmark, cursor);
446 
447  return bookmark;
448  }
449 
450  private void readScreenSettings(BookmarkBase bookmark, Cursor cursor)
451  {
452  BookmarkBase.ScreenSettings screenSettings = bookmark.getScreenSettings();
453  screenSettings.setColors(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_COLORS)));
454  screenSettings.setResolution(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_RESOLUTION)));
455  screenSettings.setWidth(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_WIDTH)));
456  screenSettings.setHeight(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_HEIGHT)));
457  }
458 
459  private void readPerformanceFlags(BookmarkBase bookmark, Cursor cursor)
460  {
461  BookmarkBase.PerformanceFlags perfFlags = bookmark.getPerformanceFlags();
462  perfFlags.setRemoteFX(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_RFX)) != 0);
463  perfFlags.setGfx(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_GFX)) != 0);
464  perfFlags.setH264(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_H264)) != 0);
465  perfFlags.setWallpaper(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_WALLPAPER)) !=
466  0);
467  perfFlags.setTheming(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_THEME)) != 0);
468  perfFlags.setFullWindowDrag(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_DRAG)) !=
469  0);
470  perfFlags.setMenuAnimations(
471  cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_MENU_ANIMATIONS)) != 0);
472  perfFlags.setFontSmoothing(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_FONTS)) !=
473  0);
474  perfFlags.setDesktopComposition(
475  cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_COMPOSITION)) != 0);
476  }
477 
478  private void readScreenSettings3G(BookmarkBase bookmark, Cursor cursor)
479  {
480  BookmarkBase.ScreenSettings screenSettings = bookmark.getAdvancedSettings().getScreen3G();
481  screenSettings.setColors(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_COLORS_3G)));
482  screenSettings.setResolution(
483  cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_RESOLUTION_3G)));
484  screenSettings.setWidth(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_WIDTH_3G)));
485  screenSettings.setHeight(cursor.getInt(cursor.getColumnIndex(KEY_SCREEN_HEIGHT_3G)));
486  }
487 
488  private void readPerformanceFlags3G(BookmarkBase bookmark, Cursor cursor)
489  {
490  BookmarkBase.PerformanceFlags perfFlags = bookmark.getAdvancedSettings().getPerformance3G();
491  perfFlags.setRemoteFX(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_RFX_3G)) != 0);
492  perfFlags.setGfx(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_GFX_3G)) != 0);
493  perfFlags.setH264(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_H264_3G)) != 0);
494  perfFlags.setWallpaper(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_WALLPAPER_3G)) !=
495  0);
496  perfFlags.setTheming(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_THEME_3G)) != 0);
497  perfFlags.setFullWindowDrag(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_DRAG_3G)) !=
498  0);
499  perfFlags.setMenuAnimations(
500  cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_MENU_ANIMATIONS_3G)) != 0);
501  perfFlags.setFontSmoothing(cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_FONTS_3G)) !=
502  0);
503  perfFlags.setDesktopComposition(
504  cursor.getInt(cursor.getColumnIndex(KEY_PERFORMANCE_COMPOSITION_3G)) != 0);
505  }
506 
507  private void fillScreenSettingsContentValues(BookmarkBase.ScreenSettings settings,
508  ContentValues values)
509  {
510  values.put(BookmarkDB.DB_KEY_SCREEN_COLORS, settings.getColors());
511  values.put(BookmarkDB.DB_KEY_SCREEN_RESOLUTION, settings.getResolution());
512  values.put(BookmarkDB.DB_KEY_SCREEN_WIDTH, settings.getWidth());
513  values.put(BookmarkDB.DB_KEY_SCREEN_HEIGHT, settings.getHeight());
514  }
515 
516  private void fillPerformanceFlagsContentValues(BookmarkBase.PerformanceFlags perfFlags,
517  ContentValues values)
518  {
519  values.put(BookmarkDB.DB_KEY_PERFORMANCE_RFX, perfFlags.getRemoteFX());
520  values.put(BookmarkDB.DB_KEY_PERFORMANCE_GFX, perfFlags.getGfx());
521  values.put(BookmarkDB.DB_KEY_PERFORMANCE_H264, perfFlags.getH264());
522  values.put(BookmarkDB.DB_KEY_PERFORMANCE_WALLPAPER, perfFlags.getWallpaper());
523  values.put(BookmarkDB.DB_KEY_PERFORMANCE_THEME, perfFlags.getTheming());
524  values.put(BookmarkDB.DB_KEY_PERFORMANCE_DRAG, perfFlags.getFullWindowDrag());
525  values.put(BookmarkDB.DB_KEY_PERFORMANCE_MENU_ANIMATIONS, perfFlags.getMenuAnimations());
526  values.put(BookmarkDB.DB_KEY_PERFORMANCE_FONTS, perfFlags.getFontSmoothing());
527  values.put(BookmarkDB.DB_KEY_PERFORMANCE_COMPOSITION, perfFlags.getDesktopComposition());
528  }
529 
530  private long insertScreenSettings(SQLiteDatabase db, BookmarkBase.ScreenSettings settings)
531  {
532  ContentValues values = new ContentValues();
533  fillScreenSettingsContentValues(settings, values);
534  return db.insertOrThrow(BookmarkDB.DB_TABLE_SCREEN, null, values);
535  }
536 
537  private boolean updateScreenSettings(SQLiteDatabase db, BookmarkBase bookmark)
538  {
539  ContentValues values = new ContentValues();
540  fillScreenSettingsContentValues(bookmark.getScreenSettings(), values);
541  String whereClause = BookmarkDB.ID + " IN "
542  + "(SELECT " + BookmarkDB.DB_KEY_SCREEN_SETTINGS + " FROM " +
543  getBookmarkTableName() + " WHERE " + BookmarkDB.ID + " = " +
544  bookmark.getId() + ");";
545  return (db.update(BookmarkDB.DB_TABLE_SCREEN, values, whereClause, null) == 1);
546  }
547 
548  private boolean updateScreenSettings3G(SQLiteDatabase db, BookmarkBase bookmark)
549  {
550  ContentValues values = new ContentValues();
551  fillScreenSettingsContentValues(bookmark.getAdvancedSettings().getScreen3G(), values);
552  String whereClause = BookmarkDB.ID + " IN "
553  + "(SELECT " + BookmarkDB.DB_KEY_SCREEN_SETTINGS_3G + " FROM " +
554  getBookmarkTableName() + " WHERE " + BookmarkDB.ID + " = " +
555  bookmark.getId() + ");";
556  return (db.update(BookmarkDB.DB_TABLE_SCREEN, values, whereClause, null) == 1);
557  }
558 
559  private long insertPerformanceFlags(SQLiteDatabase db, BookmarkBase.PerformanceFlags perfFlags)
560  {
561  ContentValues values = new ContentValues();
562  fillPerformanceFlagsContentValues(perfFlags, values);
563  return db.insertOrThrow(BookmarkDB.DB_TABLE_PERFORMANCE, null, values);
564  }
565 
566  private boolean updatePerformanceFlags(SQLiteDatabase db, BookmarkBase bookmark)
567  {
568  ContentValues values = new ContentValues();
569  fillPerformanceFlagsContentValues(bookmark.getPerformanceFlags(), values);
570  String whereClause = BookmarkDB.ID + " IN "
571  + "(SELECT " + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS + " FROM " +
572  getBookmarkTableName() + " WHERE " + BookmarkDB.ID + " = " +
573  bookmark.getId() + ");";
574  return (db.update(BookmarkDB.DB_TABLE_PERFORMANCE, values, whereClause, null) == 1);
575  }
576 
577  private boolean updatePerformanceFlags3G(SQLiteDatabase db, BookmarkBase bookmark)
578  {
579  ContentValues values = new ContentValues();
580  fillPerformanceFlagsContentValues(bookmark.getAdvancedSettings().getPerformance3G(),
581  values);
582  String whereClause = BookmarkDB.ID + " IN "
583  + "(SELECT " + BookmarkDB.DB_KEY_PERFORMANCE_FLAGS_3G + " FROM " +
584  getBookmarkTableName() + " WHERE " + BookmarkDB.ID + " = " +
585  bookmark.getId() + ");";
586  return (db.update(BookmarkDB.DB_TABLE_PERFORMANCE, values, whereClause, null) == 1);
587  }
588 
589  // safety wrappers
590  // in case of getReadableDatabase it could happen that upgradeDB gets called which is
591  // a problem if the DB is only readable
592  private SQLiteDatabase getWritableDatabase()
593  {
594  return bookmarkDB.getWritableDatabase();
595  }
596 
597  private SQLiteDatabase getReadableDatabase()
598  {
599  SQLiteDatabase db;
600  try
601  {
602  db = bookmarkDB.getReadableDatabase();
603  }
604  catch (SQLiteException e)
605  {
606  db = bookmarkDB.getWritableDatabase();
607  }
608  return db;
609  }
610 }