ConflictsResolveActivity.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // TODO rotate when conflict dialog open
  57. // TODO cancel leads to white activity?!
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. if (savedInstanceState != null) {
  62. conflictUpload = savedInstanceState.getParcelable(EXTRA_CONFLICT_UPLOAD);
  63. localBehaviour = savedInstanceState.getInt(EXTRA_LOCAL_BEHAVIOUR);
  64. } else {
  65. conflictUpload = getIntent().getParcelableExtra(EXTRA_CONFLICT_UPLOAD);
  66. localBehaviour = getIntent().getIntExtra(EXTRA_LOCAL_BEHAVIOUR, localBehaviour);
  67. }
  68. if (conflictUpload != null) {
  69. localBehaviour = conflictUpload.getLocalAction();
  70. }
  71. }
  72. @Override
  73. protected void onSaveInstanceState(Bundle outState) {
  74. super.onSaveInstanceState(outState);
  75. outState.putParcelable(EXTRA_CONFLICT_UPLOAD, conflictUpload);
  76. outState.putInt(EXTRA_LOCAL_BEHAVIOUR, localBehaviour);
  77. }
  78. @Override
  79. public void conflictDecisionMade(Decision decision) {
  80. OCFile file = getFile();
  81. switch (decision) {
  82. case CANCEL:
  83. // nothing to do
  84. break;
  85. case KEEP_LOCAL: // Upload
  86. FileUploader.uploadUpdateFile(
  87. this,
  88. getAccount(),
  89. file,
  90. localBehaviour,
  91. FileUploader.NameCollisionPolicy.OVERWRITE
  92. );
  93. if (conflictUpload != null) {
  94. uploadsStorageManager.removeUpload(conflictUpload);
  95. }
  96. break;
  97. case KEEP_BOTH: // Upload
  98. FileUploader.uploadUpdateFile(
  99. this,
  100. getAccount(),
  101. file,
  102. localBehaviour,
  103. FileUploader.NameCollisionPolicy.RENAME
  104. );
  105. if (conflictUpload != null) {
  106. uploadsStorageManager.removeUpload(conflictUpload);
  107. }
  108. break;
  109. case KEEP_SERVER: // Download
  110. if (!this.shouldDeleteLocal()) {
  111. // Overwrite local file
  112. Intent intent = new Intent(this, FileDownloader.class);
  113. intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());
  114. intent.putExtra(FileDownloader.EXTRA_FILE, file);
  115. if (conflictUpload != null) {
  116. intent.putExtra(FileDownloader.EXTRA_CONFLICT_UPLOAD, conflictUpload);
  117. }
  118. startService(intent);
  119. }
  120. break;
  121. }
  122. finish();
  123. }
  124. @Override
  125. protected void onStart() {
  126. super.onStart();
  127. if (getAccount() == null) {
  128. finish();
  129. }
  130. OCFile file = getFile();
  131. if (getFile() == null) {
  132. Log_OC.e(TAG, "No file received");
  133. finish();
  134. }
  135. Optional<User> userOptional = getUser();
  136. if (!userOptional.isPresent()) {
  137. Toast.makeText(this, "Error creating conflict dialog!", Toast.LENGTH_LONG).show();
  138. finish();
  139. }
  140. // Check whether the file is contained in the current Account
  141. Fragment prev = getSupportFragmentManager().findFragmentByTag("conflictDialog");
  142. FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  143. if (prev == null) {
  144. if (getStorageManager().fileExists(file.getRemotePath())) {
  145. ConflictsResolveDialog dialog = new ConflictsResolveDialog(this,
  146. getFile(),
  147. conflictUpload,
  148. userOptional.get()
  149. );
  150. dialog.show(fragmentTransaction, "conflictDialog");
  151. } else {
  152. // Account was changed to a different one - just finish
  153. finish();
  154. }
  155. }
  156. }
  157. /**
  158. * @return whether the local version of the files is to be deleted.
  159. */
  160. private boolean shouldDeleteLocal() {
  161. return localBehaviour == FileUploader.LOCAL_BEHAVIOUR_DELETE;
  162. }
  163. }