-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
WPAlertDialogFragment.java
185 lines (163 loc) · 7.34 KB
/
WPAlertDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.wordpress.android.util;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import com.actionbarsherlock.app.SherlockDialogFragment;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.ui.prefs.BlogPreferencesActivity;
import org.wordpress.android.ui.stats.WPComLoginActivity;
public class WPAlertDialogFragment extends SherlockDialogFragment implements
DialogInterface.OnClickListener {
private static boolean isXMLRPC = false;
private static boolean isLoadMore = false;
private static boolean isLearnMore = false;
public static WPAlertDialogFragment newInstance(String message) {
return newInstance(message, null, false, null, null);
}
// XMLRPC Error
public static WPAlertDialogFragment newInstance(String message, String error) {
return newInstance(message, error, true, null, null);
}
// Load More Posts Override Warning
public static WPAlertDialogFragment newInstance(String message, String error,
boolean loadMore) {
return newInstance(message, error, loadMore, null, null);
}
public static WPAlertDialogFragment newInstance(String message, String error, boolean loadMore,
String infoTitle, String infoUrl) {
WPAlertDialogFragment adf = new WPAlertDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("alert-message", message);
if (error != null) {
bundle.putString("alert-error", error);
}
if (infoTitle != null && infoUrl != null) {
bundle.putString("info-title", infoTitle);
bundle.putString("info-url", infoUrl);
}
adf.setArguments(bundle);
isLoadMore = loadMore;
return adf;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
if (isXMLRPC) {
String error = this.getArguments().getString("alert-error");
if (error == null)
error = getString(R.string.error_generic);
String message = this.getArguments().getString("alert-message");
if (message == null)
message = getString(R.string.error_generic);
if (error.contains("code 403") || error.contains("code 503")) {
//invalid credentials
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setTitle(R.string.connection_error);
if (WordPress.currentBlog.isDotcomFlag()) {
// Remove wpcom password since it is no longer valid
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this.getActivity().getApplicationContext()).edit();
editor.remove(WordPress.WPCOM_PASSWORD_PREFERENCE);
editor.commit();
b.setMessage(getResources().getText(R.string.incorrect_credentials) + " " + getResources().getText(R.string.please_sign_in));
b.setPositiveButton(R.string.sign_in, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent authIntent = new Intent(getActivity(), WPComLoginActivity.class);
authIntent.putExtra("wpcom", true);
authIntent.putExtra("auth-only", true);
getActivity().startActivity(authIntent);
}
});
} else {
b.setMessage(getResources().getText(R.string.incorrect_credentials) + " " + getResources().getText(R.string.load_settings));
b.setCancelable(true);
b.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent settingsIntent = new Intent(getActivity(), BlogPreferencesActivity.class);
getActivity().startActivity(settingsIntent);
}
});
b.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
return b.create();
} else {
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setTitle(R.string.connection_error);
b.setMessage((error.contains("code 405")) ? error : message);
b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
b.setCancelable(true);
return b.create();
}
} else if (isLoadMore) {
String error = this.getArguments().getString("alert-error");
String message = this.getArguments().getString("alert-message");
//invalid credentials
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setTitle(error);
b.setMessage(message);
b.setCancelable(true);
b.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
OnDialogConfirmListener act = (OnDialogConfirmListener) getActivity();
act.onDialogConfirm();
}
});
b.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return b.create();
} else {
String infoTitle = this.getArguments().getString("info-title");
final String infoURL = this.getArguments().getString("info-url");
String error = this.getArguments().getString("alert-error");
if (error != null)
b.setTitle(error);
else
b.setTitle(R.string.error);
b.setPositiveButton("OK", this);
if (infoTitle != null && infoURL != null) {
b.setNeutralButton(infoTitle, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(infoURL)));
}
});
}
b.setMessage(this.getArguments().getString("alert-message"));
return b.create();
}
}
public interface OnDialogConfirmListener {
public void onDialogConfirm();
}
@Override
public void onClick(DialogInterface dialog, int which) {
}
}