package com.ultron.jarvistv import android.content.Context import android.content.Intent import android.net.Uri /** * High-level tasks Jarvis can perform. These combine plain Android intents (fast, reliable) with * the accessibility engine (for in-app navigation like "click the first search result"). * Every function returns a short human-readable status that gets echoed to the Jarvis screen * and back to the operator. */ object Actions { /** Launch any installed app by package name, e.g. com.google.android.youtube.tv */ fun launchApp(ctx: Context, pkg: String): String { val intent = ctx.packageManager.getLeanbackLaunchIntentForPackage(pkg) ?: ctx.packageManager.getLaunchIntentForPackage(pkg) ?: return "App not found: $pkg" intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ctx.startActivity(intent) return "Launching $pkg" } /** Open a URL in the default browser / handler. */ fun openUrl(ctx: Context, url: String): String { val fixed = if (url.startsWith("http")) url else "https://$url" ctx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(fixed)) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) return "Opening $fixed" } /** Google search results for a query. */ fun googleSearch(ctx: Context, query: String): String { openUrl(ctx, "https://www.google.com/search?q=" + Uri.encode(query)) return "Googling \"$query\"" } /** Play a specific YouTube video id directly (deep link the app understands). */ fun playYouTubeId(ctx: Context, videoId: String): String { val i = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:$videoId")) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ctx.startActivity(i) return "Playing YouTube video $videoId" } /** * Search YouTube for [query] and auto-play the first result. Opens the YouTube search screen * via intent, then hands off to the accessibility engine to click the top result. */ fun searchAndPlayYouTube(ctx: Context, query: String): String { val search = Intent("com.google.android.youtube.action.open.search").apply { setPackage(youtubePackage(ctx)) putExtra("query", query) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } try { ctx.startActivity(search) } catch (e: Exception) { // Fallback: web results page in the YouTube app / browser openUrl(ctx, "https://www.youtube.com/results?search_query=" + Uri.encode(query)) } // Give the results list ~2.5s to render, then click the first video tile. JarvisAccessibilityService.instance?.delayed(2500) { val a11y = JarvisAccessibilityService.instance ?: return@delayed // On TV the first result gets focus; DPAD_CENTER equivalent = click focused node. if (!a11y.clickText(query)) a11y.tap(400f, 300f) } return "Searching YouTube for \"$query\" and playing the top result" } /** Show an image full-screen (opens it in the system image viewer). */ fun showImage(ctx: Context, url: String): String { val i = Intent(Intent.ACTION_VIEW) .setDataAndType(Uri.parse(url), "image/*") .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ctx.startActivity(Intent.createChooser(i, "Jarvis").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) return "Showing image" } /** Stream a direct video URL (mp4/hls) in the system player. */ fun streamVideo(ctx: Context, url: String): String { val i = Intent(Intent.ACTION_VIEW) .setDataAndType(Uri.parse(url), "video/*") .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ctx.startActivity(i) return "Streaming video" } private fun youtubePackage(ctx: Context): String { // TV build first, then phone build as a fallback. val pm = ctx.packageManager return listOf("com.google.android.youtube.tv", "com.google.android.youtube") .firstOrNull { try { pm.getPackageInfo(it, 0); true } catch (e: Exception) { false } } ?: "com.google.android.youtube.tv" } }