SettingsActivity.java 44 KB

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