11 package com.freerdp.freerdpcore.presentation;
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;
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;
37 import java.io.IOException;
38 import java.util.ArrayList;
40 public class BookmarkActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
42 public static final String PARAM_CONNECTION_REFERENCE =
"conRef";
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;
60 private static boolean settings_changed =
false;
61 private static boolean new_bookmark =
false;
62 private int current_preferences;
64 @Override
public void onCreate(Bundle savedInstanceState)
66 super.onCreate(savedInstanceState);
68 PreferenceManager mgr = getPreferenceManager();
70 mgr.setSharedPreferencesName(
"TEMP");
71 mgr.setSharedPreferencesMode(MODE_PRIVATE);
76 Bundle bundle = getIntent().getExtras();
80 if (bundle.containsKey(PARAM_CONNECTION_REFERENCE))
82 String refStr = bundle.getString(PARAM_CONNECTION_REFERENCE);
85 bookmark =
GlobalApp.getManualBookmarkGateway().findById(
103 bookmark.setLabel(file);
110 bookmark.setLabel(
new File(file).getName());
113 catch (IOException e)
115 Log.e(TAG,
"Failed reading RDP file", e);
122 if (bookmark ==
null)
126 if (current_preferences == PREFERENCES_ADVANCED &&
129 PreferenceScreen screen = getPreferenceScreen();
130 screen.removePreference(findPreference(
"bookmark.enable_gateway"));
131 screen.removePreference(findPreference(
"bookmark.gateway"));
134 updateH264Preferences();
137 bookmark.writeToSharedPreferences(mgr.getSharedPreferences());
140 settings_changed =
false;
144 if (getIntent() ==
null || getIntent().getData() ==
null)
146 addPreferencesFromResource(R.xml.bookmark_settings);
147 current_preferences = PREFERENCES_BOOKMARK;
149 else if (getIntent().getData().toString().equals(
"preferences://screen_settings"))
151 addPreferencesFromResource(R.xml.screen_settings);
152 current_preferences = PREFERENCES_SCREEN;
154 else if (getIntent().getData().toString().equals(
"preferences://performance_flags"))
156 addPreferencesFromResource(R.xml.performance_flags);
157 current_preferences = PREFERENCES_PERFORMANCE;
159 else if (getIntent().getData().toString().equals(
"preferences://screen_settings_3g"))
161 addPreferencesFromResource(R.xml.screen_settings_3g);
162 current_preferences = PREFERENCES_SCREEN3G;
164 else if (getIntent().getData().toString().equals(
"preferences://performance_flags_3g"))
166 addPreferencesFromResource(R.xml.performance_flags_3g);
167 current_preferences = PREFERENCES_PERFORMANCE3G;
169 else if (getIntent().getData().toString().equals(
"preferences://advanced_settings"))
171 addPreferencesFromResource(R.xml.advanced_settings);
172 current_preferences = PREFERENCES_ADVANCED;
174 else if (getIntent().getData().toString().equals(
"preferences://credentials_settings"))
176 addPreferencesFromResource(R.xml.credentials_settings);
177 current_preferences = PREFERENCES_CREDENTIALS;
179 else if (getIntent().getData().toString().equals(
"preferences://gateway_settings"))
181 addPreferencesFromResource(R.xml.gateway_settings);
182 current_preferences = PREFERENCES_GATEWAY;
184 else if (getIntent().getData().toString().equals(
"preferences://debug_settings"))
186 addPreferencesFromResource(R.xml.debug_settings);
187 current_preferences = PREFERENCES_DEBUG;
191 addPreferencesFromResource(R.xml.bookmark_settings);
192 current_preferences = PREFERENCES_BOOKMARK;
196 SharedPreferences spref = mgr.getSharedPreferences();
200 mgr.getSharedPreferences().registerOnSharedPreferenceChangeListener(
this);
203 setIntentComponentNames();
205 updateH264Preferences();
208 private void updateH264Preferences()
212 final int preferenceIdList[] = { R.string.preference_key_h264,
213 R.string.preference_key_h264_3g };
215 PreferenceManager mgr = getPreferenceManager();
216 for (
int id : preferenceIdList)
218 final String key = getString(
id);
219 Preference preference = mgr.findPreference(key);
220 if (preference !=
null)
222 preference.setEnabled(
false);
233 s = rdpFile.getString(
"full address");
237 if (s.lastIndexOf(
":") > s.lastIndexOf(
"]"))
241 String port = s.substring(s.lastIndexOf(
":") + 1);
242 bookmark.setPort(Integer.parseInt(port));
244 catch (NumberFormatException e)
246 Log.e(TAG,
"Malformed address");
249 s = s.substring(0, s.lastIndexOf(
":"));
253 if (s.startsWith(
"[") && s.endsWith(
"]"))
254 s = s.substring(1, s.length() - 1);
256 bookmark.setHostname(s);
259 i = rdpFile.getInteger(
"server port");
263 s = rdpFile.getString(
"username");
265 bookmark.setUsername(s);
267 s = rdpFile.getString(
"domain");
269 bookmark.setDomain(s);
271 i = rdpFile.getInteger(
"connect to console");
273 bookmark.getAdvancedSettings().setConsoleMode(i == 1);
276 private void setIntentComponentNames()
281 ComponentName compName =
283 ArrayList<String> prefKeys =
new ArrayList<>();
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");
294 for (String p : prefKeys)
296 Preference pref = findPreference(p);
298 pref.getIntent().setComponent(compName);
302 @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
304 settings_changed =
true;
305 switch (current_preferences)
307 case PREFERENCES_DEBUG:
308 debugSettingsChanged(sharedPreferences, key);
311 case PREFERENCES_BOOKMARK:
312 bookmarkSettingsChanged(sharedPreferences, key);
315 case PREFERENCES_ADVANCED:
316 advancedSettingsChanged(sharedPreferences, key);
319 case PREFERENCES_CREDENTIALS:
320 credentialsSettingsChanged(sharedPreferences, key);
323 case PREFERENCES_SCREEN:
324 case PREFERENCES_SCREEN3G:
325 screenSettingsChanged(sharedPreferences, key);
328 case PREFERENCES_GATEWAY:
329 gatewaySettingsChanged(sharedPreferences, key);
337 private void initSettings(SharedPreferences sharedPreferences)
339 switch (current_preferences)
341 case PREFERENCES_BOOKMARK:
342 initBookmarkSettings(sharedPreferences);
345 case PREFERENCES_ADVANCED:
346 initAdvancedSettings(sharedPreferences);
349 case PREFERENCES_CREDENTIALS:
350 initCredentialsSettings(sharedPreferences);
353 case PREFERENCES_SCREEN:
354 initScreenSettings(sharedPreferences);
357 case PREFERENCES_SCREEN3G:
358 initScreenSettings3G(sharedPreferences);
361 case PREFERENCES_GATEWAY:
362 initGatewaySettings(sharedPreferences);
365 case PREFERENCES_DEBUG:
366 initDebugSettings(sharedPreferences);
374 private void initBookmarkSettings(SharedPreferences sharedPreferences)
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");
383 private void bookmarkSettingsChanged(SharedPreferences sharedPreferences, String key)
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"))
393 String username = sharedPreferences.getString(key,
"<none>");
394 if (username.length() == 0)
396 findPreference(
"bookmark.credentials").setSummary(username);
398 else if (key.equals(
"bookmark.resolution") || key.equals(
"bookmark.colors") ||
399 key.equals(
"bookmark.width") || key.equals(
"bookmark.height"))
401 String resolution = sharedPreferences.getString(
"bookmark.resolution",
"800x600");
405 if (resolution.equals(
"automatic"))
407 resolution = getResources().getString(R.string.resolution_automatic);
409 if (resolution.equals(
"custom"))
411 resolution = getResources().getString(R.string.resolution_custom);
413 if (resolution.equals(
"fitscreen"))
415 resolution = getResources().getString(R.string.resolution_fit);
417 resolution +=
"@" + sharedPreferences.getInt(
"bookmark.colors", 16);
418 findPreference(
"bookmark.screen").setSummary(resolution);
422 private void initAdvancedSettings(SharedPreferences sharedPreferences)
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");
432 private void advancedSettingsChanged(SharedPreferences sharedPreferences, String key)
434 if (key.equals(
"bookmark.enable_gateway_settings"))
436 boolean enabled = sharedPreferences.getBoolean(key,
false);
437 findPreference(
"bookmark.gateway_settings").setEnabled(enabled);
439 else if (key.equals(
"bookmark.enable_3g_settings"))
441 boolean enabled = sharedPreferences.getBoolean(key,
false);
442 findPreference(
"bookmark.screen_3g").setEnabled(enabled);
443 findPreference(
"bookmark.performance_3g").setEnabled(enabled);
445 else if (key.equals(
"bookmark.security"))
447 ListPreference listPreference = (ListPreference)findPreference(key);
448 CharSequence security = listPreference.getEntries()[sharedPreferences.getInt(key, 0)];
449 listPreference.setSummary(security);
451 else if (key.equals(
"bookmark.resolution_3g") || key.equals(
"bookmark.colors_3g") ||
452 key.equals(
"bookmark.width_3g") || key.equals(
"bookmark.height_3g"))
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);
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,
""));
468 private void initCredentialsSettings(SharedPreferences sharedPreferences)
470 credentialsSettingsChanged(sharedPreferences,
"bookmark.username");
471 credentialsSettingsChanged(sharedPreferences,
"bookmark.password");
472 credentialsSettingsChanged(sharedPreferences,
"bookmark.domain");
475 private void credentialsSettingsChanged(SharedPreferences sharedPreferences, String key)
477 if (key.equals(
"bookmark.username"))
478 findPreference(key).setSummary(sharedPreferences.getString(key,
""));
479 else if (key.equals(
"bookmark.password"))
481 if (sharedPreferences.getString(key,
"").length() == 0)
482 findPreference(key).setSummary(
483 getResources().getString(R.string.settings_password_empty));
485 findPreference(key).setSummary(
486 getResources().getString(R.string.settings_password_present));
488 else if (key.equals(
"bookmark.domain"))
489 findPreference(key).setSummary(sharedPreferences.getString(key,
""));
492 private void initScreenSettings(SharedPreferences sharedPreferences)
494 screenSettingsChanged(sharedPreferences,
"bookmark.colors");
495 screenSettingsChanged(sharedPreferences,
"bookmark.resolution");
496 screenSettingsChanged(sharedPreferences,
"bookmark.width");
497 screenSettingsChanged(sharedPreferences,
"bookmark.height");
500 private void initScreenSettings3G(SharedPreferences sharedPreferences)
502 screenSettingsChanged(sharedPreferences,
"bookmark.colors_3g");
503 screenSettingsChanged(sharedPreferences,
"bookmark.resolution_3g");
504 screenSettingsChanged(sharedPreferences,
"bookmark.width_3g");
505 screenSettingsChanged(sharedPreferences,
"bookmark.height_3g");
508 private void screenSettingsChanged(SharedPreferences sharedPreferences, String key)
512 if (findPreference(key) ==
null)
515 if (key.equals(
"bookmark.colors") || key.equals(
"bookmark.colors_3g"))
517 ListPreference listPreference = (ListPreference)findPreference(key);
518 listPreference.setSummary(listPreference.getEntry());
520 else if (key.equals(
"bookmark.resolution") || key.equals(
"bookmark.resolution_3g"))
522 ListPreference listPreference = (ListPreference)findPreference(key);
523 listPreference.setSummary(listPreference.getEntry());
525 String value = listPreference.getValue();
526 boolean enabled = value.equalsIgnoreCase(
"custom");
527 if (key.equals(
"bookmark.resolution"))
529 findPreference(
"bookmark.width").setEnabled(enabled);
530 findPreference(
"bookmark.height").setEnabled(enabled);
534 findPreference(
"bookmark.width_3g").setEnabled(enabled);
535 findPreference(
"bookmark.height_3g").setEnabled(enabled);
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)));
544 private void initDebugSettings(SharedPreferences sharedPreferences)
546 debugSettingsChanged(sharedPreferences,
"bookmark.debug_level");
547 debugSettingsChanged(sharedPreferences,
"bookmark.async_channel");
548 debugSettingsChanged(sharedPreferences,
"bookmark.async_update");
551 private void initGatewaySettings(SharedPreferences sharedPreferences)
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");
560 private void debugSettingsChanged(SharedPreferences sharedPreferences, String key)
562 if (key.equals(
"bookmark.debug_level"))
564 String level = sharedPreferences.getString(key,
"INFO");
565 Preference pref = findPreference(
"bookmark.debug_level");
566 pref.setDefaultValue(level);
568 else if (key.equals(
"bookmark.async_channel"))
570 boolean enabled = sharedPreferences.getBoolean(key,
false);
571 Preference pref = findPreference(
"bookmark.async_channel");
572 pref.setDefaultValue(enabled);
574 else if (key.equals(
"bookmark.async_update"))
576 boolean enabled = sharedPreferences.getBoolean(key,
false);
577 Preference pref = findPreference(
"bookmark.async_update");
578 pref.setDefaultValue(enabled);
582 private void gatewaySettingsChanged(SharedPreferences sharedPreferences, String key)
584 if (key.equals(
"bookmark.gateway_hostname"))
586 findPreference(key).setSummary(sharedPreferences.getString(key,
""));
588 else if (key.equals(
"bookmark.gateway_port"))
590 findPreference(key).setSummary(String.valueOf(sharedPreferences.getInt(key, 443)));
592 else if (key.equals(
"bookmark.gateway_username"))
594 findPreference(key).setSummary(sharedPreferences.getString(key,
""));
596 else if (key.equals(
"bookmark.gateway_password"))
598 if (sharedPreferences.getString(key,
"").length() == 0)
599 findPreference(key).setSummary(
600 getResources().getString(R.string.settings_password_empty));
602 findPreference(key).setSummary(
603 getResources().getString(R.string.settings_password_present));
605 else if (key.equals(
"bookmark.gateway_domain"))
606 findPreference(key).setSummary(sharedPreferences.getString(key,
""));
609 private boolean verifySettings(SharedPreferences sharedPreferences)
612 boolean verifyFailed =
false;
615 if (sharedPreferences.getString(
"bookmark.label",
"").length() == 0)
619 if (!verifyFailed && sharedPreferences.getString(
"bookmark.hostname",
"").length() == 0)
623 if (!verifyFailed && sharedPreferences.getInt(
"bookmark.port", -1) <= 0)
627 return (!verifyFailed);
630 private void finishAndResetBookmark()
633 getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
638 @Override
public void onBackPressed()
641 if (current_preferences != PREFERENCES_BOOKMARK)
643 super.onBackPressed();
644 getPreferenceManager()
645 .getSharedPreferences()
646 .unregisterOnSharedPreferenceChangeListener(
this);
650 SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
651 if (!verifySettings(sharedPreferences))
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() {
660 public void onClick(DialogInterface dialog, int which)
662 finishAndResetBookmark();
665 .setNegativeButton(R.string.cont,
666 new DialogInterface.OnClickListener() {
668 public void onClick(DialogInterface dialog, int which)
681 if (new_bookmark || settings_changed)
683 AlertDialog.Builder builder =
new AlertDialog.Builder(
this);
684 builder.setTitle(R.string.dlg_title_save_bookmark)
685 .setMessage(R.string.dlg_save_bookmark)
688 new DialogInterface.OnClickListener() {
689 @Override public void onClick(DialogInterface dialog, int which)
692 bookmark.readFromSharedPreferences(
693 getPreferenceManager().getSharedPreferences());
695 BookmarkBaseGateway bookmarkGateway;
696 if (bookmark.getType() == BookmarkBase.TYPE_MANUAL)
698 bookmarkGateway = GlobalApp.getManualBookmarkGateway();
701 GlobalApp.getQuickConnectHistoryGateway().removeHistoryItem(
702 bookmark.<ManualBookmark>get().getHostname());
712 if (bookmark.getId() > 0)
713 bookmarkGateway.update(bookmark);
715 bookmarkGateway.insert(bookmark);
717 finishAndResetBookmark();
720 .setNegativeButton(R.string.no,
721 new DialogInterface.OnClickListener() {
723 public void onClick(DialogInterface dialog, int which)
725 finishAndResetBookmark();
732 finishAndResetBookmark();