FreeRDP
Loading...
Searching...
No Matches
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
11package com.freerdp.freerdpcore.presentation;
12
13import android.content.Context;
14import android.content.SharedPreferences;
15import android.os.Bundle;
16import androidx.appcompat.app.AppCompatDelegate;
17import androidx.preference.ListPreference;
18import androidx.preference.PreferenceManager;
19import android.widget.Toast;
20
21import androidx.appcompat.app.AlertDialog;
22import androidx.appcompat.app.AppCompatActivity;
23import androidx.preference.Preference;
24import androidx.preference.PreferenceFragmentCompat;
25
26import com.freerdp.freerdpcore.R;
27
28import java.io.File;
29import java.util.UUID;
30
32 extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
33{
34 @Override protected void onCreate(Bundle savedInstanceState)
35 {
36 super.onCreate(savedInstanceState);
37 setContentView(R.layout.activity_settings);
38
39 // Ensure app setting defaults are initialised and the client name is set.
40 get(this);
41
42 if (getSupportActionBar() != null)
43 {
44 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
45 }
46
47 if (savedInstanceState == null)
48 {
49 getSupportFragmentManager()
50 .beginTransaction()
51 .replace(R.id.settings_fragment_container, new MainFragment())
52 .commit();
53 }
54 }
55
56 @Override public boolean onSupportNavigateUp()
57 {
58 if (getSupportFragmentManager().getBackStackEntryCount() > 0)
59 {
60 getSupportFragmentManager().popBackStack();
61 }
62 else
63 {
64 finish();
65 }
66 return true;
67 }
68
69 @Override
70 public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref)
71 {
72 PreferenceFragmentCompat fragment =
73 (PreferenceFragmentCompat)getSupportFragmentManager().getFragmentFactory().instantiate(
74 getClassLoader(), pref.getFragment());
75 fragment.setArguments(pref.getExtras());
76
77 getSupportFragmentManager()
78 .beginTransaction()
79 .replace(R.id.settings_fragment_container, fragment)
80 .addToBackStack(null)
81 .commit();
82 return true;
83 }
84
85 // =========================================================================
86 // Inner PreferenceFragmentCompat classes — one per settings category.
87 // The app:fragment attribute in settings_app_headers.xml references these
88 // using the $ notation, e.g. "...ApplicationSettingsActivity$ClientFragment".
89 // =========================================================================
90
92 public static class MainFragment extends PreferenceFragmentCompat
93 {
94 @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
95 {
96 setPreferencesFromResource(R.xml.settings_app_headers, rootKey);
97 }
98 }
99
100 // -------------------------------------------------------------------------
101
102 public static class ClientFragment extends PreferenceFragmentCompat
103 {
104 @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
105 {
106 setPreferencesFromResource(R.xml.settings_app_client, rootKey);
107 }
108 }
109
110 // -------------------------------------------------------------------------
111
112 public static class UiFragment extends PreferenceFragmentCompat
113 {
114 @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
115 {
116 setPreferencesFromResource(R.xml.settings_app_ui, rootKey);
117 ListPreference theme = findPreference(getString(R.string.pref_key_theme));
118 if (theme != null)
119 theme.setOnPreferenceChangeListener((p, v) -> {
120 AppCompatDelegate.setDefaultNightMode(nightModeFor((String)v));
121 requireActivity().recreate();
122 return true;
123 });
124 }
125 }
126
127 // -------------------------------------------------------------------------
128
129 public static class PowerFragment extends PreferenceFragmentCompat
130 {
131 @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
132 {
133 setPreferencesFromResource(R.xml.settings_app_power, rootKey);
134 }
135 }
136
137 // -------------------------------------------------------------------------
138
139 public static class SecurityFragment extends PreferenceFragmentCompat
140 {
141 @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
142 {
143 setPreferencesFromResource(R.xml.settings_app_security, rootKey);
144
145 Preference clearCache =
146 findPreference(getString(R.string.preference_key_security_clear_certificate_cache));
147 if (clearCache != null)
148 clearCache.setOnPreferenceClickListener(pref -> {
149 showClearCacheDialog();
150 return true;
151 });
152 }
153
154 private void showClearCacheDialog()
155 {
156 new AlertDialog
157 .Builder(requireContext()) // TODO: DialogFragment with ViewModel to survive config
158 // changes
159 .setTitle(R.string.dlg_title_clear_cert_cache)
160 .setMessage(R.string.dlg_msg_clear_cert_cache)
161 .setPositiveButton(android.R.string.ok,
162 (d, w) -> {
163 clearCertificateCache();
164 d.dismiss();
165 })
166 .setNegativeButton(android.R.string.cancel, (d, w) -> d.dismiss())
167 .setIcon(android.R.drawable.ic_delete)
168 .show();
169 }
170
171 private boolean deleteDirectory(File dir)
172 {
173 if (dir.isDirectory())
174 {
175 String[] children = dir.list();
176 for (String file : children)
177 {
178 if (!deleteDirectory(new File(dir, file)))
179 return false;
180 }
181 }
182 return dir.delete();
183 }
184
185 private void clearCertificateCache()
186 {
187 Context context = requireContext();
188 File dir = new File(context.getFilesDir() + "/.freerdp");
189 if (dir.exists())
190 {
191 if (deleteDirectory(dir))
192 Toast.makeText(context, R.string.info_reset_success, Toast.LENGTH_LONG).show();
193 else
194 Toast.makeText(context, R.string.info_reset_failed, Toast.LENGTH_LONG).show();
195 }
196 else
197 {
198 Toast.makeText(context, R.string.info_reset_success, Toast.LENGTH_LONG).show();
199 }
200 }
201 }
202
203 public static class ExperimentalFragment extends PreferenceFragmentCompat
204 {
205 @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
206 {
207 setPreferencesFromResource(R.xml.settings_app_experimental, rootKey);
208 }
209 }
210
211 // Preference key convention: "experimental.<feature>".
212 public static boolean isExperimentalEnabled(Context context, String feature)
213 {
214 return get(context).getBoolean("experimental." + feature, false);
215 }
216
217 public static SharedPreferences get(Context context)
218 {
219 Context appContext = context.getApplicationContext();
220 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_client, false);
221 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_power, false);
222 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_security, false);
223 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_ui, false);
224 PreferenceManager.setDefaultValues(appContext, R.xml.settings_app_experimental, false);
225 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
226
227 final String key = context.getString(R.string.preference_key_client_name);
228 final String value = preferences.getString(key, "");
229 if (value.isEmpty())
230 {
231 final String android_id = UUID.randomUUID().toString();
232 final String defaultValue = context.getString(R.string.preference_default_client_name);
233 final String name = defaultValue + "-" + android_id;
234 preferences.edit().putString(key, name.substring(0, 31)).apply();
235 }
236
237 return preferences;
238 }
239
240 public static int getDisconnectTimeout(Context context)
241 {
242 SharedPreferences preferences = get(context);
243 return preferences.getInt(
244 context.getString(R.string.preference_key_power_disconnect_timeout), 0);
245 }
246
247 public static boolean getKeepScreenOnWhenConnected(Context context)
248 {
249 SharedPreferences preferences = get(context);
250 return preferences.getBoolean(
251 context.getString(R.string.preference_key_power_keep_screen_on_when_connected), false);
252 }
253
254 public static boolean getHideStatusBar(Context context)
255 {
256 SharedPreferences preferences = get(context);
257 return preferences.getBoolean(context.getString(R.string.preference_key_ui_hide_status_bar),
258 false);
259 }
260
261 public static boolean getHideNavigationBar(Context context)
262 {
263 SharedPreferences preferences = get(context);
264 return preferences.getBoolean(
265 context.getString(R.string.preference_key_ui_hide_navigation_bar), false);
266 }
267
268 public static boolean getFitRoundedCorners(Context context)
269 {
270 SharedPreferences preferences = get(context);
271 return preferences.getBoolean(
272 context.getString(R.string.preference_key_ui_fit_rounded_corners), false);
273 }
274
275 public static boolean getUseBackAsAltf4(Context context)
276 {
277 SharedPreferences preferences = get(context);
278 return preferences.getBoolean(
279 context.getString(R.string.preference_key_ui_use_back_as_altf4), false);
280 }
281
282 public static boolean getAcceptAllCertificates(Context context)
283 {
284 SharedPreferences preferences = get(context);
285 return preferences.getBoolean(
286 context.getString(R.string.preference_key_accept_certificates), false);
287 }
288
289 public static boolean getSwapMouseButtons(Context context)
290 {
291 SharedPreferences preferences = get(context);
292 return preferences.getBoolean(
293 context.getString(R.string.preference_key_ui_swap_mouse_buttons), false);
294 }
295
296 public static boolean getInvertScrolling(Context context)
297 {
298 SharedPreferences preferences = get(context);
299 return preferences.getBoolean(
300 context.getString(R.string.preference_key_ui_invert_scrolling), false);
301 }
302
303 public static boolean getAskOnExit(Context context)
304 {
305 SharedPreferences preferences = get(context);
306 return preferences.getBoolean(context.getString(R.string.preference_key_ui_ask_on_exit),
307 true);
308 }
309
310 public static boolean getAutoScrollTouchPointer(Context context)
311 {
312 SharedPreferences preferences = get(context);
313 return preferences.getBoolean(
314 context.getString(R.string.preference_key_ui_auto_scroll_touchpointer), false);
315 }
316
317 public static String getClientName(Context context)
318 {
319 SharedPreferences preferences = get(context);
320 return preferences.getString(context.getString(R.string.preference_key_client_name), "");
321 }
322
323 public static int getNightMode(Context context)
324 {
325 return nightModeFor(
326 get(context).getString(context.getString(R.string.pref_key_theme), "auto"));
327 }
328
329 private static int nightModeFor(String value)
330 {
331 switch (value)
332 {
333 case "light":
334 return AppCompatDelegate.MODE_NIGHT_NO;
335 case "dark":
336 return AppCompatDelegate.MODE_NIGHT_YES;
337 default:
338 return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
339 }
340 }
341}
Definition wtypes.h:263