comparison src/com/five_ten_sg/connectbot/HostEditorActivity.java @ 127:69db8af9508d

host editor database values override summary text only if non-empty
author Carl Byington <carl@five-ten-sg.com>
date Thu, 19 Jun 2014 09:40:28 -0700
parents 446dbcf606eb
children 377357365d68
comparison
equal deleted inserted replaced
126:f7e3abdbc8a5 127:69db8af9508d
20 import java.nio.charset.Charset; 20 import java.nio.charset.Charset;
21 import java.util.Arrays; 21 import java.util.Arrays;
22 import java.util.HashMap; 22 import java.util.HashMap;
23 import java.util.LinkedList; 23 import java.util.LinkedList;
24 import java.util.List; 24 import java.util.List;
25 import java.util.HashMap;
25 import java.util.Map; 26 import java.util.Map;
26 import java.util.Map.Entry; 27 import java.util.Map.Entry;
27 import java.util.Set; 28 import java.util.Set;
28 29
29 import com.five_ten_sg.connectbot.bean.HostBean; 30 import com.five_ten_sg.connectbot.bean.HostBean;
52 public class CursorPreferenceHack implements SharedPreferences { 53 public class CursorPreferenceHack implements SharedPreferences {
53 protected final String table; 54 protected final String table;
54 protected final long id; 55 protected final long id;
55 56
56 protected Map<String, String> values = new HashMap<String, String>(); 57 protected Map<String, String> values = new HashMap<String, String>();
57 // protected Map<String, String> pubkeys = new HashMap<String, String>(); 58 protected Map<String, CharSequence> summaries = new HashMap<String, CharSequence>();
58 59
59 public CursorPreferenceHack(String table, long id) { 60 public CursorPreferenceHack(String table, long id) {
60 this.table = table; 61 this.table = table;
61 this.id = id; 62 this.id = id;
62 cacheValues(); 63 cacheValues();
80 } 81 }
81 } 82 }
82 83
83 cursor.close(); 84 cursor.close();
84 db.close(); 85 db.close();
85 // db = pubkeydb.getReadableDatabase();
86 // cursor = db.query(PubkeyDatabase.TABLE_PUBKEYS,
87 // new String[] { "_id", PubkeyDatabase.FIELD_PUBKEY_NICKNAME },
88 // null, null, null, null, null);
89 //
90 // if (cursor.moveToFirst()) {
91 // do {
92 // String pubkeyid = String.valueOf(cursor.getLong(0));
93 // String value = cursor.getString(1);
94 // pubkeys.put(pubkeyid, value);
95 // } while (cursor.moveToNext());
96 // }
97 //
98 // cursor.close();
99 // db.close();
100 } 86 }
101 87
102 public boolean contains(String key) { 88 public boolean contains(String key) {
103 return values.containsKey(key); 89 return values.containsKey(key);
104 } 90 }
259 } 245 }
260 }; 246 };
261 this.pref = new CursorPreferenceHack(HostDatabase.TABLE_HOSTS, hostId); 247 this.pref = new CursorPreferenceHack(HostDatabase.TABLE_HOSTS, hostId);
262 this.pref.registerOnSharedPreferenceChangeListener(this); 248 this.pref.registerOnSharedPreferenceChangeListener(this);
263 this.addPreferencesFromResource(R.xml.host_prefs); 249 this.addPreferencesFromResource(R.xml.host_prefs);
250 // get all the text summaries
251 getSummaries();
264 // disable all preferences that are not applicable to this host 252 // disable all preferences that are not applicable to this host
265 findPreference(HostDatabase.FIELD_HOST_PUBKEYID).setEnabled(enableSSHFeatures); 253 findPreference(HostDatabase.FIELD_HOST_PUBKEYID).setEnabled(enableSSHFeatures);
266 findPreference(HostDatabase.FIELD_HOST_USEAUTHAGENT).setEnabled(enableSSHFeatures); 254 findPreference(HostDatabase.FIELD_HOST_USEAUTHAGENT).setEnabled(enableSSHFeatures);
267 findPreference(HostDatabase.FIELD_HOST_POSTLOGIN).setEnabled(!enable5250Features); 255 findPreference(HostDatabase.FIELD_HOST_POSTLOGIN).setEnabled(!enable5250Features);
268 findPreference(HostDatabase.FIELD_HOST_COMPRESSION).setEnabled(enableSSHFeatures); 256 findPreference(HostDatabase.FIELD_HOST_COMPRESSION).setEnabled(enableSSHFeatures);
269 findPreference(HostDatabase.FIELD_HOST_HTTPPROXY).setEnabled(enableAsyncFeatures); 257 findPreference(HostDatabase.FIELD_HOST_HTTPPROXY).setEnabled(enableAsyncFeatures);
270 findPreference(HostDatabase.FIELD_HOST_WANTSESSION).setEnabled(enableSSHFeatures); 258 findPreference(HostDatabase.FIELD_HOST_WANTSESSION).setEnabled(enableSSHFeatures);
271 findPreference(HostDatabase.FIELD_HOST_USERNAME).setEnabled(enableSSHFeatures || enable5250Features); 259 findPreference(HostDatabase.FIELD_HOST_USERNAME).setEnabled(enableSSHFeatures || enable5250Features);
272 findPreference(HostDatabase.FIELD_HOST_EMULATION).setEnabled(!enable5250Features); 260 findPreference(HostDatabase.FIELD_HOST_EMULATION).setEnabled(!enable5250Features);
273 findPreference(HostDatabase.CATEGORY_5250).setEnabled(enable5250Features); 261 findPreference(HostDatabase.CATEGORY_5250).setEnabled(enable5250Features);
274 findPreference(HostDatabase.FIELD_HOST_ENCRYPTION5250).setEnabled(enable5250Features);
275 findPreference(HostDatabase.CATEGORY_X11).setEnabled(enableSSHFeatures); 262 findPreference(HostDatabase.CATEGORY_X11).setEnabled(enableSSHFeatures);
276 findPreference(HostDatabase.FIELD_HOST_X11HOST).setEnabled(enableSSHFeatures);
277 findPreference(HostDatabase.FIELD_HOST_X11PORT).setEnabled(enableSSHFeatures);
278 // add all existing pubkeys to our listpreference for user to choose from 263 // add all existing pubkeys to our listpreference for user to choose from
279 // TODO: may be an issue here when this activity is recycled after adding a new pubkey 264 // TODO: may be an issue here when this activity is recycled after adding a new pubkey
280 // TODO: should consider moving into onStart, but we dont have a good way of resetting the listpref after filling once 265 // TODO: should consider moving into onStart, but we dont have a good way of resetting the listpref after filling once
281 ListPreference pubkeyPref = (ListPreference)findPreference(HostDatabase.FIELD_HOST_PUBKEYID); 266 ListPreference pubkeyPref = (ListPreference)findPreference(HostDatabase.FIELD_HOST_PUBKEYID);
282 List<CharSequence> pubkeyNicks = new LinkedList<CharSequence> (Arrays.asList(pubkeyPref.getEntries())); 267 List<CharSequence> pubkeyNicks = new LinkedList<CharSequence> (Arrays.asList(pubkeyPref.getEntries()));
332 this.pubkeydb.close(); 317 this.pubkeydb.close();
333 this.pubkeydb = null; 318 this.pubkeydb = null;
334 } 319 }
335 } 320 }
336 321
322 private void getSummaries() {
323 // get all the original text summaries
324 for (String key : this.pref.values.keySet()) {
325 Preference pref = this.findPreference(key);
326 if (pref == null) continue;
327 if (pref instanceof CheckBoxPreference) continue;
328 CharSequence value = this.pref.getSummary(key, "");
329 summaries.put(key, value);
330 }
331
337 private void updateSummaries() { 332 private void updateSummaries() {
338 // for all text preferences, set hint as current database value 333 // for all text preferences, set hint as current database value if it is non-empty
339 for (String key : this.pref.values.keySet()) { 334 for (String key : this.pref.values.keySet()) {
340 if (key.equals(HostDatabase.FIELD_HOST_POSTLOGIN)) continue;
341 if (key.equals(HostDatabase.FIELD_HOST_EMULATION)) continue;
342
343 Preference pref = this.findPreference(key); 335 Preference pref = this.findPreference(key);
344
345 if (pref == null) continue; 336 if (pref == null) continue;
346
347 if (pref instanceof CheckBoxPreference) continue; 337 if (pref instanceof CheckBoxPreference) continue;
348
349 CharSequence value = this.pref.getString(key, ""); 338 CharSequence value = this.pref.getString(key, "");
350 339
351 if (key.equals(HostDatabase.FIELD_HOST_PUBKEYID)) { 340 if (key.equals(HostDatabase.FIELD_HOST_PUBKEYID)) {
352 try { 341 try {
353 int pubkeyId = Integer.parseInt((String) value); 342 int pubkeyId = Integer.parseInt(value.toString());
354 343
355 if (pubkeyId >= 0) 344 if (pubkeyId >= 0)
356 pref.setSummary(pubkeydb.getNickname(pubkeyId)); 345 pref.setSummary(pubkeydb.getNickname(pubkeyId));
357 else if (pubkeyId == HostDatabase.PUBKEYID_ANY) 346 else if (pubkeyId == HostDatabase.PUBKEYID_ANY)
358 pref.setSummary(R.string.list_pubkeyids_any); 347 pref.setSummary(R.string.list_pubkeyids_any);
365 // Fall through. 354 // Fall through.
366 } 355 }
367 } 356 }
368 else if (pref instanceof ListPreference) { 357 else if (pref instanceof ListPreference) {
369 ListPreference listPref = (ListPreference) pref; 358 ListPreference listPref = (ListPreference) pref;
370 int entryIndex = listPref.findIndexOfValue((String) value); 359 int entryIndex = listPref.findIndexOfValue(value);
371 360 if (entryIndex >= 0) value = listPref.getEntries()[entryIndex];
372 if (entryIndex >= 0) 361 }
373 value = listPref.getEntries()[entryIndex]; 362
374 } 363 if ((value == null) || value.length() == 0)) value = summaries.get(key);
375
376 pref.setSummary(value); 364 pref.setSummary(value);
377 } 365 }
378 } 366 }
379 367
380 private void initCharsetPref(final ListPreference charsetPref) { 368 private void initCharsetPref(final ListPreference charsetPref) {