package space.bloggin.scribe import android.app.Activity import android.app.KeyguardManager import android.content.Context import android.content.Intent import android.os.Build import android.os.Bundle import android.widget.Toast /** * Invisible go-between. The listening service can't open apps directly from the background, * so it opens this, which wakes the screen, asks you to unlock if needed, opens the target * and disappears. Also used by the Quick Settings tile to start listening. */ class LaunchActivity : Activity() { companion object { private const val EXTRA_TARGET = "target" const val ACTION_START_LISTENING = "space.bloggin.scribe.START_LISTENING" fun go(ctx: Context, target: Intent) { val i = Intent(ctx, LaunchActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION) .putExtra(EXTRA_TARGET, target) try { ctx.startActivity(i) } catch (e: Exception) { EventLog.add("Couldn't open: ${e.message}") } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setShowWhenLocked(true) setTurnScreenOn(true) if (intent?.action == ACTION_START_LISTENING) { ListenService.start(this) finish() return } val target: Intent? = if (Build.VERSION.SDK_INT >= 33) { intent?.getParcelableExtra(EXTRA_TARGET, Intent::class.java) } else { @Suppress("DEPRECATION") intent?.getParcelableExtra(EXTRA_TARGET) } if (target == null) { finish() return } val km = getSystemService(KeyguardManager::class.java) if (km != null && km.isKeyguardLocked) { km.requestDismissKeyguard(this, object : KeyguardManager.KeyguardDismissCallback() { override fun onDismissSucceeded() = open(target) override fun onDismissCancelled() = finish() override fun onDismissError() = finish() }) } else { open(target) } } private fun open(target: Intent) { try { startActivity(target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } catch (e: Exception) { Toast.makeText(this, "Hey Dumby couldn't open that", Toast.LENGTH_SHORT).show() EventLog.add("Couldn't open: ${e.message}") } finish() } }