comparison app/src/main/java/com/five_ten_sg/connectbot/util/HostDatabase.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/util/HostDatabase.java@91a31873c42a
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * ConnectBot: simple, powerful, open-source SSH client for Android
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.five_ten_sg.connectbot.util;
19
20 import java.nio.charset.Charset;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import com.five_ten_sg.connectbot.bean.HostBean;
28 import com.five_ten_sg.connectbot.bean.PortForwardBean;
29 import android.content.ContentValues;
30 import android.content.Context;
31 import android.database.Cursor;
32 import android.database.sqlite.SQLiteDatabase;
33 import android.database.sqlite.SQLiteException;
34 import android.util.Log;
35
36 import ch.ethz.ssh2.KnownHosts;
37
38 /**
39 * Contains information about various SSH hosts, include public hostkey if known
40 * from previous sessions.
41 *
42 * @author jsharkey
43 */
44 public class HostDatabase extends RobustSQLiteOpenHelper {
45
46 public final static String TAG = "ConnectBot.HostDatabase";
47
48 public final static String DB_NAME = "hosts";
49 public final static int DB_VERSION = 28;
50
51 public final static String TABLE_HOSTS = "hosts";
52 public final static String FIELD_HOST_NICKNAME = "nickname";
53 public final static String FIELD_HOST_PROTOCOL = "protocol";
54 public final static String FIELD_HOST_USERNAME = "username";
55 public final static String FIELD_HOST_HOSTNAME = "hostname";
56 public final static String FIELD_HOST_PORT = "port";
57 public final static String FIELD_HOST_HOSTKEYALGO = "hostkeyalgo";
58 public final static String FIELD_HOST_HOSTKEY = "hostkey";
59 public final static String FIELD_HOST_LASTCONNECT = "lastconnect";
60 public final static String FIELD_HOST_COLOR = "color";
61 public final static String FIELD_HOST_USEKEYS = "usekeys";
62 public final static String FIELD_HOST_USEAUTHAGENT = "useauthagent";
63 public final static String FIELD_HOST_POSTLOGIN = "postlogin";
64 public final static String FIELD_HOST_PUBKEYID = "pubkeyid";
65 public final static String FIELD_HOST_WANTSESSION = "wantsession";
66 public final static String FIELD_HOST_DELKEY = "delkey";
67 public final static String FIELD_HOST_FONTSIZE = "fontsize";
68 public final static String FIELD_HOST_FIXEDSIZE = "fixed_size";
69 public final static String FIELD_HOST_FIXEDWIDTH = "fixed_width";
70 public final static String FIELD_HOST_FIXEDHEIGHT = "fixed_height";
71 public final static String FIELD_HOST_COMPRESSION = "compression";
72 public final static String FIELD_HOST_HTTPPROXY = "httpproxy";
73 public final static String FIELD_HOST_ENCODING = "encoding";
74 public final static String FIELD_HOST_STAYCONNECTED = "stayconnected";
75 public final static String FIELD_HOST_WANTX11FORWARD = "wantx11forward";
76 public final static String FIELD_HOST_X11HOST = "x11host";
77 public final static String FIELD_HOST_X11PORT = "x11port";
78 public final static String FIELD_HOST_MONITOR = "monitor";
79 public final static String FIELD_HOST_EMULATION = "emulation";
80 public final static String FIELD_HOST_ENCRYPTION5250 = "encryption5250";
81 public final static String FIELD_HOST_LIBRARY5250 = "library5250";
82 public final static String FIELD_HOST_MENU5250 = "menu5250";
83 public final static String FIELD_HOST_PROGRAM5250 = "program5250";
84 public final static String CATEGORY_5250 = "5250";
85 public final static String CATEGORY_X11 = "x11";
86
87 public final static String TABLE_PORTFORWARDS = "portforwards";
88 public final static String FIELD_PORTFORWARD_HOSTID = "hostid";
89 public final static String FIELD_PORTFORWARD_NICKNAME = "nickname";
90 public final static String FIELD_PORTFORWARD_TYPE = "type";
91 public final static String FIELD_PORTFORWARD_SOURCEPORT = "sourceport";
92 public final static String FIELD_PORTFORWARD_DESTADDR = "destaddr";
93 public final static String FIELD_PORTFORWARD_DESTPORT = "destport";
94
95 public final static String TABLE_COLORS = "colors";
96 public final static String FIELD_COLOR_SCHEME = "scheme";
97 public final static String FIELD_COLOR_NUMBER = "number";
98 public final static String FIELD_COLOR_VALUE = "value";
99
100 public final static String TABLE_COLOR_DEFAULTS = "colorDefaults";
101 public final static String FIELD_COLOR_FG = "fg";
102 public final static String FIELD_COLOR_BG = "bg";
103
104 public final static int DEFAULT_FG_COLOR = 7;
105 public final static int DEFAULT_BG_COLOR = 0;
106
107 public final static String COLOR_RED = "red";
108 public final static String COLOR_GREEN = "green";
109 public final static String COLOR_BLUE = "blue";
110 public final static String COLOR_GRAY = "gray";
111
112 public final static String PORTFORWARD_LOCAL = "local";
113 public final static String PORTFORWARD_REMOTE = "remote";
114 public final static String PORTFORWARD_DYNAMIC4 = "dynamic4";
115 public final static String PORTFORWARD_DYNAMIC5 = "dynamic5";
116
117 public final static String DELKEY_DEL = "del";
118 public final static String DELKEY_BACKSPACE = "backspace";
119
120 public final static String AUTHAGENT_NO = "no";
121 public final static String AUTHAGENT_CONFIRM = "confirm";
122 public final static String AUTHAGENT_YES = "yes";
123
124 public final static String ENCODING_DEFAULT = Charset.defaultCharset().name();
125
126 public final static String X11HOST_DEFAULT = "localhost";
127 public final static int X11PORT_DEFAULT = 6000;
128
129 public final static long PUBKEYID_NEVER = -2;
130 public final static long PUBKEYID_ANY = -1;
131
132 public static final int DEFAULT_COLOR_SCHEME = 0;
133
134 // Table creation strings
135 public static final String CREATE_TABLE_COLOR_DEFAULTS =
136 "CREATE TABLE " + TABLE_COLOR_DEFAULTS
137 + " (" + FIELD_COLOR_SCHEME + " INTEGER NOT NULL, "
138 + FIELD_COLOR_FG + " INTEGER NOT NULL DEFAULT " + DEFAULT_FG_COLOR + ", "
139 + FIELD_COLOR_BG + " INTEGER NOT NULL DEFAULT " + DEFAULT_BG_COLOR + ")";
140 public static final String CREATE_TABLE_COLOR_DEFAULTS_INDEX =
141 "CREATE INDEX " + TABLE_COLOR_DEFAULTS + FIELD_COLOR_SCHEME + "index ON "
142 + TABLE_COLOR_DEFAULTS + " (" + FIELD_COLOR_SCHEME + ");";
143
144 private static final String WHERE_SCHEME_AND_COLOR = FIELD_COLOR_SCHEME + " = ? AND "
145 + FIELD_COLOR_NUMBER + " = ?";
146
147 static {
148 addTableName(TABLE_HOSTS);
149 addTableName(TABLE_PORTFORWARDS);
150 addIndexName(TABLE_PORTFORWARDS + FIELD_PORTFORWARD_HOSTID + "index");
151 addTableName(TABLE_COLORS);
152 addIndexName(TABLE_COLORS + FIELD_COLOR_SCHEME + "index");
153 addTableName(TABLE_COLOR_DEFAULTS);
154 addIndexName(TABLE_COLOR_DEFAULTS + FIELD_COLOR_SCHEME + "index");
155 }
156
157 public static final Object[] dbLock = new Object[0];
158
159 public HostDatabase(Context context) {
160 super(context, DB_NAME, null, DB_VERSION);
161 getWritableDatabase().close();
162 }
163
164 @Override
165 public void onCreate(SQLiteDatabase db) {
166 super.onCreate(db);
167 db.execSQL("CREATE TABLE " + TABLE_HOSTS
168 + " (_id INTEGER PRIMARY KEY, "
169 + FIELD_HOST_NICKNAME + " TEXT, "
170 + FIELD_HOST_PROTOCOL + " TEXT DEFAULT 'ssh', "
171 + FIELD_HOST_USERNAME + " TEXT, "
172 + FIELD_HOST_HOSTNAME + " TEXT, "
173 + FIELD_HOST_PORT + " INTEGER, "
174 + FIELD_HOST_HOSTKEYALGO + " TEXT, "
175 + FIELD_HOST_HOSTKEY + " BLOB, "
176 + FIELD_HOST_LASTCONNECT + " INTEGER, "
177 + FIELD_HOST_COLOR + " TEXT, "
178 + FIELD_HOST_USEKEYS + " TEXT, "
179 + FIELD_HOST_USEAUTHAGENT + " TEXT, "
180 + FIELD_HOST_POSTLOGIN + " TEXT, "
181 + FIELD_HOST_PUBKEYID + " INTEGER DEFAULT " + PUBKEYID_ANY + ", "
182 + FIELD_HOST_DELKEY + " TEXT DEFAULT '" + DELKEY_DEL + "', "
183 + FIELD_HOST_FONTSIZE + " REAL, "
184 + FIELD_HOST_FIXEDSIZE + " TEXT DEFAULT '" + Boolean.toString(false) + "', "
185 + FIELD_HOST_FIXEDWIDTH + " INTEGER, "
186 + FIELD_HOST_FIXEDHEIGHT + " INTEGER, "
187 + FIELD_HOST_WANTSESSION + " TEXT DEFAULT '" + Boolean.toString(true) + "', "
188 + FIELD_HOST_COMPRESSION + " TEXT DEFAULT '" + Boolean.toString(false) + "', "
189 + FIELD_HOST_HTTPPROXY + " TEXT, "
190 + FIELD_HOST_ENCODING + " TEXT DEFAULT '" + ENCODING_DEFAULT + "', "
191 + FIELD_HOST_STAYCONNECTED + " TEXT, "
192 + FIELD_HOST_WANTX11FORWARD + " TEXT DEFAULT '" + Boolean.toString(false) + "', "
193 + FIELD_HOST_X11HOST + " TEXT DEFAULT '" + X11HOST_DEFAULT + "', "
194 + FIELD_HOST_X11PORT + " INTEGER DEFAULT " + X11PORT_DEFAULT + ", "
195 + FIELD_HOST_MONITOR + " TEXT, "
196 + FIELD_HOST_EMULATION + " TEXT, "
197 + FIELD_HOST_ENCRYPTION5250 + " TEXT, "
198 + FIELD_HOST_LIBRARY5250 + " TEXT, "
199 + FIELD_HOST_MENU5250 + " TEXT, "
200 + FIELD_HOST_PROGRAM5250 + " TEXT)");
201 db.execSQL("CREATE TABLE " + TABLE_PORTFORWARDS
202 + " (_id INTEGER PRIMARY KEY, "
203 + FIELD_PORTFORWARD_HOSTID + " INTEGER, "
204 + FIELD_PORTFORWARD_NICKNAME + " TEXT, "
205 + FIELD_PORTFORWARD_TYPE + " TEXT NOT NULL DEFAULT " + PORTFORWARD_LOCAL + ", "
206 + FIELD_PORTFORWARD_SOURCEPORT + " INTEGER NOT NULL DEFAULT 8080, "
207 + FIELD_PORTFORWARD_DESTADDR + " TEXT, "
208 + FIELD_PORTFORWARD_DESTPORT + " TEXT)");
209 db.execSQL("CREATE INDEX " + TABLE_PORTFORWARDS + FIELD_PORTFORWARD_HOSTID + "index ON "
210 + TABLE_PORTFORWARDS + " (" + FIELD_PORTFORWARD_HOSTID + ");");
211 db.execSQL("CREATE TABLE " + TABLE_COLORS
212 + " (_id INTEGER PRIMARY KEY, "
213 + FIELD_COLOR_NUMBER + " INTEGER, "
214 + FIELD_COLOR_VALUE + " INTEGER, "
215 + FIELD_COLOR_SCHEME + " INTEGER)");
216 db.execSQL("CREATE INDEX " + TABLE_COLORS + FIELD_COLOR_SCHEME + "index ON "
217 + TABLE_COLORS + " (" + FIELD_COLOR_SCHEME + ");");
218 db.execSQL(CREATE_TABLE_COLOR_DEFAULTS);
219 db.execSQL(CREATE_TABLE_COLOR_DEFAULTS_INDEX);
220 }
221
222 @Override
223 public void onRobustUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws SQLiteException {
224 // Versions of the database before 510ConnectBot will be shot without warning.
225 if (oldVersion < 24) {
226 db.execSQL("DROP TABLE IF EXISTS " + TABLE_HOSTS);
227 onCreate(db);
228 return;
229 }
230
231 switch (oldVersion) {
232 case 24:
233 db.execSQL("ALTER TABLE " + TABLE_HOSTS
234 + " ADD COLUMN " + FIELD_HOST_MONITOR + " TEXT");
235
236 case 25:
237 db.execSQL("ALTER TABLE " + TABLE_HOSTS
238 + " ADD COLUMN " + FIELD_HOST_EMULATION + " TEXT");
239 db.execSQL("ALTER TABLE " + TABLE_HOSTS
240 + " ADD COLUMN " + FIELD_HOST_ENCRYPTION5250 + " TEXT");
241
242 case 26:
243 db.execSQL("ALTER TABLE " + TABLE_HOSTS
244 + " ADD COLUMN " + FIELD_HOST_LIBRARY5250 + " TEXT");
245 db.execSQL("ALTER TABLE " + TABLE_HOSTS
246 + " ADD COLUMN " + FIELD_HOST_MENU5250 + " TEXT");
247 db.execSQL("ALTER TABLE " + TABLE_HOSTS
248 + " ADD COLUMN " + FIELD_HOST_PROGRAM5250 + " TEXT");
249
250 case 27:
251 db.execSQL("ALTER TABLE " + TABLE_HOSTS
252 + " ADD COLUMN " + FIELD_HOST_FIXEDSIZE + " TEXT DEFAULT '" + Boolean.toString(false) + "'");
253 db.execSQL("ALTER TABLE " + TABLE_HOSTS
254 + " ADD COLUMN " + FIELD_HOST_FIXEDWIDTH + " INTEGER");
255 db.execSQL("ALTER TABLE " + TABLE_HOSTS
256 + " ADD COLUMN " + FIELD_HOST_FIXEDHEIGHT + " INTEGER");
257 }
258 }
259
260 /**
261 * Touch a specific host to update its "last connected" field.
262 * @param nickname Nickname field of host to update
263 */
264 public void touchHost(HostBean host) {
265 long now = System.currentTimeMillis() / 1000;
266 ContentValues values = new ContentValues();
267 values.put(FIELD_HOST_LASTCONNECT, now);
268
269 synchronized (dbLock) {
270 SQLiteDatabase db = this.getWritableDatabase();
271 db.update(TABLE_HOSTS, values, "_id = ?", new String[] { String.valueOf(host.getId()) });
272 }
273 }
274
275 /**
276 * Create a new host using the given parameters.
277 */
278 public HostBean saveHost(HostBean host) {
279 long id;
280
281 synchronized (dbLock) {
282 SQLiteDatabase db = this.getWritableDatabase();
283 id = db.insert(TABLE_HOSTS, null, host.getValues());
284 }
285
286 host.setId(id);
287 return host;
288 }
289
290 /**
291 * Update a field in a host record.
292 */
293 public boolean updateFontSize(HostBean host) {
294 long id = host.getId();
295
296 if (id < 0)
297 return false;
298
299 ContentValues updates = new ContentValues();
300 updates.put(FIELD_HOST_FONTSIZE, host.getFontSize());
301
302 synchronized (dbLock) {
303 SQLiteDatabase db = getWritableDatabase();
304 db.update(TABLE_HOSTS, updates, "_id = ?",
305 new String[] { String.valueOf(id) });
306 }
307
308 return true;
309 }
310
311 /**
312 * Delete a specific host by its <code>_id</code> value.
313 */
314 public void deleteHost(HostBean host) {
315 if (host.getId() < 0)
316 return;
317
318 synchronized (dbLock) {
319 SQLiteDatabase db = this.getWritableDatabase();
320 db.delete(TABLE_HOSTS, "_id = ?", new String[] { String.valueOf(host.getId()) });
321 }
322 }
323
324 /**
325 * Return a cursor that contains information about all known hosts.
326 * @param sortColors If true, sort by color, otherwise sort by nickname.
327 */
328 public List<HostBean> getHosts(boolean sortColors) {
329 String sortField = sortColors ? FIELD_HOST_COLOR : FIELD_HOST_NICKNAME;
330 List<HostBean> hosts;
331
332 synchronized (dbLock) {
333 SQLiteDatabase db = this.getReadableDatabase();
334 Cursor c = db.query(TABLE_HOSTS, null, null, null, null, null, sortField + " ASC");
335 hosts = createHostBeans(c);
336 c.close();
337 }
338
339 return hosts;
340 }
341
342 /**
343 * @param hosts
344 * @param c
345 */
346 private List<HostBean> createHostBeans(Cursor c) {
347 List<HostBean> hosts = new LinkedList<HostBean>();
348 final int COL_ID = c.getColumnIndexOrThrow("_id"),
349 COL_NICKNAME = c.getColumnIndexOrThrow(FIELD_HOST_NICKNAME),
350 COL_PROTOCOL = c.getColumnIndexOrThrow(FIELD_HOST_PROTOCOL),
351 COL_USERNAME = c.getColumnIndexOrThrow(FIELD_HOST_USERNAME),
352 COL_HOSTNAME = c.getColumnIndexOrThrow(FIELD_HOST_HOSTNAME),
353 COL_PORT = c.getColumnIndexOrThrow(FIELD_HOST_PORT),
354 COL_LASTCONNECT = c.getColumnIndexOrThrow(FIELD_HOST_LASTCONNECT),
355 COL_COLOR = c.getColumnIndexOrThrow(FIELD_HOST_COLOR),
356 COL_USEKEYS = c.getColumnIndexOrThrow(FIELD_HOST_USEKEYS),
357 COL_USEAUTHAGENT = c.getColumnIndexOrThrow(FIELD_HOST_USEAUTHAGENT),
358 COL_POSTLOGIN = c.getColumnIndexOrThrow(FIELD_HOST_POSTLOGIN),
359 COL_PUBKEYID = c.getColumnIndexOrThrow(FIELD_HOST_PUBKEYID),
360 COL_WANTSESSION = c.getColumnIndexOrThrow(FIELD_HOST_WANTSESSION),
361 COL_DELKEY = c.getColumnIndexOrThrow(FIELD_HOST_DELKEY),
362 COL_FONTSIZE = c.getColumnIndexOrThrow(FIELD_HOST_FONTSIZE),
363 COL_FIXEDSIZE = c.getColumnIndexOrThrow(FIELD_HOST_FIXEDSIZE),
364 COL_FIXEDWIDTH = c.getColumnIndexOrThrow(FIELD_HOST_FIXEDWIDTH),
365 COL_FIXEDHEIGHT = c.getColumnIndexOrThrow(FIELD_HOST_FIXEDHEIGHT),
366 COL_COMPRESSION = c.getColumnIndexOrThrow(FIELD_HOST_COMPRESSION),
367 COL_HTTPPROXY = c.getColumnIndexOrThrow(FIELD_HOST_HTTPPROXY),
368 COL_ENCODING = c.getColumnIndexOrThrow(FIELD_HOST_ENCODING),
369 COL_STAYCONNECTED = c.getColumnIndexOrThrow(FIELD_HOST_STAYCONNECTED),
370 COL_WANTX11FORWARD = c.getColumnIndexOrThrow(FIELD_HOST_WANTX11FORWARD),
371 COL_X11HOST = c.getColumnIndexOrThrow(FIELD_HOST_X11HOST),
372 COL_X11PORT = c.getColumnIndexOrThrow(FIELD_HOST_X11PORT),
373 COL_MONITOR = c.getColumnIndexOrThrow(FIELD_HOST_MONITOR),
374 COL_EMULATION = c.getColumnIndexOrThrow(FIELD_HOST_EMULATION),
375 COL_ENCRYPTION5250 = c.getColumnIndexOrThrow(FIELD_HOST_ENCRYPTION5250),
376 COL_LIBRARY5250 = c.getColumnIndexOrThrow(FIELD_HOST_LIBRARY5250),
377 COL_MENU5250 = c.getColumnIndexOrThrow(FIELD_HOST_MENU5250),
378 COL_PROGRAM5250 = c.getColumnIndexOrThrow(FIELD_HOST_PROGRAM5250);
379
380 while (c.moveToNext()) {
381 HostBean host = new HostBean();
382 host.setId(c.getLong(COL_ID));
383 host.setNickname(c.getString(COL_NICKNAME));
384 host.setProtocol(c.getString(COL_PROTOCOL));
385 host.setUsername(c.getString(COL_USERNAME));
386 host.setHostname(c.getString(COL_HOSTNAME));
387 host.setPort(c.getInt(COL_PORT));
388 host.setLastConnect(c.getLong(COL_LASTCONNECT));
389 host.setColor(c.getString(COL_COLOR));
390 host.setUseKeys(Boolean.valueOf(c.getString(COL_USEKEYS)));
391 host.setUseAuthAgent(c.getString(COL_USEAUTHAGENT));
392 host.setPostLogin(c.getString(COL_POSTLOGIN));
393 host.setPubkeyId(c.getLong(COL_PUBKEYID));
394 host.setDelKey(c.getString(COL_DELKEY));
395 host.setFontSize(c.getFloat(COL_FONTSIZE));
396 host.setFixedSize(Boolean.valueOf(c.getString(COL_FIXEDSIZE)));
397 host.setFixedWidth(c.getInt(COL_FIXEDWIDTH));
398 host.setFixedHeight(c.getInt(COL_FIXEDHEIGHT));
399 host.setWantSession(Boolean.valueOf(c.getString(COL_WANTSESSION)));
400 host.setCompression(Boolean.valueOf(c.getString(COL_COMPRESSION)));
401 host.setHttpproxy(c.getString(COL_HTTPPROXY));
402 host.setEncoding(c.getString(COL_ENCODING));
403 host.setStayConnected(Boolean.valueOf(c.getString(COL_STAYCONNECTED)));
404 host.setWantX11Forward(Boolean.valueOf(c.getString(COL_WANTX11FORWARD)));
405 host.setX11Host(c.getString(COL_X11HOST));
406 host.setX11Port(c.getInt(COL_X11PORT));
407 host.setMonitor(c.getString(COL_MONITOR));
408 host.setHostEmulation(c.getString(COL_EMULATION));
409 host.setEncryption5250(c.getString(COL_ENCRYPTION5250));
410 host.setLibrary(c.getString(COL_LIBRARY5250));
411 host.setInitialMenu(c.getString(COL_MENU5250));
412 host.setProgram(c.getString(COL_PROGRAM5250));
413 hosts.add(host);
414 }
415
416 return hosts;
417 }
418
419 /**
420 * @param c
421 * @return
422 */
423 private HostBean getFirstHostBean(Cursor c) {
424 HostBean host = null;
425 List<HostBean> hosts = createHostBeans(c);
426
427 if (hosts.size() > 0)
428 host = hosts.get(0);
429
430 c.close();
431 return host;
432 }
433
434 /**
435 * @param nickname
436 * @param protocol
437 * @param username
438 * @param hostname
439 * @param hostname2
440 * @param port
441 * @return
442 */
443 public HostBean findHost(Map<String, String> selection) {
444 StringBuilder selectionBuilder = new StringBuilder();
445 Iterator<Entry<String, String>> i = selection.entrySet().iterator();
446 List<String> selectionValuesList = new LinkedList<String>();
447 int n = 0;
448
449 while (i.hasNext()) {
450 Entry<String, String> entry = i.next();
451
452 if (entry.getValue() == null)
453 continue;
454
455 if (n++ > 0)
456 selectionBuilder.append(" AND ");
457
458 selectionBuilder.append(entry.getKey())
459 .append(" = ?");
460 selectionValuesList.add(entry.getValue());
461 }
462
463 String selectionValues[] = new String[selectionValuesList.size()];
464 selectionValuesList.toArray(selectionValues);
465 selectionValuesList = null;
466 HostBean host;
467
468 synchronized (dbLock) {
469 SQLiteDatabase db = getReadableDatabase();
470 Cursor c = db.query(TABLE_HOSTS, null,
471 selectionBuilder.toString(),
472 selectionValues,
473 null, null, null);
474 host = getFirstHostBean(c);
475 }
476
477 return host;
478 }
479
480 /**
481 * @param hostId
482 * @return
483 */
484 public HostBean findHostById(long hostId) {
485 HostBean host;
486
487 synchronized (dbLock) {
488 SQLiteDatabase db = getReadableDatabase();
489 Cursor c = db.query(TABLE_HOSTS, null,
490 "_id = ?", new String[] { String.valueOf(hostId) },
491 null, null, null);
492 host = getFirstHostBean(c);
493 }
494
495 return host;
496 }
497
498 /**
499 * Record the given hostkey into database under this nickname.
500 * @param hostname
501 * @param port
502 * @param hostkeyalgo
503 * @param hostkey
504 */
505 public void saveKnownHost(String hostname, int port, String hostkeyalgo, byte[] hostkey) {
506 ContentValues values = new ContentValues();
507 values.put(FIELD_HOST_HOSTKEYALGO, hostkeyalgo);
508 values.put(FIELD_HOST_HOSTKEY, hostkey);
509
510 synchronized (dbLock) {
511 SQLiteDatabase db = getReadableDatabase();
512 db.update(TABLE_HOSTS, values,
513 FIELD_HOST_HOSTNAME + " = ? AND " + FIELD_HOST_PORT + " = ?",
514 new String[] { hostname, String.valueOf(port) });
515 Log.d(TAG, String.format("Finished saving hostkey information for '%s'", hostname));
516 }
517 }
518
519 /**
520 * Build list of known hosts for Trilead library.
521 * @return
522 */
523 public KnownHosts getKnownHosts() {
524 KnownHosts known = new KnownHosts();
525
526 synchronized (dbLock) {
527 SQLiteDatabase db = this.getReadableDatabase();
528 Cursor c = db.query(TABLE_HOSTS, new String[] { FIELD_HOST_HOSTNAME,
529 FIELD_HOST_PORT, FIELD_HOST_HOSTKEYALGO, FIELD_HOST_HOSTKEY
530 },
531 null, null, null, null, null);
532
533 if (c != null) {
534 int COL_HOSTNAME = c.getColumnIndexOrThrow(FIELD_HOST_HOSTNAME),
535 COL_PORT = c.getColumnIndexOrThrow(FIELD_HOST_PORT),
536 COL_HOSTKEYALGO = c.getColumnIndexOrThrow(FIELD_HOST_HOSTKEYALGO),
537 COL_HOSTKEY = c.getColumnIndexOrThrow(FIELD_HOST_HOSTKEY);
538
539 while (c.moveToNext()) {
540 String hostname = c.getString(COL_HOSTNAME),
541 hostkeyalgo = c.getString(COL_HOSTKEYALGO);
542 int port = c.getInt(COL_PORT);
543 byte[] hostkey = c.getBlob(COL_HOSTKEY);
544
545 if (hostkeyalgo == null || hostkeyalgo.length() == 0) continue;
546
547 if (hostkey == null || hostkey.length == 0) continue;
548
549 try {
550 known.addHostkey(new String[] { String.format("%s:%d", hostname, port) }, hostkeyalgo, hostkey);
551 }
552 catch (Exception e) {
553 Log.e(TAG, "Problem while adding a known host from database", e);
554 }
555 }
556
557 c.close();
558 }
559 }
560
561 return known;
562 }
563
564 /**
565 * Unset any hosts using a pubkey ID that has been deleted.
566 * @param pubkeyId
567 */
568 public void stopUsingPubkey(long pubkeyId) {
569 if (pubkeyId < 0) return;
570
571 ContentValues values = new ContentValues();
572 values.put(FIELD_HOST_PUBKEYID, PUBKEYID_ANY);
573
574 synchronized (dbLock) {
575 SQLiteDatabase db = this.getWritableDatabase();
576 db.update(TABLE_HOSTS, values, FIELD_HOST_PUBKEYID + " = ?", new String[] { String.valueOf(pubkeyId) });
577 }
578
579 Log.d(TAG, String.format("Set all hosts using pubkey id %d to -1", pubkeyId));
580 }
581
582 /*
583 * Methods for dealing with port forwards attached to hosts
584 */
585
586 /**
587 * Returns a list of all the port forwards associated with a particular host ID.
588 * @param host the host for which we want the port forward list
589 * @return port forwards associated with host ID
590 */
591 public List<PortForwardBean> getPortForwardsForHost(HostBean host) {
592 List<PortForwardBean> portForwards = new LinkedList<PortForwardBean>();
593
594 synchronized (dbLock) {
595 SQLiteDatabase db = this.getReadableDatabase();
596 Cursor c = db.query(TABLE_PORTFORWARDS, new String[] {
597 "_id", FIELD_PORTFORWARD_NICKNAME, FIELD_PORTFORWARD_TYPE, FIELD_PORTFORWARD_SOURCEPORT,
598 FIELD_PORTFORWARD_DESTADDR, FIELD_PORTFORWARD_DESTPORT
599 },
600 FIELD_PORTFORWARD_HOSTID + " = ?", new String[] { String.valueOf(host.getId()) },
601 null, null, null);
602
603 while (c.moveToNext()) {
604 PortForwardBean pfb = new PortForwardBean(
605 c.getInt(0),
606 host.getId(),
607 c.getString(1),
608 c.getString(2),
609 c.getInt(3),
610 c.getString(4),
611 c.getInt(5));
612 portForwards.add(pfb);
613 }
614
615 c.close();
616 }
617
618 return portForwards;
619 }
620
621 /**
622 * Update the parameters of a port forward in the database.
623 * @param pfb {@link PortForwardBean} to save
624 * @return true on success
625 */
626 public boolean savePortForward(PortForwardBean pfb) {
627 boolean success = false;
628
629 synchronized (dbLock) {
630 SQLiteDatabase db = getWritableDatabase();
631
632 if (pfb.getId() < 0) {
633 long id = db.insert(TABLE_PORTFORWARDS, null, pfb.getValues());
634 pfb.setId(id);
635 success = true;
636 }
637 else {
638 if (db.update(TABLE_PORTFORWARDS, pfb.getValues(), "_id = ?", new String[] { String.valueOf(pfb.getId()) }) > 0)
639 success = true;
640 }
641 }
642
643 return success;
644 }
645
646 /**
647 * Deletes a port forward from the database.
648 * @param pfb {@link PortForwardBean} to delete
649 */
650 public void deletePortForward(PortForwardBean pfb) {
651 if (pfb.getId() < 0)
652 return;
653
654 synchronized (dbLock) {
655 SQLiteDatabase db = this.getWritableDatabase();
656 db.delete(TABLE_PORTFORWARDS, "_id = ?", new String[] { String.valueOf(pfb.getId()) });
657 }
658 }
659
660 public Integer[] getColorsForScheme(int scheme) {
661 Integer[] colors = Colors.defaults.clone();
662
663 synchronized (dbLock) {
664 SQLiteDatabase db = getReadableDatabase();
665 Cursor c = db.query(TABLE_COLORS, new String[] {
666 FIELD_COLOR_NUMBER, FIELD_COLOR_VALUE
667 },
668 FIELD_COLOR_SCHEME + " = ?",
669 new String[] { String.valueOf(scheme) },
670 null, null, null);
671
672 while (c.moveToNext()) {
673 colors[c.getInt(0)] = Integer.valueOf(c.getInt(1));
674 }
675
676 c.close();
677 }
678
679 return colors;
680 }
681
682 public void setColorForScheme(int scheme, int number, int value) {
683 final SQLiteDatabase db;
684 final String[] whereArgs = new String[] { String.valueOf(scheme), String.valueOf(number) };
685
686 if (value == Colors.defaults[number]) {
687 synchronized (dbLock) {
688 db = getWritableDatabase();
689 db.delete(TABLE_COLORS,
690 WHERE_SCHEME_AND_COLOR, whereArgs);
691 }
692 }
693 else {
694 final ContentValues values = new ContentValues();
695 values.put(FIELD_COLOR_VALUE, value);
696
697 synchronized (dbLock) {
698 db = getWritableDatabase();
699 final int rowsAffected = db.update(TABLE_COLORS, values,
700 WHERE_SCHEME_AND_COLOR, whereArgs);
701
702 if (rowsAffected == 0) {
703 values.put(FIELD_COLOR_SCHEME, scheme);
704 values.put(FIELD_COLOR_NUMBER, number);
705 db.insert(TABLE_COLORS, null, values);
706 }
707 }
708 }
709 }
710
711 public void setGlobalColor(int number, int value) {
712 setColorForScheme(DEFAULT_COLOR_SCHEME, number, value);
713 }
714
715 public int[] getDefaultColorsForScheme(int scheme) {
716 int[] colors = new int[] { DEFAULT_FG_COLOR, DEFAULT_BG_COLOR };
717
718 synchronized (dbLock) {
719 SQLiteDatabase db = getReadableDatabase();
720 Cursor c = db.query(TABLE_COLOR_DEFAULTS,
721 new String[] { FIELD_COLOR_FG, FIELD_COLOR_BG },
722 FIELD_COLOR_SCHEME + " = ?",
723 new String[] { String.valueOf(scheme) },
724 null, null, null);
725
726 if (c.moveToFirst()) {
727 colors[0] = c.getInt(0);
728 colors[1] = c.getInt(1);
729 }
730
731 c.close();
732 }
733
734 return colors;
735 }
736
737 public int[] getGlobalDefaultColors() {
738 return getDefaultColorsForScheme(DEFAULT_COLOR_SCHEME);
739 }
740
741 public void setDefaultColorsForScheme(int scheme, int fg, int bg) {
742 SQLiteDatabase db;
743 String schemeWhere = null;
744 String[] whereArgs;
745 schemeWhere = FIELD_COLOR_SCHEME + " = ?";
746 whereArgs = new String[] { String.valueOf(scheme) };
747 ContentValues values = new ContentValues();
748 values.put(FIELD_COLOR_FG, fg);
749 values.put(FIELD_COLOR_BG, bg);
750
751 synchronized (dbLock) {
752 db = getWritableDatabase();
753 int rowsAffected = db.update(TABLE_COLOR_DEFAULTS, values,
754 schemeWhere, whereArgs);
755
756 if (rowsAffected == 0) {
757 values.put(FIELD_COLOR_SCHEME, scheme);
758 db.insert(TABLE_COLOR_DEFAULTS, null, values);
759 }
760 }
761 }
762 }