Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ButtonTest/app/src/main/java/com/nophp/attendancescanner/MainActivity.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
223 lines (187 sloc)
6.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nophp.attendancescanner; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffColorFilter; | |
import android.graphics.drawable.Drawable; | |
import android.media.Image; | |
import android.media.MediaPlayer; | |
import android.os.StrictMode; | |
import android.support.annotation.Nullable; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v4.graphics.drawable.DrawableCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.Toast; | |
import com.google.zxing.Result; | |
import com.google.zxing.ResultPoint; | |
import com.google.zxing.client.android.BeepManager; | |
import com.google.zxing.integration.android.IntentIntegrator; | |
import com.google.zxing.integration.android.IntentResult; | |
import com.journeyapps.barcodescanner.BarcodeCallback; | |
import com.journeyapps.barcodescanner.BarcodeResult; | |
import com.journeyapps.barcodescanner.BarcodeView; | |
import com.journeyapps.barcodescanner.CaptureManager; | |
import com.journeyapps.barcodescanner.DecoratedBarcodeView; | |
import com.journeyapps.barcodescanner.camera.CameraSettings; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.nio.charset.StandardCharsets; | |
import java.util.List; | |
import java.util.Scanner; | |
public class MainActivity extends AppCompatActivity | |
{ | |
public BeepManager beep; | |
public static android.support.v4.app.FragmentManager fragman; | |
private DecoratedBarcodeView barcodeView = null; | |
CaptureManager capture; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
View decorView = getWindow().getDecorView(); | |
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; | |
decorView.setSystemUiVisibility(uiOptions); | |
barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner); | |
barcodeView.getViewFinder().setVisibility(View.VISIBLE); | |
barcodeView.setStatusText(""); | |
CameraSettings cameraSettings = new CameraSettings(); | |
cameraSettings.setRequestedCameraId(1); | |
barcodeView.getBarcodeView().setCameraSettings(cameraSettings); | |
BarcodeCallback callback = new BarcodeCallback() | |
{ | |
@Override | |
public void barcodeResult(BarcodeResult result) | |
{ | |
if (result.getText() != null) { | |
scanner(result); | |
} | |
} | |
@Override | |
public void possibleResultPoints(List<ResultPoint> resultPoints) | |
{ | |
} | |
}; | |
barcodeView.decodeContinuous(callback); | |
//setContentView(barcodeView); | |
if (android.os.Build.VERSION.SDK_INT > 9) | |
{ | |
StrictMode.ThreadPolicy policy = new | |
StrictMode.ThreadPolicy.Builder().permitAll().build(); | |
StrictMode.setThreadPolicy(policy); | |
} | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
barcodeView.resume(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
barcodeView.pause(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
capture.onDestroy(); | |
} | |
protected void scanner(BarcodeResult result) | |
{ | |
if (result != null) | |
{ | |
if (result.getText() == null) | |
{ | |
Toast.makeText(this, "Scanning Cancelled", Toast.LENGTH_LONG).show(); | |
} | |
else | |
{ | |
String text = "https://ziemni.me/rwp/scanner.php?qr=" + result.getText(); | |
System.out.println(text); | |
String textReturn = readWeb(text.toLowerCase()); | |
String errorCheckedText = errorCheck(textReturn); | |
if (errorCheckedText == "Success!") | |
{ | |
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.beep); | |
mediaPlayer.start(); | |
ImageView image = (ImageView)findViewById(R.id.circle); | |
image.setImageResource(R.drawable.green); | |
} | |
else if (errorCheckedText == "Late!") | |
{ | |
ImageView image = (ImageView)findViewById(R.id.circle); | |
image.setImageResource(R.drawable.orange); | |
} | |
else | |
{ | |
ImageView image = (ImageView)findViewById(R.id.circle); | |
image.setImageResource(R.drawable.red); | |
} | |
Toast.makeText(this, errorCheckedText, Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
public String errorCheck(String hashed) | |
{ | |
String segments[] = hashed.split("\""); | |
String text = segments[segments.length - 3]; | |
System.out.println(text); | |
String toastMessage = "?"; | |
if (text.contains("0")) | |
{ | |
toastMessage = "Success!"; | |
} | |
else if (text.contains("1")) | |
{ | |
toastMessage = "Error 1: Incorrect hash"; | |
} | |
else if (text.contains("2")) | |
{ | |
toastMessage = "Error 2: Scanner ID is not an integer"; | |
} | |
else if (text.contains("3")) | |
{ | |
toastMessage = "Error 3: Issue reaching database, try again!"; | |
} | |
else if (text.contains("4")) | |
{ | |
toastMessage = "Error 4: QR code does not match"; | |
} | |
else if (text.contains("5")) | |
{ | |
toastMessage = "Error 5: QR code expired"; | |
} | |
else if (text == "FAIL") | |
{ | |
toastMessage = "Error A1: Failure to establish internet connection"; | |
} | |
return toastMessage; | |
} | |
public String readWeb(String text) | |
{ | |
try (Scanner scanner = new Scanner(new URL(text).openStream(), StandardCharsets.UTF_8.toString())) | |
{ | |
scanner.useDelimiter("\\A"); | |
return scanner.hasNext() ? scanner.next() : ""; | |
} | |
catch (MalformedURLException e) | |
{ | |
System.out.println("Malformed URL: " + e.getMessage()); | |
} | |
catch (IOException e) | |
{ | |
System.out.println("I/O Error: " + e.getMessage()); | |
} | |
return "FAIL"; | |
} | |
} |