Preferences.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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.accounts.AuthenticatorException;
  26. import android.accounts.OperationCanceledException;
  27. import android.content.Intent;
  28. import android.content.SharedPreferences;
  29. import android.content.pm.PackageInfo;
  30. import android.content.pm.PackageManager.NameNotFoundException;
  31. import android.content.res.Configuration;
  32. import android.graphics.drawable.ColorDrawable;
  33. import android.graphics.drawable.Drawable;
  34. import android.net.Uri;
  35. import android.os.Build;
  36. import android.os.Bundle;
  37. import android.os.Environment;
  38. import android.preference.ListPreference;
  39. import android.preference.Preference;
  40. import android.preference.Preference.OnPreferenceChangeListener;
  41. import android.preference.Preference.OnPreferenceClickListener;
  42. import android.preference.PreferenceActivity;
  43. import android.preference.PreferenceCategory;
  44. import android.preference.PreferenceManager;
  45. import android.preference.PreferenceScreen;
  46. import android.preference.SwitchPreference;
  47. import android.support.annotation.LayoutRes;
  48. import android.support.annotation.Nullable;
  49. import android.support.v7.app.ActionBar;
  50. import android.support.v7.app.AppCompatDelegate;
  51. import android.support.v7.widget.Toolbar;
  52. import android.view.Menu;
  53. import android.view.MenuInflater;
  54. import android.view.MenuItem;
  55. import android.view.View;
  56. import android.view.ViewGroup;
  57. import android.widget.Toast;
  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.datamodel.ArbitraryDataProvider;
  63. import com.owncloud.android.datamodel.ExternalLinksProvider;
  64. import com.owncloud.android.datamodel.OCFile;
  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.CheckBoxPreferenceWithLongTitle;
  73. import com.owncloud.android.utils.AnalyticsUtils;
  74. import com.owncloud.android.utils.DisplayUtils;
  75. import com.owncloud.android.utils.ThemeUtils;
  76. import java.io.IOException;
  77. /**
  78. * An Activity that allows the user to change the application's settings.
  79. *
  80. * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used with AppCompat.
  81. */
  82. public class Preferences extends PreferenceActivity
  83. implements StorageMigration.StorageMigrationProgressListener {
  84. private static final String TAG = Preferences.class.getSimpleName();
  85. private static final String SCREEN_NAME = "Settings";
  86. private static final int ACTION_SELECT_UPLOAD_PATH = 1;
  87. private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
  88. private static final int ACTION_REQUEST_PASSCODE = 5;
  89. private static final int ACTION_CONFIRM_PASSCODE = 6;
  90. private static final int ACTION_REQUEST_CODE_DAVDROID_SETUP = 10;
  91. public static final String SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI = "SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI";
  92. /**
  93. * The user's server base uri.
  94. */
  95. private Uri mUri;
  96. private SwitchPreference pCode;
  97. private SwitchPreference fPrint;
  98. private SwitchPreference mShowHiddenFiles;
  99. private Preference pAboutApp;
  100. private AppCompatDelegate mDelegate;
  101. private String mUploadPath;
  102. private PreferenceCategory mPrefInstantUploadCategory;
  103. private CheckBoxPreferenceWithLongTitle mPrefInstantUpload;
  104. private Preference mPrefInstantUploadBehaviour;
  105. private Preference mPrefInstantUploadPath;
  106. private Preference mPrefInstantUploadUseSubfolders;
  107. private Preference mPrefInstantPictureUploadOnlyOnCharging;
  108. private Preference mPrefInstantUploadPathWiFi;
  109. private CheckBoxPreferenceWithLongTitle mPrefInstantVideoUpload;
  110. private Preference mPrefInstantVideoUploadPath;
  111. private Preference mPrefInstantVideoUploadUseSubfolders;
  112. private Preference mPrefInstantVideoUploadPathWiFi;
  113. private Preference mPrefInstantVideoUploadOnlyOnCharging;
  114. private String mUploadVideoPath;
  115. private ListPreference mPrefStoragePath;
  116. private String mStoragePath;
  117. public static class PreferenceKeys {
  118. public static final String STORAGE_PATH = "storage_path";
  119. public static final String INSTANT_UPLOAD_PATH = "instant_upload_path";
  120. public static final String INSTANT_VIDEO_UPLOAD_PATH = "instant_video_upload_path";
  121. }
  122. @SuppressWarnings("deprecation")
  123. @Override
  124. public void onCreate(Bundle savedInstanceState) {
  125. if (ThemeUtils.themingEnabled()) {
  126. setTheme(R.style.FallbackThemingTheme);
  127. }
  128. getDelegate().installViewFactory();
  129. getDelegate().onCreate(savedInstanceState);
  130. super.onCreate(savedInstanceState);
  131. addPreferencesFromResource(R.xml.preferences);
  132. ActionBar actionBar = getSupportActionBar();
  133. actionBar.setDisplayHomeAsUpEnabled(true);
  134. ThemeUtils.setColoredTitle(actionBar, getString(R.string.actionbar_settings));
  135. actionBar.setBackgroundDrawable(new ColorDrawable(ThemeUtils.primaryColor()));
  136. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  137. getWindow().setStatusBarColor(ThemeUtils.primaryDarkColor());
  138. }
  139. Drawable backArrow = getResources().getDrawable(R.drawable.ic_arrow_back);
  140. actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontColor()));
  141. int accentColor = ThemeUtils.primaryAccentColor();
  142. // retrieve user's base uri
  143. setupBaseUri();
  144. // For adding content description tag to a title field in the action bar
  145. int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
  146. View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
  147. if (actionBarTitleView != null) { // it's null in Android 2.x
  148. getWindow().getDecorView().findViewById(actionBarTitleId).
  149. setContentDescription(getString(R.string.actionbar_settings));
  150. }
  151. // Load package info
  152. String temp;
  153. try {
  154. PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  155. temp = pkg.versionName;
  156. } catch (NameNotFoundException e) {
  157. temp = "";
  158. Log_OC.e(TAG, "Error while showing about dialog", e);
  159. }
  160. final String appVersion = temp;
  161. // Register context menu for list of preferences.
  162. registerForContextMenu(getListView());
  163. // General
  164. PreferenceCategory preferenceCategoryGeneral = (PreferenceCategory) findPreference("general");
  165. preferenceCategoryGeneral.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_general),
  166. accentColor));
  167. // Synced folders
  168. PreferenceCategory preferenceCategoryFolderSync = (PreferenceCategory) findPreference("folder_sync");
  169. preferenceCategoryFolderSync.setTitle(ThemeUtils.getColoredTitle(getString(R.string.drawer_folder_sync),
  170. accentColor));
  171. PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("preference_screen");
  172. if (!getResources().getBoolean(R.bool.syncedFolder_light)) {
  173. preferenceScreen.removePreference(preferenceCategoryFolderSync);
  174. } else {
  175. // Upload on WiFi
  176. final ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  177. final Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  178. final SwitchPreference pUploadOnWifiCheckbox = (SwitchPreference) findPreference("synced_folder_on_wifi");
  179. pUploadOnWifiCheckbox.setChecked(
  180. arbitraryDataProvider.getBooleanValue(account, SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI));
  181. pUploadOnWifiCheckbox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  182. @Override
  183. public boolean onPreferenceClick(Preference preference) {
  184. arbitraryDataProvider.storeOrUpdateKeyValue(account, SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI,
  185. String.valueOf(pUploadOnWifiCheckbox.isChecked()));
  186. return true;
  187. }
  188. });
  189. Preference pSyncedFolder = findPreference("folder_sync_folders");
  190. if (pSyncedFolder != null) {
  191. if (getResources().getBoolean(R.bool.syncedFolder_light)
  192. && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  193. pSyncedFolder.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  194. @Override
  195. public boolean onPreferenceClick(Preference preference) {
  196. Intent folderSyncIntent = new Intent(getApplicationContext(), FolderSyncActivity.class);
  197. folderSyncIntent.putExtra(FolderSyncActivity.EXTRA_SHOW_SIDEBAR, false);
  198. startActivity(folderSyncIntent);
  199. return true;
  200. }
  201. });
  202. } else {
  203. preferenceCategoryFolderSync.removePreference(pSyncedFolder);
  204. }
  205. }
  206. }
  207. PreferenceCategory preferenceCategoryDetails = (PreferenceCategory) findPreference("details");
  208. preferenceCategoryDetails.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_details),
  209. accentColor));
  210. boolean fPassCodeEnabled = getResources().getBoolean(R.bool.passcode_enabled);
  211. pCode = (SwitchPreference) findPreference(PassCodeActivity.PREFERENCE_SET_PASSCODE);
  212. if (pCode != null && fPassCodeEnabled) {
  213. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  214. @Override
  215. public boolean onPreferenceChange(Preference preference, Object newValue) {
  216. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  217. Boolean incoming = (Boolean) newValue;
  218. i.setAction(
  219. incoming ? PassCodeActivity.ACTION_REQUEST_WITH_RESULT :
  220. PassCodeActivity.ACTION_CHECK_WITH_RESULT
  221. );
  222. startActivityForResult(i, incoming ? ACTION_REQUEST_PASSCODE :
  223. ACTION_CONFIRM_PASSCODE);
  224. // Don't update just yet, we will decide on it in onActivityResult
  225. return false;
  226. }
  227. });
  228. } else {
  229. preferenceCategoryDetails.removePreference(pCode);
  230. }
  231. boolean fPrintEnabled = getResources().getBoolean(R.bool.fingerprint_enabled);
  232. fPrint = (SwitchPreference) findPreference(FingerprintActivity.PREFERENCE_USE_FINGERPRINT);
  233. if (fPrint != null) {
  234. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  235. if (FingerprintActivity.isFingerprintCapable(MainApp.getAppContext()) && fPrintEnabled) {
  236. fPrint.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  237. @Override
  238. public boolean onPreferenceChange(Preference preference, Object newValue) {
  239. Boolean incoming = (Boolean) newValue;
  240. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  241. if (FingerprintActivity.isFingerprintReady(MainApp.getAppContext())) {
  242. SharedPreferences appPrefs =
  243. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  244. SharedPreferences.Editor editor = appPrefs.edit();
  245. editor.putBoolean("use_fingerprint", incoming);
  246. editor.apply();
  247. return true;
  248. } else {
  249. if (incoming) {
  250. Toast.makeText(
  251. MainApp.getAppContext(),
  252. R.string.prefs_fingerprint_notsetup,
  253. Toast.LENGTH_LONG)
  254. .show();
  255. fPrint.setChecked(false);
  256. }
  257. SharedPreferences appPrefs =
  258. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  259. SharedPreferences.Editor editor = appPrefs.edit();
  260. editor.putBoolean("use_fingerprint", false);
  261. editor.apply();
  262. return false;
  263. }
  264. } else {
  265. return false;
  266. }
  267. }
  268. });
  269. if (!FingerprintActivity.isFingerprintReady(MainApp.getAppContext())) {
  270. fPrint.setChecked(false);
  271. }
  272. } else {
  273. preferenceCategoryDetails.removePreference(fPrint);
  274. }
  275. } else {
  276. preferenceCategoryDetails.removePreference(fPrint);
  277. }
  278. }
  279. boolean fShowHiddenFilesEnabled = getResources().getBoolean(R.bool.passcode_enabled);
  280. mShowHiddenFiles = (SwitchPreference) findPreference("show_hidden_files");
  281. if (fShowHiddenFilesEnabled) {
  282. mShowHiddenFiles.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  283. @Override
  284. public boolean onPreferenceClick(Preference preference) {
  285. SharedPreferences appPrefs =
  286. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  287. SharedPreferences.Editor editor = appPrefs.edit();
  288. editor.putBoolean("show_hidden_files_pref", mShowHiddenFiles.isChecked());
  289. editor.apply();
  290. return true;
  291. }
  292. });
  293. } else {
  294. preferenceCategoryDetails.removePreference(mShowHiddenFiles);
  295. }
  296. PreferenceCategory preferenceCategoryMore = (PreferenceCategory) findPreference("more");
  297. preferenceCategoryMore.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_more),
  298. accentColor));
  299. boolean calendarContactsEnabled = getResources().getBoolean(R.bool.davdroid_integration_enabled);
  300. Preference pCalendarContacts = findPreference("calendar_contacts");
  301. if (pCalendarContacts != null) {
  302. if (calendarContactsEnabled) {
  303. pCalendarContacts.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  304. @Override
  305. public boolean onPreferenceClick(Preference preference) {
  306. try {
  307. launchDavDroidLogin();
  308. } catch (Throwable t) {
  309. Log_OC.e(TAG, "Base Uri for account could not be resolved to call DAVdroid!", t);
  310. Toast.makeText(
  311. MainApp.getAppContext(),
  312. R.string.prefs_calendar_contacts_address_resolve_error,
  313. Toast.LENGTH_SHORT)
  314. .show();
  315. }
  316. return true;
  317. }
  318. });
  319. } else {
  320. preferenceCategoryMore.removePreference(pCalendarContacts);
  321. }
  322. }
  323. boolean contactsBackupEnabled = !getResources().getBoolean(R.bool.show_drawer_contacts_backup)
  324. && getResources().getBoolean(R.bool.contacts_backup);
  325. Preference pContactsBackup = findPreference("contacts");
  326. if (pCalendarContacts != null) {
  327. if (contactsBackupEnabled) {
  328. pContactsBackup.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  329. @Override
  330. public boolean onPreferenceClick(Preference preference) {
  331. Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class);
  332. contactsIntent.putExtra(ContactsPreferenceActivity.EXTRA_SHOW_SIDEBAR, false);
  333. startActivity(contactsIntent);
  334. return true;
  335. }
  336. });
  337. } else {
  338. preferenceCategoryMore.removePreference(pContactsBackup);
  339. }
  340. }
  341. if (!fShowHiddenFilesEnabled && !fPrintEnabled && !fPassCodeEnabled) {
  342. preferenceScreen.removePreference(preferenceCategoryDetails);
  343. }
  344. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  345. Preference pHelp = findPreference("help");
  346. if (pHelp != null) {
  347. if (helpEnabled) {
  348. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  349. @Override
  350. public boolean onPreferenceClick(Preference preference) {
  351. String helpWeb = getString(R.string.url_help);
  352. if (helpWeb != null && helpWeb.length() > 0) {
  353. Uri uriUrl = Uri.parse(helpWeb);
  354. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  355. startActivity(intent);
  356. }
  357. return true;
  358. }
  359. });
  360. } else {
  361. preferenceCategoryMore.removePreference(pHelp);
  362. }
  363. }
  364. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  365. Preference pRecommend = findPreference("recommend");
  366. if (pRecommend != null) {
  367. if (recommendEnabled) {
  368. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  369. @Override
  370. public boolean onPreferenceClick(Preference preference) {
  371. Intent intent = new Intent(Intent.ACTION_SENDTO);
  372. intent.setType("text/plain");
  373. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  374. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  375. String appName = getString(R.string.app_name);
  376. String downloadUrl = getString(R.string.url_app_download);
  377. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  378. String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl);
  379. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  380. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  381. startActivity(intent);
  382. return true;
  383. }
  384. });
  385. } else {
  386. preferenceCategoryMore.removePreference(pRecommend);
  387. }
  388. }
  389. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  390. Preference pFeedback = findPreference("feedback");
  391. if (pFeedback != null) {
  392. if (feedbackEnabled) {
  393. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  394. @Override
  395. public boolean onPreferenceClick(Preference preference) {
  396. String feedbackMail = getString(R.string.mail_feedback);
  397. String feedback = getText(R.string.prefs_feedback) + " - android v" + appVersion;
  398. Intent intent = new Intent(Intent.ACTION_SENDTO);
  399. intent.setType("text/plain");
  400. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  401. intent.setData(Uri.parse(feedbackMail));
  402. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  403. startActivity(intent);
  404. return true;
  405. }
  406. });
  407. } else {
  408. preferenceCategoryMore.removePreference(pFeedback);
  409. }
  410. }
  411. boolean loggerEnabled = getResources().getBoolean(R.bool.logger_enabled) || BuildConfig.DEBUG;
  412. Preference pLogger = findPreference("logger");
  413. if (pLogger != null) {
  414. if (loggerEnabled) {
  415. pLogger.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  416. @Override
  417. public boolean onPreferenceClick(Preference preference) {
  418. Intent loggerIntent = new Intent(getApplicationContext(), LogHistoryActivity.class);
  419. startActivity(loggerIntent);
  420. return true;
  421. }
  422. });
  423. } else {
  424. preferenceCategoryMore.removePreference(pLogger);
  425. }
  426. }
  427. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  428. Preference pImprint = findPreference("imprint");
  429. if (pImprint != null) {
  430. if (imprintEnabled) {
  431. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  432. @Override
  433. public boolean onPreferenceClick(Preference preference) {
  434. String imprintWeb = getString(R.string.url_imprint);
  435. if (imprintWeb != null && imprintWeb.length() > 0) {
  436. Uri uriUrl = Uri.parse(imprintWeb);
  437. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  438. startActivity(intent);
  439. }
  440. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  441. return true;
  442. }
  443. });
  444. } else {
  445. preferenceCategoryMore.removePreference(pImprint);
  446. }
  447. }
  448. mPrefStoragePath = (ListPreference) findPreference(PreferenceKeys.STORAGE_PATH);
  449. if (mPrefStoragePath != null) {
  450. StoragePoint[] storageOptions = DataStorageProvider.getInstance().getAvailableStoragePoints();
  451. String[] entries = new String[storageOptions.length];
  452. String[] values = new String[storageOptions.length];
  453. for (int i = 0; i < storageOptions.length; ++i) {
  454. entries[i] = storageOptions[i].getDescription();
  455. values[i] = storageOptions[i].getPath();
  456. }
  457. mPrefStoragePath.setEntries(entries);
  458. mPrefStoragePath.setEntryValues(values);
  459. mPrefStoragePath.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  460. @Override
  461. public boolean onPreferenceChange(Preference preference, Object newValue) {
  462. String newPath = (String) newValue;
  463. if (mStoragePath.equals(newPath)) {
  464. return true;
  465. }
  466. StorageMigration storageMigration = new StorageMigration(Preferences.this, mStoragePath, newPath);
  467. storageMigration.setStorageMigrationProgressListener(Preferences.this);
  468. storageMigration.migrate();
  469. return false;
  470. }
  471. });
  472. }
  473. mPrefInstantUploadCategory = (PreferenceCategory) findPreference("instant_uploading_category");
  474. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  475. // Instant upload via preferences on pre Android Marshmallow
  476. mPrefInstantUploadPath = findPreference("instant_upload_path");
  477. if (mPrefInstantUploadPath != null) {
  478. mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  479. @Override
  480. public boolean onPreferenceClick(Preference preference) {
  481. if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
  482. mUploadPath += OCFile.PATH_SEPARATOR;
  483. }
  484. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  485. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
  486. startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
  487. return true;
  488. }
  489. });
  490. }
  491. mPrefInstantUploadCategory = (PreferenceCategory) findPreference("instant_uploading_category");
  492. mPrefInstantUploadUseSubfolders = findPreference("instant_upload_path_use_subfolders");
  493. mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
  494. mPrefInstantPictureUploadOnlyOnCharging = findPreference("instant_upload_on_charging");
  495. mPrefInstantUpload = (CheckBoxPreferenceWithLongTitle) findPreference("instant_uploading");
  496. toggleInstantPictureOptions(mPrefInstantUpload.isChecked());
  497. mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  498. @Override
  499. public boolean onPreferenceChange(Preference preference, Object newValue) {
  500. toggleInstantPictureOptions((Boolean) newValue);
  501. toggleInstantUploadBehaviour(mPrefInstantVideoUpload.isChecked(), (Boolean) newValue);
  502. return true;
  503. }
  504. });
  505. mPrefInstantVideoUploadPath = findPreference(PreferenceKeys.INSTANT_VIDEO_UPLOAD_PATH);
  506. if (mPrefInstantVideoUploadPath != null) {
  507. mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  508. @Override
  509. public boolean onPreferenceClick(Preference preference) {
  510. if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
  511. mUploadVideoPath += OCFile.PATH_SEPARATOR;
  512. }
  513. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  514. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH,
  515. mUploadVideoPath);
  516. startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
  517. return true;
  518. }
  519. });
  520. }
  521. mPrefInstantVideoUploadUseSubfolders = findPreference("instant_video_upload_path_use_subfolders");
  522. mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
  523. mPrefInstantVideoUpload = (CheckBoxPreferenceWithLongTitle) findPreference("instant_video_uploading");
  524. mPrefInstantVideoUploadOnlyOnCharging = findPreference("instant_video_upload_on_charging");
  525. toggleInstantVideoOptions(mPrefInstantVideoUpload.isChecked());
  526. mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  527. @Override
  528. public boolean onPreferenceChange(Preference preference, Object newValue) {
  529. toggleInstantVideoOptions((Boolean) newValue);
  530. toggleInstantUploadBehaviour(
  531. (Boolean) newValue,
  532. mPrefInstantUpload.isChecked());
  533. return true;
  534. }
  535. });
  536. mPrefInstantUploadBehaviour = findPreference("prefs_instant_behaviour");
  537. toggleInstantUploadBehaviour(
  538. mPrefInstantVideoUpload.isChecked(),
  539. mPrefInstantUpload.isChecked());
  540. loadInstantUploadPath();
  541. loadInstantUploadVideoPath();
  542. } else {
  543. // Instant upload is handled via synced folders on Android Lollipop and up
  544. getPreferenceScreen().removePreference(mPrefInstantUploadCategory);
  545. }
  546. // About category
  547. PreferenceCategory preferenceCategoryAbout = (PreferenceCategory) findPreference("about");
  548. preferenceCategoryAbout.setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_about),
  549. accentColor));
  550. /* About App */
  551. pAboutApp = findPreference("about_app");
  552. if (pAboutApp != null) {
  553. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  554. pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
  555. }
  556. // privacy
  557. boolean privacyEnabled = getResources().getBoolean(R.bool.privacy_enabled);
  558. Preference privacyPreference = findPreference("privacy");
  559. if (privacyPreference != null) {
  560. if (privacyEnabled) {
  561. privacyPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  562. @Override
  563. public boolean onPreferenceClick(Preference preference) {
  564. String privacyUrl = getString(R.string.privacy_url);
  565. if (privacyUrl.length() > 0) {
  566. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  567. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE,
  568. getResources().getString(R.string.privacy));
  569. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, privacyUrl);
  570. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  571. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, -1);
  572. startActivity(externalWebViewIntent);
  573. }
  574. return true;
  575. }
  576. });
  577. } else {
  578. preferenceCategoryAbout.removePreference(privacyPreference);
  579. }
  580. }
  581. loadExternalSettingLinks(preferenceCategoryMore);
  582. loadStoragePath();
  583. }
  584. private void launchDavDroidLogin()
  585. throws com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
  586. OperationCanceledException,
  587. AuthenticatorException,
  588. IOException {
  589. Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  590. Intent davDroidLoginIntent = new Intent();
  591. davDroidLoginIntent.setClassName("at.bitfire.davdroid", "at.bitfire.davdroid.ui.setup.LoginActivity");
  592. if (getPackageManager().resolveActivity(davDroidLoginIntent, 0) != null) {
  593. // arguments
  594. if (mUri != null) {
  595. davDroidLoginIntent.putExtra("url", mUri.toString() + AccountUtils.DAV_PATH);
  596. }
  597. davDroidLoginIntent.putExtra("username", AccountUtils.getAccountUsername(account.name));
  598. //loginIntent.putExtra("password", "...");
  599. startActivityForResult(davDroidLoginIntent, ACTION_REQUEST_CODE_DAVDROID_SETUP);
  600. } else {
  601. // DAVdroid not installed
  602. Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=at.bitfire.davdroid"));
  603. // launch market(s)
  604. if (installIntent.resolveActivity(getPackageManager()) != null) {
  605. startActivity(installIntent);
  606. } else {
  607. // no f-droid market app or Play store installed --> launch browser for f-droid url
  608. Intent downloadIntent = new Intent(Intent.ACTION_VIEW,
  609. Uri.parse("https://f-droid.org/repository/browse/?fdid=at.bitfire.davdroid"));
  610. startActivity(downloadIntent);
  611. Toast.makeText(
  612. MainApp.getAppContext(),
  613. R.string.prefs_calendar_contacts_no_store_error,
  614. Toast.LENGTH_SHORT)
  615. .show();
  616. }
  617. }
  618. }
  619. private void setupBaseUri() {
  620. // retrieve and set user's base URI
  621. Thread t = new Thread(new Runnable() {
  622. public void run() {
  623. try {
  624. Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  625. OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
  626. mUri = OwnCloudClientManagerFactory.getDefaultSingleton().
  627. getClientFor(ocAccount, getApplicationContext()).getBaseUri();
  628. } catch (Throwable t) {
  629. Log_OC.e(TAG,"Error retrieving user's base URI", t);
  630. }
  631. }
  632. });
  633. t.start();
  634. }
  635. private void toggleInstantPictureOptions(Boolean value){
  636. if (value) {
  637. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
  638. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
  639. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadUseSubfolders);
  640. mPrefInstantUploadCategory.addPreference(mPrefInstantPictureUploadOnlyOnCharging);
  641. } else {
  642. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
  643. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
  644. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadUseSubfolders);
  645. mPrefInstantUploadCategory.removePreference(mPrefInstantPictureUploadOnlyOnCharging);
  646. }
  647. }
  648. private void toggleInstantVideoOptions(Boolean value){
  649. if (value) {
  650. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
  651. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
  652. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadUseSubfolders);
  653. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadOnlyOnCharging);
  654. } else {
  655. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
  656. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
  657. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadUseSubfolders);
  658. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadOnlyOnCharging);
  659. }
  660. }
  661. private void toggleInstantUploadBehaviour(Boolean video, Boolean picture){
  662. if (picture || video) {
  663. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadBehaviour);
  664. } else {
  665. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadBehaviour);
  666. }
  667. }
  668. @Override
  669. protected void onResume() {
  670. super.onResume();
  671. AnalyticsUtils.setCurrentScreenName(this, SCREEN_NAME, TAG);
  672. SharedPreferences appPrefs =
  673. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  674. boolean state = appPrefs.getBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
  675. pCode.setChecked(state);
  676. }
  677. @Override
  678. public boolean onCreateOptionsMenu(Menu menu) {
  679. super.onCreateOptionsMenu(menu);
  680. return true;
  681. }
  682. @Override
  683. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  684. super.onMenuItemSelected(featureId, item);
  685. Intent intent;
  686. switch (item.getItemId()) {
  687. case android.R.id.home:
  688. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  689. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  690. startActivity(intent);
  691. break;
  692. default:
  693. Log_OC.w(TAG, "Unknown menu item triggered");
  694. return false;
  695. }
  696. return true;
  697. }
  698. @Override
  699. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  700. super.onActivityResult(requestCode, resultCode, data);
  701. if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK) {
  702. OCFile folderToUpload = data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  703. mUploadPath = folderToUpload.getRemotePath();
  704. mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
  705. // Show the path on summary preference
  706. mPrefInstantUploadPath.setSummary(mUploadPath);
  707. saveInstantUploadPathOnPreferences();
  708. } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK) {
  709. OCFile folderToUploadVideo = data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  710. mUploadVideoPath = folderToUploadVideo.getRemotePath();
  711. mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
  712. // Show the video path on summary preference
  713. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  714. saveInstantUploadVideoPathOnPreferences();
  715. } else if (requestCode == ACTION_REQUEST_PASSCODE && resultCode == RESULT_OK) {
  716. String passcode = data.getStringExtra(PassCodeActivity.KEY_PASSCODE);
  717. if (passcode != null && passcode.length() == 4) {
  718. SharedPreferences.Editor appPrefs = PreferenceManager
  719. .getDefaultSharedPreferences(getApplicationContext()).edit();
  720. for (int i = 1; i <= 4; ++i) {
  721. appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i-1, i));
  722. }
  723. appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, true);
  724. appPrefs.apply();
  725. Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
  726. }
  727. } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
  728. if (data.getBooleanExtra(PassCodeActivity.KEY_CHECK_RESULT, false)) {
  729. SharedPreferences.Editor appPrefs = PreferenceManager
  730. .getDefaultSharedPreferences(getApplicationContext()).edit();
  731. appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
  732. appPrefs.apply();
  733. Toast.makeText(this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();
  734. }
  735. } else if (requestCode == ACTION_REQUEST_CODE_DAVDROID_SETUP && resultCode == RESULT_OK) {
  736. Toast.makeText(this, R.string.prefs_calendar_contacts_sync_setup_successful, Toast.LENGTH_LONG).show();
  737. }
  738. }
  739. public ActionBar getSupportActionBar() {
  740. return getDelegate().getSupportActionBar();
  741. }
  742. public void setSupportActionBar(@Nullable Toolbar toolbar) {
  743. getDelegate().setSupportActionBar(toolbar);
  744. }
  745. @Override
  746. public MenuInflater getMenuInflater() {
  747. return getDelegate().getMenuInflater();
  748. }
  749. @Override
  750. public void setContentView(@LayoutRes int layoutResID) {
  751. getDelegate().setContentView(layoutResID);
  752. }
  753. @Override
  754. public void setContentView(View view) {
  755. getDelegate().setContentView(view);
  756. }
  757. @Override
  758. public void setContentView(View view, ViewGroup.LayoutParams params) {
  759. getDelegate().setContentView(view, params);
  760. }
  761. @Override
  762. public void addContentView(View view, ViewGroup.LayoutParams params) {
  763. getDelegate().addContentView(view, params);
  764. }
  765. @Override
  766. protected void onPostResume() {
  767. super.onPostResume();
  768. getDelegate().onPostResume();
  769. }
  770. @Override
  771. protected void onTitleChanged(CharSequence title, int color) {
  772. super.onTitleChanged(title, color);
  773. getDelegate().setTitle(title);
  774. }
  775. @Override
  776. public void onConfigurationChanged(Configuration newConfig) {
  777. super.onConfigurationChanged(newConfig);
  778. getDelegate().onConfigurationChanged(newConfig);
  779. }
  780. @Override
  781. protected void onPostCreate(Bundle savedInstanceState) {
  782. super.onPostCreate(savedInstanceState);
  783. getDelegate().onPostCreate(savedInstanceState);
  784. }
  785. @Override
  786. protected void onDestroy() {
  787. super.onDestroy();
  788. getDelegate().onDestroy();
  789. }
  790. @Override
  791. protected void onStop() {
  792. super.onStop();
  793. getDelegate().onStop();
  794. }
  795. public void invalidateOptionsMenu() {
  796. getDelegate().invalidateOptionsMenu();
  797. }
  798. private AppCompatDelegate getDelegate() {
  799. if (mDelegate == null) {
  800. mDelegate = AppCompatDelegate.create(this, null);
  801. }
  802. return mDelegate;
  803. }
  804. private void loadExternalSettingLinks(PreferenceCategory preferenceCategory) {
  805. if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
  806. ExternalLinksProvider externalLinksProvider = new ExternalLinksProvider(getContentResolver());
  807. for (final ExternalLink link : externalLinksProvider.getExternalLink(ExternalLinkType.SETTINGS)) {
  808. // only add if it does not exist, in case activity is re-used
  809. if (findPreference(link.id.toString()) == null) {
  810. Preference p = new Preference(this);
  811. p.setTitle(link.name);
  812. p.setKey(link.id.toString());
  813. p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  814. @Override
  815. public boolean onPreferenceClick(Preference preference) {
  816. Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class);
  817. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, link.name);
  818. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, link.url);
  819. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  820. externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, link.id);
  821. startActivity(externalWebViewIntent);
  822. return true;
  823. }
  824. });
  825. preferenceCategory.addPreference(p);
  826. }
  827. }
  828. }
  829. }
  830. /**
  831. * Load upload path set on preferences
  832. */
  833. private void loadInstantUploadPath() {
  834. SharedPreferences appPrefs =
  835. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  836. mUploadPath = appPrefs.getString(PreferenceKeys.INSTANT_UPLOAD_PATH, getString(R.string.instant_upload_path));
  837. mPrefInstantUploadPath.setSummary(mUploadPath);
  838. }
  839. /**
  840. * Save storage path
  841. */
  842. private void saveStoragePath(String newStoragePath) {
  843. SharedPreferences appPrefs =
  844. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  845. mStoragePath = newStoragePath;
  846. MainApp.setStoragePath(mStoragePath);
  847. SharedPreferences.Editor editor = appPrefs.edit();
  848. editor.putString(PreferenceKeys.STORAGE_PATH, mStoragePath);
  849. editor.apply();
  850. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(mStoragePath);
  851. mPrefStoragePath.setSummary(storageDescription);
  852. mPrefStoragePath.setValue(newStoragePath);
  853. }
  854. /**
  855. * Load storage path set on preferences
  856. */
  857. private void loadStoragePath() {
  858. SharedPreferences appPrefs =
  859. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  860. mStoragePath = appPrefs.getString(PreferenceKeys.STORAGE_PATH, Environment.getExternalStorageDirectory()
  861. .getAbsolutePath());
  862. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(mStoragePath);
  863. mPrefStoragePath.setSummary(storageDescription);
  864. }
  865. /**
  866. * Save the "Instant Upload Path" on preferences
  867. */
  868. private void saveInstantUploadPathOnPreferences() {
  869. SharedPreferences appPrefs =
  870. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  871. SharedPreferences.Editor editor = appPrefs.edit();
  872. editor.putString(PreferenceKeys.INSTANT_UPLOAD_PATH, mUploadPath);
  873. editor.apply();
  874. }
  875. /**
  876. * Load upload video path set on preferences
  877. */
  878. private void loadInstantUploadVideoPath() {
  879. SharedPreferences appPrefs =
  880. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  881. mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
  882. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  883. }
  884. /**
  885. * Save the "Instant Video Upload Path" on preferences
  886. */
  887. private void saveInstantUploadVideoPathOnPreferences() {
  888. SharedPreferences appPrefs =
  889. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  890. SharedPreferences.Editor editor = appPrefs.edit();
  891. editor.putString(PreferenceKeys.INSTANT_VIDEO_UPLOAD_PATH, mUploadVideoPath);
  892. editor.apply();
  893. }
  894. @Override
  895. public void onStorageMigrationFinished(String storagePath, boolean succeed) {
  896. if (succeed) {
  897. saveStoragePath(storagePath);
  898. }
  899. }
  900. @Override
  901. public void onCancelMigration() {
  902. // Migration was canceled so we don't do anything
  903. }
  904. }