Skip to content

Commit

Permalink
Added debug activity, allow http connections #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Apr 16, 2020
1 parent fbd0ded commit 369ca8e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 31 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<application
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Expand Down
26 changes: 25 additions & 1 deletion app/src/main/java/de/bulling/barcodebuddyscanner/Api/BBApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand Down Expand Up @@ -67,7 +70,12 @@ public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
callback.onError(BBApiCallback.ERROR_NETWORK, t.getMessage(), null);
if (t instanceof IOException) {
callback.onError(BBApiCallback.ERROR_NETWORK, t.getMessage(), null);
} else {
callback.onError(BBApiCallback.ERROR_OTHER, t.getMessage(), null);
}

}
});
}
Expand All @@ -79,4 +87,20 @@ public void getVersionInfo(final BBApiCallback callback) {
public void postBarcode(String barcode, final BBApiCallback callback) {
processResponse(REQUEST_ACTION_BARCODE, this.bbApi.postBarcode(this.apiKey, barcode), callback);
}



public void postBarcodeDebug(String barcode, final BBApiCallback callback) {
this.bbApi.postBarcodeDebug(this.apiKey, barcode).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
callback.onResult(response);
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
int x=0;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonElement;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
Expand All @@ -18,4 +19,10 @@ public interface BBService {
@POST("action/scan")
Call<JsonElement> postBarcode(@Header("BBUDDY-API-KEY") String authorization, @Field("barcode") String barcode);



@FormUrlEncoded
@POST("action/scan")
Call<ResponseBody> postBarcodeDebug(@Header("BBUDDY-API-KEY") String authorization, @Field("barcode") String barcode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

Expand All @@ -16,11 +15,13 @@
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import java.io.IOException;
import java.util.List;

import de.bulling.barcodebuddyscanner.Api.BBApi;
import de.bulling.barcodebuddyscanner.Api.BBApiCallback;
import de.bulling.barcodebuddyscanner.Helper.BeepManager;
import okhttp3.ResponseBody;
import retrofit2.Response;


Expand Down Expand Up @@ -64,33 +65,30 @@ public void barcodeResult(BarcodeResult result) {
lastBarcode = result.getText();
barcodeView.setStatusText(result.getText());
beepManager.playBeepSoundAndVibrate();
bbApi.postBarcode(result.getText(), new BBApiCallback() {
@Override
public void onResult(Object result) {
if (result instanceof String)
Toast.makeText(ContinuousCaptureActivity.this,
(String) result, Toast.LENGTH_SHORT).show();
else
Toast.makeText(ContinuousCaptureActivity.this,
R.string.error_unex, Toast.LENGTH_LONG).show();
}

@Override
public void onError(int errorCode, String errorMessage, Response<JsonElement> response) {
Toast.makeText(ContinuousCaptureActivity.this,
getString(R.string.error) + errorMessage,
Toast.LENGTH_LONG).show();
if (IS_DEBUG) {
String debugMessage = errorMessage + "\n";
if (response != null) {
debugMessage = debugMessage + "Received:\n" + response.raw().body();
}
Intent i = new Intent(ContinuousCaptureActivity.this, DebugActivity.class);
i.putExtra("debug", debugMessage);
ContinuousCaptureActivity.this.startActivity(i);
if (!IS_DEBUG) {
bbApi.postBarcode(result.getText(), new BBApiCallback() {
@Override
public void onResult(Object result) {
if (result instanceof String)
Toast.makeText(ContinuousCaptureActivity.this,
(String) result, Toast.LENGTH_SHORT).show();
else
Toast.makeText(ContinuousCaptureActivity.this,
R.string.error_unex, Toast.LENGTH_LONG).show();
}
}
});

@Override
public void onError(int errorCode, String errorMessage, Response<JsonElement> response) {
Toast.makeText(ContinuousCaptureActivity.this,
getString(R.string.error) + errorMessage,
Toast.LENGTH_LONG).show();
}
});
} else {
doDebugScan(result.getText());
}


}

Expand All @@ -102,20 +100,47 @@ public void possibleResultPoints(List<ResultPoint> resultPoints) {
@Override
protected void onResume() {
super.onResume();

barcodeView.resume();
}

@Override
protected void onPause() {
super.onPause();

barcodeView.pause();
}

private void doDebugScan(String barcode) {
bbApi.postBarcodeDebug(barcode, new BBApiCallback() {
@Override
public void onResult(Object result) {
Response<ResponseBody> response = (Response<ResponseBody>) result;
String debugMessage = "Received:\n\n'";
try {
debugMessage = debugMessage + response.body().string() + "'";
} catch (IOException e) {
e.printStackTrace();
}
Intent i = new Intent(ContinuousCaptureActivity.this, DebugActivity.class);
i.putExtra("debug", debugMessage);
ContinuousCaptureActivity.this.startActivity(i);
}

public void triggerScan(View view) {
barcodeView.decodeSingle(callback);
@Override
public void onError(int errorCode, String errorMessage, Response<JsonElement> response) {
String debugMessage = errorMessage + "\n";
if (response != null) {
debugMessage = debugMessage + "Received:\n'" + response.body().getAsString() + "'\n\n";
try {
debugMessage = debugMessage + "Received:\n'" + response.errorBody().string() + "'";
} catch (IOException e) {
e.printStackTrace();
}
}
Intent i = new Intent(ContinuousCaptureActivity.this, DebugActivity.class);
i.putExtra("debug", debugMessage);
ContinuousCaptureActivity.this.startActivity(i);
}
});
}

}

0 comments on commit 369ca8e

Please sign in to comment.