Skip to content

Commit d71fb77

Browse files
committed
Merge branch 'issue627'
2 parents b958f82 + 654a493 commit d71fb77

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

gpslogger/src/main/java/com/mendhak/gpslogger/ui/Dialogs.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,26 @@
3232
import com.mendhak.gpslogger.common.Strings;
3333
import com.ms.square.android.expandabletextview.ExpandableTextView;
3434

35-
import java.util.Arrays;
35+
import java.io.PrintWriter;
36+
import java.io.StringWriter;
3637

3738
public class Dialogs {
3839
private static MaterialDialog pd;
3940

4041
protected static String getFormattedErrorMessageForDisplay(String message, Throwable throwable) {
41-
String html = "";
42+
StringBuilder html = new StringBuilder();
4243
if(!Strings.isNullOrEmpty(message)){
43-
html += "<b>" + message.replace("\r\n","<br />").replace("\n","<br />") + "</b> <br /><br />";
44+
html.append("<b>").append(message.replace("\r\n","<br />")
45+
.replace("\n","<br />")).append("</b> <br /><br />");
4446
}
4547

46-
if(throwable != null){
47-
if(!Strings.isNullOrEmpty(throwable.getMessage())){
48-
html += throwable.getMessage().replace("\r\n","<br />") + "<br />";
49-
}
48+
while(throwable != null && !Strings.isNullOrEmpty(throwable.getMessage())){
49+
html.append(throwable.getMessage().replace("\r\n","<br />"))
50+
.append("<br /><br />");
51+
throwable=throwable.getCause();
5052
}
51-
return html;
53+
54+
return html.toString();
5255
}
5356

5457
protected static String getFormattedErrorMessageForPlainText(String message, Throwable throwable){
@@ -58,14 +61,15 @@ protected static String getFormattedErrorMessageForPlainText(String message, Thr
5861
sb.append(message).append("\r\n");
5962
}
6063

61-
if(throwable != null){
62-
if(!Strings.isNullOrEmpty(throwable.getMessage())){
63-
sb.append(throwable.getMessage()).append("\r\n");
64-
}
65-
66-
if(throwable.getStackTrace().length > 0) {
67-
sb.append(Arrays.toString(throwable.getStackTrace()));
64+
while (throwable != null && !Strings.isNullOrEmpty(throwable.getMessage())) {
65+
sb.append("\r\n\r\n").append(throwable.getMessage()).append("\r\n");
66+
if (throwable.getStackTrace().length > 0) {
67+
StringWriter sw = new StringWriter();
68+
PrintWriter pw = new PrintWriter(sw);
69+
throwable.printStackTrace(pw);
70+
sb.append(sw.toString());
6871
}
72+
throwable = throwable.getCause();
6973
}
7074

7175
return sb.toString();
@@ -97,7 +101,7 @@ public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogActio
97101
public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
98102

99103
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
100-
android.content.ClipData clip = android.content.ClipData.newPlainText("Gpslogger error message", getFormattedErrorMessageForPlainText(errorMessage, throwable));
104+
android.content.ClipData clip = android.content.ClipData.newPlainText("Gpslogger error message", getFormattedErrorMessageForPlainText(friendlyMessage, throwable));
101105
clipboard.setPrimaryClip(clip);
102106

103107
materialDialog.dismiss();

gpslogger/src/main/java/com/mendhak/gpslogger/ui/fragments/settings/CustomUrlFragment.java

+33
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,28 @@
2424
import android.preference.EditTextPreference;
2525
import android.preference.Preference;
2626
import com.mendhak.gpslogger.R;
27+
import com.mendhak.gpslogger.common.AppSettings;
28+
import com.mendhak.gpslogger.common.EventBusHook;
29+
import com.mendhak.gpslogger.common.events.UploadEvents;
2730
import com.mendhak.gpslogger.common.network.Networks;
2831
import com.mendhak.gpslogger.common.PreferenceHelper;
2932
import com.mendhak.gpslogger.common.PreferenceNames;
3033
import com.mendhak.gpslogger.common.network.ServerType;
3134
import com.mendhak.gpslogger.common.slf4j.Logs;
35+
import com.mendhak.gpslogger.loggers.customurl.CustomUrlJob;
36+
import com.mendhak.gpslogger.loggers.customurl.CustomUrlRequest;
3237
import com.mendhak.gpslogger.senders.PreferenceValidator;
3338
import com.mendhak.gpslogger.ui.Dialogs;
3439
import com.mendhak.gpslogger.ui.fragments.PermissionedPreferenceFragment;
40+
import com.path.android.jobqueue.JobManager;
41+
3542
import org.slf4j.Logger;
3643
import java.net.MalformedURLException;
3744
import java.net.URL;
3845
import java.text.MessageFormat;
3946

47+
import de.greenrobot.event.EventBus;
48+
4049
public class CustomUrlFragment extends PermissionedPreferenceFragment implements
4150
PreferenceValidator,
4251
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
@@ -56,8 +65,15 @@ public void onCreate(Bundle savedInstanceState) {
5665
urlPathPreference.setOnPreferenceChangeListener(this);
5766

5867
findPreference("customurl_legend_1").setOnPreferenceClickListener(this);
68+
findPreference("customurl_http_test").setOnPreferenceClickListener(this);
5969
findPreference("customurl_validatecustomsslcert").setOnPreferenceClickListener(this);
6070

71+
registerEventBus();
72+
73+
}
74+
75+
private void registerEventBus() {
76+
EventBus.getDefault().register(this);
6177
}
6278

6379
@Override
@@ -104,9 +120,26 @@ else if(preference.getKey().equals("customurl_validatecustomsslcert")){
104120

105121
return true;
106122
}
123+
else if (preference.getKey().equals("customurl_http_test")){
124+
LOG.debug("Sending test HTTP GET request to " + PreferenceHelper.getInstance().getCustomLoggingUrl() );
125+
Dialogs.progress(getActivity(), getString(R.string.please_wait), getString(R.string.please_wait));
126+
JobManager jobManager = AppSettings.getJobManager();
127+
jobManager.addJobInBackground(new CustomUrlJob(new CustomUrlRequest(PreferenceHelper.getInstance().getCustomLoggingUrl()), new UploadEvents.CustomUrl()));
128+
}
107129
return false;
108130
}
109131

132+
@EventBusHook
133+
public void onEventMainThread(UploadEvents.CustomUrl c){
134+
LOG.debug("Custom URL test, success: " + c.success);
135+
Dialogs.hideProgress();
136+
if(!c.success){
137+
Dialogs.error(getString(R.string.error), c.message, c.throwable.getMessage(),c.throwable, getActivity());
138+
}
139+
else {
140+
Dialogs.alert(getString(R.string.success), "", getActivity());
141+
}
142+
}
110143

111144

112145
}

gpslogger/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,6 @@
444444
<string name="customurl_http_body">HTTP Body</string>
445445
<string name="customurl_http_headers">HTTP Headers</string>
446446
<string name="customurl_http_method">HTTP Method</string>
447+
<string name="customurl_http_test">HTTP Test</string>
448+
447449
</resources>

gpslogger/src/main/res/xml/customurlsettings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@
3737
<Preference android:key="customurl_validatecustomsslcert"
3838
android:title="@string/ssl_certificate_validate" />
3939

40+
<Preference android:key="customurl_http_test" android:title="@string/customurl_http_test" />
41+
4042

4143
</PreferenceScreen>

gpslogger/src/test/java/com/mendhak/gpslogger/ui/DialogsTest.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void GetFormattedErrorMessageForDisplay_WithMessageAndThrowable() {
1919
Throwable t = new Throwable("Parameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n");
2020
t.setStackTrace(new StackTraceElement[]{new StackTraceElement("XXX", "yyy", "BBB", 99)});
2121

22-
String expected = "<b>An error occurred.<br />220 ----------------- There was a problem<br />No username specified.<br />Server not configured properly.</b> <br /><br />Parameter not set.<br />Exception handling not configured.<br />Eat oranges they are great.<br /><br />";
22+
String expected = "<b>An error occurred.<br />220 ----------------- There was a problem<br />No username specified.<br />Server not configured properly.</b> <br /><br />Parameter not set.<br />Exception handling not configured.<br />Eat oranges they are great.<br /><br /><br />";
2323
assertThat("Message formatted for error display", Dialogs.getFormattedErrorMessageForDisplay(message, t), is(expected));
2424

2525
}
@@ -49,16 +49,20 @@ public void GetFormattedErrorMessageForDisplay_NewLinesReplaced(){
4949
}
5050

5151
@Test
52-
public void GetFormattedErrorMessageForPlainText_WithMessageOrThrowable(){
52+
public void GetFormattedErrorMessageForPlainText_WithMessageAndThrowable() {
5353
String message = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.";
5454
Throwable t = new Throwable("Parameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n");
5555
t.setStackTrace(new StackTraceElement[]{new StackTraceElement("XXX", "yyy", "BBB", 99)});
5656

57-
String plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\nParameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n\r\n[XXX.yyy(BBB:99)]";
57+
String plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\n\r\n\r\nParameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n\r\njava.lang.Throwable: Parameter not set.\r\nException handling not configured.\r\nEat oranges they are great.\r\n\n\tat XXX.yyy(BBB:99)\n";
5858
assertThat("Message formatted for emailing copying", Dialogs.getFormattedErrorMessageForPlainText(message, t), is(plainText));
59+
}
5960

60-
plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\nThis is a detailed message\r\n";
61-
t = new Throwable("This is a detailed message");
61+
@Test
62+
public void GetFormattedErrorMessageForPlainText_WithMessageOnly() {
63+
String message = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.";
64+
String plainText = "An error occurred.\r\n220 ----------------- There was a problem\r\nNo username specified.\r\nServer not configured properly.\r\n\r\n\r\nThis is a detailed message\r\n";
65+
Throwable t = new Throwable("This is a detailed message");
6266
t.setStackTrace(new StackTraceElement[0]);
6367
assertThat("Message formatted without stacktrace fully present", Dialogs.getFormattedErrorMessageForPlainText(message, t), is(plainText));
6468

0 commit comments

Comments
 (0)