Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. package com.rmp.app1admin;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.pm.PackageManager;
  8. import android.net.Uri;
  9. import android.os.AsyncTask;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.support.v4.app.ActivityCompat;
  13. import android.support.v4.content.ContextCompat;
  14. import android.support.v7.app.AlertDialog;
  15. import android.util.Log;
  16. import android.widget.Toast;
  17.  
  18. import com.google.zxing.Result;
  19.  
  20. import java.io.BufferedReader;
  21. import java.io.BufferedWriter;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.io.OutputStream;
  26. import java.io.OutputStreamWriter;
  27. import java.net.HttpURLConnection;
  28. import java.net.MalformedURLException;
  29. import java.net.URL;
  30.  
  31. import me.dm7.barcodescanner.zxing.ZXingScannerView;
  32.  
  33. import static android.Manifest.permission.CAMERA;
  34.  
  35. public class QRCodeScannerActivity extends Activity implements ZXingScannerView.ResultHandler
  36. {
  37. private static final int REQUEST_CAMERA = 1;
  38. private ZXingScannerView mScannerView;
  39. public static final int CONNECTION_TIMEOUT=10000;
  40. public static final int READ_TIMEOUT=15000;
  41. public void onCreate(Bundle savedinstancestate)
  42. {
  43. super.onCreate(savedinstancestate);
  44. mScannerView = new ZXingScannerView(this);
  45. setContentView(mScannerView);
  46. int currentapiVersion = Build.VERSION.SDK_INT;
  47. if (currentapiVersion >= Build.VERSION_CODES.M)
  48. {
  49. if (checkPermission())
  50. {
  51. Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
  52.  
  53. }
  54. else
  55. {
  56. requestPermission();
  57. }
  58. }
  59.  
  60. }
  61. private boolean checkPermission()
  62. {
  63. return ( ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA ) == PackageManager.PERMISSION_GRANTED);
  64. }
  65. private void requestPermission()
  66. {
  67. ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
  68. }
  69. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
  70. {
  71. switch (requestCode)
  72. {
  73. case REQUEST_CAMERA:
  74. if (grantResults.length > 0)
  75. {
  76.  
  77. boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  78. if (cameraAccepted)
  79. {
  80. Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
  81. }
  82. else
  83. {
  84. Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
  85. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  86. if (shouldShowRequestPermissionRationale(CAMERA)) {
  87. showMessageOKCancel("You need to allow access to both the permissions",
  88. new DialogInterface.OnClickListener() {
  89. @Override
  90. public void onClick(DialogInterface dialog, int which) {
  91. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  92. requestPermissions(new String[]{CAMERA},
  93. REQUEST_CAMERA);
  94. }
  95. }
  96. });
  97. return;
  98. }
  99. }
  100. }
  101. }
  102. break;
  103. }
  104. }
  105. private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener)
  106. {
  107. new android.support.v7.app.AlertDialog.Builder(QRCodeScannerActivity.this)
  108. .setMessage(message)
  109. .setPositiveButton("OK", okListener)
  110. .setNegativeButton("Cancel", null)
  111. .create()
  112. .show();
  113. }
  114. @Override
  115. public void onResume()
  116. {
  117. super.onResume();
  118.  
  119. int currentapiVersion = Build.VERSION.SDK_INT;
  120. if (currentapiVersion >= Build.VERSION_CODES.M) {
  121. if (checkPermission()) {
  122. if(mScannerView == null) {
  123. mScannerView = new ZXingScannerView(this);
  124. setContentView(mScannerView);
  125. }
  126. mScannerView.setResultHandler(this);
  127. mScannerView.startCamera();
  128. } else {
  129. requestPermission();
  130. }
  131. }
  132. }
  133.  
  134. @Override
  135. public void onDestroy()
  136. {
  137. super.onDestroy();
  138. mScannerView.stopCamera();
  139. }
  140. @Override
  141. public void handleResult(Result rawResult)
  142. {
  143. final String result = rawResult.getText();
  144. Log.d("QRCodeScanner", rawResult.getText());
  145. Log.d("QRCodeScanner", rawResult.getBarcodeFormat().toString());
  146.  
  147. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  148. builder.setTitle("Scan Result");
  149. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  150. @Override
  151. public void onClick(DialogInterface dialog, int which) {
  152. mScannerView.resumeCameraPreview(QRCodeScannerActivity.this);
  153. }
  154. });
  155. builder.setNeutralButton("Verify", new DialogInterface.OnClickListener() {
  156. @Override
  157. public void onClick(DialogInterface dialog, int which)
  158. {
  159. new AsyncVerifyParking().execute(result);
  160. }
  161. });
  162. builder.setMessage(rawResult.getText());
  163. AlertDialog alert1 = builder.create();
  164. alert1.show();
  165. }
  166. private class AsyncVerifyParking extends AsyncTask<String, String, String>
  167. {
  168. ProgressDialog pdLoading = new ProgressDialog(QRCodeScannerActivity.this);
  169. HttpURLConnection conn;
  170. URL url = null;
  171.  
  172. @Override
  173. protected void onPreExecute()
  174. {
  175. super.onPreExecute();
  176. pdLoading.setMessage("\tLoading...");
  177. pdLoading.setCancelable(false);
  178. pdLoading.show();
  179. }
  180. @Override
  181. protected String doInBackground(String... params)
  182. {
  183. try
  184. {
  185. String address = getString (R.string.serveraddress);
  186. url = new URL("http://"+address+"/PDRM_WEB/verifyparking.php");
  187. }
  188. catch (MalformedURLException e)
  189. {
  190. // TODO Auto-generated catch block
  191. e.printStackTrace();
  192. return "exception";
  193. }
  194. try
  195. {
  196. conn = (HttpURLConnection)url.openConnection();
  197. conn.setReadTimeout(READ_TIMEOUT);
  198. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  199. conn.setRequestMethod("POST");
  200. conn.setDoInput(true);
  201. conn.setDoOutput(true);
  202. Uri.Builder builder = new Uri.Builder()
  203. .appendQueryParameter("qrcode", params[0]);
  204. String query = builder.build().getEncodedQuery();
  205. OutputStream os = conn.getOutputStream();
  206. BufferedWriter writer = new BufferedWriter(
  207. new OutputStreamWriter(os, "UTF-8"));
  208. writer.write(query);
  209. writer.flush();
  210. writer.close();
  211. os.close();
  212. conn.connect();
  213. }
  214. catch (IOException e1)
  215. {
  216. e1.printStackTrace();
  217. return "exception";
  218. }
  219. try
  220. {
  221. int response_code = conn.getResponseCode();
  222. if (response_code == HttpURLConnection.HTTP_OK)
  223. {
  224. InputStream input = conn.getInputStream();
  225. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  226. StringBuilder result = new StringBuilder();
  227. String line;
  228.  
  229. while ((line = reader.readLine()) != null)
  230. {
  231. result.append(line);
  232. }
  233.  
  234. return(result.toString());
  235. }
  236. else
  237. {
  238. return("unsuccessful");
  239. }
  240.  
  241. } catch (IOException e)
  242. {
  243. e.printStackTrace();
  244. return "exception";
  245. }
  246. finally {
  247. conn.disconnect();
  248. }
  249. }
  250. @Override
  251. protected void onPostExecute(String result)
  252. {
  253. pdLoading.dismiss();
  254. if(result.equalsIgnoreCase("true"))
  255. {
  256. Intent intent = new Intent(QRCodeScannerActivity.this,ScanResultActivity.class);
  257. intent.putExtra("scanresult",1);
  258. startActivity(intent);
  259. }
  260. else if (result.equalsIgnoreCase("false"))
  261. {
  262. Intent intent = new Intent(QRCodeScannerActivity.this,ScanResultActivity.class);
  263. intent.putExtra("scanresult",0);
  264. startActivity(intent);
  265.  
  266. }
  267. else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful"))
  268. {
  269. Intent intent = new Intent(QRCodeScannerActivity.this,ScanResultActivity.class);
  270. intent.putExtra("scanresult",0);
  271. startActivity(intent);
  272. }
  273. }
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement