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