Preferences.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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.net.Uri;
  33. import android.os.Build;
  34. import android.os.Bundle;
  35. import android.os.Environment;
  36. import android.preference.CheckBoxPreference;
  37. import android.preference.ListPreference;
  38. import android.preference.Preference;
  39. import android.preference.Preference.OnPreferenceChangeListener;
  40. import android.preference.Preference.OnPreferenceClickListener;
  41. import android.preference.PreferenceActivity;
  42. import android.preference.PreferenceCategory;
  43. import android.preference.PreferenceManager;
  44. import android.support.annotation.LayoutRes;
  45. import android.support.annotation.Nullable;
  46. import android.support.v7.app.ActionBar;
  47. import android.support.v7.app.AppCompatDelegate;
  48. import android.support.v7.widget.Toolbar;
  49. import android.view.Menu;
  50. import android.view.MenuInflater;
  51. import android.view.MenuItem;
  52. import android.view.View;
  53. import android.view.ViewGroup;
  54. import android.widget.Toast;
  55. import com.owncloud.android.BuildConfig;
  56. import com.owncloud.android.MainApp;
  57. import com.owncloud.android.R;
  58. import com.owncloud.android.authentication.AccountUtils;
  59. import com.owncloud.android.datamodel.OCFile;
  60. import com.owncloud.android.datastorage.DataStorageProvider;
  61. import com.owncloud.android.datastorage.StoragePoint;
  62. import com.owncloud.android.lib.common.OwnCloudAccount;
  63. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  64. import com.owncloud.android.lib.common.utils.Log_OC;
  65. import com.owncloud.android.utils.DisplayUtils;
  66. import java.io.IOException;
  67. /**
  68. * An Activity that allows the user to change the application's settings.
  69. *
  70. * It proxies the necessary calls via {@link android.support.v7.app.AppCompatDelegate} to be used with AppCompat.
  71. */
  72. public class Preferences extends PreferenceActivity
  73. implements StorageMigration.StorageMigrationProgressListener {
  74. private static final String TAG = Preferences.class.getSimpleName();
  75. private static final String SCREEN_NAME = "Settings";
  76. private static final int ACTION_SELECT_UPLOAD_PATH = 1;
  77. private static final int ACTION_SELECT_UPLOAD_VIDEO_PATH = 2;
  78. private static final int ACTION_REQUEST_PASSCODE = 5;
  79. private static final int ACTION_CONFIRM_PASSCODE = 6;
  80. private static final int ACTION_REQUEST_CODE_DAVDROID_SETUP = 10;
  81. /**
  82. * The user's server base uri.
  83. */
  84. private Uri mUri;
  85. private CheckBoxPreference pCode;
  86. private CheckBoxPreference mShowHiddenFiles;
  87. private Preference pAboutApp;
  88. private AppCompatDelegate mDelegate;
  89. private String mUploadPath;
  90. private PreferenceCategory mPrefInstantUploadCategory;
  91. private Preference mPrefInstantUpload;
  92. private Preference mPrefInstantUploadBehaviour;
  93. private Preference mPrefInstantUploadPath;
  94. private Preference mPrefInstantUploadUseSubfolders;
  95. private Preference mPrefInstantPictureUploadOnlyOnCharging;
  96. private Preference mPrefInstantUploadPathWiFi;
  97. private Preference mPrefInstantVideoUpload;
  98. private Preference mPrefInstantVideoUploadPath;
  99. private Preference mPrefInstantVideoUploadUseSubfolders;
  100. private Preference mPrefInstantVideoUploadPathWiFi;
  101. private Preference mPrefInstantVideoUploadOnlyOnCharging;
  102. private String mUploadVideoPath;
  103. private ListPreference mPrefStoragePath;
  104. private String mStoragePath;
  105. public static class PreferenceKeys {
  106. public static final String STORAGE_PATH = "storage_path";
  107. public static final String INSTANT_UPLOAD_PATH = "instant_upload_path";
  108. public static final String INSTANT_VIDEO_UPLOAD_PATH = "instant_video_upload_path";
  109. }
  110. @SuppressWarnings("deprecation")
  111. @Override
  112. public void onCreate(Bundle savedInstanceState) {
  113. getDelegate().installViewFactory();
  114. getDelegate().onCreate(savedInstanceState);
  115. super.onCreate(savedInstanceState);
  116. addPreferencesFromResource(R.xml.preferences);
  117. ActionBar actionBar = getSupportActionBar();
  118. actionBar.setDisplayHomeAsUpEnabled(true);
  119. actionBar.setTitle(R.string.actionbar_settings);
  120. // retrieve user's base uri
  121. setupBaseUri();
  122. // For adding content description tag to a title field in the action bar
  123. int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
  124. View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
  125. if (actionBarTitleView != null) { // it's null in Android 2.x
  126. getWindow().getDecorView().findViewById(actionBarTitleId).
  127. setContentDescription(getString(R.string.actionbar_settings));
  128. }
  129. // Load package info
  130. String temp;
  131. try {
  132. PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0);
  133. temp = pkg.versionName;
  134. } catch (NameNotFoundException e) {
  135. temp = "";
  136. Log_OC.e(TAG, "Error while showing about dialog", e);
  137. }
  138. final String appVersion = temp;
  139. // Register context menu for list of preferences.
  140. registerForContextMenu(getListView());
  141. pCode = (CheckBoxPreference) findPreference(PassCodeActivity.PREFERENCE_SET_PASSCODE);
  142. if (pCode != null) {
  143. pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  144. @Override
  145. public boolean onPreferenceChange(Preference preference, Object newValue) {
  146. Intent i = new Intent(getApplicationContext(), PassCodeActivity.class);
  147. Boolean incoming = (Boolean) newValue;
  148. i.setAction(
  149. incoming ? PassCodeActivity.ACTION_REQUEST_WITH_RESULT :
  150. PassCodeActivity.ACTION_CHECK_WITH_RESULT
  151. );
  152. startActivityForResult(i, incoming ? ACTION_REQUEST_PASSCODE :
  153. ACTION_CONFIRM_PASSCODE);
  154. // Don't update just yet, we will decide on it in onActivityResult
  155. return false;
  156. }
  157. });
  158. }
  159. mShowHiddenFiles = (CheckBoxPreference) findPreference("show_hidden_files");
  160. mShowHiddenFiles.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  161. @Override
  162. public boolean onPreferenceClick(Preference preference) {
  163. SharedPreferences appPrefs =
  164. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  165. SharedPreferences.Editor editor = appPrefs.edit();
  166. editor.putBoolean("show_hidden_files_pref", mShowHiddenFiles.isChecked());
  167. editor.commit();
  168. return true;
  169. }
  170. });
  171. PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
  172. boolean calendarContactsEnabled = getResources().getBoolean(R.bool.calendar_contacts_enabled);
  173. Preference pCalendarContacts = findPreference("calendar_contacts");
  174. if (pCalendarContacts != null) {
  175. if (calendarContactsEnabled) {
  176. pCalendarContacts.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  177. @Override
  178. public boolean onPreferenceClick(Preference preference) {
  179. try {
  180. launchDavDroidLogin();
  181. } catch (Throwable t) {
  182. Log_OC.e(TAG, "Base Uri for account could not be resolved to call DAVdroid!", t);
  183. Toast.makeText(
  184. MainApp.getAppContext(),
  185. R.string.prefs_calendar_contacts_address_resolve_error,
  186. Toast.LENGTH_SHORT)
  187. .show();
  188. }
  189. return true;
  190. }
  191. });
  192. } else {
  193. preferenceCategory.removePreference(pCalendarContacts);
  194. }
  195. }
  196. boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
  197. Preference pHelp = findPreference("help");
  198. if (pHelp != null) {
  199. if (helpEnabled) {
  200. pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  201. @Override
  202. public boolean onPreferenceClick(Preference preference) {
  203. String helpWeb = getString(R.string.url_help);
  204. if (helpWeb != null && helpWeb.length() > 0) {
  205. Uri uriUrl = Uri.parse(helpWeb);
  206. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  207. startActivity(intent);
  208. }
  209. return true;
  210. }
  211. });
  212. } else {
  213. preferenceCategory.removePreference(pHelp);
  214. }
  215. }
  216. boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled);
  217. Preference pRecommend = findPreference("recommend");
  218. if (pRecommend != null) {
  219. if (recommendEnabled) {
  220. pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  221. @Override
  222. public boolean onPreferenceClick(Preference preference) {
  223. Intent intent = new Intent(Intent.ACTION_SENDTO);
  224. intent.setType("text/plain");
  225. intent.setData(Uri.parse(getString(R.string.mail_recommend)));
  226. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  227. String appName = getString(R.string.app_name);
  228. String downloadUrl = getString(R.string.url_app_download);
  229. String recommendSubject = String.format(getString(R.string.recommend_subject), appName);
  230. String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl);
  231. intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject);
  232. intent.putExtra(Intent.EXTRA_TEXT, recommendText);
  233. startActivity(intent);
  234. return true;
  235. }
  236. });
  237. } else {
  238. preferenceCategory.removePreference(pRecommend);
  239. }
  240. }
  241. boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled);
  242. Preference pFeedback = findPreference("feedback");
  243. if (pFeedback != null) {
  244. if (feedbackEnabled) {
  245. pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  246. @Override
  247. public boolean onPreferenceClick(Preference preference) {
  248. String feedbackMail = getString(R.string.mail_feedback);
  249. String feedback = getText(R.string.prefs_feedback) + " - android v" + appVersion;
  250. Intent intent = new Intent(Intent.ACTION_SENDTO);
  251. intent.setType("text/plain");
  252. intent.putExtra(Intent.EXTRA_SUBJECT, feedback);
  253. intent.setData(Uri.parse(feedbackMail));
  254. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  255. startActivity(intent);
  256. return true;
  257. }
  258. });
  259. } else {
  260. preferenceCategory.removePreference(pFeedback);
  261. }
  262. }
  263. boolean loggerEnabled = getResources().getBoolean(R.bool.logger_enabled) || BuildConfig.DEBUG;
  264. Preference pLogger = findPreference("logger");
  265. if (pLogger != null) {
  266. if (loggerEnabled) {
  267. pLogger.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  268. @Override
  269. public boolean onPreferenceClick(Preference preference) {
  270. Intent loggerIntent = new Intent(getApplicationContext(), LogHistoryActivity.class);
  271. startActivity(loggerIntent);
  272. return true;
  273. }
  274. });
  275. } else {
  276. preferenceCategory.removePreference(pLogger);
  277. }
  278. }
  279. boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
  280. Preference pImprint = findPreference("imprint");
  281. if (pImprint != null) {
  282. if (imprintEnabled) {
  283. pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  284. @Override
  285. public boolean onPreferenceClick(Preference preference) {
  286. String imprintWeb = getString(R.string.url_imprint);
  287. if (imprintWeb != null && imprintWeb.length() > 0) {
  288. Uri uriUrl = Uri.parse(imprintWeb);
  289. Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
  290. startActivity(intent);
  291. }
  292. //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG");
  293. return true;
  294. }
  295. });
  296. } else {
  297. preferenceCategory.removePreference(pImprint);
  298. }
  299. }
  300. mPrefStoragePath = (ListPreference) findPreference(PreferenceKeys.STORAGE_PATH);
  301. if (mPrefStoragePath != null) {
  302. StoragePoint[] storageOptions = DataStorageProvider.getInstance().getAvailableStoragePoints();
  303. String[] entries = new String[storageOptions.length];
  304. String[] values = new String[storageOptions.length];
  305. for (int i = 0; i < storageOptions.length; ++i) {
  306. entries[i] = storageOptions[i].getDescription();
  307. values[i] = storageOptions[i].getPath();
  308. }
  309. mPrefStoragePath.setEntries(entries);
  310. mPrefStoragePath.setEntryValues(values);
  311. mPrefStoragePath.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  312. @Override
  313. public boolean onPreferenceChange(Preference preference, Object newValue) {
  314. String newPath = (String) newValue;
  315. if (mStoragePath.equals(newPath)) {
  316. return true;
  317. }
  318. StorageMigration storageMigration = new StorageMigration(Preferences.this, mStoragePath, newPath);
  319. storageMigration.setStorageMigrationProgressListener(Preferences.this);
  320. storageMigration.migrate();
  321. return false;
  322. }
  323. });
  324. }
  325. mPrefInstantUploadCategory = (PreferenceCategory) findPreference("instant_uploading_category");
  326. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  327. // Instant upload via preferences on pre Android Marshmallow
  328. mPrefInstantUploadPath = findPreference("instant_upload_path");
  329. if (mPrefInstantUploadPath != null) {
  330. mPrefInstantUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  331. @Override
  332. public boolean onPreferenceClick(Preference preference) {
  333. if (!mUploadPath.endsWith(OCFile.PATH_SEPARATOR)) {
  334. mUploadPath += OCFile.PATH_SEPARATOR;
  335. }
  336. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  337. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH, mUploadPath);
  338. startActivityForResult(intent, ACTION_SELECT_UPLOAD_PATH);
  339. return true;
  340. }
  341. });
  342. }
  343. mPrefInstantUploadCategory = (PreferenceCategory) findPreference("instant_uploading_category");
  344. mPrefInstantUploadUseSubfolders = findPreference("instant_upload_path_use_subfolders");
  345. mPrefInstantUploadPathWiFi = findPreference("instant_upload_on_wifi");
  346. mPrefInstantPictureUploadOnlyOnCharging = findPreference("instant_upload_on_charging");
  347. mPrefInstantUpload = findPreference("instant_uploading");
  348. toggleInstantPictureOptions(((CheckBoxPreference) mPrefInstantUpload).isChecked());
  349. mPrefInstantUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  350. @Override
  351. public boolean onPreferenceChange(Preference preference, Object newValue) {
  352. toggleInstantPictureOptions((Boolean) newValue);
  353. toggleInstantUploadBehaviour(
  354. ((CheckBoxPreference) mPrefInstantVideoUpload).isChecked(),
  355. (Boolean) newValue);
  356. return true;
  357. }
  358. });
  359. mPrefInstantVideoUploadPath = findPreference(PreferenceKeys.INSTANT_VIDEO_UPLOAD_PATH);
  360. if (mPrefInstantVideoUploadPath != null){
  361. mPrefInstantVideoUploadPath.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  362. @Override
  363. public boolean onPreferenceClick(Preference preference) {
  364. if (!mUploadVideoPath.endsWith(OCFile.PATH_SEPARATOR)) {
  365. mUploadVideoPath += OCFile.PATH_SEPARATOR;
  366. }
  367. Intent intent = new Intent(Preferences.this, UploadPathActivity.class);
  368. intent.putExtra(UploadPathActivity.KEY_INSTANT_UPLOAD_PATH,
  369. mUploadVideoPath);
  370. startActivityForResult(intent, ACTION_SELECT_UPLOAD_VIDEO_PATH);
  371. return true;
  372. }
  373. });
  374. }
  375. mPrefInstantVideoUploadUseSubfolders = findPreference("instant_video_upload_path_use_subfolders");
  376. mPrefInstantVideoUploadPathWiFi = findPreference("instant_video_upload_on_wifi");
  377. mPrefInstantVideoUpload = findPreference("instant_video_uploading");
  378. mPrefInstantVideoUploadOnlyOnCharging = findPreference("instant_video_upload_on_charging");
  379. toggleInstantVideoOptions(((CheckBoxPreference) mPrefInstantVideoUpload).isChecked());
  380. mPrefInstantVideoUpload.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  381. @Override
  382. public boolean onPreferenceChange(Preference preference, Object newValue) {
  383. toggleInstantVideoOptions((Boolean) newValue);
  384. toggleInstantUploadBehaviour(
  385. (Boolean) newValue,
  386. ((CheckBoxPreference) mPrefInstantUpload).isChecked());
  387. return true;
  388. }
  389. });
  390. mPrefInstantUploadBehaviour = findPreference("prefs_instant_behaviour");
  391. toggleInstantUploadBehaviour(
  392. ((CheckBoxPreference) mPrefInstantVideoUpload).isChecked(),
  393. ((CheckBoxPreference) mPrefInstantUpload).isChecked());
  394. loadInstantUploadPath();
  395. loadInstantUploadVideoPath();
  396. } else {
  397. // Instant upload is handled via synced folders on Android Lollipop and up
  398. getPreferenceScreen().removePreference(mPrefInstantUploadCategory);
  399. }
  400. /* About App */
  401. pAboutApp = findPreference("about_app");
  402. if (pAboutApp != null) {
  403. pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name)));
  404. pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion));
  405. }
  406. loadStoragePath();
  407. }
  408. private void launchDavDroidLogin()
  409. throws com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
  410. OperationCanceledException,
  411. AuthenticatorException,
  412. IOException {
  413. Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  414. Intent davDroidLoginIntent = new Intent();
  415. davDroidLoginIntent.setClassName("at.bitfire.davdroid", "at.bitfire.davdroid.ui.setup.LoginActivity");
  416. if (getPackageManager().resolveActivity(davDroidLoginIntent, 0) != null) {
  417. // arguments
  418. if (mUri != null) {
  419. davDroidLoginIntent.putExtra("url", mUri.toString() + AccountUtils.DAV_PATH);
  420. }
  421. davDroidLoginIntent.putExtra("username", AccountUtils.getAccountUsername(account.name));
  422. //loginIntent.putExtra("password", "...");
  423. startActivityForResult(davDroidLoginIntent, ACTION_REQUEST_CODE_DAVDROID_SETUP);
  424. } else {
  425. // DAVdroid not installed
  426. Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=at.bitfire.davdroid"));
  427. // launch market(s)
  428. if (installIntent.resolveActivity(getPackageManager()) != null) {
  429. startActivity(installIntent);
  430. } else {
  431. // no f-droid market app or Play store installed --> launch browser for f-droid url
  432. Intent downloadIntent = new Intent(Intent.ACTION_VIEW,
  433. Uri.parse("https://f-droid.org/repository/browse/?fdid=at.bitfire.davdroid"));
  434. startActivity(downloadIntent);
  435. Toast.makeText(
  436. MainApp.getAppContext(),
  437. R.string.prefs_calendar_contacts_no_store_error,
  438. Toast.LENGTH_SHORT)
  439. .show();
  440. }
  441. }
  442. }
  443. private void setupBaseUri() {
  444. // retrieve and set user's base URI
  445. Thread t = new Thread(new Runnable() {
  446. public void run() {
  447. try {
  448. Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
  449. OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
  450. mUri = OwnCloudClientManagerFactory.getDefaultSingleton().
  451. getClientFor(ocAccount, getApplicationContext()).getBaseUri();
  452. } catch (Throwable t) {
  453. Log_OC.e(TAG,"Error retrieving user's base URI", t);
  454. }
  455. }
  456. });
  457. t.start();
  458. }
  459. private void toggleInstantPictureOptions(Boolean value){
  460. if (value) {
  461. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPathWiFi);
  462. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadPath);
  463. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadUseSubfolders);
  464. mPrefInstantUploadCategory.addPreference(mPrefInstantPictureUploadOnlyOnCharging);
  465. } else {
  466. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPathWiFi);
  467. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadPath);
  468. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadUseSubfolders);
  469. mPrefInstantUploadCategory.removePreference(mPrefInstantPictureUploadOnlyOnCharging);
  470. }
  471. }
  472. private void toggleInstantVideoOptions(Boolean value){
  473. if (value) {
  474. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPathWiFi);
  475. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadPath);
  476. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadUseSubfolders);
  477. mPrefInstantUploadCategory.addPreference(mPrefInstantVideoUploadOnlyOnCharging);
  478. } else {
  479. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPathWiFi);
  480. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadPath);
  481. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadUseSubfolders);
  482. mPrefInstantUploadCategory.removePreference(mPrefInstantVideoUploadOnlyOnCharging);
  483. }
  484. }
  485. private void toggleInstantUploadBehaviour(Boolean video, Boolean picture){
  486. if (picture || video) {
  487. mPrefInstantUploadCategory.addPreference(mPrefInstantUploadBehaviour);
  488. } else {
  489. mPrefInstantUploadCategory.removePreference(mPrefInstantUploadBehaviour);
  490. }
  491. }
  492. @Override
  493. protected void onResume() {
  494. super.onResume();
  495. MainApp.getFirebaseAnalyticsInstance().setCurrentScreen(this, SCREEN_NAME, TAG);
  496. SharedPreferences appPrefs =
  497. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  498. boolean state = appPrefs.getBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
  499. pCode.setChecked(state);
  500. }
  501. @Override
  502. public boolean onCreateOptionsMenu(Menu menu) {
  503. super.onCreateOptionsMenu(menu);
  504. return true;
  505. }
  506. @Override
  507. public boolean onMenuItemSelected(int featureId, MenuItem item) {
  508. super.onMenuItemSelected(featureId, item);
  509. Intent intent;
  510. switch (item.getItemId()) {
  511. case android.R.id.home:
  512. intent = new Intent(getBaseContext(), FileDisplayActivity.class);
  513. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  514. startActivity(intent);
  515. break;
  516. default:
  517. Log_OC.w(TAG, "Unknown menu item triggered");
  518. return false;
  519. }
  520. return true;
  521. }
  522. @Override
  523. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  524. super.onActivityResult(requestCode, resultCode, data);
  525. if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK) {
  526. OCFile folderToUpload = data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  527. mUploadPath = folderToUpload.getRemotePath();
  528. mUploadPath = DisplayUtils.getPathWithoutLastSlash(mUploadPath);
  529. // Show the path on summary preference
  530. mPrefInstantUploadPath.setSummary(mUploadPath);
  531. saveInstantUploadPathOnPreferences();
  532. } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK) {
  533. OCFile folderToUploadVideo = data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
  534. mUploadVideoPath = folderToUploadVideo.getRemotePath();
  535. mUploadVideoPath = DisplayUtils.getPathWithoutLastSlash(mUploadVideoPath);
  536. // Show the video path on summary preference
  537. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  538. saveInstantUploadVideoPathOnPreferences();
  539. } else if (requestCode == ACTION_REQUEST_PASSCODE && resultCode == RESULT_OK) {
  540. String passcode = data.getStringExtra(PassCodeActivity.KEY_PASSCODE);
  541. if (passcode != null && passcode.length() == 4) {
  542. SharedPreferences.Editor appPrefs = PreferenceManager
  543. .getDefaultSharedPreferences(getApplicationContext()).edit();
  544. for (int i = 1; i <= 4; ++i) {
  545. appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i-1, i));
  546. }
  547. appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, true);
  548. appPrefs.apply();
  549. Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
  550. }
  551. } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
  552. if (data.getBooleanExtra(PassCodeActivity.KEY_CHECK_RESULT, false)) {
  553. SharedPreferences.Editor appPrefs = PreferenceManager
  554. .getDefaultSharedPreferences(getApplicationContext()).edit();
  555. appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
  556. appPrefs.apply();
  557. Toast.makeText(this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();
  558. }
  559. } else if (requestCode == ACTION_REQUEST_CODE_DAVDROID_SETUP && resultCode == RESULT_OK) {
  560. Toast.makeText(this, R.string.prefs_calendar_contacts_sync_setup_successful, Toast.LENGTH_LONG).show();
  561. }
  562. }
  563. public ActionBar getSupportActionBar() {
  564. return getDelegate().getSupportActionBar();
  565. }
  566. public void setSupportActionBar(@Nullable Toolbar toolbar) {
  567. getDelegate().setSupportActionBar(toolbar);
  568. }
  569. @Override
  570. public MenuInflater getMenuInflater() {
  571. return getDelegate().getMenuInflater();
  572. }
  573. @Override
  574. public void setContentView(@LayoutRes int layoutResID) {
  575. getDelegate().setContentView(layoutResID);
  576. }
  577. @Override
  578. public void setContentView(View view) {
  579. getDelegate().setContentView(view);
  580. }
  581. @Override
  582. public void setContentView(View view, ViewGroup.LayoutParams params) {
  583. getDelegate().setContentView(view, params);
  584. }
  585. @Override
  586. public void addContentView(View view, ViewGroup.LayoutParams params) {
  587. getDelegate().addContentView(view, params);
  588. }
  589. @Override
  590. protected void onPostResume() {
  591. super.onPostResume();
  592. getDelegate().onPostResume();
  593. }
  594. @Override
  595. protected void onTitleChanged(CharSequence title, int color) {
  596. super.onTitleChanged(title, color);
  597. getDelegate().setTitle(title);
  598. }
  599. @Override
  600. public void onConfigurationChanged(Configuration newConfig) {
  601. super.onConfigurationChanged(newConfig);
  602. getDelegate().onConfigurationChanged(newConfig);
  603. }
  604. @Override
  605. protected void onPostCreate(Bundle savedInstanceState) {
  606. super.onPostCreate(savedInstanceState);
  607. getDelegate().onPostCreate(savedInstanceState);
  608. }
  609. @Override
  610. protected void onDestroy() {
  611. super.onDestroy();
  612. getDelegate().onDestroy();
  613. }
  614. @Override
  615. protected void onStop() {
  616. super.onStop();
  617. getDelegate().onStop();
  618. }
  619. public void invalidateOptionsMenu() {
  620. getDelegate().invalidateOptionsMenu();
  621. }
  622. private AppCompatDelegate getDelegate() {
  623. if (mDelegate == null) {
  624. mDelegate = AppCompatDelegate.create(this, null);
  625. }
  626. return mDelegate;
  627. }
  628. /**
  629. * Load upload path set on preferences
  630. */
  631. private void loadInstantUploadPath() {
  632. SharedPreferences appPrefs =
  633. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  634. mUploadPath = appPrefs.getString(PreferenceKeys.INSTANT_UPLOAD_PATH, getString(R.string.instant_upload_path));
  635. mPrefInstantUploadPath.setSummary(mUploadPath);
  636. }
  637. /**
  638. * Save storage path
  639. */
  640. private void saveStoragePath(String newStoragePath) {
  641. SharedPreferences appPrefs =
  642. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  643. mStoragePath = newStoragePath;
  644. MainApp.setStoragePath(mStoragePath);
  645. SharedPreferences.Editor editor = appPrefs.edit();
  646. editor.putString(PreferenceKeys.STORAGE_PATH, mStoragePath);
  647. editor.commit();
  648. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(mStoragePath);
  649. mPrefStoragePath.setSummary(storageDescription);
  650. mPrefStoragePath.setValue(newStoragePath);
  651. }
  652. /**
  653. * Load storage path set on preferences
  654. */
  655. private void loadStoragePath() {
  656. SharedPreferences appPrefs =
  657. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  658. mStoragePath = appPrefs.getString(PreferenceKeys.STORAGE_PATH, Environment.getExternalStorageDirectory()
  659. .getAbsolutePath());
  660. String storageDescription = DataStorageProvider.getInstance().getStorageDescriptionByPath(mStoragePath);
  661. mPrefStoragePath.setSummary(storageDescription);
  662. }
  663. /**
  664. * Save the "Instant Upload Path" on preferences
  665. */
  666. private void saveInstantUploadPathOnPreferences() {
  667. SharedPreferences appPrefs =
  668. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  669. SharedPreferences.Editor editor = appPrefs.edit();
  670. editor.putString(PreferenceKeys.INSTANT_UPLOAD_PATH, mUploadPath);
  671. editor.commit();
  672. }
  673. /**
  674. * Load upload video path set on preferences
  675. */
  676. private void loadInstantUploadVideoPath() {
  677. SharedPreferences appPrefs =
  678. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  679. mUploadVideoPath = appPrefs.getString("instant_video_upload_path", getString(R.string.instant_upload_path));
  680. mPrefInstantVideoUploadPath.setSummary(mUploadVideoPath);
  681. }
  682. /**
  683. * Save the "Instant Video Upload Path" on preferences
  684. */
  685. private void saveInstantUploadVideoPathOnPreferences() {
  686. SharedPreferences appPrefs =
  687. PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  688. SharedPreferences.Editor editor = appPrefs.edit();
  689. editor.putString(PreferenceKeys.INSTANT_VIDEO_UPLOAD_PATH, mUploadVideoPath);
  690. editor.commit();
  691. }
  692. @Override
  693. public void onStorageMigrationFinished(String storagePath, boolean succeed) {
  694. if (succeed) {
  695. saveStoragePath(storagePath);
  696. }
  697. }
  698. @Override
  699. public void onCancelMigration() {
  700. // Migration was canceled so we don't do anything
  701. }
  702. }