Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package validate.com.merchant;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Typeface;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.text.Editable;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.zxing.integration.android.IntentIntegrator;
- import com.google.zxing.integration.android.IntentResult;
- import com.validate.R;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import validate.com.Interfaces.VolleyResponseCallBack;
- import validate.com.QR_Scanner.SimpleScannerActivity;
- import validate.com.Util.Utils;
- import validate.com.WebService_API.API;
- import validate.com.WebService_API.AppConstants;
- import validate.com.WebService_API.ConnectionExecutor;
- import validate.com.WebService_API.JsonParser;
- import validate.com.WebService_API.VolleyRequestHandler;
- import validate.com.Model.VoucherItem;
- import validate.com.Model.validateItem;
- /**
- * Created by Mina on 06/06/2015.
- */
- public class validate_fragment extends Fragment implements View.OnClickListener, VolleyResponseCallBack {
- private static final String TAG = "validate_fragment";
- private static int QR_REQUEST_CODE = 100;
- private Button scanqr, check, button0, button1, button2, button3,
- button4, button5, button6, button7, button8, button9, clearbtn, delbtn;
- boolean focus1 = false, focus2 = false, focus3 = false;
- public EditText mTextView, mTextView2, mTextView3;
- private static final int MAX_LENGTH = 3;
- InputMethodManager imm;
- LinearLayout codeLinearLayout;
- private API api;
- private TextView title;
- private Typeface tf;
- private VolleyRequestHandler _volleyRequestHandler;
- public static validate_fragment newInstance(){
- validate_fragment fragment = new validate_fragment();
- return fragment;
- }
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- setRetainInstance(true);
- imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- super.onCreate(savedInstanceState);
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.validate_frag, null, false);
- setupUI(view);
- api = new API(getActivity().getApplicationContext());
- _volleyRequestHandler = new VolleyRequestHandler(getActivity());
- codeLinearLayout = (LinearLayout) view.findViewById(R.id.codeLinear);
- return view;
- }
- public void ScanQR() {
- IntentIntegrator integrator = new IntentIntegrator(getActivity());
- integrator.setPrompt("place QR code in window to validate");
- integrator.setCaptureActivity(SimpleScannerActivity.class);
- integrator.forSupportFragment(this).initiateScan();
- }
- public void get_code() {
- if (mTextView.getText().toString() != null && mTextView2.getText().toString() != null && mTextView3.getText().toString() != null) {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(mTextView.getText().toString());
- stringBuilder.append(mTextView2.getText().toString());
- stringBuilder.append(mTextView3.getText().toString());
- check_method(stringBuilder.toString());
- }
- }
- private void setupUI(View view) {
- tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Black.ttf");
- title = (TextView) view.findViewById(R.id.vouchertxt);
- title.setTypeface(tf);
- //declare textviews
- mTextView = (EditText) view.findViewById(R.id.code1);
- mTextView.requestFocus();
- mTextView.setInputType(0);
- mTextView2 = (EditText) view.findViewById(R.id.code2);
- mTextView2.setInputType(0);
- mTextView3 = (EditText) view.findViewById(R.id.code3);
- mTextView3.setInputType(0);
- // declare function buttons
- clearbtn = (Button) view.findViewById(R.id.buttonclear);
- clearbtn.setOnClickListener(this);
- delbtn = (Button) view.findViewById(R.id.buttonDel);
- delbtn.setOnClickListener(this);
- check = (Button) view.findViewById(R.id.buttoncheck);
- check.setOnClickListener(this);
- scanqr = (Button) view.findViewById(R.id.scanqr);
- scanqr.setOnClickListener(this);
- //declare pad button
- button0 = (Button) view.findViewById(R.id.button0);
- button0.setOnClickListener(this);
- button1 = (Button) view.findViewById(R.id.button1);
- button1.setOnClickListener(this);
- button2 = (Button) view.findViewById(R.id.button2);
- button2.setOnClickListener(this);
- button3 = (Button) view.findViewById(R.id.button3);
- button3.setOnClickListener(this);
- button4 = (Button) view.findViewById(R.id.button4);
- button4.setOnClickListener(this);
- button5 = (Button) view.findViewById(R.id.button5);
- button5.setOnClickListener(this);
- button6 = (Button) view.findViewById(R.id.button6);
- button6.setOnClickListener(this);
- button7 = (Button) view.findViewById(R.id.button7);
- button7.setOnClickListener(this);
- button8 = (Button) view.findViewById(R.id.button8);
- button8.setOnClickListener(this);
- button9 = (Button) view.findViewById(R.id.button9);
- button9.setOnClickListener(this);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d("TAG", "DATA");
- IntentResult ScanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
- if (ScanResult != null) {
- String content = ScanResult.getContents();
- check_method(content);
- } else {
- Toast.makeText(getActivity(),
- "No scan data received!", Toast.LENGTH_SHORT).show();
- }
- }
- private void check_method(String code) {
- if (api.isNetworkConnected(getActivity().getApplicationContext())) {
- String merchant_id = api.getSetting(AppConstants.TAG_ID);
- String token = api.getSetting(AppConstants.TAG_TOKEN);
- try {
- Utils.ShowLoader(getActivity());
- checkVoucher(code , token);
- //api.Check_Voucher_Code(code, web_callback, true, token);
- } catch (Exception e) {
- e.getMessage();
- }
- } else {
- Toast.makeText(getActivity().getApplicationContext(), getActivity().getApplicationContext().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
- }
- }
- private ConnectionExecutor.onHttpRequestListener web_callback = new ConnectionExecutor.onHttpRequestListener() {
- @Override
- public void onHttpResult(ConnectionExecutor.HTTPResponse response, String send_data) {
- Utils.HideLoader();
- if (response.response == ConnectionExecutor.ResponseType.SUCCESS) {
- ArrayList<VoucherItem> items = new ArrayList<>();
- items.clear();
- try {
- JSONArray jsonArray = new JSONArray();
- ArrayList<validateItem> validations_item = new ArrayList<>();
- JSONObject jsonObject = (JSONObject) response.data;
- JsonParser jsonParser = new JsonParser();
- jsonArray.put(jsonObject);
- items = jsonParser.get_merchant_items(jsonArray);
- VoucherItem item = items.get(0);
- validations_item = jsonParser.validations_item;
- if (item != null) {
- switch (item.getStatus()) {
- case "1":
- valid_voucher(item, validations_item);
- break;
- case "2":
- Utils.showAlertDialog(getActivity(), "Alert", "Don't Accept Voucher Validated Before", false, false);
- break;
- case "0":
- Utils.showAlertDialog(getActivity(), "Error", "Voucher Invalid", false, false);
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- if ((response.value).equals(AppConstants.ERROR_AUTHENTICATE)) {
- Utils.showAlertDialog(getActivity(), "Action Required", "Your token expired , please re-login", false, true);
- } else {
- Toast.makeText(getActivity().getApplicationContext(), response.value, Toast.LENGTH_LONG).show();
- }
- }
- }
- };
- private void valid_voucher(VoucherItem item, ArrayList<validateItem> validations_item) {
- Intent intent = new Intent(getActivity().getApplicationContext(), validate_activity.class);
- intent.putExtra(AppConstants.TAG_SEND_VOUCHER, item);
- Bundle bundle = new Bundle();
- bundle.putSerializable(AppConstants.TAG_SEND_VALIDATIONS, validations_item);
- intent.putExtras(bundle);
- getActivity().startActivity(intent);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button0:
- writeNumber(48);
- break;
- case R.id.button1:
- writeNumber(49);
- break;
- case R.id.button2:
- writeNumber(50);
- break;
- case R.id.button3:
- writeNumber(51);
- break;
- case R.id.button4:
- writeNumber(52);
- break;
- case R.id.button5:
- writeNumber(53);
- break;
- case R.id.button6:
- writeNumber(54);
- break;
- case R.id.button7:
- writeNumber(55);
- break;
- case R.id.button8:
- writeNumber(56);
- break;
- case R.id.button9:
- writeNumber(57);
- break;
- case R.id.buttonclear:
- clear(codeLinearLayout);
- break;
- case R.id.buttoncheck:
- get_code();
- break;
- case R.id.scanqr:
- ScanQR();
- break;
- case R.id.buttonDel:
- deletelastnumber();
- break;
- }
- }
- private void checkVoucher(String code , String token) throws JSONException{
- JSONObject jsonObject = new JSONObject();
- jsonObject.put(AppConstants.TAG_CODE, code);
- _volleyRequestHandler.HandleRequests(VolleyRequestHandler.CHECK_VOUCHER_INT , jsonObject ,this , token );
- }
- private void writeNumber(int n) {
- View view = getActivity().getWindow().getCurrentFocus();
- if (view == null && view.getClass() != EditText.class) return;
- EditText mEditText = (EditText) view;
- Editable mEditable = mEditText.getText();
- int start = mEditText.getSelectionStart();
- if (start == 3) {
- View nextFocus = mEditText.focusSearch(View.FOCUS_UP);
- if (nextFocus != null) {
- nextFocus.requestFocus();
- EditText mEditText2 = (EditText) nextFocus;
- Editable mEditable2 = mEditText2.getText();
- int start2 = mEditText2.getSelectionStart();
- mEditable2.insert(start2, Character.toString((char) n));
- }
- } else {
- mEditable.insert(start, Character.toString((char) n));
- }
- }
- private void deletelastnumber() {
- View view = getActivity().getWindow().getCurrentFocus();
- if (view == null && view.getClass() != EditText.class) return;
- EditText mEditText = (EditText) view;
- Editable mEditable = mEditText.getText();
- int start = mEditText.getSelectionEnd();
- if (start == 0) {
- View previousFocus = mEditText.focusSearch(View.FOCUS_DOWN);
- if (previousFocus != null) previousFocus.requestFocus();
- } else if (mEditable != null && start > 0) {
- mEditable.delete(start - 1, start);
- }
- }
- private void clear(ViewGroup viewGroup) {
- for (int i = 0; i < viewGroup.getChildCount(); i++) {
- View v = viewGroup.getChildAt(i);
- if (v instanceof EditText) {
- ((EditText) v).getText().clear();
- }
- }
- if (mTextView != null) {
- mTextView.requestFocus();
- }
- }
- @Override
- public void RequestSuccess(Object response) {
- Utils.HideLoader();
- Toast.makeText(getActivity() , response.toString() , Toast.LENGTH_LONG).show();
- }
- @Override
- public void RequestFailed(String error) {
- Utils.HideLoader();
- Toast.makeText(getActivity() , error , Toast.LENGTH_LONG).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement