Merge branch 'pr/311' into pull_request_311_with_develop
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / ConflictsResolveDialog.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.dialog;
20
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.support.v4.app.Fragment;
26 import android.support.v4.app.FragmentTransaction;
27
28 import com.actionbarsherlock.app.SherlockDialogFragment;
29 import com.actionbarsherlock.app.SherlockFragmentActivity;
30 import com.owncloud.android.R;
31 import com.owncloud.android.utils.DisplayUtils;
32
33
34 /**
35 * Dialog which will be displayed to user upon keep-in-sync file conflict.
36 *
37 * @author Bartek Przybylski
38 *
39 */
40 public class ConflictsResolveDialog extends SherlockDialogFragment {
41
42 public static enum Decision {
43 CANCEL,
44 KEEP_BOTH,
45 OVERWRITE
46 }
47
48 OnConflictDecisionMadeListener mListener;
49
50 public static ConflictsResolveDialog newInstance(String path, OnConflictDecisionMadeListener listener) {
51 ConflictsResolveDialog f = new ConflictsResolveDialog();
52 Bundle args = new Bundle();
53 args.putString("remotepath", path);
54 f.setArguments(args);
55 f.mListener = listener;
56 return f;
57 }
58
59 @Override
60 public Dialog onCreateDialog(Bundle savedInstanceState) {
61 String remotepath = getArguments().getString("remotepath");
62 return new AlertDialog.Builder(getSherlockActivity())
63 .setIcon(DisplayUtils.getSeasonalIconId())
64 .setTitle(R.string.conflict_title)
65 .setMessage(String.format(getString(R.string.conflict_message), remotepath))
66 .setPositiveButton(R.string.conflict_overwrite,
67 new DialogInterface.OnClickListener() {
68
69 @Override
70 public void onClick(DialogInterface dialog, int which) {
71 if (mListener != null)
72 mListener.conflictDecisionMade(Decision.OVERWRITE);
73 }
74 })
75 .setNeutralButton(R.string.conflict_keep_both,
76 new DialogInterface.OnClickListener() {
77 @Override
78 public void onClick(DialogInterface dialog, int which) {
79 if (mListener != null)
80 mListener.conflictDecisionMade(Decision.KEEP_BOTH);
81 }
82 })
83 .setNegativeButton(R.string.conflict_dont_upload,
84 new DialogInterface.OnClickListener() {
85 @Override
86 public void onClick(DialogInterface dialog, int which) {
87 if (mListener != null)
88 mListener.conflictDecisionMade(Decision.CANCEL);
89 }
90 })
91 .create();
92 }
93
94 public void showDialog(SherlockFragmentActivity activity) {
95 Fragment prev = activity.getSupportFragmentManager().findFragmentByTag("dialog");
96 FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
97 if (prev != null) {
98 ft.remove(prev);
99 }
100 ft.addToBackStack(null);
101
102 this.show(ft, "dialog");
103 }
104
105 public void dismissDialog(SherlockFragmentActivity activity) {
106 Fragment prev = activity.getSupportFragmentManager().findFragmentByTag(getTag());
107 if (prev != null) {
108 FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
109 ft.remove(prev);
110 ft.commit();
111 }
112 }
113
114 @Override
115 public void onCancel(DialogInterface dialog) {
116 mListener.conflictDecisionMade(Decision.CANCEL);
117 }
118
119 public interface OnConflictDecisionMadeListener {
120 public void conflictDecisionMade(Decision decision);
121 }
122 }