Untitled
unknown
plain_text
3 years ago
3.5 kB
31
Indexable
class MyAsyncService: Service() { private val blockedWords = arrayOf("bla") private val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS) private val working = AtomicBoolean(true) private val runnable = Runnable { while (working.get()){ //Thread.sleep(1100) screen() //getScreenshot() } } override fun onCreate() { super.onCreate() Thread(runnable).start() } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { onTaskRemoved(intent) return START_STICKY } override fun onBind(p0: Intent?): IBinder? { return null } private fun screen(){ try { val process = Runtime.getRuntime().exec("su") val outputStream = OutputStreamWriter(process.outputStream) outputStream.write("/system/bin/screencap -p\n") outputStream.flush() outputStream.write("exit\n") outputStream.flush() outputStream.close() val options = BitmapFactory.Options() options.inSampleSize = 4 val bitmap = BitmapFactory.decodeStream(process.inputStream, null , options) processScreenshot(bitmap!!) } catch (e: IOException) { Log.i("BitmapScreencap", e.toString()) } } private fun processScreenshot(bitmap: Bitmap){ var count = 0 val result = recognizer.process(bitmap, 0) .addOnSuccessListener { visionText -> val resultText = visionText.text Log.i("Recognizer", resultText) for (block in visionText.textBlocks) { if (count != 0) break for (line in block.lines) { if (count != 0) break for (element in line.elements) { if (count != 0) break val elementText = element.text for (blockedWord in blockedWords){ if (elementText.lowercase() == blockedWord.lowercase()){ Log.i("Recognizer", "detected blocked word: $elementText | $blockedWord") val startMain = Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK; startActivity(startMain) count = 1 break } } } } } } .addOnFailureListener { e -> Log.i("recognizer failure", e.toString()) } } override fun onTaskRemoved(rootIntent: Intent) { val restartServiceIntent = Intent(applicationContext, this.javaClass) restartServiceIntent.setPackage(packageName) startService(restartServiceIntent) super.onTaskRemoved(rootIntent) } override fun onDestroy() { super.onDestroy() working.set(false) } }
Editor is loading...