SettingsActivity.java 43 KB

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