SettingsActivity.java 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * @author Chris Narkiewicz
  7. *
  8. * Copyright (C) 2011 Bartek Przybylski
  9. * Copyright (C) 2016 ownCloud Inc.
  10. * Copyright (C) 2016 Nextcloud
  11. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. package com.owncloud.android.ui.activity;
  27. import android.app.Activity;
  28. import android.content.Intent;
  29. import android.content.SharedPreferences;
  30. import android.content.pm.PackageInfo;
  31. import android.content.pm.PackageManager.NameNotFoundException;
  32. import android.content.res.Configuration;
  33. import android.graphics.drawable.ColorDrawable;
  34. import android.net.Uri;
  35. import android.os.Build;
  36. import android.os.Bundle;
  37. import android.preference.ListPreference;
  38. import android.preference.Preference;
  39. import android.preference.PreferenceCategory;
  40. import android.preference.PreferenceManager;
  41. import android.preference.PreferenceScreen;
  42. import android.preference.SwitchPreference;
  43. import android.text.TextUtils;
  44. import android.view.Menu;
  45. import android.view.MenuInflater;
  46. import android.view.MenuItem;
  47. import android.view.View;
  48. import android.view.ViewGroup;
  49. import android.webkit.URLUtil;
  50. import com.nextcloud.client.account.User;
  51. import com.nextcloud.client.account.UserAccountManager;
  52. import com.nextcloud.client.di.Injectable;
  53. import com.nextcloud.client.etm.EtmActivity;
  54. import com.nextcloud.client.logger.ui.LogsActivity;
  55. import com.nextcloud.client.network.ClientFactory;
  56. import com.nextcloud.client.preferences.AppPreferences;
  57. import com.nextcloud.client.preferences.AppPreferencesImpl;
  58. import com.nextcloud.client.preferences.DarkMode;
  59. import com.owncloud.android.BuildConfig;
  60. import com.owncloud.android.MainApp;
  61. import com.owncloud.android.R;
  62. import com.owncloud.android.authentication.AuthenticatorActivity;
  63. import com.owncloud.android.authentication.PassCodeManager;
  64. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  65. import com.owncloud.android.datamodel.ExternalLinksProvider;
  66. import com.owncloud.android.datastorage.DataStorageProvider;
  67. import com.owncloud.android.datastorage.StoragePoint;
  68. import com.owncloud.android.lib.common.ExternalLink;
  69. import com.owncloud.android.lib.common.ExternalLinkType;
  70. import com.owncloud.android.lib.common.utils.Log_OC;
  71. import com.owncloud.android.providers.DocumentsStorageProvider;
  72. import com.owncloud.android.ui.asynctasks.LoadingVersionNumberTask;
  73. import com.owncloud.android.utils.DeviceCredentialUtils;
  74. import com.owncloud.android.utils.DisplayUtils;
  75. import com.owncloud.android.utils.EncryptionUtils;
  76. import com.owncloud.android.utils.MimeTypeUtil;
  77. import com.owncloud.android.utils.theme.ThemeButtonUtils;
  78. import com.owncloud.android.utils.theme.ThemeColorUtils;
  79. import com.owncloud.android.utils.theme.ThemeTextUtils;
  80. import com.owncloud.android.utils.theme.ThemeToolbarUtils;
  81. import com.owncloud.android.utils.theme.ThemeUtils;
  82. import java.util.ArrayList;
  83. import java.util.List;
  84. import javax.inject.Inject;
  85. import androidx.annotation.LayoutRes;
  86. import androidx.annotation.NonNull;
  87. import androidx.annotation.RequiresApi;
  88. import androidx.annotation.VisibleForTesting;
  89. import androidx.appcompat.app.ActionBar;
  90. import androidx.appcompat.app.AlertDialog;
  91. import androidx.appcompat.app.AppCompatDelegate;
  92. /**
  93. * An Activity that allows the user to change the application's settings.
  94. *
  95. * It proxies the necessary calls via {@link androidx.appcompat.app.AppCompatDelegate} to be used with AppCompat.
  96. */
  97. public class SettingsActivity extends ThemedPreferenceActivity
  98. implements StorageMigration.StorageMigrationProgressListener, LoadingVersionNumberTask.VersionDevInterface,
  99. Injectable {
  100. private static final String TAG = SettingsActivity.class.getSimpleName();
  101. public static final String PREFERENCE_LOCK= "lock";
  102. public static final String LOCK_NONE = "none";
  103. public static final String LOCK_PASSCODE = "passcode";
  104. public static final String LOCK_DEVICE_CREDENTIALS = "device_credentials";
  105. public final static String PREFERENCE_USE_FINGERPRINT = "use_fingerprint";
  106. public static final String PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS = "show_media_scan_notifications";
  107. private static final int ACTION_REQUEST_PASSCODE = 5;
  108. private static final int ACTION_CONFIRM_PASSCODE = 6;
  109. private static final int ACTION_CONFIRM_DEVICE_CREDENTIALS = 7;
  110. private static final int ACTION_REQUEST_CODE_DAVDROID_SETUP = 10;
  111. private static final int TRUE_VALUE = 1;
  112. private static final String DAV_PATH = "/remote.php/dav";
  113. public static final String SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI = "SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI";
  114. private Uri serverBaseUri;
  115. private ListPreference lock;
  116. private SwitchPreference showHiddenFiles;
  117. private AppCompatDelegate delegate;
  118. private ListPreference prefStoragePath;
  119. private String storagePath;
  120. private String pendingLock;
  121. private User user;
  122. @Inject ArbitraryDataProvider arbitraryDataProvider;
  123. @Inject AppPreferences preferences;
  124. @Inject UserAccountManager accountManager;
  125. @Inject ClientFactory clientFactory;
  126. @SuppressWarnings("deprecation")
  127. @Override
  128. public void onCreate(Bundle savedInstanceState) {
  129. if (ThemeUtils.themingEnabled(this)) {
  130. setTheme(R.style.FallbackThemingTheme);
  131. }
  132. getDelegate().installViewFactory();
  133. getDelegate().onCreate(savedInstanceState);
  134. super.onCreate(savedInstanceState);
  135. addPreferencesFromResource(R.xml.preferences);
  136. setupActionBar();
  137. // Register context menu for list of preferences.
  138. registerForContextMenu(getListView());
  139. int accentColor = ThemeColorUtils.primaryAccentColor(this);
  140. String appVersion = getAppVersion();
  141. PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("preference_screen");
  142. user = accountManager.getUser();
  143. // retrieve user's base uri
  144. setupBaseUri();
  145. // General
  146. setupGeneralCategory(accentColor);
  147. // Synced folders
  148. setupAutoUploadCategory(accentColor, preferenceScreen);
  149. // Details
  150. setupDetailsCategory(accentColor, preferenceScreen);
  151. // More
  152. setupMoreCategory(accentColor);
  153. // About
  154. setupAboutCategory(accentColor, appVersion);
  155. // Dev
  156. setupDevCategory(accentColor, preferenceScreen);
  157. }
  158. private void setupDevCategory(int accentColor, PreferenceScreen preferenceScreen) {
  159. // Dev category
  160. PreferenceCategory preferenceCategoryDev = (PreferenceCategory) findPreference("dev_category");
  161. if (getResources().getBoolean(R.bool.is_beta)) {
  162. preferenceCategoryDev.setTitle(ThemeTextUtils.getColoredTitle(getString(R.string.prefs_category_dev),
  163. accentColor));
  164. /* Link to dev apks */
  165. Preference pDevLink = findPreference("dev_link");
  166. if (pDevLink != null) {
  167. if (getResources().getBoolean(R.bool.dev_version_direct_download_enabled)) {
  168. pDevLink.setOnPreferenceClickListener(preference -> {
  169. FileActivity.checkForNewDevVersion(this, getApplicationContext());
  170. return true;
  171. });
  172. } else {
  173. preferenceCategoryDev.removePreference(pDevLink);
  174. }
  175. }
  176. /* Link to dev changelog */
  177. Preference pChangelogLink = findPreference("changelog_link");
  178. if (pChangelogLink != null) {
  179. pChangelogLink.setOnPreferenceClickListener(preference -> {
  180. DisplayUtils.startLinkIntent(this, R.string.dev_changelog);
  181. return true;
  182. });
  183. }
  184. /* Engineering Test Mode */
  185. Preference pEtm = findPreference("etm");
  186. if (pEtm != null) {
  187. pEtm.setOnPreferenceClickListener(preference -> {
  188. EtmActivity.launch(this);
  189. return true;
  190. });
  191. }
  192. } else {
  193. preferenceScreen.removePreference(preferenceCategoryDev);
  194. }
  195. }
  196. private void setupAboutCategory(int accentColor, String appVersion) {
  197. PreferenceCategory preferenceCategoryAbout = (PreferenceCategory) findPreference("about");
  198. preferenceCategoryAbout.setTitle(ThemeTextUtils.getColoredTitle(getString(R.string.prefs_category_about),
  199. accentColor));
  200. /* About App */
  201. Preference pAboutApp = findPreference("about_app");
  202. if (pAboutApp != null) {
  203. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  204. String buildNumber = getResources().getString(R.string.buildNumber);
  205. if (TextUtils.isEmpty(buildNumber)) {
  206. pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
  207. } else {
  208. pAboutApp.setSummary(String.format(getString(R.string.about_version_with_build),
  209. appVersion,
  210. buildNumber));
  211. }
  212. }
  213. // license
  214. boolean licenseEnabled = getResources().getBoolean(R.bool.license_enabled);
  215. Preference licensePreference = findPreference("license");
  216. if (licensePreference != null) {
  217. if (licenseEnabled) {
  218. licensePreference.setSummary(R.string.prefs_gpl_v2);
  219. licensePreference.setOnPreferenceClickListener(preference -> {
  220. DisplayUtils.startLinkIntent(this, R.string.license_url);
  221. return true;
  222. });
  223. } else {
  224. preferenceCategoryAbout.removePreference(licensePreference);
  225. }
  226. }
  227. // privacy
  228. boolean privacyEnabled = getResources().getBoolean(R.bool.privacy_enabled);
  229. Preference privacyPreference = findPreference("privacy");
  230. if (privacyPreference != null) {
  231. if (privacyEnabled && URLUtil.isValidUrl(getString(R.string.privacy_url))) {
  232. privacyPreference.setOnPreferenceClickListener(preference -> {
  233. try {
  234. Uri privacyUrl = Uri.parse(getString(R.string.privacy_url));
  235. String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(privacyUrl.getLastPathSegment());
  236. Intent intent;
  237. if ("application/pdf".equals(mimeType)) {
  238. intent = new Intent(Intent.ACTION_VIEW, privacyUrl);
  239. DisplayUtils.startIntentIfAppAvailable(intent, this, R.string.no_pdf_app_available);
  240. } else {
  241. intent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  242. intent.putExtra(ExternalSiteWebView.EXTRA_TITLE,
  243. getResources().getString(R.string.privacy));
  244. intent.putExtra(ExternalSiteWebView.EXTRA_URL, privacyUrl.toString());
  245. intent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  246. intent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, -1);
  247. }
  248. startActivity(intent);
  249. } catch (Exception e) {
  250. Log_OC.e(TAG, "Could not parse privacy url");
  251. preferenceCategoryAbout.removePreference(privacyPreference);
  252. }
  253. return true;
  254. });
  255. } else {
  256. preferenceCategoryAbout.removePreference(privacyPreference);
  257. }
  258. }
  259. // source code
  260. boolean sourcecodeEnabled = getResources().getBoolean(R.bool.sourcecode_enabled);
  261. Preference sourcecodePreference = findPreference("sourcecode");
  262. if (sourcecodePreference != null) {
  263. if (sourcecodeEnabled) {
  264. sourcecodePreference.setOnPreferenceClickListener(preference -> {
  265. DisplayUtils.startLinkIntent(this, R.string.sourcecode_url);
  266. return true;
  267. });
  268. } else {
  269. preferenceCategoryAbout.removePreference(sourcecodePreference);
  270. }
  271. }
  272. }
  273. private void setupMoreCategory(int accentColor) {
  274. PreferenceCategory preferenceCategoryMore = (PreferenceCategory) findPreference("more");
  275. preferenceCategoryMore.setTitle(ThemeTextUtils.getColoredTitle(getString(R.string.prefs_category_more),
  276. accentColor));
  277. setupAutoUploadPreference(preferenceCategoryMore);
  278. setupCalendarPreference(preferenceCategoryMore);
  279. setupContactsBackupPreference(preferenceCategoryMore);
  280. setupE2EMnemonicPreference(preferenceCategoryMore);
  281. setupHelpPreference(preferenceCategoryMore);
  282. setupRecommendPreference(preferenceCategoryMore);
  283. setupLoggingPreference(preferenceCategoryMore);
  284. setupImprintPreference(preferenceCategoryMore);
  285. loadExternalSettingLinks(preferenceCategoryMore);
  286. }
  287. private void setupImprintPreference(PreferenceCategory preferenceCategoryMore) {
  288. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  289. Preference pImprint = findPreference("imprint");
  290. if (pImprint != null) {
  291. if (imprintEnabled) {
  292. pImprint.setOnPreferenceClickListener(preference -> {
  293. String imprintWeb = getString(R.string.url_imprint);
  294. if (!imprintWeb.isEmpty()) {
  295. Uri uriUrl = Uri.parse(imprintWeb);
  296. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  297. DisplayUtils.startIntentIfAppAvailable(intent, this, R.string.no_browser_available);
  298. }
  299. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  300. return true;
  301. });
  302. } else {
  303. preferenceCategoryMore.removePreference(pImprint);
  304. }
  305. }
  306. }
  307. private void setupLoggingPreference(PreferenceCategory preferenceCategoryMore) {
  308. boolean loggerEnabled = getResources().getBoolean(R.bool.logger_enabled) || BuildConfig.DEBUG;
  309. Preference pLogger = findPreference("logger");
  310. if (pLogger != null) {
  311. if (loggerEnabled) {
  312. pLogger.setOnPreferenceClickListener(preference -> {
  313. Intent loggerIntent = new Intent(getApplicationContext(), LogsActivity.class);
  314. startActivity(loggerIntent);
  315. return true;
  316. });
  317. } else {
  318. preferenceCategoryMore.removePreference(pLogger);
  319. }
  320. }
  321. }
  322. private void setupRecommendPreference(PreferenceCategory preferenceCategoryMore) {
  323. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  324. Preference pRecommend = findPreference("recommend");
  325. if (pRecommend != null) {
  326. if (recommendEnabled) {
  327. pRecommend.setOnPreferenceClickListener(preference -> {
  328. Intent intent = new Intent(Intent.ACTION_SEND);
  329. intent.setType("text/plain");
  330. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  331. String appName = getString(R.string.app_name);
  332. String downloadUrlGooglePlayStore = getString(R.string.url_app_download);
  333. String downloadUrlFDroid = getString(R.string.fdroid_link);
  334. String downloadUrls = String.format(getString(R.string.recommend_urls),
  335. downloadUrlGooglePlayStore, downloadUrlFDroid);
  336. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  337. String recommendText = String.format(getString(R.string.recommend_text),
  338. appName, downloadUrls);
  339. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  340. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  341. startActivity(intent);
  342. return true;
  343. });
  344. } else {
  345. preferenceCategoryMore.removePreference(pRecommend);
  346. }
  347. }
  348. }
  349. private void setupE2EMnemonicPreference(PreferenceCategory preferenceCategoryMore) {
  350. String mnemonic = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.MNEMONIC);
  351. Preference pMnemonic = findPreference("mnemonic");
  352. if (pMnemonic != null) {
  353. if (!mnemonic.isEmpty() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  354. if (DeviceCredentialUtils.areCredentialsAvailable(this)) {
  355. pMnemonic.setOnPreferenceClickListener(preference -> {
  356. Intent i = new Intent(MainApp.getAppContext(), RequestCredentialsActivity.class);
  357. i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  358. startActivityForResult(i, PassCodeManager.PASSCODE_ACTIVITY);
  359. return true;
  360. });
  361. } else {
  362. pMnemonic.setEnabled(false);
  363. pMnemonic.setSummary(R.string.prefs_e2e_no_device_credentials);
  364. }
  365. } else {
  366. preferenceCategoryMore.removePreference(pMnemonic);
  367. }
  368. }
  369. }
  370. private void setupHelpPreference(PreferenceCategory preferenceCategoryMore) {
  371. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  372. Preference pHelp = findPreference("help");
  373. if (pHelp != null) {
  374. if (helpEnabled) {
  375. pHelp.setOnPreferenceClickListener(preference -> {
  376. String helpWeb = getString(R.string.url_help);
  377. if (!helpWeb.isEmpty()) {
  378. Uri uriUrl = Uri.parse(helpWeb);
  379. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  380. DisplayUtils.startIntentIfAppAvailable(intent, this, R.string.no_browser_available);
  381. }
  382. return true;
  383. });
  384. } else {
  385. preferenceCategoryMore.removePreference(pHelp);
  386. }
  387. }
  388. }
  389. private void setupAutoUploadPreference(PreferenceCategory preferenceCategoryMore) {
  390. Preference autoUpload = findPreference("syncedFolders");
  391. if (getResources().getBoolean(R.bool.syncedFolder_light)) {
  392. preferenceCategoryMore.removePreference(autoUpload);
  393. } else {
  394. autoUpload.setOnPreferenceClickListener(preference -> {
  395. Intent intent = new Intent(this, SyncedFoldersActivity.class);
  396. startActivity(intent);
  397. return true;
  398. });
  399. }
  400. }
  401. private void setupContactsBackupPreference(PreferenceCategory preferenceCategoryMore) {
  402. boolean contactsBackupEnabled = !getResources().getBoolean(R.bool.show_drawer_contacts_backup)
  403. && getResources().getBoolean(R.bool.contacts_backup);
  404. Preference pContactsBackup = findPreference("contacts");
  405. if (pContactsBackup != null) {
  406. if (contactsBackupEnabled) {
  407. pContactsBackup.setOnPreferenceClickListener(preference -> {
  408. ContactsPreferenceActivity.startActivityWithoutSidebar(this);
  409. return true;
  410. });
  411. } else {
  412. preferenceCategoryMore.removePreference(pContactsBackup);
  413. }
  414. }
  415. }
  416. private void setupCalendarPreference(PreferenceCategory preferenceCategoryMore) {
  417. boolean calendarContactsEnabled = getResources().getBoolean(R.bool.davdroid_integration_enabled);
  418. Preference pCalendarContacts = findPreference("calendar_contacts");
  419. if (pCalendarContacts != null) {
  420. if (calendarContactsEnabled) {
  421. final Activity activity = this;
  422. pCalendarContacts.setOnPreferenceClickListener(preference -> {
  423. try {
  424. launchDavDroidLogin();
  425. } catch (Throwable t) {
  426. Log_OC.e(TAG, "Base Uri for account could not be resolved to call DAVdroid!", t);
  427. DisplayUtils.showSnackMessage(
  428. activity,
  429. R.string.prefs_calendar_contacts_address_resolve_error
  430. );
  431. }
  432. return true;
  433. });
  434. } else {
  435. preferenceCategoryMore.removePreference(pCalendarContacts);
  436. }
  437. }
  438. }
  439. private void setupDetailsCategory(int accentColor, PreferenceScreen preferenceScreen) {
  440. PreferenceCategory preferenceCategoryDetails = (PreferenceCategory) findPreference("details");
  441. preferenceCategoryDetails.setTitle(ThemeTextUtils.getColoredTitle(getString(R.string.prefs_category_details),
  442. accentColor));
  443. boolean fPassCodeEnabled = getResources().getBoolean(R.bool.passcode_enabled);
  444. boolean fDeviceCredentialsEnabled = getResources().getBoolean(R.bool.device_credentials_enabled);
  445. boolean fShowHiddenFilesEnabled = getResources().getBoolean(R.bool.show_hidden_files_enabled);
  446. boolean fSyncedFolderLightEnabled = getResources().getBoolean(R.bool.syncedFolder_light);
  447. boolean fShowMediaScanNotifications = preferences.isShowMediaScanNotifications();
  448. setupLockPreference(preferenceCategoryDetails, fPassCodeEnabled, fDeviceCredentialsEnabled);
  449. setupHiddenFilesPreference(preferenceCategoryDetails, fShowHiddenFilesEnabled);
  450. setupShowMediaScanNotifications(preferenceCategoryDetails, fShowMediaScanNotifications);
  451. if (!fPassCodeEnabled && !fDeviceCredentialsEnabled && !fShowHiddenFilesEnabled && fSyncedFolderLightEnabled
  452. && fShowMediaScanNotifications) {
  453. preferenceScreen.removePreference(preferenceCategoryDetails);
  454. }
  455. }
  456. private void setupShowMediaScanNotifications(PreferenceCategory preferenceCategoryDetails,
  457. boolean fShowMediaScanNotifications) {
  458. SwitchPreference mShowMediaScanNotifications = (SwitchPreference) findPreference(PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS);
  459. if (fShowMediaScanNotifications) {
  460. preferenceCategoryDetails.removePreference(mShowMediaScanNotifications);
  461. }
  462. }
  463. private void setupHiddenFilesPreference(PreferenceCategory preferenceCategoryDetails,
  464. boolean fShowHiddenFilesEnabled) {
  465. showHiddenFiles = (SwitchPreference) findPreference("show_hidden_files");
  466. if (fShowHiddenFilesEnabled) {
  467. showHiddenFiles.setOnPreferenceClickListener(preference -> {
  468. preferences.setShowHiddenFilesEnabled(showHiddenFiles.isChecked());
  469. return true;
  470. });
  471. } else {
  472. preferenceCategoryDetails.removePreference(showHiddenFiles);
  473. }
  474. }
  475. private void setupLockPreference(PreferenceCategory preferenceCategoryDetails,
  476. boolean passCodeEnabled,
  477. boolean deviceCredentialsEnabled) {
  478. lock = (ListPreference) findPreference(PREFERENCE_LOCK);
  479. if (lock != null && (passCodeEnabled || deviceCredentialsEnabled)) {
  480. ArrayList<String> lockEntries = new ArrayList<>(3);
  481. lockEntries.add(getString(R.string.prefs_lock_none));
  482. lockEntries.add(getString(R.string.prefs_lock_using_passcode));
  483. lockEntries.add(getString(R.string.prefs_lock_using_device_credentials));
  484. ArrayList<String> lockValues = new ArrayList<>(3);
  485. lockValues.add(LOCK_NONE);
  486. lockValues.add(LOCK_PASSCODE);
  487. lockValues.add(LOCK_DEVICE_CREDENTIALS);
  488. if (!passCodeEnabled) {
  489. lockEntries.remove(1);
  490. lockValues.remove(1);
  491. } else if (!deviceCredentialsEnabled || Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
  492. !DeviceCredentialUtils.areCredentialsAvailable(getApplicationContext())) {
  493. lockEntries.remove(2);
  494. lockValues.remove(2);
  495. }
  496. String[] lockEntriesArr = new String[lockEntries.size()];
  497. lockEntriesArr = lockEntries.toArray(lockEntriesArr);
  498. String[] lockValuesArr = new String[lockValues.size()];
  499. lockValuesArr = lockValues.toArray(lockValuesArr);
  500. lock.setEntries(lockEntriesArr);
  501. lock.setEntryValues(lockValuesArr);
  502. lock.setSummary(lock.getEntry());
  503. lock.setOnPreferenceChangeListener((preference, o) -> {
  504. pendingLock = LOCK_NONE;
  505. String oldValue = ((ListPreference) preference).getValue();
  506. String newValue = (String) o;
  507. if (!oldValue.equals(newValue)) {
  508. if (LOCK_NONE.equals(oldValue)) {
  509. enableLock(newValue);
  510. } else {
  511. pendingLock = newValue;
  512. disableLock(oldValue);
  513. }
  514. }
  515. return false;
  516. });
  517. } else {
  518. preferenceCategoryDetails.removePreference(lock);
  519. }
  520. }
  521. private void setupAutoUploadCategory(int accentColor, PreferenceScreen preferenceScreen) {
  522. PreferenceCategory preferenceCategorySyncedFolders =
  523. (PreferenceCategory) findPreference("synced_folders_category");
  524. preferenceCategorySyncedFolders.setTitle(ThemeTextUtils.getColoredTitle(getString(R.string.drawer_synced_folders),
  525. accentColor));
  526. if (!getResources().getBoolean(R.bool.syncedFolder_light)) {
  527. preferenceScreen.removePreference(preferenceCategorySyncedFolders);
  528. } else {
  529. // Upload on WiFi
  530. final ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  531. final SwitchPreference pUploadOnWifiCheckbox = (SwitchPreference) findPreference("synced_folder_on_wifi");
  532. pUploadOnWifiCheckbox.setChecked(
  533. arbitraryDataProvider.getBooleanValue(user, SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI));
  534. pUploadOnWifiCheckbox.setOnPreferenceClickListener(preference -> {
  535. arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI,
  536. String.valueOf(pUploadOnWifiCheckbox.isChecked()));
  537. return true;
  538. });
  539. Preference pSyncedFolder = findPreference("synced_folders_configure_folders");
  540. if (pSyncedFolder != null) {
  541. if (getResources().getBoolean(R.bool.syncedFolder_light)
  542. && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  543. pSyncedFolder.setOnPreferenceClickListener(preference -> {
  544. Intent intent = new Intent(this, SyncedFoldersActivity.class);
  545. startActivity(intent);
  546. return true;
  547. });
  548. } else {
  549. preferenceCategorySyncedFolders.removePreference(pSyncedFolder);
  550. }
  551. }
  552. }
  553. }
  554. private void enableLock(String lock) {
  555. pendingLock = LOCK_NONE;
  556. if (LOCK_PASSCODE.equals(lock)) {
  557. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  558. i.setAction(PassCodeActivity.ACTION_REQUEST_WITH_RESULT);
  559. startActivityForResult(i, ACTION_REQUEST_PASSCODE);
  560. } else if (LOCK_DEVICE_CREDENTIALS.equals(lock)){
  561. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
  562. !DeviceCredentialUtils.areCredentialsAvailable(getApplicationContext())) {
  563. DisplayUtils.showSnackMessage(this, R.string.prefs_lock_device_credentials_not_setup);
  564. } else {
  565. DisplayUtils.showSnackMessage(this, R.string.prefs_lock_device_credentials_enabled);
  566. changeLockSetting(LOCK_DEVICE_CREDENTIALS);
  567. }
  568. }
  569. }
  570. private void changeLockSetting(String value) {
  571. lock.setValue(value);
  572. lock.setSummary(lock.getEntry());
  573. DocumentsStorageProvider.notifyRootsChanged(this);
  574. }
  575. private void disableLock(String lock) {
  576. if (LOCK_PASSCODE.equals(lock)) {
  577. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  578. i.setAction(PassCodeActivity.ACTION_CHECK_WITH_RESULT);
  579. startActivityForResult(i, ACTION_CONFIRM_PASSCODE);
  580. } else if (LOCK_DEVICE_CREDENTIALS.equals(lock)) {
  581. Intent i = new Intent(getApplicationContext(), RequestCredentialsActivity.class);
  582. startActivityForResult(i, ACTION_CONFIRM_DEVICE_CREDENTIALS);
  583. }
  584. }
  585. private void setupGeneralCategory(int accentColor) {
  586. PreferenceCategory preferenceCategoryGeneral = (PreferenceCategory) findPreference("general");
  587. preferenceCategoryGeneral.setTitle(ThemeTextUtils.getColoredTitle(getString(R.string.prefs_category_general),
  588. accentColor));
  589. prefStoragePath = (ListPreference) findPreference(AppPreferencesImpl.STORAGE_PATH);
  590. if (prefStoragePath != null) {
  591. StoragePoint[] storageOptions = DataStorageProvider.getInstance().getAvailableStoragePoints();
  592. String[] entries = new String[storageOptions.length];
  593. String[] values = new String[storageOptions.length];
  594. for (int i = 0; i < storageOptions.length; ++i) {
  595. entries[i] = storageOptions[i].getDescription();
  596. values[i] = storageOptions[i].getPath();
  597. }
  598. prefStoragePath.setEntries(entries);
  599. prefStoragePath.setEntryValues(values);
  600. prefStoragePath.setOnPreferenceChangeListener((preference, newValue) -> {
  601. String newPath = (String) newValue;
  602. if (storagePath.equals(newPath)) {
  603. return true;
  604. }
  605. StorageMigration storageMigration = new StorageMigration(this, storagePath, newPath);
  606. storageMigration.setStorageMigrationProgressListener(this);
  607. storageMigration.migrate();
  608. return false;
  609. });
  610. }
  611. loadStoragePath();
  612. ListPreference themePref = (ListPreference) findPreference("darkMode");
  613. List<String> themeEntries = new ArrayList<>(3);
  614. themeEntries.add(getString(R.string.prefs_value_theme_light));
  615. themeEntries.add(getString(R.string.prefs_value_theme_dark));
  616. themeEntries.add(getString(R.string.prefs_value_theme_system));
  617. List<String> themeValues = new ArrayList<>(3);
  618. themeValues.add(DarkMode.LIGHT.name());
  619. themeValues.add(DarkMode.DARK.name());
  620. themeValues.add(DarkMode.SYSTEM.name());
  621. themePref.setEntries(themeEntries.toArray(new String[0]));
  622. themePref.setEntryValues(themeValues.toArray(new String[0]));
  623. if (TextUtils.isEmpty(themePref.getEntry())) {
  624. themePref.setValue(DarkMode.SYSTEM.name());
  625. themePref.setSummary(TextUtils.isEmpty(themePref.getEntry()) ? DarkMode.SYSTEM.name() : themePref.getEntry());
  626. }
  627. themePref.setOnPreferenceChangeListener((preference, newValue) -> {
  628. DarkMode mode = DarkMode.valueOf((String) newValue);
  629. preferences.setDarkThemeMode(mode);
  630. MainApp.setAppTheme(mode);
  631. return true;
  632. });
  633. }
  634. private String getAppVersion() {
  635. String temp;
  636. try {
  637. PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  638. temp = pkg.versionName;
  639. } catch (NameNotFoundException e) {
  640. temp = "";
  641. Log_OC.e(TAG, "Error while showing about dialog", e);
  642. }
  643. return temp;
  644. }
  645. @Override
  646. public boolean onOptionsItemSelected(MenuItem item) {
  647. finish();
  648. return super.onOptionsItemSelected(item);
  649. }
  650. private void setupActionBar() {
  651. ActionBar actionBar = getDelegate().getSupportActionBar();
  652. if (actionBar != null) {
  653. ThemeToolbarUtils.setColoredTitle(actionBar, getString(R.string.actionbar_settings), this);
  654. ThemeToolbarUtils.colorStatusBar(this);
  655. actionBar.setBackgroundDrawable(new ColorDrawable(ThemeColorUtils.primaryAppbarColor(this)));
  656. actionBar.setDisplayHomeAsUpEnabled(true);
  657. ThemeToolbarUtils.tintBackButton(actionBar, this);
  658. }
  659. }
  660. private void launchDavDroidLogin() {
  661. Intent davDroidLoginIntent = new Intent();
  662. davDroidLoginIntent.setClassName("at.bitfire.davdroid", "at.bitfire.davdroid.ui.setup.LoginActivity");
  663. if (getPackageManager().resolveActivity(davDroidLoginIntent, 0) != null) {
  664. // arguments
  665. if (serverBaseUri != null) {
  666. davDroidLoginIntent.putExtra("url", serverBaseUri.toString() + DAV_PATH);
  667. davDroidLoginIntent.putExtra("loginFlow", TRUE_VALUE);
  668. davDroidLoginIntent.setData(Uri.parse(serverBaseUri.toString() + AuthenticatorActivity.WEB_LOGIN));
  669. davDroidLoginIntent.putExtra("davPath", DAV_PATH);
  670. }
  671. davDroidLoginIntent.putExtra("username", UserAccountManager.getUsername(user.toPlatformAccount()));
  672. startActivityForResult(davDroidLoginIntent, ACTION_REQUEST_CODE_DAVDROID_SETUP);
  673. } else {
  674. // DAVdroid not installed
  675. Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=at.bitfire.davdroid"));
  676. // launch market(s)
  677. if (installIntent.resolveActivity(getPackageManager()) != null) {
  678. startActivity(installIntent);
  679. } else {
  680. // no f-droid market app or Play store installed --> launch browser for f-droid url
  681. Intent downloadIntent = new Intent(Intent.ACTION_VIEW,
  682. Uri.parse("https://f-droid.org/repository/browse/?fdid=at.bitfire.davdroid"));
  683. DisplayUtils.startIntentIfAppAvailable(downloadIntent, this, R.string.no_browser_available);
  684. DisplayUtils.showSnackMessage(this, R.string.prefs_calendar_contacts_no_store_error);
  685. }
  686. }
  687. }
  688. private void setupBaseUri() {
  689. // retrieve and set user's base URI
  690. Thread t = new Thread(() -> {
  691. try {
  692. serverBaseUri = clientFactory.create(user).getBaseUri();
  693. } catch (Exception e) {
  694. Log_OC.e(TAG, "Error retrieving user's base URI", e);
  695. }
  696. });
  697. t.start();
  698. }
  699. @Override
  700. public boolean onCreateOptionsMenu(Menu menu) {
  701. super.onCreateOptionsMenu(menu);
  702. return true;
  703. }
  704. @Override
  705. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  706. super.onActivityResult(requestCode, resultCode, data);
  707. if (requestCode == ACTION_REQUEST_PASSCODE && resultCode == RESULT_OK) {
  708. String passcode = data.getStringExtra(PassCodeActivity.KEY_PASSCODE);
  709. if (passcode != null && passcode.length() == 4) {
  710. SharedPreferences.Editor appPrefs = PreferenceManager
  711. .getDefaultSharedPreferences(getApplicationContext()).edit();
  712. for (int i = 1; i <= 4; ++i) {
  713. appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i - 1, i));
  714. }
  715. appPrefs.apply();
  716. changeLockSetting(LOCK_PASSCODE);
  717. DisplayUtils.showSnackMessage(this, R.string.pass_code_stored);
  718. }
  719. } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
  720. if (data.getBooleanExtra(PassCodeActivity.KEY_CHECK_RESULT, false)) {
  721. changeLockSetting(LOCK_NONE);
  722. DisplayUtils.showSnackMessage(this, R.string.pass_code_removed);
  723. if (!LOCK_NONE.equals(pendingLock)) {
  724. enableLock(pendingLock);
  725. }
  726. }
  727. } else if (requestCode == ACTION_REQUEST_CODE_DAVDROID_SETUP && resultCode == RESULT_OK) {
  728. DisplayUtils.showSnackMessage(this, R.string.prefs_calendar_contacts_sync_setup_successful);
  729. } else if (requestCode == ACTION_CONFIRM_DEVICE_CREDENTIALS && resultCode == RESULT_OK &&
  730. Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
  731. data.getIntExtra(RequestCredentialsActivity.KEY_CHECK_RESULT,
  732. RequestCredentialsActivity.KEY_CHECK_RESULT_FALSE) ==
  733. RequestCredentialsActivity.KEY_CHECK_RESULT_TRUE) {
  734. changeLockSetting(LOCK_NONE);
  735. DisplayUtils.showSnackMessage(this, R.string.credentials_disabled);
  736. if (!LOCK_NONE.equals(pendingLock)) {
  737. enableLock(pendingLock);
  738. }
  739. } else if (requestCode == PassCodeManager.PASSCODE_ACTIVITY && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  740. handleMnemonicRequest(data);
  741. }
  742. }
  743. @RequiresApi(api = Build.VERSION_CODES.M)
  744. @VisibleForTesting
  745. public void handleMnemonicRequest(Intent data) {
  746. if (data == null) {
  747. DisplayUtils.showSnackMessage(this, "Error retrieving mnemonic!");
  748. } else {
  749. if (data.getIntExtra(RequestCredentialsActivity.KEY_CHECK_RESULT,
  750. RequestCredentialsActivity.KEY_CHECK_RESULT_FALSE) ==
  751. RequestCredentialsActivity.KEY_CHECK_RESULT_TRUE) {
  752. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  753. String mnemonic = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.MNEMONIC);
  754. AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.FallbackTheming_Dialog);
  755. AlertDialog alertDialog = builder.setTitle(R.string.prefs_e2e_mnemonic)
  756. .setMessage(mnemonic)
  757. .setPositiveButton(R.string.common_ok, (dialog, which) -> dialog.dismiss())
  758. .create();
  759. alertDialog.show();
  760. ThemeButtonUtils.themeBorderlessButton(alertDialog.getButton(AlertDialog.BUTTON_POSITIVE));
  761. }
  762. }
  763. }
  764. @Override
  765. @NonNull
  766. public MenuInflater getMenuInflater() {
  767. return getDelegate().getMenuInflater();
  768. }
  769. @Override
  770. public void setContentView(@LayoutRes int layoutResID) {
  771. getDelegate().setContentView(layoutResID);
  772. }
  773. @Override
  774. public void setContentView(View view) {
  775. getDelegate().setContentView(view);
  776. }
  777. @Override
  778. public void setContentView(View view, ViewGroup.LayoutParams params) {
  779. getDelegate().setContentView(view, params);
  780. }
  781. @Override
  782. public void addContentView(View view, ViewGroup.LayoutParams params) {
  783. getDelegate().addContentView(view, params);
  784. }
  785. @Override
  786. protected void onPostResume() {
  787. super.onPostResume();
  788. getDelegate().onPostResume();
  789. }
  790. @Override
  791. protected void onTitleChanged(CharSequence title, int color) {
  792. super.onTitleChanged(title, color);
  793. getDelegate().setTitle(title);
  794. }
  795. @Override
  796. public void onConfigurationChanged(@NonNull Configuration newConfig) {
  797. super.onConfigurationChanged(newConfig);
  798. getDelegate().onConfigurationChanged(newConfig);
  799. }
  800. @Override
  801. protected void onPostCreate(Bundle savedInstanceState) {
  802. super.onPostCreate(savedInstanceState);
  803. getDelegate().onPostCreate(savedInstanceState);
  804. }
  805. @Override
  806. protected void onDestroy() {
  807. super.onDestroy();
  808. getDelegate().onDestroy();
  809. }
  810. @Override
  811. protected void onStop() {
  812. super.onStop();
  813. getDelegate().onStop();
  814. }
  815. public void invalidateOptionsMenu() {
  816. getDelegate().invalidateOptionsMenu();
  817. }
  818. private AppCompatDelegate getDelegate() {
  819. if (delegate == null) {
  820. delegate = AppCompatDelegate.create(this, null);
  821. }
  822. return delegate;
  823. }
  824. private void loadExternalSettingLinks(PreferenceCategory preferenceCategory) {
  825. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  826. ExternalLinksProvider externalLinksProvider = new ExternalLinksProvider(getContentResolver());
  827. for (final ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.SETTINGS)) {
  828. // only add if it does not exist, in case activity is re-used
  829. if (findPreference(link.id.toString()) == null) {
  830. Preference p = new Preference(this);
  831. p.setTitle(link.name);
  832. p.setKey(link.id.toString());
  833. p.setOnPreferenceClickListener(preference -> {
  834. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  835. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, link.name);
  836. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, link.url);
  837. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  838. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, link.id);
  839. startActivity(externalWebViewIntent);
  840. return true;
  841. });
  842. preferenceCategory.addPreference(p);
  843. }
  844. }
  845. }
  846. }
  847. /**
  848. * Save storage path
  849. */
  850. private void saveStoragePath(String newStoragePath) {
  851. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  852. storagePath = newStoragePath;
  853. MainApp.setStoragePath(storagePath);
  854. SharedPreferences.Editor editor = appPrefs.edit();
  855. editor.putString(AppPreferencesImpl.STORAGE_PATH, storagePath);
  856. editor.apply();
  857. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(storagePath);
  858. prefStoragePath.setSummary(storageDescription);
  859. prefStoragePath.setValue(newStoragePath);
  860. }
  861. /**
  862. * Load storage path set on preferences
  863. */
  864. private void loadStoragePath() {
  865. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  866. // Load storage path from shared preferences. Use private internal storage by default.
  867. storagePath = appPrefs.getString(AppPreferencesImpl.STORAGE_PATH,
  868. getApplicationContext().getFilesDir().getAbsolutePath());
  869. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(storagePath);
  870. prefStoragePath.setSummary(storageDescription);
  871. }
  872. @Override
  873. public void onStorageMigrationFinished(String storagePath, boolean succeed) {
  874. if (succeed) {
  875. saveStoragePath(storagePath);
  876. }
  877. }
  878. @Override
  879. public void onCancelMigration() {
  880. // Migration was canceled so we don't do anything
  881. }
  882. @Override
  883. public void returnVersion(Integer latestVersion) {
  884. FileActivity.showDevSnackbar(this, latestVersion, true, false);
  885. }
  886. }