FreeRDP
ApplicationSettingsActivity.java
1 /*
2  Application Settings 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.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;
27 
28 import com.freerdp.freerdpcore.R;
29 import com.freerdp.freerdpcore.utils.AppCompatPreferenceActivity;
30 
31 import java.io.File;
32 import java.util.List;
33 import java.util.UUID;
34 
36 {
37  private static boolean isXLargeTablet(Context context)
38  {
39  return (context.getResources().getConfiguration().screenLayout &
40  Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
41  }
42 
43  @Override protected void onCreate(Bundle savedInstanceState)
44  {
45  super.onCreate(savedInstanceState);
46  setupActionBar();
47  }
48 
49  private void setupActionBar()
50  {
51  android.app.ActionBar actionBar = getActionBar();
52  if (actionBar != null)
53  {
54  actionBar.setDisplayHomeAsUpEnabled(true);
55  }
56  }
57 
58  @Override public boolean onIsMultiPane()
59  {
60  return isXLargeTablet(this);
61  }
62 
63  @Override
64  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
65  public void onBuildHeaders(List<Header> target)
66  {
67  loadHeadersFromResource(R.xml.settings_app_headers, target);
68  }
69 
70  protected boolean isValidFragment(String fragmentName)
71  {
72  return PreferenceFragment.class.getName().equals(fragmentName) ||
73  ClientPreferenceFragment.class.getName().equals(fragmentName) ||
74  UiPreferenceFragment.class.getName().equals(fragmentName) ||
75  PowerPreferenceFragment.class.getName().equals(fragmentName) ||
76  SecurityPreferenceFragment.class.getName().equals(fragmentName);
77  }
78 
79  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
80  public static class ClientPreferenceFragment
81  extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
82  {
83  @Override public void onCreate(Bundle savedInstanceState)
84  {
85  super.onCreate(savedInstanceState);
86  addPreferencesFromResource(R.xml.settings_app_client);
87  SharedPreferences preferences = get(getActivity());
88  preferences.registerOnSharedPreferenceChangeListener(this);
89  }
90 
91  @Override
92  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
93  {
94  if (isAdded())
95  {
96  final String clientNameKey = getString(R.string.preference_key_client_name);
97 
98  get(getActivity());
99  if (key.equals(clientNameKey))
100  {
101  final String clientNameValue = sharedPreferences.getString(clientNameKey, "");
102  EditTextPreference pref = (EditTextPreference)findPreference(clientNameKey);
103  pref.setText(clientNameValue);
104  }
105  }
106  }
107  }
108 
109  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
110  public static class UiPreferenceFragment extends PreferenceFragment
111  {
112  @Override public void onCreate(Bundle savedInstanceState)
113  {
114  super.onCreate(savedInstanceState);
115  addPreferencesFromResource(R.xml.settings_app_ui);
116  }
117  }
118 
119  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
120  public static class PowerPreferenceFragment extends PreferenceFragment
121  {
122  @Override public void onCreate(Bundle savedInstanceState)
123  {
124  super.onCreate(savedInstanceState);
125  addPreferencesFromResource(R.xml.settings_app_power);
126  }
127  }
128 
129  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
130  public static class SecurityPreferenceFragment extends PreferenceFragment
131  {
132  @Override public void onCreate(Bundle savedInstanceState)
133  {
134  super.onCreate(savedInstanceState);
135  addPreferencesFromResource(R.xml.settings_app_security);
136  }
137 
138  @Override
139  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
140  Preference preference)
141  {
142  final String clear =
143  getString(R.string.preference_key_security_clear_certificate_cache);
144  if (preference.getKey().equals(clear))
145  {
146  showDialog();
147  return true;
148  }
149  else
150  {
151  return super.onPreferenceTreeClick(preferenceScreen, preference);
152  }
153  }
154 
155  private void showDialog()
156  {
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() {
162  @Override
163  public void onClick(DialogInterface dialog, int which)
164  {
165  clearCertificateCache();
166  dialog.dismiss();
167  }
168  })
169  .setNegativeButton(android.R.string.cancel,
170  new DialogInterface.OnClickListener() {
171  @Override
172  public void onClick(DialogInterface dialog, int which)
173  {
174  dialog.dismiss();
175  }
176  })
177  .setIcon(android.R.drawable.ic_delete)
178  .show();
179  }
180 
181  private boolean deleteDirectory(File dir)
182  {
183  if (dir.isDirectory())
184  {
185  String[] children = dir.list();
186  for (String file : children)
187  {
188  if (!deleteDirectory(new File(dir, file)))
189  return false;
190  }
191  }
192  return dir.delete();
193  }
194 
195  private void clearCertificateCache()
196  {
197  Context context = getActivity();
198  if ((new File(context.getFilesDir() + "/.freerdp")).exists())
199  {
200  if (deleteDirectory(new File(context.getFilesDir() + "/.freerdp")))
201  Toast.makeText(context, R.string.info_reset_success, Toast.LENGTH_LONG).show();
202  else
203  Toast.makeText(context, R.string.info_reset_failed, Toast.LENGTH_LONG).show();
204  }
205  else
206  Toast.makeText(context, R.string.info_reset_success, Toast.LENGTH_LONG).show();
207  }
208  }
209 
210  public static SharedPreferences get(Context context)
211  {
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);
218 
219  final String key = context.getString(R.string.preference_key_client_name);
220  final String value = preferences.getString(key, "");
221  if (value.isEmpty())
222  {
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();
227  }
228 
229  return preferences;
230  }
231 
232  public static int getDisconnectTimeout(Context context)
233  {
234  SharedPreferences preferences = get(context);
235  return preferences.getInt(
236  context.getString(R.string.preference_key_power_disconnect_timeout), 0);
237  }
238 
239  public static boolean getHideStatusBar(Context context)
240  {
241  SharedPreferences preferences = get(context);
242  return preferences.getBoolean(context.getString(R.string.preference_key_ui_hide_status_bar),
243  false);
244  }
245 
246  public static boolean getHideActionBar(Context context)
247  {
248  SharedPreferences preferences = get(context);
249  return preferences.getBoolean(context.getString(R.string.preference_key_ui_hide_action_bar),
250  false);
251  }
252 
253  public static boolean getUseBackAsAltf4(Context context)
254  {
255  SharedPreferences preferences = get(context);
256  return preferences.getBoolean(
257  context.getString(R.string.preference_key_ui_use_back_as_altf4), true);
258  }
259 
260  public static boolean getAcceptAllCertificates(Context context)
261  {
262  SharedPreferences preferences = get(context);
263  return preferences.getBoolean(
264  context.getString(R.string.preference_key_accept_certificates), false);
265  }
266 
267  public static boolean getHideZoomControls(Context context)
268  {
269  SharedPreferences preferences = get(context);
270  return preferences.getBoolean(
271  context.getString(R.string.preference_key_ui_hide_zoom_controls), false);
272  }
273 
274  public static boolean getSwapMouseButtons(Context context)
275  {
276  SharedPreferences preferences = get(context);
277  return preferences.getBoolean(
278  context.getString(R.string.preference_key_ui_swap_mouse_buttons), false);
279  }
280 
281  public static boolean getInvertScrolling(Context context)
282  {
283  SharedPreferences preferences = get(context);
284  return preferences.getBoolean(
285  context.getString(R.string.preference_key_ui_invert_scrolling), false);
286  }
287 
288  public static boolean getAskOnExit(Context context)
289  {
290  SharedPreferences preferences = get(context);
291  return preferences.getBoolean(context.getString(R.string.preference_key_ui_ask_on_exit),
292  false);
293  }
294 
295  public static boolean getAutoScrollTouchPointer(Context context)
296  {
297  SharedPreferences preferences = get(context);
298  return preferences.getBoolean(
299  context.getString(R.string.preference_key_ui_auto_scroll_touchpointer), false);
300  }
301 
302  public static String getClientName(Context context)
303  {
304  SharedPreferences preferences = get(context);
305  return preferences.getString(context.getString(R.string.preference_key_client_name), "");
306  }
307 }