Back to Blog

Tell copilot to secure the Jetpack compose app

Sandy LaneSandy Lane

Video: Tell copilot to secure the Jetpack compose app by Taught by Celeste AI - AI Coding Coach

Watch full page →

Tell Copilot to Secure the Jetpack Compose App and Prevent Screenshots

In this example, we learn how to instruct GitHub Copilot to help secure a Jetpack Compose application by preventing screenshots and screen recordings. This is a crucial feature for apps handling sensitive information, enhancing user privacy and data protection.

Code

import android.os.Bundle
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Prevent screenshots and screen recordings by setting FLAG_SECURE
    window.setFlags(
      WindowManager.LayoutParams.FLAG_SECURE,
      WindowManager.LayoutParams.FLAG_SECURE
    )

    setContent {
      MyApp()
    }
  }
}

@Composable
fun MyApp() {
  Surface(color = MaterialTheme.colorScheme.background) {
    Text(text = "Secure Jetpack Compose App")
  }
}

Key Points

  • Use window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE) in your activity to block screenshots and screen recordings.
  • This flag enhances app security by preventing sensitive UI content from being captured externally.
  • The approach works seamlessly with Jetpack Compose by applying it in the activity's onCreate method before setting the content.
  • Preventing screenshots is important for apps dealing with confidential or personal user data.