0% found this document useful (0 votes)
6 views

MainFragment JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

MainFragment JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.stream.

businessinventorymanagerapp;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.activity.result.ActivityResultCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanIntentResult;
import com.journeyapps.barcodescanner.ScanOptions;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.stream.businessinventorymanagerapp.databinding.ActivityMainBinding;
import com.stream.businessinventorymanagerapp.databinding.FragmentMainBinding;

/*
* A simple {@link Fragment} subclass.
* Use the {@link MainFragment#newInstance} factory method to
* create an instance of this fragment.
*/

public class MainFragment extends Fragment {

FragmentMainBinding fragmentMainBinding;

private final androidx.activity.result.ActivityResultLauncher<ScanOptions>


scannerLauncher =
registerForActivityResult(new ScanContract(), new
ActivityResultCallback<ScanIntentResult>() {
@Override
public void onActivityResult(ScanIntentResult resultOFQRScan) {
if (resultOFQRScan.getContents() == null)
{
Toast.makeText(getContext(), "Cancelled Scanning",
Toast.LENGTH_SHORT).show();
}
else
{
openUPIScannerGooglePay(resultOFQRScan.getContents());
openUPIScannerPhonePe(resultOFQRScan.getContents());
openUPIScannerPaytm(resultOFQRScan.getContents());
}
}
});

/*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters


private String mParam1;
private String mParam2;

*/

public MainFragment() {
// Required empty public constructor
}

/*
// TODO: Rename and change types and number of parameters
public static MainFragment newInstance(String param1, String param2) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// if (getArguments() != null) {
// mParam1 = getArguments().getString(ARG_PARAM1);
// mParam2 = getArguments().getString(ARG_PARAM2);
// }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

fragmentMainBinding.scanQRButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {

ScanOptions options = new ScanOptions();


options.setDesiredBarcodeFormats(ScanOptions.QR_CODE);
options.setBeepEnabled(false);
options.setTorchEnabled(false);
options.setOrientationLocked(true);
scannerLauncher.launch(options);

}
});

// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_main, container, false);

}
private void openUPIScannerGooglePay(String result) {

Uri gpayUri = Uri.parse(result);


// Create an intent with ACTION_VIEW to open Google Pay
Intent intentGpay = new Intent(Intent.ACTION_VIEW, gpayUri);
intentGpay.setPackage("com.google.android.apps.nbu.paisa.user"); // Common
package name for Google Pay in India

if (intentGpay.resolveActivity(requireActivity().getPackageManager()) !=
null) {
startActivity(intentGpay);
}

private void openUPIScannerPhonePe(String result) {

Uri phonePeUri = Uri.parse(result);


// Create an Intent to open PhonePe
Intent intentPhonePe = new Intent(Intent.ACTION_VIEW, phonePeUri);
intentPhonePe.setPackage("com.phonepe.app"); // PhonePe package name

if (intentPhonePe.resolveActivity(requireActivity().getPackageManager()) !=
null) {
startActivity(intentPhonePe);
}

private void openUPIScannerPaytm(String result) {

Uri paytmUri = Uri.parse(result);


// Create an Intent to open Paytm
Intent intentPaytm = new Intent(Intent.ACTION_VIEW, paytmUri);
intentPaytm.setPackage("net.one97.paytm"); // Paytm package name

if (intentPaytm.resolveActivity(requireActivity().getPackageManager()) !=
null) {
startActivity(intentPaytm);
}

You might also like