app/src/main/kotlin/space/subread/anki/CaptureTile.kt (1306 bytes)
1 package space.subread.anki 2 3 import android.annotation.SuppressLint 4 import android.app.PendingIntent 5 import android.content.Intent 6 import android.os.Build 7 import android.service.quicksettings.Tile 8 import android.service.quicksettings.TileService 9 10 /** The tile in the quick settings: one tap starts the capture, one tap stops it. */ 11 class CaptureTile : TileService() { 12 13 override fun onStartListening() { 14 val tile = qsTile ?: return 15 tile.state = if (CaptureService.instance != null) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE 16 tile.updateTile() 17 } 18 19 // The Intent form is the only one before Android 14; the version check keeps it there. 20 @SuppressLint("StartActivityAndCollapseDeprecated") 21 override fun onClick() { 22 if (CaptureService.instance != null) { 23 CaptureService.stop(this) 24 onStartListening() 25 return 26 } 27 val intent = Intent(this, CaptureActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 28 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { 29 startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)) 30 } else { 31 @Suppress("DEPRECATION") 32 startActivityAndCollapse(intent) 33 } 34 } 35 }