Preferences.java 34 KB

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