SHARE
TWEET

Untitled

a guest May 24th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ordo.azurewebsites.net.ordo;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.preference.PreferenceManager;
  8. import android.view.ViewGroup;
  9. import android.widget.Toast;
  10.  
  11. import com.google.firebase.auth.FirebaseUser;
  12. import com.google.zxing.Result;
  13.  
  14. import me.dm7.barcodescanner.zxing.ZXingScannerView;
  15.  
  16. public class SimpleScannerActivity extends BaseScannerActivity implements ZXingScannerView.ResultHandler {
  17.     private ZXingScannerView mScannerView;
  18.  
  19.     @Override
  20.     public void onCreate(Bundle state) {
  21.         super.onCreate(state);
  22.         setContentView(R.layout.activity_simple_scanner);
  23.         setupToolbar();
  24.  
  25.         ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
  26.         mScannerView = new ZXingScannerView(this);
  27.         contentFrame.addView(mScannerView);
  28.     }
  29.  
  30.     @Override
  31.     public void onResume() {
  32.         super.onResume();
  33.         mScannerView.setResultHandler(this);
  34.         mScannerView.startCamera();
  35.     }
  36.  
  37.     @Override
  38.     public void onPause() {
  39.         super.onPause();
  40.         mScannerView.stopCamera();
  41.     }
  42.  
  43.     @Override
  44.     public void handleResult(Result rawResult) {
  45.        Toast.makeText(this, "Contents = " + rawResult.getText() +
  46.                 ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
  47.  
  48.         try {
  49.             int code = Integer.parseInt(rawResult.getText());
  50.             Toast.makeText(this, "p1 = " + String.valueOf(code/100) +
  51.                ", p2 = " + String.valueOf(code%100), Toast.LENGTH_SHORT).show();
  52.  
  53.             //salvem user-ul
  54.             SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
  55.             SharedPreferences.Editor editor = sharedPref.edit();
  56.             editor.putInt(getString(R.string.save_restaurant_id),code/100);
  57.             editor.putInt(getString(R.string.save_table),code%100);
  58.             editor.commit();
  59.  
  60.             Intent intent = MainActivity.newIntent(this,true);
  61.             startActivity(intent);
  62.  
  63.         }catch (Exception e){
  64.             Toast.makeText(this, "Te rog scaneaza un cod valid!", Toast.LENGTH_SHORT).show();
  65.             Intent intent = MainActivity.newIntent(this,false);
  66.             finish();
  67.             startActivity(intent);
  68.         }
  69.  
  70.  
  71.  
  72.  
  73.         finish();
  74.  
  75.  
  76.         // Note:
  77.         // * Wait 2 seconds to resume the preview.
  78.         // * On older devices continuously stopping and resuming camera preview can result in freezing the app.
  79.         // * I don't know why this is the case but I don't have the time to figure out.
  80.         Handler handler = new Handler();
  81.         handler.postDelayed(new Runnable() {
  82.             @Override
  83.             public void run() {
  84.                 mScannerView.resumeCameraPreview(SimpleScannerActivity.this);
  85.             }
  86.         }, 2000);
  87.     }
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top