#!/bin/bash

######################################################################
#Copyright (C) 2025 Kris Occhipinti https://filmsbykris.com
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation version 3 of the License.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
######################################################################

app="$(adb shell pm list packages | cut -d\: -f2 | fzf --prompt="Select a Package: ")"
[[ $app ]] || exit

function list_activities() {
  adb shell dumpsys package "$app" |
    grep Activity |
    grep "$app" |
    sed -e 's/^[[:space:]]*//' |
    grep $app |
    awk '{print $2}' |
    sort -u
}

activities="$(list_activities)"
[[ $activities ]] || exit

function launch_activity() {
  adb shell am start "$activity"
}

[[ "$activities" != *$'\n'* ]] && activity="$activities" || activity="$(echo "$activities" | fzf --prompt="Select an Activity: ")"
[[ "$activity" ]] || exit
launch_activity

