ConflictsResolveActivity.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2012 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. * <p/>
  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. package com.owncloud.android.ui.activity;
  22. import android.content.Intent;
  23. import android.os.Bundle;
  24. import android.widget.Toast;
  25. import com.nextcloud.client.account.User;
  26. import com.nextcloud.java.util.Optional;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.datamodel.UploadsStorageManager;
  29. import com.owncloud.android.db.OCUpload;
  30. import com.owncloud.android.files.services.FileDownloader;
  31. import com.owncloud.android.files.services.FileUploader;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import com.owncloud.android.ui.dialog.ConflictsResolveDialog;
  34. import com.owncloud.android.ui.dialog.ConflictsResolveDialog.Decision;
  35. import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener;
  36. import javax.inject.Inject;
  37. import androidx.fragment.app.Fragment;
  38. import androidx.fragment.app.FragmentTransaction;
  39. /**
  40. * Wrapper activity which will be launched if keep-in-sync file will be modified by external
  41. * application.
  42. */
  43. public class ConflictsResolveActivity extends FileActivity implements OnConflictDecisionMadeListener {
  44. /**
  45. * A nullable upload entry that must be removed when and if the conflict is resolved.
  46. */
  47. public static final String EXTRA_CONFLICT_UPLOAD = "CONFLICT_UPLOAD";
  48. /**
  49. * Specify the upload local behaviour when there is no CONFLICT_UPLOAD.
  50. */
  51. public static final String EXTRA_LOCAL_BEHAVIOUR = "LOCAL_BEHAVIOUR";
  52. private static final String TAG = ConflictsResolveActivity.class.getSimpleName();
  53. @Inject UploadsStorageManager uploadsStorageManager;
  54. private OCUpload conflictUpload;
  55. private int localBehaviour = FileUploader.LOCAL_BEHAVIOUR_FORGET;
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. if (savedInstanceState != null) {
  60. conflictUpload = savedInstanceState.getParcelable(EXTRA_CONFLICT_UPLOAD);
  61. localBehaviour = savedInstanceState.getInt(EXTRA_LOCAL_BEHAVIOUR);
  62. } else {
  63. conflictUpload = getIntent().getParcelableExtra(EXTRA_CONFLICT_UPLOAD);
  64. localBehaviour = getIntent().getIntExtra(EXTRA_LOCAL_BEHAVIOUR, localBehaviour);
  65. }
  66. if (conflictUpload != null) {
  67. localBehaviour = conflictUpload.getLocalAction();
  68. }
  69. }
  70. @Override
  71. protected void onSaveInstanceState(Bundle outState) {
  72. super.onSaveInstanceState(outState);
  73. outState.putParcelable(EXTRA_CONFLICT_UPLOAD, conflictUpload);
  74. outState.putInt(EXTRA_LOCAL_BEHAVIOUR, localBehaviour);
  75. }
  76. @Override
  77. public void conflictDecisionMade(Decision decision) {
  78. OCFile file = getFile();
  79. switch (decision) {
  80. case CANCEL:
  81. // nothing to do
  82. break;
  83. case KEEP_LOCAL: // Upload
  84. FileUploader.uploadUpdateFile(
  85. this,
  86. getAccount(),
  87. file,
  88. localBehaviour,
  89. FileUploader.NameCollisionPolicy.OVERWRITE
  90. );
  91. if (conflictUpload != null) {
  92. uploadsStorageManager.removeUpload(conflictUpload);
  93. }
  94. break;
  95. case KEEP_BOTH: // Upload
  96. FileUploader.uploadUpdateFile(
  97. this,
  98. getAccount(),
  99. file,
  100. localBehaviour,
  101. FileUploader.NameCollisionPolicy.RENAME
  102. );
  103. if (conflictUpload != null) {
  104. uploadsStorageManager.removeUpload(conflictUpload);
  105. }
  106. break;
  107. case KEEP_SERVER: // Download
  108. if (!this.shouldDeleteLocal()) {
  109. // Overwrite local file
  110. Intent intent = new Intent(this, FileDownloader.class);
  111. intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());
  112. intent.putExtra(FileDownloader.EXTRA_FILE, file);
  113. if (conflictUpload != null) {
  114. intent.putExtra(FileDownloader.EXTRA_CONFLICT_UPLOAD, conflictUpload);
  115. }
  116. startService(intent);
  117. }
  118. break;
  119. }
  120. finish();
  121. }
  122. @Override
  123. protected void onStart() {
  124. super.onStart();
  125. if (getAccount() == null) {
  126. finish();
  127. }
  128. OCFile file = getFile();
  129. if (getFile() == null) {
  130. Log_OC.e(TAG, "No file received");
  131. finish();
  132. }
  133. Optional<User> userOptional = getUser();
  134. if (!userOptional.isPresent()) {
  135. Toast.makeText(this, "Error creating conflict dialog!", Toast.LENGTH_LONG).show();
  136. finish();
  137. }
  138. // Check whether the file is contained in the current Account
  139. Fragment prev = getSupportFragmentManager().findFragmentByTag("conflictDialog");
  140. FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  141. if (prev != null) {
  142. fragmentTransaction.remove(prev);
  143. }
  144. if (getStorageManager().fileExists(file.getRemotePath())) {
  145. ConflictsResolveDialog dialog = ConflictsResolveDialog.newInstance(getFile(),
  146. conflictUpload,
  147. userOptional.get());
  148. dialog.show(fragmentTransaction, "conflictDialog");
  149. } else {
  150. // Account was changed to a different one - just finish
  151. finish();
  152. }
  153. }
  154. /**
  155. * @return whether the local version of the files is to be deleted.
  156. */
  157. private boolean shouldDeleteLocal() {
  158. return localBehaviour == FileUploader.LOCAL_BEHAVIOUR_DELETE;
  159. }
  160. }