Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
4 changes: 4 additions & 0 deletions MobileApp/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
buildConfigField "String", "SERVER_URL", "\"" + properties.getProperty('server_url') + "\""
}

buildTypes {
Expand Down
12 changes: 9 additions & 3 deletions MobileApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.QrScanner"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".RedirectionActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand All @@ -22,10 +27,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CaptureAct"
<activity
android:name=".CaptureAct"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"/>
android:theme="@style/zxing_CaptureTheme" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,30 @@
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

Button scanBtn,submitBtn;
Button scanBtn;

Context context;
String token;
EditText urlInput, server;
ActivityResultLauncher<ScanOptions> barLaucher = registerForActivityResult(new ScanContract(), result->
ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(), result->
{
if(result.getContents() !=null)
{
token = result.getContents();
Log.i("TOKEN", "token: "+token);
urlInput.setVisibility(View.VISIBLE);
submitBtn.setVisibility(View.VISIBLE);
server.setVisibility(View.VISIBLE);
scanBtn.setVisibility(View.GONE);

Intent intent = new Intent(context, RedirectionActivity.class);
intent.putExtra("token",token);
context.startActivity(intent);

}else {
token = null;
Expand All @@ -51,14 +37,9 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context = this;
scanBtn = findViewById(R.id.scanbtn);
submitBtn = findViewById(R.id.submit);
urlInput = findViewById(R.id.urlInput);
server = findViewById(R.id.server);

submitBtn.setVisibility(View.GONE);
urlInput.setVisibility(View.GONE);
server.setVisibility(View.GONE);

scanBtn.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -67,14 +48,7 @@ public void onClick(View view) {
}
});

submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (urlInput.getText().toString().length() != 0){ //needs improvement
sendUrl(urlInput.getText().toString());
}
}
});

}

private void scanQrCode()
Expand All @@ -83,64 +57,10 @@ private void scanQrCode()
options.setBeepEnabled(true);
options.setOrientationLocked(true);
options.setCaptureActivity(CaptureAct.class);
barLaucher.launch(options);
barLauncher.launch(options);
}

private void sendUrl(String url){
if (token != null && server.getText().toString().length() != 0){
MyTask task = new MyTask();
task.execute(url);
}else {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG);
}


}
private class MyTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
String responseBody;
try {
URL endpoint = new URL("http://"+server.getText().toString()+"/api");
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);

String requestBody = "{\"token\":\"" + token + "\", \"url\":\"" + params[0] + "\"}";

OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody.getBytes());
outputStream.flush();
outputStream.close();

int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
responseBody = readStream(inputStream);
inputStream.close();
} else {
responseBody = "HTTP Error: " + responseCode;
}
connection.disconnect();
} catch (Exception e) {
responseBody = e.toString();

}
return responseBody;
}
private String readStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toString();
}
protected void onPostExecute(String result) {
Log.i("received", "message: "+result);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.qrcodecopier.qrscanner;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qrcodecopier.qrscanner.utils.MyProperties;
import com.qrcodecopier.qrscanner.utils.Utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class RedirectionActivity extends AppCompatActivity {

Button submitBtn;
EditText urlInput;

TextView alertText, resultText;

String token;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redirection);

submitBtn = findViewById(R.id.submit);
urlInput = findViewById(R.id.urlInput);
alertText = findViewById(R.id.alertmsg);
resultText = findViewById(R.id.resultmsg);


if (getIntent().hasExtra("token")){
processToken(getIntent().getStringExtra("token"));
}else {
processToken(null);
}

submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = urlInput.getText().toString();
if (input.length() != 0){ //needs improvement
if (verifyUrl(input))
sendUrl(input);
}
}
});
}

private void showResult(String msg,boolean positive){
resultText.setText(msg);
if (positive){
resultText.setTextColor(Color.BLACK);
resultText.setBackgroundColor(Color.parseColor("#89FC00"));
}else {
resultText.setTextColor(Color.WHITE);
resultText.setBackgroundColor(Color.parseColor("#DC0073"));
}
}
private boolean verifyUrl(String url){
String regex = "^((http|https)://)?(www\\.)?[a-zA-Z0-9]+(\\.[a-zA-Z]{2,})+(/.*)?$";
return url.matches(regex);
}
private void processToken(String token){
String positiveMsg=getResources().getString(R.string.positive_msg);
String negativeMsg=getResources().getString(R.string.negative_msg);
if (token == null || notAsExpected(token)){
this.token = null;
alertText.setText(negativeMsg);
alertText.setTextColor(Color.WHITE);
alertText.setBackgroundColor(Color.parseColor("#DC0073"));
}else{
this.token = token;
alertText.setText(positiveMsg);
alertText.setTextColor(Color.BLACK);
alertText.setBackgroundColor(Color.parseColor("#89FC00"));
}
}

private boolean notAsExpected(String token){
return token.contains(" ;)({}:");
}
private void sendUrl(String url){
if (token != null){
MyTask task = new MyTask();
task.execute(url);
}else {
String error= "Error: No token included";
showResult(error,false);
}


}

@SuppressLint("StaticFieldLeak")
private class MyTask extends AsyncTask<String, Void, Pair<String,Boolean>> {

MyProperties properties = MyProperties.getInstance();
protected Pair<String,Boolean> doInBackground(String... params) {
String responseBody;
boolean success = true;
try {
URL endpoint = new URL(properties.getServerURl());
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);

String requestBody = Utils.buildRequestBody(token,params[0]);

OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody.getBytes());
outputStream.flush();
outputStream.close();

int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
responseBody="Done. ";
responseBody += readStream(inputStream);
inputStream.close();
} else {
responseBody = "HTTP Error: " + responseCode;
success = false;
}
connection.disconnect();
} catch (Exception e) {
responseBody = e.toString();
success = false;

}
return new Pair<>(responseBody,success);
}
private String readStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toString();
}
protected void onPostExecute(Pair<String, Boolean> result) {
showResult(result.first,result.second);
}
}
}
Loading