61ac8f5fe2275a6bac9e3bf32558ffc1d3268efc
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / ConflictsResolveActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.ui.activity;
20
21 import com.owncloud.android.Log_OC;
22 import com.owncloud.android.datamodel.DataStorageManager;
23 import com.owncloud.android.datamodel.FileDataStorageManager;
24 import com.owncloud.android.datamodel.OCFile;
25 import com.owncloud.android.files.services.FileUploader;
26 import com.owncloud.android.ui.dialog.ConflictsResolveDialog;
27 import com.owncloud.android.ui.dialog.ConflictsResolveDialog.Decision;
28 import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener;
29
30 import android.content.Intent;
31 import android.os.Bundle;
32
33 /**
34 * Wrapper activity which will be launched if keep-in-sync file will be modified by external
35 * application.
36 *
37 * @author Bartek Przybylski
38 * @author David A. Velasco
39 */
40 public class ConflictsResolveActivity extends FileActivity implements OnConflictDecisionMadeListener {
41
42 private String TAG = ConflictsResolveActivity.class.getSimpleName();
43
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 }
48
49 @Override
50 public void ConflictDecisionMade(Decision decision) {
51 Intent i = new Intent(getApplicationContext(), FileUploader.class);
52
53 switch (decision) {
54 case CANCEL:
55 finish();
56 return;
57 case OVERWRITE:
58 i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
59 break;
60 case KEEP_BOTH:
61 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
62 break;
63 default:
64 Log_OC.wtf(TAG, "Unhandled conflict decision " + decision);
65 return;
66 }
67 i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
68 i.putExtra(FileUploader.KEY_FILE, getFile());
69 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
70
71 startService(i);
72 finish();
73 }
74
75 @Override
76 protected void onAccountSet(boolean stateWasRecovered) {
77 if (getAccount() != null) {
78 OCFile file = getFile();
79 if (getFile() == null) {
80 Log_OC.e(TAG, "No conflictive file received");
81 finish();
82 } else {
83 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
84 DataStorageManager storageManager = new FileDataStorageManager(getAccount(), getContentResolver());
85 file = storageManager.getFileByPath(file.getRemotePath()); // file = null if not in the current Account
86 if (file != null) {
87 setFile(file);
88 ConflictsResolveDialog d = ConflictsResolveDialog.newInstance(file.getRemotePath(), this);
89 d.showDialog(this);
90
91 } else {
92 // account was changed to a different one - just finish
93 finish();
94 }
95 }
96
97 } else {
98 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
99 finish();
100 }
101
102 }
103 }