5959import org .json .JSONObject ;
6060
6161import java .io .IOException ;
62- import java .io .InputStream ;
63- import java .io .OutputStream ;
6462import java .net .HttpURLConnection ;
6563import java .net .URL ;
6664
6765import eightbitlab .com .blurview .BlurView ;
68- import io .sentry .Sentry ;
6966
7067public class MainActivity extends FoxActivity implements SwipeRefreshLayout .OnRefreshListener , SearchView .OnQueryTextListener , SearchView .OnCloseListener , OverScrollManager .OverScrollHelper {
7168 private static final String TAG = "MainActivity" ;
@@ -258,8 +255,10 @@ public void commonNext() {
258255 if (MainApplication .isCrashReportingEnabled ()) {
259256 SharedPreferences preferences = getSharedPreferences ("sentry" , MODE_PRIVATE );
260257 String lastExitReason = preferences .getString ("lastExitReason" , "" );
258+ if (BuildConfig .DEBUG ) Log .d ("NoodleDebug" , "Last Exit Reason: " + lastExitReason );
261259 if (lastExitReason .equals ("crash" )) {
262260 String lastEventId = preferences .getString ("lastEventId" , "" );
261+ if (BuildConfig .DEBUG ) Log .d ("NoodleDebug" , "Last Event ID: " + lastEventId );
263262 if (!lastEventId .equals ("" )) {
264263 // Three edit texts for the user to enter their email, name and a description of the issue
265264 EditText email = new EditText (this );
@@ -299,47 +298,31 @@ public void commonNext() {
299298 connection .setRequestMethod ("POST" );
300299 connection .setRequestProperty ("Content-Type" , "application/json" );
301300 connection .setRequestProperty ("Authorization" , "Bearer " + BuildConfig .SENTRY_TOKEN );
302- connection .setDoOutput (true );
303- connection .setDoInput (true );
304- connection .setChunkedStreamingMode (0 );
305- connection .connect ();
306- // Write the JSON data to the output stream
307- OutputStream outputStream = connection .getOutputStream ();
308- // Name and email are optional, so if they are empty, set them to
309- // Anonymous and Anonymous respectively
301+ // Setups the JSON body
310302 String nameString = name .getText ().toString ();
311303 String emailString = email .getText ().toString ();
312304 if (nameString .equals ("" )) nameString = "Anonymous" ;
313305 if (emailString .equals ("" )) emailString = "Anonymous" ;
314- String finalNameString = nameString ;
315- String finalEmailString = emailString ;
316- outputStream .write (new JSONObject () {{
317- put ("event_id" , lastEventId );
318- put ("name" , finalNameString );
319- put ("email" , finalEmailString );
320- put ("comments" , description .getText ().toString ());
321- }}.toString ().getBytes ());
322- outputStream .flush ();
323- outputStream .close ();
324- // Read the response
325- InputStream inputStream = connection .getInputStream ();
326- byte [] buffer = new byte [1024 ];
327- int length ;
328- StringBuilder stringBuilder = new StringBuilder ();
329- while ((length = inputStream .read (buffer )) != -1 ) {
330- stringBuilder .append (new String (buffer , 0 , length ));
306+ JSONObject body = new JSONObject ();
307+ body .put ("event_id" , lastEventId );
308+ body .put ("name" , nameString );
309+ body .put ("email" , emailString );
310+ body .put ("comments" , description .getText ().toString ());
311+ // Send the request
312+ connection .setDoOutput (true );
313+ connection .getOutputStream ().write (body .toString ().getBytes ());
314+ connection .connect ();
315+ // For debug builds, log the response code and response body
316+ if (BuildConfig .DEBUG ) {
317+ Log .d ("NoodleDebug" , "Response Code: " + connection .getResponseCode ());
331318 }
332- inputStream .close ();
333- connection .disconnect ();
334- if (BuildConfig .DEBUG ) Log .d ("Sentry" , stringBuilder .toString ());
335- // Valid responses will be a json object with a key "id"
336- JSONObject jsonObject = new JSONObject (stringBuilder .toString ());
337- if (jsonObject .has ("id" )) {
338- // Show a toast to the user to confirm that the feedback has been sent
319+ // Check if the request was successful
320+ if (connection .getResponseCode () == 200 ) {
339321 runOnUiThread (() -> Toast .makeText (this , R .string .sentry_dialogue_success , Toast .LENGTH_LONG ).show ());
340322 } else {
341- // Show a toast to the user to confirm that the feedback has not been sent
342- runOnUiThread (() -> Toast .makeText (this , R .string .sentry_dialogue_failed_toast , Toast .LENGTH_LONG ).show ());
323+ runOnUiThread (() -> Toast .makeText (this ,
324+ R .string .sentry_dialogue_failed_toast ,
325+ Toast .LENGTH_LONG ).show ());
343326 }
344327 } catch (IOException | JSONException ignored ) {
345328 // Show a toast if the user feedback could not be submitted
@@ -348,7 +331,8 @@ public void commonNext() {
348331 }).start ();
349332 }).setNegativeButton (R .string .cancel , (dialog , which ) -> {
350333 preferences .edit ().remove ("lastEventId" ).apply ();
351- Sentry .captureMessage ("User has ignored the crash" );
334+ preferences .edit ().putString ("lastExitReason" , "" ).apply ();
335+ Log .w (TAG , "User cancelled sentry dialog" );
352336 }).show ();
353337 }
354338 }
0 commit comments