SettingsActivity.java 43 KB

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