SyncFileNotEnoughSpaceDialogFragment.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.theme.ThemeButtonUtils;
  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,
  51. new String[] {
  52. file.getFileName(),
  53. properFileSize,
  54. properDiskAvailableSpace});
  55. args.putParcelable(ARG_PASSED_FILE, file);
  56. // Defining buttons
  57. if (file.isFolder()) {
  58. args.putInt(ARG_POSITIVE_BTN_RES, R.string.sync_not_enough_space_dialog_action_choose);
  59. }
  60. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
  61. args.putInt(ARG_NEGATIVE_BTN_RES, R.string.sync_not_enough_space_dialog_action_free_space);
  62. }
  63. args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_cancel);
  64. frag.setArguments(args);
  65. return frag;
  66. }
  67. @Override
  68. public void onStart() {
  69. super.onStart();
  70. AlertDialog alertDialog = (AlertDialog) getDialog();
  71. if (alertDialog != null) {
  72. ThemeButtonUtils.themeBorderlessButton(alertDialog.getButton(AlertDialog.BUTTON_POSITIVE),
  73. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL),
  74. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE));
  75. }
  76. }
  77. @NonNull
  78. @Override
  79. public Dialog onCreateDialog(Bundle savedInstanceState) {
  80. Bundle arguments = getArguments();
  81. if (arguments == null) {
  82. throw new IllegalArgumentException("Arguments may not be null");
  83. }
  84. targetFile = arguments.getParcelable(ARG_PASSED_FILE);
  85. setOnConfirmationListener(this);
  86. return super.onCreateDialog(savedInstanceState);
  87. }
  88. /**
  89. * (Only if file is a folder), will access the destination folder to allow user to choose what to synchronize
  90. */
  91. @Override
  92. public void onConfirmation(String callerTag) {
  93. OCFileListFragment frag = (OCFileListFragment) getTargetFragment();
  94. if (frag != null && targetFile != null) {
  95. frag.onItemClicked(targetFile);
  96. }
  97. }
  98. /**
  99. * Will abort/cancel the process (is neutral to "hack" android button position ._.)
  100. */
  101. @Override
  102. public void onNeutral(String callerTag) {
  103. // Nothing
  104. }
  105. /**
  106. * Will access to storage manager in order to empty useless files
  107. */
  108. @RequiresApi(api = Build.VERSION_CODES.N_MR1)
  109. @Override
  110. public void onCancel(String callerTag) {
  111. Intent storageIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
  112. startActivityForResult(storageIntent, REQUEST_CODE_STORAGE);
  113. }
  114. }