SettingsActivity.java 43 KB

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