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