6a3fa219ce20ec331e70ea91697837cb2de301d4
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.ui.dialog;
21
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.support.v4.app.Fragment;
27 import android.support.v4.app.FragmentTransaction;
28
29 import com.actionbarsherlock.app.SherlockDialogFragment;
30 import com.actionbarsherlock.app.SherlockFragmentActivity;
31 import com.owncloud.android.R;
32
33 /**
34 * Dialog which will be displayed to user upon keep-in-sync file conflict.
35 *
36 * @author Bartek Przybylski
37 *
38 */
39 public class ConflictsResolveDialog extends SherlockDialogFragment {
40
41 public static enum Decision {
42 CANCEL,
43 KEEP_BOTH,
44 OVERWRITE
45 }
46
47 OnConflictDecisionMadeListener mListener;
48
49 public static ConflictsResolveDialog newInstance(String path, OnConflictDecisionMadeListener listener) {
50 ConflictsResolveDialog f = new ConflictsResolveDialog();
51 Bundle args = new Bundle();
52 args.putString("remotepath", path);
53 f.setArguments(args);
54 f.mListener = listener;
55 return f;
56 }
57
58 @Override
59 public Dialog onCreateDialog(Bundle savedInstanceState) {
60 String remotepath = getArguments().getString("remotepath");
61 return new AlertDialog.Builder(getSherlockActivity())
62 .setIcon(R.drawable.icon)
63 .setTitle(R.string.conflict_title)
64 .setMessage(String.format(getString(R.string.conflict_message), remotepath))
65 .setPositiveButton(R.string.conflict_overwrite,
66 new DialogInterface.OnClickListener() {
67
68 @Override
69 public void onClick(DialogInterface dialog, int which) {
70 if (mListener != null)
71 mListener.ConflictDecisionMade(Decision.OVERWRITE);
72 }
73 })
74 .setNeutralButton(R.string.conflict_keep_both,
75 new DialogInterface.OnClickListener() {
76 @Override
77 public void onClick(DialogInterface dialog, int which) {
78 if (mListener != null)
79 mListener.ConflictDecisionMade(Decision.KEEP_BOTH);
80 }
81 })
82 .setNegativeButton(R.string.conflict_dont_upload,
83 new DialogInterface.OnClickListener() {
84 @Override
85 public void onClick(DialogInterface dialog, int which) {
86 if (mListener != null)
87 mListener.ConflictDecisionMade(Decision.CANCEL);
88 }
89 })
90 .create();
91 }
92
93 public void showDialog(SherlockFragmentActivity activity) {
94 Fragment prev = activity.getSupportFragmentManager().findFragmentByTag("dialog");
95 FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
96 if (prev != null) {
97 ft.remove(prev);
98 }
99 ft.addToBackStack(null);
100
101 this.show(ft, "dialog");
102 }
103
104 public void dismissDialog(SherlockFragmentActivity activity) {
105 Fragment prev = activity.getSupportFragmentManager().findFragmentByTag(getTag());
106 if (prev != null) {
107 FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
108 ft.remove(prev);
109 ft.commit();
110 }
111 }
112
113 @Override
114 public void onCancel(DialogInterface dialog) {
115 mListener.ConflictDecisionMade(Decision.CANCEL);
116 }
117
118 public interface OnConflictDecisionMadeListener {
119 public void ConflictDecisionMade(Decision decision);
120 }
121 }