SyncFileNotEnoughSpaceDialogFragment.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Kilian Périsset
  5. * Copyright (C) 2020 Infomaniak Network SA
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License (GPLv3),
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.ui.dialog;
  20. import android.app.Dialog;
  21. import android.content.Intent;
  22. import android.os.Build;
  23. import android.os.Bundle;
  24. import android.os.storage.StorageManager;
  25. import com.owncloud.android.R;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  28. import com.owncloud.android.ui.fragment.OCFileListFragment;
  29. import com.owncloud.android.utils.DisplayUtils;
  30. import com.owncloud.android.utils.ThemeUtils;
  31. import androidx.annotation.NonNull;
  32. import androidx.annotation.RequiresApi;
  33. import androidx.appcompat.app.AlertDialog;
  34. /**
  35. * Dialog requiring confirmation when a file/folder is too "big" to be synchronized/downloaded on device.
  36. */
  37. public class SyncFileNotEnoughSpaceDialogFragment extends ConfirmationDialogFragment implements
  38. ConfirmationDialogFragmentListener {
  39. private static final String ARG_PASSED_FILE = "fragment_parent_caller";
  40. private static final int REQUEST_CODE_STORAGE = 20;
  41. private OCFile targetFile;
  42. public static SyncFileNotEnoughSpaceDialogFragment newInstance(OCFile file, long availableDeviceSpace) {
  43. Bundle args = new Bundle();
  44. SyncFileNotEnoughSpaceDialogFragment frag = new SyncFileNotEnoughSpaceDialogFragment();
  45. String properFileSize = DisplayUtils.bytesToHumanReadable(file.getFileLength());
  46. String properDiskAvailableSpace = DisplayUtils.bytesToHumanReadable(availableDeviceSpace);
  47. // Defining title, message and resources
  48. args.putInt(ARG_TITLE_ID, R.string.sync_not_enough_space_dialog_title);
  49. args.putInt(ARG_MESSAGE_RESOURCE_ID, R.string.sync_not_enough_space_dialog_placeholder);
  50. args.putStringArray(ARG_MESSAGE_ARGUMENTS, new String[]{file.getFileName(), properFileSize, properDiskAvailableSpace});
  51. args.putParcelable(ARG_PASSED_FILE, file);
  52. // Defining buttons
  53. if (file.isFolder()) {
  54. args.putInt(ARG_POSITIVE_BTN_RES, R.string.sync_not_enough_space_dialog_action_choose);
  55. }
  56. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
  57. args.putInt(ARG_NEGATIVE_BTN_RES, R.string.sync_not_enough_space_dialog_action_free_space);
  58. }
  59. args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_cancel);
  60. frag.setArguments(args);
  61. return frag;
  62. }
  63. @Override
  64. public void onStart() {
  65. super.onStart();
  66. int color = ThemeUtils.primaryAccentColor(getActivity());
  67. AlertDialog alertDialog = (AlertDialog) getDialog();
  68. if (alertDialog != null) {
  69. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  70. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(color);
  71. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  72. }
  73. }
  74. @NonNull
  75. @Override
  76. public Dialog onCreateDialog(Bundle savedInstanceState) {
  77. Dialog dialog = super.onCreateDialog(savedInstanceState);
  78. Bundle arguments = getArguments();
  79. if (arguments == null) {
  80. throw new IllegalArgumentException("Arguments may not be null");
  81. }
  82. targetFile = arguments.getParcelable(ARG_PASSED_FILE);
  83. setOnConfirmationListener(this);
  84. return dialog;
  85. }
  86. /**
  87. * (Only if file is a folder), will access the destination folder to allow user to choose what to synchronize
  88. */
  89. @Override
  90. public void onConfirmation(String callerTag) {
  91. OCFileListFragment frag = (OCFileListFragment) getTargetFragment();
  92. if (frag != null && targetFile != null) {
  93. frag.onItemClicked(targetFile);
  94. }
  95. }
  96. /**
  97. * Will abort/cancel the process (is neutral to "hack" android button position ._.)
  98. */
  99. @Override
  100. public void onNeutral(String callerTag) {
  101. // Nothing
  102. }
  103. /**
  104. * Will access to storage manager in order to empty useless files
  105. */
  106. @RequiresApi(api = Build.VERSION_CODES.N_MR1)
  107. @Override
  108. public void onCancel(String callerTag) {
  109. Intent storageIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
  110. startActivityForResult(storageIntent, REQUEST_CODE_STORAGE);
  111. }
  112. }