@@ -15,32 +15,14 @@ import android.widget.Toast
15
15
import androidx.core.app.ActivityCompat
16
16
import androidx.core.content.ContextCompat
17
17
import androidx.fragment.app.Fragment
18
+ import com.example.spendwise.adapter.TransactionAdapter
19
+ import com.example.spendwise.model.TransactionData
18
20
19
- // TODO: Rename parameter arguments, choose names that match
20
- // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
21
- // private const val ARG_PARAM1 = "param1"
22
- // private const val ARG_PARAM2 = "param2"
23
21
24
- /* *
25
- * A simple [Fragment] subclass.
26
- * Use the [Messages.newInstance] factory method to
27
- * create an instance of this fragment.
28
- */
29
22
class Messages : Fragment () {
30
23
31
24
private val SMS_PERMISSION_CODE = 101
32
25
33
- // TODO: Rename and change types of parameters
34
- // private var param1: String? = null
35
- // private var param2: String? = null
36
-
37
- // override fun onCreate(savedInstanceState: Bundle?) {
38
- // super.onCreate(savedInstanceState)
39
- // arguments?.let {
40
- // param1 = it.getString(ARG_PARAM1)
41
- // param2 = it.getString(ARG_PARAM2)
42
- // }
43
- // }
44
26
45
27
override fun onCreateView (
46
28
inflater : LayoutInflater , container : ViewGroup ? ,
@@ -135,11 +117,76 @@ class Messages : Fragment() {
135
117
val adapter = SmsAdapter (requireContext(), R .layout.list_item_sms, smsList)
136
118
listView.adapter = adapter
137
119
}
120
+ private fun displaySms2 (listView : ListView , showBankMessages : Boolean ) {
121
+ val transactions = mutableListOf<TransactionData >()
122
+
123
+ val cursor: Cursor ? = requireContext().contentResolver.query(
124
+ Telephony .Sms .CONTENT_URI ,
125
+ null ,
126
+ Telephony .Sms .TYPE + " = ?" ,
127
+ arrayOf(Telephony .Sms .MESSAGE_TYPE_INBOX .toString()),
128
+ Telephony .Sms .DATE + " DESC"
129
+ )
130
+
131
+ cursor?.let {
132
+ val addressColumn = it.getColumnIndex(Telephony .Sms .ADDRESS )
133
+ val bodyColumn = it.getColumnIndex(Telephony .Sms .BODY )
134
+ val dateColumn = it.getColumnIndex(Telephony .Sms .DATE )
135
+
136
+ while (it.moveToNext()) {
137
+ val sender = it.getString(addressColumn)
138
+ val message = it.getString(bodyColumn)
139
+ val dateMillis = it.getLong(dateColumn)
140
+ val date = DateFormat .format(" dd-MM-yyyy" , dateMillis).toString()
141
+ val time = DateFormat .format(" hh:mm a" , dateMillis).toString()
142
+
143
+ if (! showBankMessages || isBankMessage(sender, message)) {
144
+ val transaction = parseTransaction(sender, message, date, time)
145
+ if (transaction != null ) {
146
+ transactions.add(transaction)
147
+ }
148
+ }
149
+ }
150
+ it.close()
151
+ }
152
+
153
+ // val adapter = TransactionAdapter(requireContext(), R.layout.list_item_sms, transactions)
154
+ // val adapter = TransactionAdapter(requireContext(), transactions)
155
+ val adapter = TransactionAdapter (requireContext(), transactions)
156
+ listView.adapter = adapter
157
+ }
158
+
159
+ // private
160
+ fun parseTransaction (sender : String , message : String , date : String , time : String ): TransactionData ? {
161
+ // Attempt to extract amount and determine if it's a credit or debit
162
+ val amountRegex = Regex (" \\ b(?:Rs\\ .?|INR)?\\ s?(\\ d+(?:\\ .\\ d{1,2})?)\\ b" , RegexOption .IGNORE_CASE )
163
+ val creditKeywords = listOf (" credited" , " credit" , " deposit" )
164
+ val debitKeywords = listOf (" debited" , " debit" , " withdrawal" )
165
+
166
+ val amountMatch = amountRegex.find(message)
167
+ val isCredit = creditKeywords.any { message.contains(it, ignoreCase = true ) }
168
+ val isDebit = debitKeywords.any { message.contains(it, ignoreCase = true ) }
169
+
170
+ return if (amountMatch != null && (isCredit || isDebit)) {
171
+ val amount = amountMatch.groupValues[1 ]
172
+ TransactionData (
173
+ sender = sender,
174
+ messageBody = message,
175
+ amount = amount,
176
+ date = date,
177
+ time = time,
178
+ isCredit = isCredit
179
+ )
180
+ } else {
181
+ null
182
+ }
183
+ }
184
+
138
185
private fun isBankMessage (sender : String , message : String ): Boolean {
139
186
// Common patterns for bank sender IDs
140
187
val bankPatterns = listOf (
141
188
Regex (" ^[A-Za-z]{2,}-\\ d{2,}$" ), // Example: "AX-12345"
142
- Regex (" ^[A-Za-z]{3,}$" ), // Example: "ICICI", "SBI"
189
+ Regex (" ^[A-Za-z]{3,}$" ), // Example: "ICICI", "SBI"
143
190
Regex (" ^[A-Za-z]{2,}\\ d{1,}$" ), // Example: "ICICI1", "HDFC123"
144
191
Regex (" ^[A-Za-z]+\\ d+$" ) // Example: "Bank123", "MyBank456"
145
192
)
@@ -150,95 +197,31 @@ class Messages : Fragment() {
150
197
" CITIBANK" , " BOB" , " KOTAKBANK" , " YESBANK" , " IDFCFIRST"
151
198
)
152
199
153
- // Keywords indicating financial transactions
154
- val transactionKeywords = listOf (" credited " , " debited " , " withdrawn " , " deposited " , " transferred " )
155
-
156
- // Regex pattern to detect numeric amounts (e.g. , "Rs. 5000 ", "$300 ", "INR 2500")
157
- val amountPattern = Regex ( " \\ b(?:Rs \\ .?|INR| \\ $)? \\ s? \\ d{1,}(?:, \\ d{3})*(?: \\ . \\ d{1,2})? \\ b " )
200
+ // Keywords commonly found in bank messages
201
+ val bankKeywords = listOf (
202
+ " transaction " , " debit " , " credit " , " account " , " balance " ,
203
+ " payment " , " bank " , " debited " , " credited " ,
204
+ )
158
205
159
206
// Check if sender contains only numbers (not a bank message)
160
- if (sender.matches(Regex (" ^\\ d+$" ))) {
161
- return false
162
- }
207
+ // if (sender.matches(Regex("^\\d+$"))) {
208
+ // return false
209
+ // }
163
210
164
211
// Check if sender matches any pattern
165
212
val matchesPattern = bankPatterns.any { pattern -> sender.matches(pattern) }
166
213
167
214
// Check if sender is in the known banks list (ignoring case)
168
215
val isKnownBank = knownBanks.any { bank -> sender.equals(bank, ignoreCase = true ) }
169
216
170
- // Check if message contains transaction -related keywords
171
- val containsTransactionKeywords = transactionKeywords .any { keyword ->
217
+ // Check if message contains any bank -related keywords
218
+ val containsBankKeywords = bankKeywords .any { keyword ->
172
219
message.contains(keyword, ignoreCase = true )
173
220
}
174
221
175
- // Check if message contains a numeric amount
176
- val containsAmount = amountPattern.containsMatchIn(message)
177
-
178
- // Return true if sender is valid and message indicates a financial transaction
179
- return (matchesPattern || isKnownBank) && containsTransactionKeywords && containsAmount
222
+ // Return true if either sender or message matches bank criteria
223
+ return matchesPattern || isKnownBank || containsBankKeywords
180
224
}
181
225
182
- //
183
- // private fun isBankMessage(sender: String, message: String): Boolean {
184
- // // Common patterns for bank sender IDs
185
- // val bankPatterns = listOf(
186
- // Regex("^[A-Za-z]{2,}-\\d{2,}$"), // Example: "AX-12345"
187
- // Regex("^[A-Za-z]{3,}$"), // Example: "ICICI", "SBI"
188
- // Regex("^[A-Za-z]{2,}\\d{1,}$"), // Example: "ICICI1", "HDFC123"
189
- // Regex("^[A-Za-z]+\\d+$") // Example: "Bank123", "MyBank456"
190
- // )
191
- //
192
- // // Custom sender names (specific to region or known banks)
193
- // val knownBanks = listOf(
194
- // "ICICIBANK", "SBIBANK", "HDFCBANK", "AXISBANK", "PNB",
195
- // "CITIBANK", "BOB", "KOTAKBANK", "YESBANK", "IDFCFIRST"
196
- // )
197
- //
198
- // // Keywords commonly found in bank messages
199
- // val bankKeywords = listOf(
200
- // "transaction", "debit", "credit", "account", "balance",
201
- // "payment", "statement", "otp", "loan", "bank"
202
- // )
203
- //
204
- // // Check if sender contains only numbers (not a bank message)
205
- // if (sender.matches(Regex("^\\d+$"))) {
206
- // return false
207
- // }
208
- //
209
- // // Check if sender matches any pattern
210
- // val matchesPattern = bankPatterns.any { pattern -> sender.matches(pattern) }
211
- //
212
- // // Check if sender is in the known banks list (ignoring case)
213
- // val isKnownBank = knownBanks.any { bank -> sender.equals(bank, ignoreCase = true) }
214
- //
215
- // // Check if message contains any bank-related keywords
216
- // val containsBankKeywords = bankKeywords.any { keyword ->
217
- // message.contains(keyword, ignoreCase = true)
218
- // }
219
- //
220
- // // Return true if either sender or message matches bank criteria
221
- // return matchesPattern || isKnownBank || containsBankKeywords
222
- // }
223
-
224
-
225
- // companion object {
226
- // /**
227
- // * Use this factory method to create a new instance of
228
- // * this fragment using the provided parameters.
229
- // *
230
- // * @param param1 Parameter 1.
231
- // * @param param2 Parameter 2.
232
- // * @return A new instance of fragment Messages.
233
- // */
234
- // // TODO: Rename and change types and number of parameters
235
- // @JvmStatic
236
- // fun newInstance(param1: String, param2: String) =
237
- // Messages().apply {
238
- // arguments = Bundle().apply {
239
- // putString(ARG_PARAM1, param1)
240
- // putString(ARG_PARAM2, param2)
241
- // }
242
- // }
243
- // }
226
+
244
227
}
0 commit comments