Advertisement
Guest User

Scan

a guest
Nov 18th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. package com.example.scan;
  2.  
  3. import android.app.Dialog;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.pm.PackageManager;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.view.Window;
  14. import android.widget.Button;
  15. import android.widget.Toast;
  16.  
  17. import androidx.appcompat.app.AlertDialog;
  18. import androidx.appcompat.app.AppCompatActivity;
  19. import androidx.core.app.ActivityCompat;
  20. import androidx.core.content.ContextCompat;
  21. import androidx.recyclerview.widget.LinearLayoutManager;
  22. import androidx.recyclerview.widget.RecyclerView;
  23.  
  24. import com.example.scan.apihelper.BaseApiService;
  25. import com.example.scan.apihelper.UtilsApi;
  26. import com.example.scan.apihelper.list_kursi;
  27. import com.google.zxing.Result;
  28.  
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31.  
  32. import java.io.IOException;
  33. import java.util.List;
  34.  
  35. import me.dm7.barcodescanner.zxing.ZXingScannerView;
  36. import okhttp3.ResponseBody;
  37. import retrofit2.Call;
  38. import retrofit2.Callback;
  39. import retrofit2.Response;
  40.  
  41. import static android.Manifest.permission.CAMERA;
  42.  
  43. public class scan extends AppCompatActivity implements ZXingScannerView.ResultHandler{
  44. private ZXingScannerView mScannerView;
  45. public String scanResult;
  46. private BaseApiService apiInterface;
  47. private static final int REQUEST_CAMERA = 1;
  48. ProgressDialog loading;
  49. private Dialog customDialog;
  50. Context mContext;
  51. BaseApiService mApiService;
  52.  
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
  57. setContentView(mScannerView);
  58. mContext = this;
  59. mApiService = UtilsApi.getAPIService();
  60.  
  61. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
  62. if (checkPermission()) {
  63. } else {
  64. requestPermission();
  65. }
  66. }
  67.  
  68. private boolean checkPermission() {
  69. return (ContextCompat.checkSelfPermission(scan.this, CAMERA) ==
  70. PackageManager.PERMISSION_GRANTED);
  71. }
  72.  
  73. private void requestPermission() {
  74. ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
  75. }
  76.  
  77.  
  78. public void displayAlertMessage(String message, DialogInterface.OnClickListener listener) {
  79. new AlertDialog.Builder(scan.this)
  80. .setMessage(message)
  81. .setPositiveButton("OK", listener)
  82. .setNegativeButton("Cancel", null)
  83. .create()
  84. .show();
  85. }
  86.  
  87. @Override
  88. public void onResume() {
  89. super.onResume();
  90. mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
  91. mScannerView.startCamera(); // Start camera on resume
  92. }
  93.  
  94. @Override
  95. public void onPause() {
  96. super.onPause();
  97. mScannerView.stopCamera(); // Stop camera on pause
  98. }
  99.  
  100. @Override
  101. public void handleResult(Result result) {
  102. mScannerView.resumeCameraPreview(this);
  103. scanResult = result.getText();
  104. // createTable("" + scanResult);
  105.  
  106. requestScan(scanResult);
  107. }
  108.  
  109. private void requestScan(String hasil_scan) {
  110. loading = ProgressDialog.show(scan.this, null, "Harap Tunggu...", true, false);
  111. mApiService.scanRequest(hasil_scan)
  112. .enqueue(new Callback<ResponseBody>() {
  113. @Override
  114. public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
  115. if (response.isSuccessful()) {
  116. loading.dismiss();
  117. try {
  118. JSONObject jsonRESULTS = new JSONObject(response.body().string());
  119. if (jsonRESULTS.getString("error").equals("false")) {
  120. String success_msg = jsonRESULTS.getString("error_msg");
  121. Toast.makeText(mContext, success_msg, Toast.LENGTH_SHORT).show();
  122. String id_pesan = jsonRESULTS.getJSONObject("id_pesan").getString("id_pesan");
  123. Intent pindahActivity = new Intent(scan.this, activity_tujuan.class);
  124. startActivity(pindahActivity);
  125. } else {
  126. // Jika login gagal
  127. String error_message = jsonRESULTS.getString("error_msg");
  128. Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
  129. }
  130. } catch (JSONException e) {
  131. e.printStackTrace();
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. } else {
  136. loading.dismiss();
  137. }
  138. }
  139.  
  140. @Override
  141. public void onFailure(Call<ResponseBody> call, Throwable t) {
  142. Log.e("debug", "onFailure: ERROR > " + t.toString());
  143. loading.dismiss();
  144. }
  145. });
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement