Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.scan;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.Toast;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import com.example.scan.apihelper.BaseApiService;
- import com.example.scan.apihelper.UtilsApi;
- import com.example.scan.apihelper.list_kursi;
- import com.google.zxing.Result;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.util.List;
- import me.dm7.barcodescanner.zxing.ZXingScannerView;
- import okhttp3.ResponseBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import static android.Manifest.permission.CAMERA;
- public class scan extends AppCompatActivity implements ZXingScannerView.ResultHandler{
- private ZXingScannerView mScannerView;
- public String scanResult;
- private BaseApiService apiInterface;
- private static final int REQUEST_CAMERA = 1;
- ProgressDialog loading;
- private Dialog customDialog;
- Context mContext;
- BaseApiService mApiService;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
- setContentView(mScannerView);
- mContext = this;
- mApiService = UtilsApi.getAPIService();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
- if (checkPermission()) {
- } else {
- requestPermission();
- }
- }
- private boolean checkPermission() {
- return (ContextCompat.checkSelfPermission(scan.this, CAMERA) ==
- PackageManager.PERMISSION_GRANTED);
- }
- private void requestPermission() {
- ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
- }
- public void displayAlertMessage(String message, DialogInterface.OnClickListener listener) {
- new AlertDialog.Builder(scan.this)
- .setMessage(message)
- .setPositiveButton("OK", listener)
- .setNegativeButton("Cancel", null)
- .create()
- .show();
- }
- @Override
- public void onResume() {
- super.onResume();
- mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
- mScannerView.startCamera(); // Start camera on resume
- }
- @Override
- public void onPause() {
- super.onPause();
- mScannerView.stopCamera(); // Stop camera on pause
- }
- @Override
- public void handleResult(Result result) {
- mScannerView.resumeCameraPreview(this);
- scanResult = result.getText();
- // createTable("" + scanResult);
- requestScan(scanResult);
- }
- private void requestScan(String hasil_scan) {
- loading = ProgressDialog.show(scan.this, null, "Harap Tunggu...", true, false);
- mApiService.scanRequest(hasil_scan)
- .enqueue(new Callback<ResponseBody>() {
- @Override
- public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
- if (response.isSuccessful()) {
- loading.dismiss();
- try {
- JSONObject jsonRESULTS = new JSONObject(response.body().string());
- if (jsonRESULTS.getString("error").equals("false")) {
- String success_msg = jsonRESULTS.getString("error_msg");
- Toast.makeText(mContext, success_msg, Toast.LENGTH_SHORT).show();
- String id_pesan = jsonRESULTS.getJSONObject("id_pesan").getString("id_pesan");
- Intent pindahActivity = new Intent(scan.this, activity_tujuan.class);
- startActivity(pindahActivity);
- } else {
- // Jika login gagal
- String error_message = jsonRESULTS.getString("error_msg");
- Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- loading.dismiss();
- }
- }
- @Override
- public void onFailure(Call<ResponseBody> call, Throwable t) {
- Log.e("debug", "onFailure: ERROR > " + t.toString());
- loading.dismiss();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement