-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
166 lines (146 loc) · 5.68 KB
/
MainActivity.java
File metadata and controls
166 lines (146 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import android.content.Intent;
import android.content.SharedPreferences;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private NfcAdapter nfcAdapter;
private SharedPreferences sharedPreferences;
private ArrayAdapter<String> adapter;
private Set<String> nfcIdSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.nfcListView);
Button useNfcButton = findViewById(R.id.useNfcButton);
Button clearButton = findViewById(R.id.clearButton);
sharedPreferences = getSharedPreferences("NFC_PREF", MODE_PRIVATE);
// Initialize NFC
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this, "NFC not supported", Toast.LENGTH_SHORT).show();
finish();
return;
}
// Load existing IDs
nfcIdSet = new HashSet<>(sharedPreferences.getStringSet("nfc_ids", new HashSet<>()));
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, new ArrayList<>(nfcIdSet));
listView.setAdapter(adapter);
// Setup list view long-click to delete
listView.setOnItemLongClickListener((parent, view, position, id) -> {
String item = adapter.getItem(position);
removeNfcId(item);
return true;
});
useNfcButton.setOnClickListener(v -> useStoredNfcIds());
clearButton.setOnClickListener(v -> clearAllIds());
}
private void handleNfcIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
String nfcId = bytesToHex(tag.getId());
addNfcId(nfcId);
}
}
private void addNfcId(String newId) {
// Create mutable copy
Set<String> newSet = new HashSet<>(nfcIdSet);
if (newSet.add(newId)) {
nfcIdSet = newSet;
sharedPreferences.edit().putStringSet("nfc_ids", nfcIdSet).apply();
adapter.clear();
adapter.addAll(nfcIdSet);
adapter.notifyDataSetChanged();
Toast.makeText(this, "NFC ID added: " + newId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "NFC ID already exists", Toast.LENGTH_SHORT).show();
}
}
private void removeNfcId(String id) {
Set<String> newSet = new HashSet<>(nfcIdSet);
if (newSet.remove(id)) {
nfcIdSet = newSet;
sharedPreferences.edit().putStringSet("nfc_ids", nfcIdSet).apply();
adapter.remove(id);
Toast.makeText(this, "ID removed: " + id, Toast.LENGTH_SHORT).show();
}
}
private void clearAllIds() {
nfcIdSet.clear();
sharedPreferences.edit().putStringSet("nfc_ids", nfcIdSet).apply();
adapter.clear();
Toast.makeText(this, "All IDs cleared", Toast.LENGTH_SHORT).show();
}
private void useStoredNfcIds() {
if (nfcIdSet.isEmpty()) {
Toast.makeText(this, "No NFC IDs stored!", Toast.LENGTH_SHORT).show();
} else {
String ids = String.join("\n", nfcIdSet);
Toast.makeText(this, "Using NFC IDs:\n" + ids, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onResume() {
super.onResume();
if (nfcAdapter != null) {
nfcAdapter.enableForegroundDispatch(this,
PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0),
null, null);
}
}
@Override
protected void onPause() {
super.onPause();
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleNfcIntent(intent);
}
private void handleNfcIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
byte[] idBytes = tag.getId();
String nfcId = bytesToHex(idBytes);
sharedPreferences.edit().putString("nfc_id", nfcId).apply();
updateNfcIdDisplay();
Toast.makeText(this, "NFC ID stored!", Toast.LENGTH_SHORT).show();
}
}
private void updateNfcIdDisplay() {
String storedId = sharedPreferences.getString("nfc_id", null);
nfcIdTextView.setText(storedId != null ?
"Stored NFC ID: " + storedId :
"No NFC ID stored");
}
private void useStoredNfcId() {
String storedId = sharedPreferences.getString("nfc_id", null);
if (storedId != null) {
// Replace this with your actual usage logic
Toast.makeText(this, "Using NFC ID: " + storedId, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No NFC ID stored!", Toast.LENGTH_SHORT).show();
}
}
// Helper method to convert byte array to hex string
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
}