FreeRDP
BookmarkActivity.java
1 /*
2  Bookmark editing activity
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.presentation;
12 
13 import android.app.AlertDialog;
14 import android.content.ComponentName;
15 import android.content.DialogInterface;
16 import android.content.SharedPreferences;
17 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
18 import android.os.Bundle;
19 import android.preference.ListPreference;
20 import android.preference.Preference;
21 import android.preference.PreferenceActivity;
22 import android.preference.PreferenceManager;
23 import android.preference.PreferenceScreen;
24 import android.util.Log;
25 import android.view.View;
26 
27 import com.freerdp.freerdpcore.R;
28 import com.freerdp.freerdpcore.application.GlobalApp;
29 import com.freerdp.freerdpcore.domain.BookmarkBase;
30 import com.freerdp.freerdpcore.domain.ConnectionReference;
31 import com.freerdp.freerdpcore.domain.ManualBookmark;
32 import com.freerdp.freerdpcore.services.BookmarkBaseGateway;
33 import com.freerdp.freerdpcore.services.LibFreeRDP;
34 import com.freerdp.freerdpcore.utils.RDPFileParser;
35 
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 
40 public class BookmarkActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
41 {
42  public static final String PARAM_CONNECTION_REFERENCE = "conRef";
43 
44  private static final String TAG = "BookmarkActivity";
45  private static final int PREFERENCES_BOOKMARK = 1;
46  private static final int PREFERENCES_CREDENTIALS = 2;
47  private static final int PREFERENCES_SCREEN = 3;
48  private static final int PREFERENCES_PERFORMANCE = 4;
49  private static final int PREFERENCES_ADVANCED = 5;
50  private static final int PREFERENCES_SCREEN3G = 6;
51  private static final int PREFERENCES_PERFORMANCE3G = 7;
52  private static final int PREFERENCES_GATEWAY = 8;
53  private static final int PREFERENCES_DEBUG = 9;
54  // bookmark needs to be static because the activity is started for each
55  // subview
56  // (we have to do this because Android has a bug where the style for
57  // Preferences
58  // is only applied to the first PreferenceScreen but not to subsequent ones)
59  private static BookmarkBase bookmark = null;
60  private static boolean settings_changed = false;
61  private static boolean new_bookmark = false;
62  private int current_preferences;
63 
64  @Override public void onCreate(Bundle savedInstanceState)
65  {
66  super.onCreate(savedInstanceState);
67 
68  PreferenceManager mgr = getPreferenceManager();
69  // init shared preferences for activity
70  mgr.setSharedPreferencesName("TEMP");
71  mgr.setSharedPreferencesMode(MODE_PRIVATE);
72 
73  if (bookmark == null)
74  {
75  // if we have a bookmark id set in the extras we are in edit mode
76  Bundle bundle = getIntent().getExtras();
77  if (bundle != null)
78  {
79  // See if we got a connection reference to a bookmark
80  if (bundle.containsKey(PARAM_CONNECTION_REFERENCE))
81  {
82  String refStr = bundle.getString(PARAM_CONNECTION_REFERENCE);
83  if (ConnectionReference.isManualBookmarkReference(refStr))
84  {
85  bookmark = GlobalApp.getManualBookmarkGateway().findById(
86  ConnectionReference.getManualBookmarkId(refStr));
87  new_bookmark = false;
88  }
89  else if (ConnectionReference.isHostnameReference(refStr))
90  {
91  bookmark = new ManualBookmark();
92  bookmark.<ManualBookmark>get().setLabel(
93  ConnectionReference.getHostname(refStr));
94  bookmark.<ManualBookmark>get().setHostname(
95  ConnectionReference.getHostname(refStr));
96  new_bookmark = true;
97  }
98  else if (ConnectionReference.isFileReference(refStr))
99  {
100  String file = ConnectionReference.getFile(refStr);
101 
102  bookmark = new ManualBookmark();
103  bookmark.setLabel(file);
104 
105  try
106  {
107  RDPFileParser rdpFile = new RDPFileParser(file);
108  updateBookmarkFromFile((ManualBookmark)bookmark, rdpFile);
109 
110  bookmark.setLabel(new File(file).getName());
111  new_bookmark = true;
112  }
113  catch (IOException e)
114  {
115  Log.e(TAG, "Failed reading RDP file", e);
116  }
117  }
118  }
119  }
120 
121  // last chance - ensure we really have a valid bookmark
122  if (bookmark == null)
123  bookmark = new ManualBookmark();
124 
125  // hide gateway settings if we edit a non-manual bookmark
126  if (current_preferences == PREFERENCES_ADVANCED &&
127  bookmark.getType() != ManualBookmark.TYPE_MANUAL)
128  {
129  PreferenceScreen screen = getPreferenceScreen();
130  screen.removePreference(findPreference("bookmark.enable_gateway"));
131  screen.removePreference(findPreference("bookmark.gateway"));
132  }
133 
134  updateH264Preferences();
135 
136  // update preferences from bookmark
137  bookmark.writeToSharedPreferences(mgr.getSharedPreferences());
138 
139  // no settings changed yet
140  settings_changed = false;
141  }
142 
143  // load the requested settings resource
144  if (getIntent() == null || getIntent().getData() == null)
145  {
146  addPreferencesFromResource(R.xml.bookmark_settings);
147  current_preferences = PREFERENCES_BOOKMARK;
148  }
149  else if (getIntent().getData().toString().equals("preferences://screen_settings"))
150  {
151  addPreferencesFromResource(R.xml.screen_settings);
152  current_preferences = PREFERENCES_SCREEN;
153  }
154  else if (getIntent().getData().toString().equals("preferences://performance_flags"))
155  {
156  addPreferencesFromResource(R.xml.performance_flags);
157  current_preferences = PREFERENCES_PERFORMANCE;
158  }
159  else if (getIntent().getData().toString().equals("preferences://screen_settings_3g"))
160  {
161  addPreferencesFromResource(R.xml.screen_settings_3g);
162  current_preferences = PREFERENCES_SCREEN3G;
163  }
164  else if (getIntent().getData().toString().equals("preferences://performance_flags_3g"))
165  {
166  addPreferencesFromResource(R.xml.performance_flags_3g);
167  current_preferences = PREFERENCES_PERFORMANCE3G;
168  }
169  else if (getIntent().getData().toString().equals("preferences://advanced_settings"))
170  {
171  addPreferencesFromResource(R.xml.advanced_settings);
172  current_preferences = PREFERENCES_ADVANCED;
173  }
174  else if (getIntent().getData().toString().equals("preferences://credentials_settings"))
175  {
176  addPreferencesFromResource(R.xml.credentials_settings);
177  current_preferences = PREFERENCES_CREDENTIALS;
178  }
179  else if (getIntent().getData().toString().equals("preferences://gateway_settings"))
180  {
181  addPreferencesFromResource(R.xml.gateway_settings);
182  current_preferences = PREFERENCES_GATEWAY;
183  }
184  else if (getIntent().getData().toString().equals("preferences://debug_settings"))
185  {
186  addPreferencesFromResource(R.xml.debug_settings);
187  current_preferences = PREFERENCES_DEBUG;
188  }
189  else
190  {
191  addPreferencesFromResource(R.xml.bookmark_settings);
192  current_preferences = PREFERENCES_BOOKMARK;
193  }
194 
195  // update UI with bookmark data
196  SharedPreferences spref = mgr.getSharedPreferences();
197  initSettings(spref);
198 
199  // register for preferences changed notification
200  mgr.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
201 
202  // set the correct component names in our preferencescreen settings
203  setIntentComponentNames();
204 
205  updateH264Preferences();
206  }
207 
208  private void updateH264Preferences()
209  {
210  if (!LibFreeRDP.hasH264Support())
211  {
212  final int preferenceIdList[] = { R.string.preference_key_h264,
213  R.string.preference_key_h264_3g };
214 
215  PreferenceManager mgr = getPreferenceManager();
216  for (int id : preferenceIdList)
217  {
218  final String key = getString(id);
219  Preference preference = mgr.findPreference(key);
220  if (preference != null)
221  {
222  preference.setEnabled(false);
223  }
224  }
225  }
226  }
227 
228  private void updateBookmarkFromFile(ManualBookmark bookmark, RDPFileParser rdpFile)
229  {
230  String s;
231  Integer i;
232 
233  s = rdpFile.getString("full address");
234  if (s != null)
235  {
236  // this gets complicated as it can include port
237  if (s.lastIndexOf(":") > s.lastIndexOf("]"))
238  {
239  try
240  {
241  String port = s.substring(s.lastIndexOf(":") + 1);
242  bookmark.setPort(Integer.parseInt(port));
243  }
244  catch (NumberFormatException e)
245  {
246  Log.e(TAG, "Malformed address");
247  }
248 
249  s = s.substring(0, s.lastIndexOf(":"));
250  }
251 
252  // or even be an ipv6 address
253  if (s.startsWith("[") && s.endsWith("]"))
254  s = s.substring(1, s.length() - 1);
255 
256  bookmark.setHostname(s);
257  }
258 
259  i = rdpFile.getInteger("server port");
260  if (i != null)
261  bookmark.setPort(i);
262 
263  s = rdpFile.getString("username");
264  if (s != null)
265  bookmark.setUsername(s);
266 
267  s = rdpFile.getString("domain");
268  if (s != null)
269  bookmark.setDomain(s);
270 
271  i = rdpFile.getInteger("connect to console");
272  if (i != null)
273  bookmark.getAdvancedSettings().setConsoleMode(i == 1);
274  }
275 
276  private void setIntentComponentNames()
277  {
278  // we set the component name for our sub-activity calls here because we
279  // don't know the package
280  // name of the main app in our library project.
281  ComponentName compName =
282  new ComponentName(getPackageName(), BookmarkActivity.class.getName());
283  ArrayList<String> prefKeys = new ArrayList<>();
284 
285  prefKeys.add("bookmark.credentials");
286  prefKeys.add("bookmark.screen");
287  prefKeys.add("bookmark.performance");
288  prefKeys.add("bookmark.advanced");
289  prefKeys.add("bookmark.screen_3g");
290  prefKeys.add("bookmark.performance_3g");
291  prefKeys.add("bookmark.gateway_settings");
292  prefKeys.add("bookmark.debug");
293 
294  for (String p : prefKeys)
295  {
296  Preference pref = findPreference(p);
297  if (pref != null)
298  pref.getIntent().setComponent(compName);
299  }
300  }
301 
302  @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
303  {
304  settings_changed = true;
305  switch (current_preferences)
306  {
307  case PREFERENCES_DEBUG:
308  debugSettingsChanged(sharedPreferences, key);
309  break;
310 
311  case PREFERENCES_BOOKMARK:
312  bookmarkSettingsChanged(sharedPreferences, key);
313  break;
314 
315  case PREFERENCES_ADVANCED:
316  advancedSettingsChanged(sharedPreferences, key);
317  break;
318 
319  case PREFERENCES_CREDENTIALS:
320  credentialsSettingsChanged(sharedPreferences, key);
321  break;
322 
323  case PREFERENCES_SCREEN:
324  case PREFERENCES_SCREEN3G:
325  screenSettingsChanged(sharedPreferences, key);
326  break;
327 
328  case PREFERENCES_GATEWAY:
329  gatewaySettingsChanged(sharedPreferences, key);
330  break;
331 
332  default:
333  break;
334  }
335  }
336 
337  private void initSettings(SharedPreferences sharedPreferences)
338  {
339  switch (current_preferences)
340  {
341  case PREFERENCES_BOOKMARK:
342  initBookmarkSettings(sharedPreferences);
343  break;
344 
345  case PREFERENCES_ADVANCED:
346  initAdvancedSettings(sharedPreferences);
347  break;
348 
349  case PREFERENCES_CREDENTIALS:
350  initCredentialsSettings(sharedPreferences);
351  break;
352 
353  case PREFERENCES_SCREEN:
354  initScreenSettings(sharedPreferences);
355  break;
356 
357  case PREFERENCES_SCREEN3G:
358  initScreenSettings3G(sharedPreferences);
359  break;
360 
361  case PREFERENCES_GATEWAY:
362  initGatewaySettings(sharedPreferences);
363  break;
364 
365  case PREFERENCES_DEBUG:
366  initDebugSettings(sharedPreferences);
367  break;
368 
369  default:
370  break;
371  }
372  }
373 
374  private void initBookmarkSettings(SharedPreferences sharedPreferences)
375  {
376  bookmarkSettingsChanged(sharedPreferences, "bookmark.label");
377  bookmarkSettingsChanged(sharedPreferences, "bookmark.hostname");
378  bookmarkSettingsChanged(sharedPreferences, "bookmark.port");
379  bookmarkSettingsChanged(sharedPreferences, "bookmark.username");
380  bookmarkSettingsChanged(sharedPreferences, "bookmark.resolution");
381  }
382 
383  private void bookmarkSettingsChanged(SharedPreferences sharedPreferences, String key)
384  {
385  if (key.equals("bookmark.label") && findPreference(key) != null)
386  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
387  else if (key.equals("bookmark.hostname") && findPreference(key) != null)
388  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
389  else if (key.equals("bookmark.port") && findPreference(key) != null)
390  findPreference(key).setSummary(String.valueOf(sharedPreferences.getInt(key, -1)));
391  else if (key.equals("bookmark.username"))
392  {
393  String username = sharedPreferences.getString(key, "<none>");
394  if (username.length() == 0)
395  username = "<none>";
396  findPreference("bookmark.credentials").setSummary(username);
397  }
398  else if (key.equals("bookmark.resolution") || key.equals("bookmark.colors") ||
399  key.equals("bookmark.width") || key.equals("bookmark.height"))
400  {
401  String resolution = sharedPreferences.getString("bookmark.resolution", "800x600");
402  // compare english string from resolutions_values_array array,
403  // decode to localized
404  // text for display
405  if (resolution.equals("automatic"))
406  {
407  resolution = getResources().getString(R.string.resolution_automatic);
408  }
409  if (resolution.equals("custom"))
410  {
411  resolution = getResources().getString(R.string.resolution_custom);
412  }
413  if (resolution.equals("fitscreen"))
414  {
415  resolution = getResources().getString(R.string.resolution_fit);
416  }
417  resolution += "@" + sharedPreferences.getInt("bookmark.colors", 16);
418  findPreference("bookmark.screen").setSummary(resolution);
419  }
420  }
421 
422  private void initAdvancedSettings(SharedPreferences sharedPreferences)
423  {
424  advancedSettingsChanged(sharedPreferences, "bookmark.enable_gateway_settings");
425  advancedSettingsChanged(sharedPreferences, "bookmark.enable_3g_settings");
426  advancedSettingsChanged(sharedPreferences, "bookmark.security");
427  advancedSettingsChanged(sharedPreferences, "bookmark.resolution_3g");
428  advancedSettingsChanged(sharedPreferences, "bookmark.remote_program");
429  advancedSettingsChanged(sharedPreferences, "bookmark.work_dir");
430  }
431 
432  private void advancedSettingsChanged(SharedPreferences sharedPreferences, String key)
433  {
434  if (key.equals("bookmark.enable_gateway_settings"))
435  {
436  boolean enabled = sharedPreferences.getBoolean(key, false);
437  findPreference("bookmark.gateway_settings").setEnabled(enabled);
438  }
439  else if (key.equals("bookmark.enable_3g_settings"))
440  {
441  boolean enabled = sharedPreferences.getBoolean(key, false);
442  findPreference("bookmark.screen_3g").setEnabled(enabled);
443  findPreference("bookmark.performance_3g").setEnabled(enabled);
444  }
445  else if (key.equals("bookmark.security"))
446  {
447  ListPreference listPreference = (ListPreference)findPreference(key);
448  CharSequence security = listPreference.getEntries()[sharedPreferences.getInt(key, 0)];
449  listPreference.setSummary(security);
450  }
451  else if (key.equals("bookmark.resolution_3g") || key.equals("bookmark.colors_3g") ||
452  key.equals("bookmark.width_3g") || key.equals("bookmark.height_3g"))
453  {
454  String resolution = sharedPreferences.getString("bookmark.resolution_3g", "800x600");
455  if (resolution.equals("automatic"))
456  resolution = getResources().getString(R.string.resolution_automatic);
457  else if (resolution.equals("custom"))
458  resolution = getResources().getString(R.string.resolution_custom);
459  resolution += "@" + sharedPreferences.getInt("bookmark.colors_3g", 16);
460  findPreference("bookmark.screen_3g").setSummary(resolution);
461  }
462  else if (key.equals("bookmark.remote_program"))
463  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
464  else if (key.equals("bookmark.work_dir"))
465  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
466  }
467 
468  private void initCredentialsSettings(SharedPreferences sharedPreferences)
469  {
470  credentialsSettingsChanged(sharedPreferences, "bookmark.username");
471  credentialsSettingsChanged(sharedPreferences, "bookmark.password");
472  credentialsSettingsChanged(sharedPreferences, "bookmark.domain");
473  }
474 
475  private void credentialsSettingsChanged(SharedPreferences sharedPreferences, String key)
476  {
477  if (key.equals("bookmark.username"))
478  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
479  else if (key.equals("bookmark.password"))
480  {
481  if (sharedPreferences.getString(key, "").length() == 0)
482  findPreference(key).setSummary(
483  getResources().getString(R.string.settings_password_empty));
484  else
485  findPreference(key).setSummary(
486  getResources().getString(R.string.settings_password_present));
487  }
488  else if (key.equals("bookmark.domain"))
489  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
490  }
491 
492  private void initScreenSettings(SharedPreferences sharedPreferences)
493  {
494  screenSettingsChanged(sharedPreferences, "bookmark.colors");
495  screenSettingsChanged(sharedPreferences, "bookmark.resolution");
496  screenSettingsChanged(sharedPreferences, "bookmark.width");
497  screenSettingsChanged(sharedPreferences, "bookmark.height");
498  }
499 
500  private void initScreenSettings3G(SharedPreferences sharedPreferences)
501  {
502  screenSettingsChanged(sharedPreferences, "bookmark.colors_3g");
503  screenSettingsChanged(sharedPreferences, "bookmark.resolution_3g");
504  screenSettingsChanged(sharedPreferences, "bookmark.width_3g");
505  screenSettingsChanged(sharedPreferences, "bookmark.height_3g");
506  }
507 
508  private void screenSettingsChanged(SharedPreferences sharedPreferences, String key)
509  {
510  // could happen during initialization because 3g and non-3g settings
511  // share this routine - just skip
512  if (findPreference(key) == null)
513  return;
514 
515  if (key.equals("bookmark.colors") || key.equals("bookmark.colors_3g"))
516  {
517  ListPreference listPreference = (ListPreference)findPreference(key);
518  listPreference.setSummary(listPreference.getEntry());
519  }
520  else if (key.equals("bookmark.resolution") || key.equals("bookmark.resolution_3g"))
521  {
522  ListPreference listPreference = (ListPreference)findPreference(key);
523  listPreference.setSummary(listPreference.getEntry());
524 
525  String value = listPreference.getValue();
526  boolean enabled = value.equalsIgnoreCase("custom");
527  if (key.equals("bookmark.resolution"))
528  {
529  findPreference("bookmark.width").setEnabled(enabled);
530  findPreference("bookmark.height").setEnabled(enabled);
531  }
532  else
533  {
534  findPreference("bookmark.width_3g").setEnabled(enabled);
535  findPreference("bookmark.height_3g").setEnabled(enabled);
536  }
537  }
538  else if (key.equals("bookmark.width") || key.equals("bookmark.width_3g"))
539  findPreference(key).setSummary(String.valueOf(sharedPreferences.getInt(key, 800)));
540  else if (key.equals("bookmark.height") || key.equals("bookmark.height_3g"))
541  findPreference(key).setSummary(String.valueOf(sharedPreferences.getInt(key, 600)));
542  }
543 
544  private void initDebugSettings(SharedPreferences sharedPreferences)
545  {
546  debugSettingsChanged(sharedPreferences, "bookmark.debug_level");
547  debugSettingsChanged(sharedPreferences, "bookmark.async_channel");
548  debugSettingsChanged(sharedPreferences, "bookmark.async_update");
549  }
550 
551  private void initGatewaySettings(SharedPreferences sharedPreferences)
552  {
553  gatewaySettingsChanged(sharedPreferences, "bookmark.gateway_hostname");
554  gatewaySettingsChanged(sharedPreferences, "bookmark.gateway_port");
555  gatewaySettingsChanged(sharedPreferences, "bookmark.gateway_username");
556  gatewaySettingsChanged(sharedPreferences, "bookmark.gateway_password");
557  gatewaySettingsChanged(sharedPreferences, "bookmark.gateway_domain");
558  }
559 
560  private void debugSettingsChanged(SharedPreferences sharedPreferences, String key)
561  {
562  if (key.equals("bookmark.debug_level"))
563  {
564  String level = sharedPreferences.getString(key, "INFO");
565  Preference pref = findPreference("bookmark.debug_level");
566  pref.setDefaultValue(level);
567  }
568  else if (key.equals("bookmark.async_channel"))
569  {
570  boolean enabled = sharedPreferences.getBoolean(key, false);
571  Preference pref = findPreference("bookmark.async_channel");
572  pref.setDefaultValue(enabled);
573  }
574  else if (key.equals("bookmark.async_update"))
575  {
576  boolean enabled = sharedPreferences.getBoolean(key, false);
577  Preference pref = findPreference("bookmark.async_update");
578  pref.setDefaultValue(enabled);
579  }
580  }
581 
582  private void gatewaySettingsChanged(SharedPreferences sharedPreferences, String key)
583  {
584  if (key.equals("bookmark.gateway_hostname"))
585  {
586  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
587  }
588  else if (key.equals("bookmark.gateway_port"))
589  {
590  findPreference(key).setSummary(String.valueOf(sharedPreferences.getInt(key, 443)));
591  }
592  else if (key.equals("bookmark.gateway_username"))
593  {
594  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
595  }
596  else if (key.equals("bookmark.gateway_password"))
597  {
598  if (sharedPreferences.getString(key, "").length() == 0)
599  findPreference(key).setSummary(
600  getResources().getString(R.string.settings_password_empty));
601  else
602  findPreference(key).setSummary(
603  getResources().getString(R.string.settings_password_present));
604  }
605  else if (key.equals("bookmark.gateway_domain"))
606  findPreference(key).setSummary(sharedPreferences.getString(key, ""));
607  }
608 
609  private boolean verifySettings(SharedPreferences sharedPreferences)
610  {
611 
612  boolean verifyFailed = false;
613  // perform sanity checks on settings
614  // Label set
615  if (sharedPreferences.getString("bookmark.label", "").length() == 0)
616  verifyFailed = true;
617 
618  // Server and port specified
619  if (!verifyFailed && sharedPreferences.getString("bookmark.hostname", "").length() == 0)
620  verifyFailed = true;
621 
622  // Server and port specified
623  if (!verifyFailed && sharedPreferences.getInt("bookmark.port", -1) <= 0)
624  verifyFailed = true;
625 
626  // if an error occurred - display toast and return false
627  return (!verifyFailed);
628  }
629 
630  private void finishAndResetBookmark()
631  {
632  bookmark = null;
633  getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
634  this);
635  finish();
636  }
637 
638  @Override public void onBackPressed()
639  {
640  // only proceed if we are in the main preferences screen
641  if (current_preferences != PREFERENCES_BOOKMARK)
642  {
643  super.onBackPressed();
644  getPreferenceManager()
645  .getSharedPreferences()
646  .unregisterOnSharedPreferenceChangeListener(this);
647  return;
648  }
649 
650  SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
651  if (!verifySettings(sharedPreferences))
652  {
653  // ask the user if he wants to cancel or continue editing
654  AlertDialog.Builder builder = new AlertDialog.Builder(this);
655  builder.setTitle(R.string.error_bookmark_incomplete_title)
656  .setMessage(R.string.error_bookmark_incomplete)
657  .setPositiveButton(R.string.cancel,
658  new DialogInterface.OnClickListener() {
659  @Override
660  public void onClick(DialogInterface dialog, int which)
661  {
662  finishAndResetBookmark();
663  }
664  })
665  .setNegativeButton(R.string.cont,
666  new DialogInterface.OnClickListener() {
667  @Override
668  public void onClick(DialogInterface dialog, int which)
669  {
670  dialog.cancel();
671  }
672  })
673  .show();
674 
675  return;
676  }
677  else
678  {
679  // ask the user if he wants to save or cancel editing if a setting
680  // has changed
681  if (new_bookmark || settings_changed)
682  {
683  AlertDialog.Builder builder = new AlertDialog.Builder(this);
684  builder.setTitle(R.string.dlg_title_save_bookmark)
685  .setMessage(R.string.dlg_save_bookmark)
686  .setPositiveButton(
687  R.string.yes,
688  new DialogInterface.OnClickListener() {
689  @Override public void onClick(DialogInterface dialog, int which)
690  {
691  // read shared prefs back to bookmark
692  bookmark.readFromSharedPreferences(
693  getPreferenceManager().getSharedPreferences());
694 
695  BookmarkBaseGateway bookmarkGateway;
696  if (bookmark.getType() == BookmarkBase.TYPE_MANUAL)
697  {
698  bookmarkGateway = GlobalApp.getManualBookmarkGateway();
699  // remove any history entry for this
700  // bookmark
701  GlobalApp.getQuickConnectHistoryGateway().removeHistoryItem(
702  bookmark.<ManualBookmark>get().getHostname());
703  }
704  else
705  {
706  assert false;
707  return;
708  }
709 
710  // insert or update bookmark and leave
711  // activity
712  if (bookmark.getId() > 0)
713  bookmarkGateway.update(bookmark);
714  else
715  bookmarkGateway.insert(bookmark);
716 
717  finishAndResetBookmark();
718  }
719  })
720  .setNegativeButton(R.string.no,
721  new DialogInterface.OnClickListener() {
722  @Override
723  public void onClick(DialogInterface dialog, int which)
724  {
725  finishAndResetBookmark();
726  }
727  })
728  .show();
729  }
730  else
731  {
732  finishAndResetBookmark();
733  }
734  }
735  }
736 }