11 package com.freerdp.freerdpcore.presentation;
13 import android.annotation.TargetApi;
14 import android.content.Context;
15 import android.content.DialogInterface;
16 import android.content.SharedPreferences;
17 import android.content.res.Configuration;
18 import android.os.Build;
19 import android.os.Bundle;
20 import android.preference.EditTextPreference;
21 import android.preference.Preference;
22 import android.preference.PreferenceFragment;
23 import android.preference.PreferenceManager;
24 import android.preference.PreferenceScreen;
25 import androidx.appcompat.app.AlertDialog;
26 import android.widget.Toast;
28 import com.freerdp.freerdpcore.R;
29 import com.freerdp.freerdpcore.utils.AppCompatPreferenceActivity;
32 import java.util.List;
33 import java.util.UUID;
37 private static boolean isXLargeTablet(Context context)
39 return (context.getResources().getConfiguration().screenLayout &
40 Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
43 @Override
protected void onCreate(Bundle savedInstanceState)
45 super.onCreate(savedInstanceState);
49 private void setupActionBar()
51 android.app.ActionBar actionBar = getActionBar();
52 if (actionBar !=
null)
54 actionBar.setDisplayHomeAsUpEnabled(
true);
58 @Override
public boolean onIsMultiPane()
60 return isXLargeTablet(
this);
64 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
65 public void onBuildHeaders(List<Header> target)
67 loadHeadersFromResource(R.xml.settings_app_headers, target);
70 protected boolean isValidFragment(String fragmentName)
72 return PreferenceFragment.class.getName().equals(fragmentName) ||
79 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
81 extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
83 @Override
public void onCreate(Bundle savedInstanceState)
85 super.onCreate(savedInstanceState);
86 addPreferencesFromResource(R.xml.settings_app_client);
87 SharedPreferences preferences =
get(getActivity());
88 preferences.registerOnSharedPreferenceChangeListener(
this);
92 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
96 final String clientNameKey = getString(R.string.preference_key_client_name);
99 if (key.equals(clientNameKey))
101 final String clientNameValue = sharedPreferences.getString(clientNameKey,
"");
102 EditTextPreference pref = (EditTextPreference)findPreference(clientNameKey);
103 pref.setText(clientNameValue);
109 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
112 @Override
public void onCreate(Bundle savedInstanceState)
114 super.onCreate(savedInstanceState);
115 addPreferencesFromResource(R.xml.settings_app_ui);
119 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
122 @Override
public void onCreate(Bundle savedInstanceState)
124 super.onCreate(savedInstanceState);
125 addPreferencesFromResource(R.xml.settings_app_power);
129 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
132 @Override
public void onCreate(Bundle savedInstanceState)
134 super.onCreate(savedInstanceState);
135 addPreferencesFromResource(R.xml.settings_app_security);
139 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
140 Preference preference)
143 getString(R.string.preference_key_security_clear_certificate_cache);
144 if (preference.getKey().equals(clear))
151 return super.onPreferenceTreeClick(preferenceScreen, preference);
155 private void showDialog()
157 new AlertDialog.Builder(getActivity())
158 .setTitle(R.string.dlg_title_clear_cert_cache)
159 .setMessage(R.string.dlg_msg_clear_cert_cache)
160 .setPositiveButton(android.R.string.ok,
161 new DialogInterface.OnClickListener() {
163 public void onClick(DialogInterface dialog, int which)
165 clearCertificateCache();
169 .setNegativeButton(android.R.string.cancel,
170 new DialogInterface.OnClickListener() {
172 public void onClick(DialogInterface dialog, int which)
177 .setIcon(android.R.drawable.ic_delete)
181 private boolean deleteDirectory(File dir)
183 if (dir.isDirectory())
185 String[] children = dir.list();
186 for (String file : children)
188 if (!deleteDirectory(
new File(dir, file)))
195 private void clearCertificateCache()
197 Context context = getActivity();
198 if ((
new File(context.getFilesDir() +
"/.freerdp")).exists())
200 if (deleteDirectory(
new File(context.getFilesDir() +
"/.freerdp")))
201 Toast.makeText(context, R.string.info_reset_success, Toast.LENGTH_LONG).show();
203 Toast.makeText(context, R.string.info_reset_failed, Toast.LENGTH_LONG).show();
206 Toast.makeText(context, R.string.info_reset_success, Toast.LENGTH_LONG).show();
210 public static SharedPreferences
get(Context context)
212 Context appContext = context.getApplicationContext();
213 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_client,
false);
214 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_power,
false);
215 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_security,
false);
216 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_ui,
false);
217 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
219 final String key = context.getString(R.string.preference_key_client_name);
220 final String value = preferences.getString(key,
"");
223 final String android_id = UUID.randomUUID().toString();
224 final String defaultValue = context.getString(R.string.preference_default_client_name);
225 final String name = defaultValue +
"-" + android_id;
226 preferences.edit().putString(key, name.substring(0, 31)).apply();
232 public static int getDisconnectTimeout(Context context)
234 SharedPreferences preferences =
get(context);
235 return preferences.getInt(
236 context.getString(R.string.preference_key_power_disconnect_timeout), 0);
239 public static boolean getHideStatusBar(Context context)
241 SharedPreferences preferences =
get(context);
242 return preferences.getBoolean(context.getString(R.string.preference_key_ui_hide_status_bar),
246 public static boolean getHideActionBar(Context context)
248 SharedPreferences preferences =
get(context);
249 return preferences.getBoolean(context.getString(R.string.preference_key_ui_hide_action_bar),
253 public static boolean getUseBackAsAltf4(Context context)
255 SharedPreferences preferences =
get(context);
256 return preferences.getBoolean(
257 context.getString(R.string.preference_key_ui_use_back_as_altf4),
true);
260 public static boolean getAcceptAllCertificates(Context context)
262 SharedPreferences preferences =
get(context);
263 return preferences.getBoolean(
264 context.getString(R.string.preference_key_accept_certificates),
false);
267 public static boolean getHideZoomControls(Context context)
269 SharedPreferences preferences =
get(context);
270 return preferences.getBoolean(
271 context.getString(R.string.preference_key_ui_hide_zoom_controls),
false);
274 public static boolean getSwapMouseButtons(Context context)
276 SharedPreferences preferences =
get(context);
277 return preferences.getBoolean(
278 context.getString(R.string.preference_key_ui_swap_mouse_buttons),
false);
281 public static boolean getInvertScrolling(Context context)
283 SharedPreferences preferences =
get(context);
284 return preferences.getBoolean(
285 context.getString(R.string.preference_key_ui_invert_scrolling),
false);
288 public static boolean getAskOnExit(Context context)
290 SharedPreferences preferences =
get(context);
291 return preferences.getBoolean(context.getString(R.string.preference_key_ui_ask_on_exit),
295 public static boolean getAutoScrollTouchPointer(Context context)
297 SharedPreferences preferences =
get(context);
298 return preferences.getBoolean(
299 context.getString(R.string.preference_key_ui_auto_scroll_touchpointer),
false);
302 public static String getClientName(Context context)
304 SharedPreferences preferences =
get(context);
305 return preferences.getString(context.getString(R.string.preference_key_client_name),
"");