harbor run -t openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-1206harbor download openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-1206solution file
package de.androidcrypto.nfchcendefemulator;
import static android.content.Context.VIBRATOR_SERVICE;
import android.content.Context;
import android.content.Intent;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ReceiveExtendedFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ReceiveExtendedFragment extends Fragment implements NfcAdapter.ReaderCallback {
// 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 ReceiveExtendedFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ReceiveFragment.
*/
// TODO: Rename and change types and number of parameters
public static ReceiveExtendedFragment newInstance(String param1, String param2) {
ReceiveExtendedFragment fragment = new ReceiveExtendedFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
TextView readResult;
private NfcAdapter mNfcAdapter;
String dumpExportString = "";
String tagIdString = "";
String tagTypeString = "";
private static final int REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE = 100;
Context contextSave;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
mNfcAdapter = NfcAdapter.getDefaultAdapter(this.getContext());
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
readResult = getView().findViewById(R.id.tvReceiveReadResult);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_receive_extended, container, false);
}
// This method is running in another thread when a card is discovered
// !!!! This method cannot cannot direct interact with the UI Thread
// Use `runOnUiThread` method to change the UI from this method
@Override
public void onTagDiscovered(Tag tag) {
// Read and or write to Tag here to the appropriate Tag Technology type class
// in this example the card should be an Ndef Technology Type
System.out.println("NFC tag discovered");
getActivity().runOnUiThread(() -> {
readResult.setText("");
});
IsoDep isoDep = null;
writeToUiAppend(readResult, "Tag found");
String[] techList = tag.getTechList();
for (int i = 0; i < techList.length; i++) {
writeToUiAppend(readResult, "TechList: " + techList[i]);
}
String tagId = Utils.bytesToHex(tag.getId());
writeToUiAppend(readResult, "TagId: " + tagId);
try {
isoDep = IsoDep.get(tag);
if (isoDep != null) {
getActivity().runOnUiThread(() -> {
Toast.makeText(this.getContext(),
"NFC tag is IsoDep compatible",
Toast.LENGTH_SHORT).show();
});
isoDep.connect();
dumpExportString = "";
getActivity().runOnUiThread(() -> {
//readResult.setText("");
});
writeToUiAppend(readResult, "IsoDep reading");
String nfcaContent = "IsoDep reading" + "\n";
// now we run the select command with AID
String nfcHceNdefAid = "D2760000850101";
byte[] aid = Utils.hexStringToByteArray(nfcHceNdefAid);
byte[] command = selectApdu(aid);
byte[] responseSelect = isoDep.transceive(command);
writeToUiAppend(readResult, "selectApdu with AID: " + Utils.bytesToHex(command));
writeToUiAppend(readResult, "selectApdu response: " + Utils.bytesToHex(responseSelect));
if (responseSelect == null) {
writeToUiAppend(readResult, "selectApdu with AID fails (null)");
} else {
writeToUiAppend(readResult, "responseSelect length: " + responseSelect.length + " data: " + Utils.bytesToHex(responseSelect));
System.out.println("responseSelect: " + Utils.bytesToHex(responseSelect));
}
if (!Utils.isSucceed(responseSelect)) {
writeToUiAppend(readResult, "responseSelect is not 90 00 - aborted");
System.out.println("responseSelect is not 90 00 - aborted ");
return;
}
// sending cc select = get the capability container
String selectCapabilityContainer = "00a4000c02e103";
command = Utils.hexStringToByteArray(selectCapabilityContainer);
byte[] responseSelectCc = isoDep.transceive(command);
writeToUiAppend(readResult, "select CC: " + Utils.bytesToHex(command));
writeToUiAppend(readResult, "select CC response: " + Utils.bytesToHex(responseSelectCc));
writeToUiAppend(readResult, "responseSelect length: " + responseSelectCc.length + " data: " + Utils.bytesToHex(responseSelectCc));
System.out.println("responseSelectCc: " + Utils.bytesToHex(responseSelectCc));
if (!Utils.isSucceed(responseSelectCc)) {
writeToUiAppend(readResult, "responseSelectCc is not 90 00 - aborted");
System.out.println("responseSelectCc is not 90 00 - aborted ");
return;
}
// Sending ReadBinary from CC...
String sendBinareFromCc = "00b000000f";
command = Utils.hexStringToByteArray(sendBinareFromCc);
byte[] responseSendBinaryFromCc = isoDep.transceive(command);
writeToUiAppend(readResult, "sendBinaryFromCc: " + Utils.bytesToHex(command));
writeToUiAppend(readResult, "sendBinaryFromCc response: " + Utils.bytesToHex(responseSendBinaryFromCc));
writeToUiAppend(readResult, "sendBinaryFromCc response length: " + responseSendBinaryFromCc.length + " data: " + Utils.bytesToHex(responseSendBinaryFromCc));
System.out.println("sendBinaryFromCc response: " + Utils.bytesToHex(responseSendBinaryFromCc));
if (!Utils.isSucceed(responseSendBinaryFromCc)) {
writeToUiAppend(readResult, "responseSendBinaryFromCc is not 90 00 - aborted");
System.out.println("responseSendBinaryFromCc is not 90 00 - aborted ");
return;
}
// Capability Container header:
byte[] capabilityContainerHeader = Arrays.copyOfRange(responseSendBinaryFromCc, 0, responseSendBinaryFromCc.length - 2);
writeToUiAppend(readResult, "capabilityContainerHeader length: " + capabilityContainerHeader.length + " data: " + Utils.bytesToHex(capabilityContainerHeader));
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py