FreeRDP
QuickConnectHistoryGateway.java
1 /*
2  Quick connect history gateway
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.database.Cursor;
14 import android.database.SQLException;
15 import android.database.sqlite.SQLiteDatabase;
16 import android.database.sqlite.SQLiteException;
17 import android.database.sqlite.SQLiteOpenHelper;
18 import android.util.Log;
19 
20 import com.freerdp.freerdpcore.domain.BookmarkBase;
21 import com.freerdp.freerdpcore.domain.QuickConnectBookmark;
22 
23 import java.util.ArrayList;
24 
26 {
27  private final static String TAG = "QuickConnectHistoryGateway";
28  private SQLiteOpenHelper historyDB;
29 
30  public QuickConnectHistoryGateway(SQLiteOpenHelper historyDB)
31  {
32  this.historyDB = historyDB;
33  }
34 
35  public ArrayList<BookmarkBase> findHistory(String filter)
36  {
37  String[] column = { HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM };
38 
39  SQLiteDatabase db = getReadableDatabase();
40  String selection =
41  (filter.length() > 0)
42  ? (HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM + " LIKE '%" + filter + "%'")
43  : null;
44  Cursor cursor = db.query(HistoryDB.QUICK_CONNECT_TABLE_NAME, column, selection, null, null,
45  null, HistoryDB.QUICK_CONNECT_TABLE_COL_TIMESTAMP);
46 
47  ArrayList<BookmarkBase> result = new ArrayList<>(cursor.getCount());
48  if (cursor.moveToFirst())
49  {
50  do
51  {
52  String hostname =
53  cursor.getString(cursor.getColumnIndex(HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM));
55  bookmark.setLabel(hostname);
56  bookmark.setHostname(hostname);
57  result.add(bookmark);
58  } while (cursor.moveToNext());
59  }
60  cursor.close();
61  return result;
62  }
63 
64  public void addHistoryItem(String item)
65  {
66  String insertHistoryItem = "INSERT OR REPLACE INTO " + HistoryDB.QUICK_CONNECT_TABLE_NAME +
67  " (" + HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM + ", " +
68  HistoryDB.QUICK_CONNECT_TABLE_COL_TIMESTAMP + ") VALUES('" +
69  item + "', datetime('now'))";
70  SQLiteDatabase db = getWritableDatabase();
71  try
72  {
73  db.execSQL(insertHistoryItem);
74  }
75  catch (SQLException e)
76  {
77  Log.v(TAG, e.toString());
78  }
79  }
80 
81  public boolean historyItemExists(String item)
82  {
83  String[] column = { HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM };
84  SQLiteDatabase db = getReadableDatabase();
85  Cursor cursor = db.query(HistoryDB.QUICK_CONNECT_TABLE_NAME, column,
86  HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM + " = '" + item + "'", null,
87  null, null, null);
88  boolean exists = (cursor.getCount() == 1);
89  cursor.close();
90  return exists;
91  }
92 
93  public void removeHistoryItem(String hostname)
94  {
95  SQLiteDatabase db = getWritableDatabase();
96  db.delete(HistoryDB.QUICK_CONNECT_TABLE_NAME,
97  HistoryDB.QUICK_CONNECT_TABLE_COL_ITEM + " = '" + hostname + "'", null);
98  }
99 
100  // safety wrappers
101  // in case of getReadableDatabase it could happen that upgradeDB gets called which is
102  // a problem if the DB is only readable
103  private SQLiteDatabase getWritableDatabase()
104  {
105  return historyDB.getWritableDatabase();
106  }
107 
108  private SQLiteDatabase getReadableDatabase()
109  {
110  SQLiteDatabase db;
111  try
112  {
113  db = historyDB.getReadableDatabase();
114  }
115  catch (SQLiteException e)
116  {
117  db = historyDB.getWritableDatabase();
118  }
119  return db;
120  }
121 }