comparison app/src/main/java/com/five_ten_sg/connectbot/HostEditorActivity.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/HostEditorActivity.java@071eccdff8ea
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;
19
20 import java.nio.charset.Charset;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import com.five_ten_sg.connectbot.bean.HostBean;
31 import com.five_ten_sg.connectbot.service.TerminalBridge;
32 import com.five_ten_sg.connectbot.service.TerminalManager;
33 import com.five_ten_sg.connectbot.util.HostDatabase;
34 import com.five_ten_sg.connectbot.util.PubkeyDatabase;
35 import android.content.ComponentName;
36 import android.content.ContentValues;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.content.ServiceConnection;
40 import android.content.SharedPreferences;
41 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
42 import android.database.Cursor;
43 import android.database.sqlite.SQLiteDatabase;
44 import android.os.Bundle;
45 import android.os.IBinder;
46 import android.preference.CheckBoxPreference;
47 import android.preference.ListPreference;
48 import android.preference.Preference;
49 import android.preference.PreferenceActivity;
50 import android.util.Log;
51
52 public class HostEditorActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
53 public class CursorPreferenceHack implements SharedPreferences {
54 protected final String table;
55 protected final long id;
56
57 protected Map<String, String> values = new HashMap<String, String>();
58
59 public CursorPreferenceHack(String table, long id) {
60 this.table = table;
61 this.id = id;
62 cacheValues();
63 }
64
65 protected final void cacheValues() {
66 // fill a cursor and cache the values locally
67 // this makes sure we dont have any floating cursor to dispose later
68 SQLiteDatabase db = hostdb.getReadableDatabase();
69 Cursor cursor = db.query(table, null, "_id = ?",
70 new String[] { String.valueOf(id) }, null, null, null);
71
72 if (cursor.moveToFirst()) {
73 for (int i = 0; i < cursor.getColumnCount(); i++) {
74 String key = cursor.getColumnName(i);
75
76 if (key.equals(HostDatabase.FIELD_HOST_HOSTKEY)) continue;
77
78 String value = cursor.getString(i);
79 values.put(key, value);
80 }
81 }
82
83 cursor.close();
84 db.close();
85 }
86
87 public boolean contains(String key) {
88 return values.containsKey(key);
89 }
90
91 public class Editor implements SharedPreferences.Editor {
92
93 private ContentValues update = new ContentValues();
94
95 public SharedPreferences.Editor clear() {
96 Log.d(this.getClass().toString(), "clear()");
97 update = new ContentValues();
98 return this;
99 }
100
101 public boolean commit() {
102 //Log.d(this.getClass().toString(), "commit() changes back to database");
103 SQLiteDatabase db = hostdb.getWritableDatabase();
104 db.update(table, update, "_id = ?", new String[] { String.valueOf(id) });
105 db.close();
106 // make sure we refresh the parent cached values
107 cacheValues();
108
109 // and update any listeners
110 for (OnSharedPreferenceChangeListener listener : listeners) {
111 listener.onSharedPreferenceChanged(CursorPreferenceHack.this, null);
112 }
113
114 return true;
115 }
116
117 // Gingerbread compatibility
118 public void apply() {
119 commit();
120 }
121
122 public android.content.SharedPreferences.Editor putBoolean(String key, boolean value) {
123 return this.putString(key, Boolean.toString(value));
124 }
125
126 public android.content.SharedPreferences.Editor putFloat(String key, float value) {
127 return this.putString(key, Float.toString(value));
128 }
129
130 public android.content.SharedPreferences.Editor putInt(String key, int value) {
131 return this.putString(key, Integer.toString(value));
132 }
133
134 public android.content.SharedPreferences.Editor putLong(String key, long value) {
135 return this.putString(key, Long.toString(value));
136 }
137
138 public android.content.SharedPreferences.Editor putString(String key, String value) {
139 //Log.d(this.getClass().toString(), String.format("Editor.putString(key=%s, value=%s)", key, value));
140 update.put(key, value);
141 return this;
142 }
143
144 public android.content.SharedPreferences.Editor remove(String key) {
145 //Log.d(this.getClass().toString(), String.format("Editor.remove(key=%s)", key));
146 update.remove(key);
147 return this;
148 }
149
150 public android.content.SharedPreferences.Editor putStringSet(String key, Set<String> value) {
151 throw new UnsupportedOperationException("HostEditor Prefs do not support Set<String>");
152 }
153 }
154
155
156 public Editor edit() {
157 //Log.d(this.getClass().toString(), "edit()");
158 return new Editor();
159 }
160
161 public Map<String, ?> getAll() {
162 return values;
163 }
164
165 public boolean getBoolean(String key, boolean defValue) {
166 return Boolean.valueOf(this.getString(key, Boolean.toString(defValue)));
167 }
168
169 public float getFloat(String key, float defValue) {
170 return Float.valueOf(this.getString(key, Float.toString(defValue)));
171 }
172
173 public int getInt(String key, int defValue) {
174 return Integer.valueOf(this.getString(key, Integer.toString(defValue)));
175 }
176
177 public long getLong(String key, long defValue) {
178 return Long.valueOf(this.getString(key, Long.toString(defValue)));
179 }
180
181 public String getString(String key, String defValue) {
182 //Log.d(this.getClass().toString(), String.format("getString(key=%s, defValue=%s)", key, defValue));
183 if (!values.containsKey(key)) return defValue;
184
185 return values.get(key);
186 }
187
188 public Set<String> getStringSet(String key, Set<String> defValue) {
189 throw new ClassCastException("HostEditor Prefs do not support Set<String>");
190 }
191
192 protected List<OnSharedPreferenceChangeListener> listeners = new LinkedList<OnSharedPreferenceChangeListener>();
193
194 public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
195 listeners.add(listener);
196 }
197
198 public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
199 listeners.remove(listener);
200 }
201
202 }
203
204 @Override
205 public SharedPreferences getSharedPreferences(String name, int mode) {
206 //Log.d(this.getClass().toString(), String.format("getSharedPreferences(name=%s)", name));
207 return this.pref;
208 }
209
210 protected static final String TAG = "ConnectBot.HostEditorActivity";
211
212 protected HostDatabase hostdb = null;
213 private PubkeyDatabase pubkeydb = null;
214
215 private CursorPreferenceHack pref;
216 private ServiceConnection connection;
217 private Map<String, CharSequence> summaries = new HashMap<String, CharSequence>();
218
219 private HostBean host;
220 private boolean enableSSHFeatures;
221 private boolean enable5250Features;
222 private boolean enableAsyncFeatures;
223
224 protected TerminalBridge hostBridge;
225
226 @Override
227 public void onCreate(Bundle icicle) {
228 super.onCreate(icicle);
229 long hostId = this.getIntent().getLongExtra(Intent.EXTRA_TITLE, -1);
230 // TODO: we could pass through a specific ContentProvider uri here
231 //this.getPreferenceManager().setSharedPreferencesName(uri);
232 this.hostdb = new HostDatabase(this);
233 this.pubkeydb = new PubkeyDatabase(this);
234 host = hostdb.findHostById(hostId);
235 enableSSHFeatures = host.isSSH();
236 enable5250Features = host.is5250();
237 enableAsyncFeatures = host.isAsync();
238 connection = new ServiceConnection() {
239 public void onServiceConnected(ComponentName className, IBinder service) {
240 TerminalManager bound = ((TerminalManager.TerminalBinder) service).getService();
241 hostBridge = bound.getConnectedBridge(host);
242 }
243 public void onServiceDisconnected(ComponentName name) {
244 hostBridge = null;
245 }
246 };
247 this.pref = new CursorPreferenceHack(HostDatabase.TABLE_HOSTS, hostId);
248 this.pref.registerOnSharedPreferenceChangeListener(this);
249 this.addPreferencesFromResource(R.xml.host_prefs);
250 // get all the text summaries
251 getSummaries();
252 // disable all preferences that are not applicable to this host
253 findPreference(HostDatabase.FIELD_HOST_PUBKEYID).setEnabled(enableSSHFeatures);
254 findPreference(HostDatabase.FIELD_HOST_USEAUTHAGENT).setEnabled(enableSSHFeatures);
255 findPreference(HostDatabase.FIELD_HOST_POSTLOGIN).setEnabled(!enable5250Features);
256 findPreference(HostDatabase.FIELD_HOST_COMPRESSION).setEnabled(enableSSHFeatures);
257 findPreference(HostDatabase.FIELD_HOST_HTTPPROXY).setEnabled(enableAsyncFeatures);
258 findPreference(HostDatabase.FIELD_HOST_WANTSESSION).setEnabled(enableSSHFeatures);
259 findPreference(HostDatabase.FIELD_HOST_USERNAME).setEnabled(enableSSHFeatures || enable5250Features);
260 findPreference(HostDatabase.FIELD_HOST_EMULATION).setEnabled(!enable5250Features);
261 findPreference(HostDatabase.CATEGORY_5250).setEnabled(enable5250Features);
262 findPreference(HostDatabase.CATEGORY_X11).setEnabled(enableSSHFeatures);
263 // add all existing pubkeys to our listpreference for user to choose from
264 // TODO: may be an issue here when this activity is recycled after adding a new pubkey
265 // TODO: should consider moving into onStart, but we dont have a good way of resetting the listpref after filling once
266 ListPreference pubkeyPref = (ListPreference)findPreference(HostDatabase.FIELD_HOST_PUBKEYID);
267 List<CharSequence> pubkeyNicks = new LinkedList<CharSequence> (Arrays.asList(pubkeyPref.getEntries()));
268 pubkeyNicks.addAll(pubkeydb.allValues(PubkeyDatabase.FIELD_PUBKEY_NICKNAME));
269 pubkeyPref.setEntries(pubkeyNicks.toArray(new CharSequence[pubkeyNicks.size()]));
270 List<CharSequence> pubkeyIds = new LinkedList<CharSequence> (Arrays.asList(pubkeyPref.getEntryValues()));
271 pubkeyIds.addAll(pubkeydb.allValues("_id"));
272 pubkeyPref.setEntryValues(pubkeyIds.toArray(new CharSequence[pubkeyIds.size()]));
273 // Populate the character set encoding list with all available
274 final ListPreference charsetPref = (ListPreference)findPreference(HostDatabase.FIELD_HOST_ENCODING);
275
276 if (CharsetHolder.isInitialized()) {
277 initCharsetPref(charsetPref);
278 }
279 else {
280 String[] currentCharsetPref = new String[1];
281 currentCharsetPref[0] = charsetPref.getValue();
282 charsetPref.setEntryValues(currentCharsetPref);
283 charsetPref.setEntries(currentCharsetPref);
284 new Thread(new Runnable() {
285 public void run() {
286 initCharsetPref(charsetPref);
287 }
288 }).start();
289 }
290
291 this.updateSummaries();
292 }
293
294 @Override
295 public void onStart() {
296 super.onStart();
297 bindService(new Intent(this, TerminalManager.class), connection, Context.BIND_AUTO_CREATE);
298
299 if (this.hostdb == null)
300 this.hostdb = new HostDatabase(this);
301
302 if (this.pubkeydb == null)
303 this.pubkeydb = new PubkeyDatabase(this);
304 }
305
306 @Override
307 public void onStop() {
308 super.onStop();
309 unbindService(connection);
310
311 if (this.hostdb != null) {
312 this.hostdb.close();
313 this.hostdb = null;
314 }
315
316 if (this.pubkeydb != null) {
317 this.pubkeydb.close();
318 this.pubkeydb = null;
319 }
320 }
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
327 if (pref == null) continue;
328
329 if (pref instanceof CheckBoxPreference) continue;
330
331 CharSequence value = pref.getSummary();
332 summaries.put(key, value);
333 }
334 }
335
336 private void updateSummaries() {
337 // for all text preferences, set hint as current database value if it is non-empty
338 for (String key : this.pref.values.keySet()) {
339 Preference pref = this.findPreference(key);
340
341 if (pref == null) continue;
342
343 if (pref instanceof CheckBoxPreference) continue;
344
345 CharSequence value = this.pref.getString(key, "");
346
347 if (key.equals(HostDatabase.FIELD_HOST_PUBKEYID)) {
348 try {
349 int pubkeyId = Integer.parseInt(value.toString());
350
351 if (pubkeyId >= 0)
352 pref.setSummary(pubkeydb.getNickname(pubkeyId));
353 else if (pubkeyId == HostDatabase.PUBKEYID_ANY)
354 pref.setSummary(R.string.list_pubkeyids_any);
355 else if (pubkeyId == HostDatabase.PUBKEYID_NEVER)
356 pref.setSummary(R.string.list_pubkeyids_none);
357
358 continue;
359 }
360 catch (NumberFormatException nfe) {
361 // Fall through.
362 }
363 }
364 else if ((pref instanceof ListPreference) && (value != null)) {
365 ListPreference listPref = (ListPreference) pref;
366 int entryIndex = listPref.findIndexOfValue(value.toString());
367
368 if (entryIndex >= 0) value = listPref.getEntries()[entryIndex];
369 }
370
371 if ((value == null) || (value.length() == 0)) value = summaries.get(key);
372
373 pref.setSummary(value);
374 }
375 }
376
377 private void initCharsetPref(final ListPreference charsetPref) {
378 charsetPref.setEntryValues(CharsetHolder.getCharsetIds());
379 charsetPref.setEntries(CharsetHolder.getCharsetNames());
380 }
381
382 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
383 // update values on changed preference
384 this.updateSummaries();
385
386 // Our CursorPreferenceHack always send null keys, so try to set charset anyway
387 if (hostBridge != null)
388 hostBridge.setCharset(sharedPreferences
389 .getString(HostDatabase.FIELD_HOST_ENCODING, HostDatabase.ENCODING_DEFAULT));
390 }
391
392 public static class CharsetHolder {
393 private static boolean initialized = false;
394
395 private static CharSequence[] charsetIds;
396 private static CharSequence[] charsetNames;
397
398 public static CharSequence[] getCharsetNames() {
399 if (charsetNames == null)
400 initialize();
401
402 return charsetNames;
403 }
404
405 public static CharSequence[] getCharsetIds() {
406 if (charsetIds == null)
407 initialize();
408
409 return charsetIds;
410 }
411
412 private synchronized static void initialize() {
413 if (initialized)
414 return;
415
416 List<CharSequence> charsetIdsList = new LinkedList<CharSequence>();
417 List<CharSequence> charsetNamesList = new LinkedList<CharSequence>();
418
419 for (Entry<String, Charset> entry : Charset.availableCharsets().entrySet()) {
420 Charset c = entry.getValue();
421
422 if (c.canEncode() && c.isRegistered()) {
423 String key = entry.getKey();
424
425 if (key.startsWith("cp")) {
426 // Custom CP437 charset changes
427 charsetIdsList.add("CP437");
428 charsetNamesList.add("CP437");
429 }
430
431 charsetIdsList.add(entry.getKey());
432 charsetNamesList.add(c.displayName());
433 }
434 }
435
436 charsetIds = charsetIdsList.toArray(new CharSequence[charsetIdsList.size()]);
437 charsetNames = charsetNamesList.toArray(new CharSequence[charsetNamesList.size()]);
438 initialized = true;
439 }
440
441 public static boolean isInitialized() {
442 return initialized;
443 }
444 }
445 }