Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check username and password in login routine #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion "27.0.3"

defaultConfig {
applicationId "com.drobisch.partkeeprscannrapp"
Expand All @@ -20,13 +20,13 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.+'
compile 'com.android.support:design:23.+'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:23.4.0'
implementation 'com.android.support:design:23.4.0'

compile 'com.journeyapps:zxing-android-embedded:3.4.0'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.3.1'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.drobisch.partkeeprscannrapp;

import android.app.Activity;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Base64;
import android.util.Log;
import android.util.Pair;
Expand Down Expand Up @@ -43,6 +45,7 @@ public class ContinuousCaptureActivity extends Activity {
private TextView mPartNameView;
private TextView mPartStockView;
private TextView mPartLocationView;
private EditText mAVGPriceField;



Expand Down Expand Up @@ -95,6 +98,7 @@ protected void onCreate(Bundle savedInstanceState) {
mPartNameView = (TextView) findViewById(R.id.partName);
mPartLocationView = (TextView) findViewById(R.id.partLocation);
mPartStockView = (TextView) findViewById(R.id.partStock);
mAVGPriceField = (EditText) findViewById(R.id.avgPrice) ;

mPartNameView.setText("");
mPartLocationView.setText("");
Expand All @@ -120,6 +124,7 @@ public void onClick(View view) {
barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.setStatusText("");
barcodeView.decodeContinuous(callback);

}

private void updatePartInfo(int partID) {
Expand All @@ -130,7 +135,7 @@ private void updatePartInfo(int partID) {
private void addStock() {
Log.d("CaptureActivity", "addStock");
if(mPartPartID != -1) {
ApiPartTask task = new ApiPartTask(mUser, mPassword, mServer, mPartPartID,"addStock", "quantity=1&price=0&comment=");
ApiPartTask task = new ApiPartTask(mUser, mPassword, mServer, mPartPartID,"addStock", "quantity=1&price=" + mAVGPriceField.getText().toString() + "&comment=");
task.execute((Void) null);
}
}
Expand Down Expand Up @@ -194,70 +199,6 @@ private boolean checkInternetConenction() {
return false;
}

private Pair<InputStream, HttpURLConnection> doHttpConnection(String urlStr, String user, String password, String json) {
InputStream in = null;
HttpURLConnection httpConn = null;
String restURI = urlStr; // "http://" + urlStr + "/api/parts/1/addStock";
int resCode = -1;

try {
URL url = new URL(restURI);
URLConnection urlConn = url.openConnection();

if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL is not an Http URL");
}

String userToken= user + ":" + password;
byte[] data = userToken.getBytes("UTF-8");
String encode = Base64.encodeToString(data, Base64.NO_WRAP);
httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("PUT");
httpConn.setRequestProperty("Authorization", "Basic " + encode);
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");

httpConn.setRequestProperty("Content-length", json.getBytes().length + "");
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setUseCaches(false);

OutputStream outputStream = httpConn.getOutputStream();
outputStream.write(json.getBytes("UTF-8"));
outputStream.close();

httpConn.connect();

resCode = httpConn.getResponseCode();

boolean redirect = false;

if (resCode != HttpURLConnection.HTTP_OK) {
if (resCode == HttpURLConnection.HTTP_MOVED_TEMP
|| resCode == HttpURLConnection.HTTP_MOVED_PERM
|| resCode == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;

}

if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
Log.d("LoginActivity","Successful URL-connection" + String.valueOf(resCode));
}
else {
Log.d("LoginActivity","Error URL-connection" + String.valueOf(resCode));
return Pair.create(null,httpConn);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return Pair.create(in,httpConn);
}

public class ApiPartTask extends AsyncTask<Void, Void, Boolean> {
private final String mUser;
Expand All @@ -270,9 +211,11 @@ public class ApiPartTask extends AsyncTask<Void, Void, Boolean> {
private Integer mPartStock = 0;
private String mPartLocation = "";
private Integer mPartID;
private double mPartAvgPrice = 0;
private Boolean error = false;
private String errorString;


ApiPartTask(String user, String password, String server, int partID, String command, String json) {
mUser = user;
mPassword = password;
Expand All @@ -296,7 +239,7 @@ protected Boolean doInBackground(Void... params) {
String restURL = mServer + "/api/parts/" + mPartID.toString();
if(mCommand != "")
restURL += "/" + mCommand;
httpResult = doHttpConnection(restURL,mUser,mPassword,mJson);
httpResult = httpCon.doHttpConnection(restURL,mUser,mPassword,mJson);
in = httpResult.first;
httpcon = httpResult.second;
/*Bundle b = new Bundle();
Expand All @@ -316,19 +259,34 @@ protected Boolean doInBackground(Void... params) {
else
{
if(httpcon != null) {
error = true;
errorString = "Connection failed with http-code " + httpcon.getResponseCode();

switch (httpcon.getResponseCode())
{
case 401:
error = true;
errorString = getString(R.string.error_incorrect_password_user);
break;
case 404:
error = true;
errorString = getString(R.string.error_part_not_exists);
break;
default:
error = true;
errorString = getString(R.string.error_http_long);
break;
}
}
else {
error = true;
errorString = "Connection failed";
errorString = getString(R.string.error_connection_long);
}
}
}

catch (IOException e1) {
e1.printStackTrace();
error = true;
errorString = getString(R.string.error_server_connect_failed);
}

try {
Expand All @@ -337,6 +295,7 @@ protected Boolean doInBackground(Void... params) {
JSONObject jsonStorage = json.getJSONObject("storageLocation");
mPartLocation = (String) jsonStorage.get("name");
mPartStock = json.getInt("stockLevel");
mPartAvgPrice = json.getDouble("averagePrice");
} catch (JSONException e) {
e.printStackTrace();
error = true;
Expand All @@ -351,19 +310,28 @@ protected void onPostExecute(final Boolean success) {
mPartLocationView.setText(mPartLocation);
mPartStockView.setText(mPartStock.toString());
mPartPartID = mPartID;
mAVGPriceField.setText(String.valueOf(Math.round(mPartAvgPrice *100.0) /100.0));
if(error == true) {
mPartPartID = -1;
Toast infoToast = Toast.makeText(getApplicationContext(),errorString,Toast.LENGTH_SHORT);
//specify the toast display position exact parent layout center. no x or y offset
infoToast.setGravity(Gravity.BOTTOM,0,390);
infoToast.show();

openMessageBox("Error", errorString);
}
}

@Override
protected void onCancelled() {

}


protected void openMessageBox(String headline, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ContinuousCaptureActivity.this);
builder.setMessage(message).setTitle(headline);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {}});
AlertDialog dialog = builder.create();
dialog.show();
}


}
}
Loading