SettingsActivity.java 43 KB

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