package bbc import ( "fmt" "regexp" "strings" "time" "streamd/adb" "streamd/uiflow" ) // openExplorePage taps the Explore bottom-nav tab and waits for the channel rail. func (a *App) openExplorePage(c *adb.Client) error { c.DismissShadeIfFocused() if nodes, err := c.DumpUI(a.CacheDir()); err == nil && a.onExplorePage(nodes) { fmt.Println("[*] already on Explore") return nil } fmt.Println("[*] open Explore…") if err := uiflow.TapUI(c, a.CacheDir(), uiflow.Match{ TextContains: a.cfg.UI.ExploreTab, DescContains: a.cfg.UI.ExploreTab, Pick: uiflow.PickFirst, }, time.Duration(a.cfg.Timeouts.ExploreNav)); err != nil { return fmt.Errorf("Explore tab: %w", err) } time.Sleep(1500 * time.Millisecond) deadline := time.Now().Add(time.Duration(a.cfg.Timeouts.ExploreNav)) for time.Now().Before(deadline) { nodes, err := c.DumpUI(a.CacheDir()) if err == nil && a.onExplorePage(nodes) { fmt.Println("[*] Explore page ready") return nil } time.Sleep(700 * time.Millisecond) } return fmt.Errorf("Explore page did not open") } // onExplorePage is true when channel logos (BBC One / BBC Two / …) are visible. // The Channels header alone is not enough — logos load a moment later. func (a *App) onExplorePage(nodes []adb.Node) bool { channelish := 0 for _, n := range nodes { if looksLikeChannelLogo(strings.TrimSpace(n.Desc)) { channelish++ } } return channelish >= 2 } func looksLikeChannelLogo(desc string) bool { d := strings.ToLower(strings.TrimSpace(desc)) if d == "" { return false } if strings.HasPrefix(d, "bbc ") || d == "cbbc" || d == "cbeebies" || d == "s4c" { return true } return false } // tapChannel finds the channel logo on the Explore rail (scrolling horizontally // when needed) and taps it. func (a *App) tapChannel(c *adb.Client, ch Channel) error { pats := uiflow.CompileRes(ch.chipPatterns()) if len(pats) == 0 { return fmt.Errorf("no chip selectors for channel %q", ch.Label) } deadline := time.Now().Add(time.Duration(a.cfg.Timeouts.ChannelFind)) scrolled := 0 for time.Now().Before(deadline) { nodes, err := c.DumpUI(a.CacheDir()) if err != nil { time.Sleep(800 * time.Millisecond) continue } // Match content-desc on the logo node (often not itself clickable — // the parent View shares bounds and receives the tap). if n := findChannelLogo(nodes, pats); n != nil { label := strings.TrimSpace(n.Desc) if label == "" { label = strings.TrimSpace(n.Text) } fmt.Printf("[*] tap channel %q @ %d,%d\n", label, n.CX(), n.CY()) return c.Tap(n.CX(), n.CY()) } // Already on the channel page? if a.onChannelPage(nodes, ch) { fmt.Printf("[*] already on %s page\n", ch.Label) return nil } logos := channelLogoNodes(nodes) if len(logos) == 0 { // Rail still loading — wait, do not swipe random mid-screen coords. time.Sleep(800 * time.Millisecond) continue } if scrolled >= a.cfg.UI.ChannelScrollMax { break } x1, y, x2 := channelRailSwipe(logos) fmt.Printf("[*] channel %q not on screen — swipe rail %d→%d @ y=%d (%d logos)\n", ch.Label, x1, x2, y, len(logos)) _ = c.Swipe(x1, y, x2, y, 350) scrolled++ time.Sleep(900 * time.Millisecond) } return fmt.Errorf("channel logo %q not found on Explore", ch.Label) } func findChannelLogo(nodes []adb.Node, pats []*regexp.Regexp) *adb.Node { var best *adb.Node for i := range nodes { n := &nodes[i] desc := strings.TrimSpace(n.Desc) text := strings.TrimSpace(n.Text) matched := false for _, p := range pats { if desc != "" && p.MatchString(desc) { matched = true break } if text != "" && p.MatchString(text) { matched = true break } } if !matched { continue } // Prefer smaller logo tiles over large hero cards. if best == nil || n.Area() < best.Area() { best = n } } return best } func channelLogoNodes(nodes []adb.Node) []adb.Node { var out []adb.Node for _, n := range nodes { if looksLikeChannelLogo(strings.TrimSpace(n.Desc)) { out = append(out, n) } } return out } func (a *App) onChannelPage(nodes []adb.Node, ch Channel) bool { label := strings.TrimSpace(ch.Label) hasTitle := false hasWatch := false for _, n := range nodes { text := strings.TrimSpace(n.Text) desc := strings.TrimSpace(n.Desc) if label != "" && (strings.EqualFold(text, label) || strings.EqualFold(desc, label)) { hasTitle = true } for _, w := range a.cfg.UI.WatchLive { if w != "" && (strings.EqualFold(text, w) || strings.EqualFold(desc, w) || strings.Contains(strings.ToLower(text), strings.ToLower(w)) || strings.Contains(strings.ToLower(desc), strings.ToLower(w))) { hasWatch = true } } } return hasTitle && hasWatch } // channelRailSwipe picks a horizontal swipe across visible channel logos. func channelRailSwipe(logos []adb.Node) (x1, y, x2 int) { minX, maxX := logos[0].X1, logos[0].X2 sumY := 0 for _, n := range logos { if n.X1 < minX { minX = n.X1 } if n.X2 > maxX { maxX = n.X2 } sumY += n.CY() } y = sumY / len(logos) width := maxX - minX if width < 400 { // Single/partial logo visible — swipe across a wide band around it. cx := (minX + maxX) / 2 return cx + 400, y, cx - 400 } x1 = minX + width*85/100 x2 = minX + width*15/100 if x1-x2 < 200 { cx := (minX + maxX) / 2 return cx + 350, y, cx - 350 } return x1, y, x2 } // tapWatchLive taps the Watch live control on the channel page. func (a *App) tapWatchLive(c *adb.Client, ch Channel) error { pats := ch.playPatterns(a.cfg.UI) if nodes, err := c.DumpUI(a.CacheDir()); err == nil { if playingUI(nodes, a.cfg.UI.PlaybackDescContains) { fmt.Println("[*] player UI already up") return nil } } fmt.Println("[*] tap Watch live…") if err := uiflow.TapUI(c, a.CacheDir(), uiflow.Match{ TextContains: pats, DescContains: pats, TextRE: pats, DescRE: pats, Pick: uiflow.PickLargest, MinArea: 800, }, time.Duration(a.cfg.Timeouts.WatchLive)); err != nil { return fmt.Errorf("Watch live: %w", err) } time.Sleep(time.Duration(a.cfg.Timeouts.AfterWatch)) return nil } func playingUI(nodes []adb.Node, signals []string) bool { for _, n := range nodes { blob := strings.ToLower(n.Text + " " + n.Desc + " " + n.ResourceID) for _, s := range signals { if s != "" && strings.Contains(blob, strings.ToLower(s)) { return true } } } return false }