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. Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class);
  389. contactsIntent.putExtra(ContactsPreferenceActivity.EXTRA_SHOW_SIDEBAR, false);
  390. startActivity(contactsIntent);
  391. return true;
  392. });
  393. } else {
  394. preferenceCategoryMore.removePreference(pContactsBackup);
  395. }
  396. }
  397. }
  398. private void setupCalendarPreference(PreferenceCategory preferenceCategoryMore) {
  399. boolean calendarContactsEnabled = getResources().getBoolean(R.bool.davdroid_integration_enabled);
  400. Preference pCalendarContacts = findPreference("calendar_contacts");
  401. if (pCalendarContacts != null) {
  402. if (calendarContactsEnabled) {
  403. final Activity activity = this;
  404. pCalendarContacts.setOnPreferenceClickListener(preference -> {
  405. try {
  406. launchDavDroidLogin();
  407. } catch (Throwable t) {
  408. Log_OC.e(TAG, "Base Uri for account could not be resolved to call DAVdroid!", t);
  409. DisplayUtils.showSnackMessage(
  410. activity,
  411. R.string.prefs_calendar_contacts_address_resolve_error
  412. );
  413. }
  414. return true;
  415. });
  416. } else {
  417. preferenceCategoryMore.removePreference(pCalendarContacts);
  418. }
  419. }
  420. }
  421. private void setupDetailsCategory(int accentColor, PreferenceScreen preferenceScreen) {
  422. PreferenceCategory preferenceCategoryDetails = (PreferenceCategory) findPreference("details");
  423. preferenceCategoryDetails.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_details),
  424. accentColor));
  425. boolean fPassCodeEnabled = getResources().getBoolean(R.bool.passcode_enabled);
  426. boolean fDeviceCredentialsEnabled = getResources().getBoolean(R.bool.device_credentials_enabled);
  427. boolean fShowHiddenFilesEnabled = getResources().getBoolean(R.bool.show_hidden_files_enabled);
  428. boolean fSyncedFolderLightEnabled = getResources().getBoolean(R.bool.syncedFolder_light);
  429. boolean fShowMediaScanNotifications = preferences.isShowMediaScanNotifications();
  430. setupLockPreference(preferenceCategoryDetails, fPassCodeEnabled, fDeviceCredentialsEnabled);
  431. setupHiddenFilesPreference(preferenceCategoryDetails, fShowHiddenFilesEnabled);
  432. setupShowMediaScanNotifications(preferenceCategoryDetails, fShowMediaScanNotifications);
  433. if (!fPassCodeEnabled && !fDeviceCredentialsEnabled && !fShowHiddenFilesEnabled && fSyncedFolderLightEnabled
  434. && fShowMediaScanNotifications) {
  435. preferenceScreen.removePreference(preferenceCategoryDetails);
  436. }
  437. }
  438. private void setupShowMediaScanNotifications(PreferenceCategory preferenceCategoryDetails,
  439. boolean fShowMediaScanNotifications) {
  440. SwitchPreference mShowMediaScanNotifications = (SwitchPreference) findPreference(PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS);
  441. if (fShowMediaScanNotifications) {
  442. preferenceCategoryDetails.removePreference(mShowMediaScanNotifications);
  443. }
  444. }
  445. private void setupHiddenFilesPreference(PreferenceCategory preferenceCategoryDetails,
  446. boolean fShowHiddenFilesEnabled) {
  447. showHiddenFiles = (SwitchPreference) findPreference("show_hidden_files");
  448. if (fShowHiddenFilesEnabled) {
  449. showHiddenFiles.setOnPreferenceClickListener(preference -> {
  450. preferences.setShowHiddenFilesEnabled(showHiddenFiles.isChecked());
  451. return true;
  452. });
  453. } else {
  454. preferenceCategoryDetails.removePreference(showHiddenFiles);
  455. }
  456. }
  457. private void setupLockPreference(PreferenceCategory preferenceCategoryDetails,
  458. boolean passCodeEnabled,
  459. boolean deviceCredentialsEnabled) {
  460. lock = (ListPreference) findPreference(PREFERENCE_LOCK);
  461. if (lock != null && (passCodeEnabled || deviceCredentialsEnabled)) {
  462. ArrayList<String> lockEntries = new ArrayList<>(3);
  463. lockEntries.add(getString(R.string.prefs_lock_none));
  464. lockEntries.add(getString(R.string.prefs_lock_using_passcode));
  465. lockEntries.add(getString(R.string.prefs_lock_using_device_credentials));
  466. ArrayList<String> lockValues = new ArrayList<>(3);
  467. lockValues.add(LOCK_NONE);
  468. lockValues.add(LOCK_PASSCODE);
  469. lockValues.add(LOCK_DEVICE_CREDENTIALS);
  470. if (!passCodeEnabled) {
  471. lockEntries.remove(1);
  472. lockValues.remove(1);
  473. } else if (!deviceCredentialsEnabled || Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
  474. !DeviceCredentialUtils.areCredentialsAvailable(getApplicationContext())) {
  475. lockEntries.remove(2);
  476. lockValues.remove(2);
  477. }
  478. String[] lockEntriesArr = new String[lockEntries.size()];
  479. lockEntriesArr = lockEntries.toArray(lockEntriesArr);
  480. String[] lockValuesArr = new String[lockValues.size()];
  481. lockValuesArr = lockValues.toArray(lockValuesArr);
  482. lock.setEntries(lockEntriesArr);
  483. lock.setEntryValues(lockValuesArr);
  484. lock.setSummary(lock.getEntry());
  485. lock.setOnPreferenceChangeListener((preference, o) -> {
  486. pendingLock = LOCK_NONE;
  487. String oldValue = ((ListPreference) preference).getValue();
  488. String newValue = (String) o;
  489. if (!oldValue.equals(newValue)) {
  490. if (LOCK_NONE.equals(oldValue)) {
  491. enableLock(newValue);
  492. } else {
  493. pendingLock = newValue;
  494. disableLock(oldValue);
  495. }
  496. }
  497. return false;
  498. });
  499. } else {
  500. preferenceCategoryDetails.removePreference(lock);
  501. }
  502. }
  503. private void setupAutoUploadCategory(int accentColor, PreferenceScreen preferenceScreen) {
  504. PreferenceCategory preferenceCategorySyncedFolders =
  505. (PreferenceCategory) findPreference("synced_folders_category");
  506. preferenceCategorySyncedFolders.setTitle(ThemeUtils.getColoredTitle(getString(R.string.drawer_synced_folders),
  507. accentColor));
  508. if (!getResources().getBoolean(R.bool.syncedFolder_light)) {
  509. preferenceScreen.removePreference(preferenceCategorySyncedFolders);
  510. } else {
  511. // Upload on WiFi
  512. final ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  513. final SwitchPreference pUploadOnWifiCheckbox = (SwitchPreference) findPreference("synced_folder_on_wifi");
  514. pUploadOnWifiCheckbox.setChecked(
  515. arbitraryDataProvider.getBooleanValue(user.toPlatformAccount(), SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI));
  516. pUploadOnWifiCheckbox.setOnPreferenceClickListener(preference -> {
  517. arbitraryDataProvider.storeOrUpdateKeyValue(user.getAccountName(), SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI,
  518. String.valueOf(pUploadOnWifiCheckbox.isChecked()));
  519. return true;
  520. });
  521. Preference pSyncedFolder = findPreference("synced_folders_configure_folders");
  522. if (pSyncedFolder != null) {
  523. if (getResources().getBoolean(R.bool.syncedFolder_light)
  524. && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  525. pSyncedFolder.setOnPreferenceClickListener(preference -> {
  526. Intent syncedFoldersIntent = new Intent(getApplicationContext(), SyncedFoldersActivity.class);
  527. syncedFoldersIntent.putExtra(SyncedFoldersActivity.EXTRA_SHOW_SIDEBAR, false);
  528. startActivity(syncedFoldersIntent);
  529. return true;
  530. });
  531. } else {
  532. preferenceCategorySyncedFolders.removePreference(pSyncedFolder);
  533. }
  534. }
  535. }
  536. }
  537. private void enableLock(String lock) {
  538. pendingLock = LOCK_NONE;
  539. if (LOCK_PASSCODE.equals(lock)) {
  540. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  541. i.setAction(PassCodeActivity.ACTION_REQUEST_WITH_RESULT);
  542. startActivityForResult(i, ACTION_REQUEST_PASSCODE);
  543. } else if (LOCK_DEVICE_CREDENTIALS.equals(lock)){
  544. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
  545. !DeviceCredentialUtils.areCredentialsAvailable(getApplicationContext())) {
  546. DisplayUtils.showSnackMessage(this, R.string.prefs_lock_device_credentials_not_setup);
  547. } else {
  548. DisplayUtils.showSnackMessage(this, R.string.prefs_lock_device_credentials_enabled);
  549. this.lock.setValue(LOCK_DEVICE_CREDENTIALS);
  550. this.lock.setSummary(this.lock.getEntry());
  551. }
  552. }
  553. }
  554. private void disableLock(String lock) {
  555. if (LOCK_PASSCODE.equals(lock)) {
  556. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  557. i.setAction(PassCodeActivity.ACTION_CHECK_WITH_RESULT);
  558. startActivityForResult(i, ACTION_CONFIRM_PASSCODE);
  559. } else if (LOCK_DEVICE_CREDENTIALS.equals(lock)) {
  560. Intent i = new Intent(getApplicationContext(), RequestCredentialsActivity.class);
  561. startActivityForResult(i, ACTION_CONFIRM_DEVICE_CREDENTIALS);
  562. }
  563. }
  564. private void setupGeneralCategory(int accentColor) {
  565. PreferenceCategory preferenceCategoryGeneral = (PreferenceCategory) findPreference("general");
  566. preferenceCategoryGeneral.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_general),
  567. accentColor));
  568. prefStoragePath = (ListPreference) findPreference(AppPreferencesImpl.STORAGE_PATH);
  569. if (prefStoragePath != null) {
  570. StoragePoint[] storageOptions = DataStorageProvider.getInstance().getAvailableStoragePoints();
  571. String[] entries = new String[storageOptions.length];
  572. String[] values = new String[storageOptions.length];
  573. for (int i = 0; i < storageOptions.length; ++i) {
  574. entries[i] = storageOptions[i].getDescription();
  575. values[i] = storageOptions[i].getPath();
  576. }
  577. prefStoragePath.setEntries(entries);
  578. prefStoragePath.setEntryValues(values);
  579. prefStoragePath.setOnPreferenceChangeListener((preference, newValue) -> {
  580. String newPath = (String) newValue;
  581. if (storagePath.equals(newPath)) {
  582. return true;
  583. }
  584. StorageMigration storageMigration = new StorageMigration(this, storagePath, newPath);
  585. storageMigration.setStorageMigrationProgressListener(this);
  586. storageMigration.migrate();
  587. return false;
  588. });
  589. }
  590. loadStoragePath();
  591. ListPreference themePref = (ListPreference) findPreference("darkMode");
  592. List<String> themeEntries = new ArrayList<>(3);
  593. themeEntries.add(getString(R.string.prefs_value_theme_light));
  594. themeEntries.add(getString(R.string.prefs_value_theme_dark));
  595. themeEntries.add(getString(R.string.prefs_value_theme_system));
  596. List<String> themeValues = new ArrayList<>(3);
  597. themeValues.add(DarkMode.LIGHT.name());
  598. themeValues.add(DarkMode.DARK.name());
  599. themeValues.add(DarkMode.SYSTEM.name());
  600. themePref.setEntries(themeEntries.toArray(new String[0]));
  601. themePref.setEntryValues(themeValues.toArray(new String[0]));
  602. if (TextUtils.isEmpty(themePref.getEntry())) {
  603. themePref.setValue(DarkMode.SYSTEM.name());
  604. themePref.setSummary(TextUtils.isEmpty(themePref.getEntry()) ? DarkMode.SYSTEM.name() : themePref.getEntry());
  605. }
  606. themePref.setOnPreferenceChangeListener((preference, newValue) -> {
  607. DarkMode mode = DarkMode.valueOf((String) newValue);
  608. preferences.setDarkThemeMode(mode);
  609. MainApp.setAppTheme(mode);
  610. return true;
  611. });
  612. }
  613. private String getAppVersion() {
  614. String temp;
  615. try {
  616. PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  617. temp = pkg.versionName;
  618. } catch (NameNotFoundException e) {
  619. temp = "";
  620. Log_OC.e(TAG, "Error while showing about dialog", e);
  621. }
  622. return temp;
  623. }
  624. @Override
  625. public boolean onOptionsItemSelected(MenuItem item) {
  626. finish();
  627. return super.onOptionsItemSelected(item);
  628. }
  629. private void setupActionBar() {
  630. ActionBar actionBar = getDelegate().getSupportActionBar();
  631. if (actionBar != null) {
  632. ThemeUtils.setColoredTitle(actionBar, getString(R.string.actionbar_settings), this);
  633. ThemeUtils.colorStatusBar(this);
  634. actionBar.setBackgroundDrawable(new ColorDrawable(ThemeUtils.primaryAppbarColor(this)));
  635. actionBar.setDisplayHomeAsUpEnabled(true);
  636. ThemeUtils.tintBackButton(actionBar, this);
  637. }
  638. }
  639. private void launchDavDroidLogin() {
  640. Intent davDroidLoginIntent = new Intent();
  641. davDroidLoginIntent.setClassName("at.bitfire.davdroid", "at.bitfire.davdroid.ui.setup.LoginActivity");
  642. if (getPackageManager().resolveActivity(davDroidLoginIntent, 0) != null) {
  643. // arguments
  644. if (serverBaseUri != null) {
  645. davDroidLoginIntent.putExtra("url", serverBaseUri.toString() + DAV_PATH);
  646. davDroidLoginIntent.putExtra("loginFlow", TRUE_VALUE);
  647. davDroidLoginIntent.setData(Uri.parse(serverBaseUri.toString() + AuthenticatorActivity.WEB_LOGIN));
  648. davDroidLoginIntent.putExtra("davPath", DAV_PATH);
  649. }
  650. davDroidLoginIntent.putExtra("username", UserAccountManager.getUsername(user.toPlatformAccount()));
  651. startActivityForResult(davDroidLoginIntent, ACTION_REQUEST_CODE_DAVDROID_SETUP);
  652. } else {
  653. // DAVdroid not installed
  654. Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=at.bitfire.davdroid"));
  655. // launch market(s)
  656. if (installIntent.resolveActivity(getPackageManager()) != null) {
  657. startActivity(installIntent);
  658. } else {
  659. // no f-droid market app or Play store installed --> launch browser for f-droid url
  660. Intent downloadIntent = new Intent(Intent.ACTION_VIEW,
  661. Uri.parse("https://f-droid.org/repository/browse/?fdid=at.bitfire.davdroid"));
  662. DisplayUtils.startIntentIfAppAvailable(downloadIntent, this, R.string.no_browser_available);
  663. DisplayUtils.showSnackMessage(this, R.string.prefs_calendar_contacts_no_store_error);
  664. }
  665. }
  666. }
  667. private void setupBaseUri() {
  668. // retrieve and set user's base URI
  669. Thread t = new Thread(() -> {
  670. try {
  671. serverBaseUri = clientFactory.create(user).getBaseUri();
  672. } catch (Exception e) {
  673. Log_OC.e(TAG, "Error retrieving user's base URI", e);
  674. }
  675. });
  676. t.start();
  677. }
  678. @Override
  679. public boolean onCreateOptionsMenu(Menu menu) {
  680. super.onCreateOptionsMenu(menu);
  681. return true;
  682. }
  683. @Override
  684. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  685. super.onActivityResult(requestCode, resultCode, data);
  686. if (requestCode == ACTION_REQUEST_PASSCODE && resultCode == RESULT_OK) {
  687. String passcode = data.getStringExtra(PassCodeActivity.KEY_PASSCODE);
  688. if (passcode != null && passcode.length() == 4) {
  689. SharedPreferences.Editor appPrefs = PreferenceManager
  690. .getDefaultSharedPreferences(getApplicationContext()).edit();
  691. for (int i = 1; i <= 4; ++i) {
  692. appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i - 1, i));
  693. }
  694. appPrefs.apply();
  695. lock.setValue(LOCK_PASSCODE);
  696. lock.setSummary(lock.getEntry());
  697. DisplayUtils.showSnackMessage(this, R.string.pass_code_stored);
  698. }
  699. } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
  700. if (data.getBooleanExtra(PassCodeActivity.KEY_CHECK_RESULT, false)) {
  701. lock.setValue(LOCK_NONE);
  702. lock.setSummary(lock.getEntry());
  703. DisplayUtils.showSnackMessage(this, R.string.pass_code_removed);
  704. if (!LOCK_NONE.equals(pendingLock)) {
  705. enableLock(pendingLock);
  706. }
  707. }
  708. } else if (requestCode == ACTION_REQUEST_CODE_DAVDROID_SETUP && resultCode == RESULT_OK) {
  709. DisplayUtils.showSnackMessage(this, R.string.prefs_calendar_contacts_sync_setup_successful);
  710. } else if (requestCode == ACTION_CONFIRM_DEVICE_CREDENTIALS && resultCode == RESULT_OK &&
  711. Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
  712. data.getIntExtra(RequestCredentialsActivity.KEY_CHECK_RESULT,
  713. RequestCredentialsActivity.KEY_CHECK_RESULT_FALSE) ==
  714. RequestCredentialsActivity.KEY_CHECK_RESULT_TRUE) {
  715. lock.setValue(LOCK_NONE);
  716. lock.setSummary(lock.getEntry());
  717. DisplayUtils.showSnackMessage(this, R.string.credentials_disabled);
  718. if (!LOCK_NONE.equals(pendingLock)) {
  719. enableLock(pendingLock);
  720. }
  721. } else if (requestCode == PassCodeManager.PASSCODE_ACTIVITY && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  722. if (data == null) {
  723. DisplayUtils.showSnackMessage(this, "Error retrieving mnemonic!");
  724. } else {
  725. if (data.getIntExtra(RequestCredentialsActivity.KEY_CHECK_RESULT,
  726. RequestCredentialsActivity.KEY_CHECK_RESULT_FALSE) ==
  727. RequestCredentialsActivity.KEY_CHECK_RESULT_TRUE) {
  728. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  729. String mnemonic = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.MNEMONIC);
  730. int accentColor = ThemeUtils.primaryAccentColor(this);
  731. AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.FallbackTheming_Dialog);
  732. builder.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_e2e_mnemonic), accentColor));
  733. builder.setMessage(mnemonic);
  734. builder.setPositiveButton(ThemeUtils.getColoredTitle(getString(R.string.common_ok), accentColor),
  735. (dialog, which) -> dialog.dismiss());
  736. builder.show();
  737. }
  738. }
  739. }
  740. }
  741. @Override
  742. @NonNull
  743. public MenuInflater getMenuInflater() {
  744. return getDelegate().getMenuInflater();
  745. }
  746. @Override
  747. public void setContentView(@LayoutRes int layoutResID) {
  748. getDelegate().setContentView(layoutResID);
  749. }
  750. @Override
  751. public void setContentView(View view) {
  752. getDelegate().setContentView(view);
  753. }
  754. @Override
  755. public void setContentView(View view, ViewGroup.LayoutParams params) {
  756. getDelegate().setContentView(view, params);
  757. }
  758. @Override
  759. public void addContentView(View view, ViewGroup.LayoutParams params) {
  760. getDelegate().addContentView(view, params);
  761. }
  762. @Override
  763. protected void onPostResume() {
  764. super.onPostResume();
  765. getDelegate().onPostResume();
  766. }
  767. @Override
  768. protected void onTitleChanged(CharSequence title, int color) {
  769. super.onTitleChanged(title, color);
  770. getDelegate().setTitle(title);
  771. }
  772. @Override
  773. public void onConfigurationChanged(Configuration newConfig) {
  774. super.onConfigurationChanged(newConfig);
  775. getDelegate().onConfigurationChanged(newConfig);
  776. }
  777. @Override
  778. protected void onPostCreate(Bundle savedInstanceState) {
  779. super.onPostCreate(savedInstanceState);
  780. getDelegate().onPostCreate(savedInstanceState);
  781. }
  782. @Override
  783. protected void onDestroy() {
  784. super.onDestroy();
  785. getDelegate().onDestroy();
  786. }
  787. @Override
  788. protected void onStop() {
  789. super.onStop();
  790. getDelegate().onStop();
  791. }
  792. public void invalidateOptionsMenu() {
  793. getDelegate().invalidateOptionsMenu();
  794. }
  795. private AppCompatDelegate getDelegate() {
  796. if (delegate == null) {
  797. delegate = AppCompatDelegate.create(this, null);
  798. }
  799. return delegate;
  800. }
  801. private void loadExternalSettingLinks(PreferenceCategory preferenceCategory) {
  802. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  803. ExternalLinksProvider externalLinksProvider = new ExternalLinksProvider(getContentResolver());
  804. for (final ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.SETTINGS)) {
  805. // only add if it does not exist, in case activity is re-used
  806. if (findPreference(link.id.toString()) == null) {
  807. Preference p = new Preference(this);
  808. p.setTitle(link.name);
  809. p.setKey(link.id.toString());
  810. p.setOnPreferenceClickListener(preference -> {
  811. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  812. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, link.name);
  813. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, link.url);
  814. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  815. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, link.id);
  816. startActivity(externalWebViewIntent);
  817. return true;
  818. });
  819. preferenceCategory.addPreference(p);
  820. }
  821. }
  822. }
  823. }
  824. /**
  825. * Save storage path
  826. */
  827. private void saveStoragePath(String newStoragePath) {
  828. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  829. storagePath = newStoragePath;
  830. MainApp.setStoragePath(storagePath);
  831. SharedPreferences.Editor editor = appPrefs.edit();
  832. editor.putString(AppPreferencesImpl.STORAGE_PATH, storagePath);
  833. editor.apply();
  834. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(storagePath);
  835. prefStoragePath.setSummary(storageDescription);
  836. prefStoragePath.setValue(newStoragePath);
  837. }
  838. /**
  839. * Load storage path set on preferences
  840. */
  841. private void loadStoragePath() {
  842. SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  843. // Load storage path from shared preferences. Use private internal storage by default.
  844. storagePath = appPrefs.getString(AppPreferencesImpl.STORAGE_PATH,
  845. getApplicationContext().getFilesDir().getAbsolutePath());
  846. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(storagePath);
  847. prefStoragePath.setSummary(storageDescription);
  848. }
  849. @Override
  850. public void onStorageMigrationFinished(String storagePath, boolean succeed) {
  851. if (succeed) {
  852. saveStoragePath(storagePath);
  853. }
  854. }
  855. @Override
  856. public void onCancelMigration() {
  857. // Migration was canceled so we don't do anything
  858. }
  859. @Override
  860. public void returnVersion(Integer latestVersion) {
  861. FileActivity.showDevSnackbar(this, latestVersion, true, false);
  862. }
  863. }