| | |
| | | import androidx.work.WorkManager |
| | | import com.example.firstapp.activity.LoginActivity |
| | | import com.example.firstapp.adapter.MyAdapter |
| | | import com.example.firstapp.core.Core |
| | | import com.example.firstapp.database.entity.Code |
| | | import com.example.firstapp.database.entity.Msg |
| | | import com.example.firstapp.database.service.RetrofitClient |
| | | import com.example.firstapp.entity.Rule |
| | | import com.example.firstapp.ui.home.HomeViewModel |
| | | import com.example.firstapp.utils.Log |
| | | import com.example.firstapp.workers.KeywordUpdateWorker |
| | | import kotlinx.coroutines.CoroutineScope |
| | | import kotlinx.coroutines.Dispatchers |
| | | import kotlinx.coroutines.launch |
| | | import java.text.SimpleDateFormat |
| | | import java.time.LocalDateTime |
| | | import java.time.format.DateTimeFormatter |
| | | import java.util.Calendar |
| | | import java.util.Date |
| | | import java.util.Locale |
| | | import java.util.concurrent.TimeUnit |
| | | |
| | | class MainActivity : AppCompatActivity() { |
| | | |
| | | // 安全防护关键词数组 |
| | | private var securityKeywordsList = emptyList<String>() |
| | | |
| | | private lateinit var binding: ActivityMainBinding |
| | | |
| | |
| | | private lateinit var adapter: MyAdapter |
| | | private lateinit var homeViewModel: HomeViewModel |
| | | |
| | | |
| | | // 权限请求代码 |
| | | private val permissionRequest = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> |
| | | if (isGranted) { |
| | | // 权限授予后注册短信监听器 |
| | | registerSmsReceiver() |
| | | syncRecentSms() |
| | | } else { |
| | | // 权限拒绝,提示用户 |
| | | Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show() |
| | | private val multiplePermissionRequest = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> |
| | | when { |
| | | permissions.getOrDefault(Manifest.permission.RECEIVE_SMS, false) && |
| | | permissions.getOrDefault(Manifest.permission.READ_SMS, false) -> { |
| | | // 两个权限都获得授权 |
| | | registerSmsReceiver() |
| | | // syncRecentSms() |
| | | initializeSecurityKeywords() |
| | | } |
| | | else -> { |
| | | // 有权限被拒绝 |
| | | Toast.makeText(this, "需要短信读取和接收权限才能正常使用功能", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | // logout() |
| | | // } |
| | | // 在此位置初始化 homeViewModel |
| | | homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) |
| | | |
| | | val navView: BottomNavigationView = binding.navView |
| | | |
| | | // homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) |
| | | // |
| | | // val navView: BottomNavigationView = binding.navView |
| | | val navView = binding.navView |
| | | val navController = findNavController(R.id.nav_host_fragment_activity_main) |
| | | // Passing each menu ID as a set of Ids because each |
| | | // menu should be considered as top level destinations. |
| | | val appBarConfiguration = AppBarConfiguration( |
| | | setOf( |
| | | R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications |
| | | ) |
| | | ) |
| | | setupActionBarWithNavController(navController, appBarConfiguration) |
| | | |
| | | // 只保留底部导航的设置 |
| | | navView.setupWithNavController(navController) |
| | | |
| | | // 检查权限 |
| | | if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != android.content.pm.PackageManager.PERMISSION_GRANTED) { |
| | | // 请求权限 |
| | | permissionRequest.launch(Manifest.permission.READ_SMS) |
| | | syncRecentSms() |
| | | if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != android.content.pm.PackageManager.PERMISSION_GRANTED || |
| | | ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != android.content.pm.PackageManager.PERMISSION_GRANTED) { |
| | | // 同时请求两个权限 |
| | | multiplePermissionRequest.launch(arrayOf( |
| | | Manifest.permission.RECEIVE_SMS, |
| | | Manifest.permission.READ_SMS |
| | | )) |
| | | } else { |
| | | // 权限已经授予,继续执行相关操作 |
| | | registerSmsReceiver() |
| | | syncRecentSms() |
| | | } |
| | | val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) |
| | | recyclerView.layoutManager = LinearLayoutManager(this) |
| | | // val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) |
| | | // recyclerView.layoutManager = LinearLayoutManager(this) |
| | | // |
| | | // // 初始化适配器 |
| | | // adapter = MyAdapter() |
| | | // recyclerView.adapter = adapter |
| | | // |
| | | // // 观察 LiveData 数据 |
| | | // homeViewModel.codeList.observe(this) { codeList -> |
| | | // // 如果 codeList 为 null,避免闪退 |
| | | // if (codeList != null) { |
| | | // adapter.submitList(codeList) |
| | | // // 滚动到顶部 |
| | | // recyclerView.scrollToPosition(0) |
| | | // } else { |
| | | // // 如果数据为空,可以显示空列表或其他处理 |
| | | // Toast.makeText(this, "No data available", Toast.LENGTH_SHORT).show() |
| | | // } |
| | | // } |
| | | |
| | | // 初始化适配器 |
| | | adapter = MyAdapter() |
| | | recyclerView.adapter = adapter |
| | | |
| | | // 观察 LiveData 数据 |
| | | homeViewModel.codeList.observe(this) { codeList -> |
| | | // 如果 codeList 为 null,避免闪退 |
| | | if (codeList != null) { |
| | | adapter.submitList(codeList) |
| | | // 滚动到顶部 |
| | | recyclerView.scrollToPosition(0) |
| | | } else { |
| | | // 如果数据为空,可以显示空列表或其他处理 |
| | | Toast.makeText(this, "No data available", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } |
| | | |
| | | // 注册广播接收器来监听数据更新 |
| | | val filter = IntentFilter("com.example.firstapp.DATA_UPDATED") |
| | | registerReceiver(object : BroadcastReceiver() { |
| | | override fun onReceive(context: Context, intent: Intent) { |
| | | // 数据已更新,刷新 LiveData |
| | | homeViewModel.loadData() |
| | | } |
| | | }, filter) |
| | | // // 注册广播接收器来监听数据更新 |
| | | // val filter = IntentFilter("com.example.firstapp.DATA_UPDATED") |
| | | // registerReceiver(object : BroadcastReceiver() { |
| | | // override fun onReceive(context: Context, intent: Intent) { |
| | | // // 数据已更新,刷新 LiveData |
| | | // homeViewModel.loadData() |
| | | // } |
| | | // }, filter) |
| | | |
| | | } |
| | | |
| | |
| | | finish() |
| | | } |
| | | |
| | | // 初始化禁用词 |
| | | private fun initializeSecurityKeywords() { |
| | | CoroutineScope(Dispatchers.IO).launch { |
| | | try { |
| | | val response = RetrofitClient.apiService.getSecurityList() |
| | | if (response.code == 200) { |
| | | securityKeywordsList = response.data.map { it.keyword } |
| | | android.util.Log.d("MainActivity", "securityKeywordsList: $securityKeywordsList") |
| | | // 确保在主线程中调用 syncRecentSms |
| | | runOnUiThread { |
| | | syncRecentSms() |
| | | } |
| | | } else { |
| | | android.util.Log.e("MainActivity", "Failed to get security list: ${response.code}") |
| | | } |
| | | } catch (e: Exception) { |
| | | android.util.Log.e("MainActivity", "Error fetching security list", e) |
| | | } |
| | | } |
| | | } |
| | | |
| | | private fun syncRecentSms() { |
| | | |
| | | try { |
| | | val calendar = Calendar.getInstance() |
| | | calendar.add(Calendar.DAY_OF_YEAR, -3) // 获取3天前的时间 |
| | | val threeDaysAgo = calendar.timeInMillis |
| | | |
| | | val cursor = contentResolver.query( |
| | | Uri.parse("content://sms/sent"), |
| | | // Uri.parse("content://sms/sent"), //发送短信 |
| | | Uri.parse("content://sms/inbox"), |
| | | arrayOf("address", "body", "date"), |
| | | "date >= ?", |
| | | arrayOf(threeDaysAgo.toString()), |
| | |
| | | |
| | | cursor?.use { |
| | | while (cursor.moveToNext()) { |
| | | //手机号 |
| | | val address = cursor.getString(cursor.getColumnIndexOrThrow("address")) |
| | | val body = cursor.getString(cursor.getColumnIndexOrThrow("body")) |
| | | val date = cursor.getLong(cursor.getColumnIndexOrThrow("date")) |
| | | //短信内容 |
| | | val messageBody = cursor.getString(cursor.getColumnIndexOrThrow("body")) |
| | | //短信时间 |
| | | val datetime = cursor.getLong(cursor.getColumnIndexOrThrow("date")) |
| | | // 这里我要写个数组,并创建个对象存放一些内容,如这个对象的属性有匹配内容,正则表达式,并循环遍历 |
| | | val ruleList = mutableListOf( |
| | | Rule("快递","京东","\\d{6}"), |
| | | Rule("快递","菜鸟","\\d{1,2}-\\d{1,2}-\\d{4}") |
| | | ) |
| | | for (rule in ruleList) { |
| | | val code = rule.extractCodeFromMessage(messageBody.toString()) |
| | | if (code!==null) { |
| | | |
| | | // 使用正则表达式提取验证码 |
| | | val regex = "\\d{4,6}".toRegex() |
| | | val matchResult = regex.find(body) |
| | | matchResult?.let { result -> |
| | | val code = result.value |
| | | // 使用 ViewModel 保存到数据库 |
| | | println("address") |
| | | println(address) |
| | | println(code) |
| | | println(date) |
| | | // homeViewModel.saveCode(address, code, date) |
| | | // 转换为 Date 对象 |
| | | val date = Date(datetime) |
| | | // 如果需要格式化显示 |
| | | val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) |
| | | val dateString = sdf.format(date) |
| | | val existingCode = Core.code.queryByTypeAndCodeAndDate(rule.content, code, dateString) |
| | | if (existingCode == null) { |
| | | android.util.Log.d("SmsReceiver", "Received SMS code: ${code}") |
| | | val msg = Msg(0, "1111", "111111", messageBody.toString(), 1, "111", 1, 1) |
| | | val msgId = Core.msg.insert(msg) |
| | | // 禁用关键词拦截,如果有禁用词则不保存在Code里面 |
| | | android.util.Log.d("首页SmsReceiver", "securityKeywordsList: $securityKeywordsList") |
| | | if (securityKeywordsList.any { it in messageBody.toString() }) { |
| | | android.util.Log.d("首页SmsReceiver", "Received SMS code: 禁用关键词拦截,不保存") |
| | | continue |
| | | } |
| | | val code = Code(0, rule.type, 1, rule.content, 1, 1, msgId, code, dateString, "中通",0,"","") |
| | | Core.code.insert(code) |
| | | android.util.Log.d("SMS_DEBUG", "历史短信已保存到数据库") |
| | | }else{ |
| | | android.util.Log.d("SmsReceiver", "Received SMS code: 已存在相同记录,不保存") |
| | | } |
| | | }else{ |
| | | android.util.Log.d("SmsReceiver", "Received SMS code: 没有匹配到历史短信内容") |
| | | } |
| | | } |
| | | // 发送广播通知数据已更新 |
| | | val updateIntent = Intent("com.example.firstapp.DATA_UPDATED") |
| | | sendBroadcast(updateIntent) |
| | | } |
| | | } |
| | | } catch (e: Exception) { |