SyncFileNotEnoughSpaceDialogFragment.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.ThemeColorUtils;
  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. int color = ThemeColorUtils.primaryAccentColor(getActivity());
  71. AlertDialog alertDialog = (AlertDialog) getDialog();
  72. if (alertDialog != null) {
  73. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  74. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(color);
  75. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  76. }
  77. }
  78. @NonNull
  79. @Override
  80. public Dialog onCreateDialog(Bundle savedInstanceState) {
  81. Bundle arguments = getArguments();
  82. if (arguments == null) {
  83. throw new IllegalArgumentException("Arguments may not be null");
  84. }
  85. targetFile = arguments.getParcelable(ARG_PASSED_FILE);
  86. setOnConfirmationListener(this);
  87. return super.onCreateDialog(savedInstanceState);
  88. }
  89. /**
  90. * (Only if file is a folder), will access the destination folder to allow user to choose what to synchronize
  91. */
  92. @Override
  93. public void onConfirmation(String callerTag) {
  94. OCFileListFragment frag = (OCFileListFragment) getTargetFragment();
  95. if (frag != null && targetFile != null) {
  96. frag.onItemClicked(targetFile);
  97. }
  98. }
  99. /**
  100. * Will abort/cancel the process (is neutral to "hack" android button position ._.)
  101. */
  102. @Override
  103. public void onNeutral(String callerTag) {
  104. // Nothing
  105. }
  106. /**
  107. * Will access to storage manager in order to empty useless files
  108. */
  109. @RequiresApi(api = Build.VERSION_CODES.N_MR1)
  110. @Override
  111. public void onCancel(String callerTag) {
  112. Intent storageIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
  113. startActivityForResult(storageIntent, REQUEST_CODE_STORAGE);
  114. }
  115. }