Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class SettingsActivity : AppCompatActivity() {
private var showCurrentFolderInPrompt = false
private var oneTapKeyboardActivation = true
private var hideKeyboardOnEnter = true
private var launchFirstMatch = false
private var actOnSuggestionTap = false
private var actOnLastSecondarySuggestion = false
private var initCmdLog = false
Expand Down Expand Up @@ -158,6 +159,7 @@ class SettingsActivity : AppCompatActivity() {
showCurrentFolderInPrompt = preferenceObject.getBoolean("showCurrentFolderInPrompt", false)
oneTapKeyboardActivation = preferenceObject.getBoolean("oneTapKeyboardActivation",true)
hideKeyboardOnEnter = preferenceObject.getBoolean("hideKeyboardOnEnter", true)
launchFirstMatch = preferenceObject.getBoolean("launchFirstMatch", false)
actOnSuggestionTap = preferenceObject.getBoolean("actOnSuggestionTap", false)
actOnLastSecondarySuggestion = preferenceObject.getBoolean("actOnLastSecondarySuggestion", false)
initCmdLog = preferenceObject.getBoolean("initCmdLog", false)
Expand Down Expand Up @@ -362,6 +364,12 @@ class SettingsActivity : AppCompatActivity() {
preferenceEditObject.putBoolean("hideKeyboardOnEnter",isChecked).apply()
changedSettingsCallback(this@SettingsActivity)
}
binding.launchFirstMatchSwitch.isChecked = launchFirstMatch
binding.launchFirstMatchSwitch.setOnCheckedChangeListener { _, isChecked ->
launchFirstMatch = isChecked
preferenceEditObject.putBoolean("launchFirstMatch",isChecked).apply()
changedSettingsCallback(this@SettingsActivity)
}
binding.actOnSuggestionTapSwitch.isChecked = actOnSuggestionTap
binding.actOnSuggestionTapSwitch.setOnCheckedChangeListener { _, isChecked ->
actOnSuggestionTap = isChecked
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/coderGtm/yantra/commands/launch/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ class Command(terminal: Terminal) : BaseCommand(terminal) {

val name = command.removePrefix(args[0]).trim().lowercase()

val launchFirstMatch = terminal.preferenceObject.getBoolean("launchFirstMatch", false)
if (launchFirstMatch) {
val firstMatch = findFirstMatchingApp(name, terminal)
if (firstMatch != null) {
output(terminal.activity.getString(R.string.launching_app, firstMatch.appName, firstMatch.packageName), terminal.theme.successTextColor)
launchApp(this@Command, firstMatch)
if (terminal.preferenceObject.getInt("appSortMode", AppSortMode.A_TO_Z.value) == AppSortMode.RECENT.value) {
terminal.appList.remove(firstMatch)
terminal.appList.add(0, firstMatch)
}
return
}
}

output(terminal.activity.getString(R.string.locating_app, name), terminal.theme.resultTextColor, Typeface.ITALIC)

val candidates = mutableListOf<AppBlock>()
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/coderGtm/yantra/commands/launch/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.os.UserManager
import com.coderGtm.yantra.R
import com.coderGtm.yantra.models.AppBlock
import com.coderGtm.yantra.models.ShortcutBlock
import com.coderGtm.yantra.terminal.Terminal
import com.coderGtm.yantra.terminal.getLaunchSuggestions

fun launchApp(command: Command, app: AppBlock) {
val launcher = command.terminal.activity.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
Expand Down Expand Up @@ -37,4 +39,17 @@ fun launchShortcut(command: Command, shortcut: ShortcutBlock) {
fun isDefaultUser(user: UserHandle, command: Command): Boolean {
val userManager = command.terminal.activity.getSystemService(Context.USER_SERVICE) as UserManager
return user == userManager.userProfiles[0]
}

fun findFirstMatchingApp(query: String, terminal: Terminal): AppBlock? {
if (query.isEmpty()) return null

val launchSuggestions = getLaunchSuggestions(query, terminal)

val firstAppSuggestion = launchSuggestions.suggestions.firstOrNull { it != "-p" && it != "-s" }
?: return null

return terminal.appList.find {
it.appName == firstAppSuggestion && it.packageName != terminal.activity.packageName
}
}
61 changes: 36 additions & 25 deletions app/src/main/java/com/coderGtm/yantra/terminal/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ import com.coderGtm.yantra.setSystemWallpaper
import java.util.Locale
import java.util.regex.Pattern

data class LaunchSuggestionsResult(
val suggestions: List<String>,
val overrideLastWord: Boolean
)

fun getLaunchSuggestions(query: String, terminal: Terminal): LaunchSuggestionsResult {
val suggestions = mutableListOf<String>()
val candidates = terminal.appList.map { it.appName }.toMutableList()
candidates.add(0, "-s")
candidates.add(0, "-p")

if (query.isNotEmpty()) {
val regex = Regex(Pattern.quote(query), RegexOption.IGNORE_CASE)
for (app in candidates) {
if (regex.containsMatchIn(app) && !suggestions.contains(app)) {
if (app.substring(0, query.length).lowercase() == query) {
suggestions.add(0, app)
continue
}
suggestions.add(app)
}
}
return LaunchSuggestionsResult(suggestions, true)
} else {
for (app in candidates) {
if (!suggestions.contains(app)) {
suggestions.add(app)
}
}
return LaunchSuggestionsResult(suggestions, false)
}
}

fun showSuggestions(
rawInput: String,
getPrimarySuggestions: Boolean,
Expand Down Expand Up @@ -72,31 +105,9 @@ fun showSuggestions(
if (!terminal.appListFetched) {
return@Thread
}
val candidates = terminal.appList.map { it.appName }.toMutableList()
candidates.add(0, "-s")
candidates.add(0, "-p")
if (args.size>1) {
//search using regex
overrideLastWord = true

val regex = Regex(Pattern.quote(reg), RegexOption.IGNORE_CASE)
for (app in candidates) {
if (regex.containsMatchIn(app) && !suggestions.contains(app)) {
if (app.substring(0, reg.length).lowercase() == reg && reg.isNotEmpty()){
suggestions.add(0, app)
continue
}
suggestions.add(app)
}
}
}
else {
for (app in candidates) {
if (!suggestions.contains(app)) {
suggestions.add(app)
}
}
}
val launchSuggestions = getLaunchSuggestions(reg, terminal)
suggestions.addAll(launchSuggestions.suggestions)
overrideLastWord = launchSuggestions.overrideLastWord
isPrimary = false
}
else if (effectivePrimaryCmd == "open") {
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@
android:text="@string/hide_keyboard_on_command_enter" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingVertical="8dp"
android:orientation="horizontal">

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/launchFirstMatchSwitch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="@string/auto_launch_first_match" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,5 @@
<string name="aqi_very_unhealthy">Muy insalubre</string>
<string name="aqi_hazardous">Peligrosa</string>
<string name="aqi_unknown">Desconocida</string>
<string name="auto_launch_first_match">Auto-ejecutar primera coincidencia con comando LAUNCH</string>
</resources>
5 changes: 3 additions & 2 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@
<string name="total_tokens_used">I token usati in totale per questa conversazione sono: %1$s</string>
<string name="ai_reset">La cronologia messaggi dell\'IA è stata eliminata!</string>
<string name="weather_feels_like_c_f"><![CDATA[=> Temperatura percepita: %1$.1f°C (%2$.1f°F)]]></string>
<string name="precipitation_chance">=> Possibilità che piova: %1$d%%</string>
<string name="snow_chance">=> Possibilità che nevichi: %1$d%%</string>
<string name="precipitation_chance"><![CDATA[=> Possibilità che piova: %1$d%%]]></string>
<string name="snow_chance"><![CDATA[=> Possibilità che nevichi: %1$d%%]]></string>
<string name="clear_init_list">Cancellare la lista init?</string>
<string name="clear_init_list_confirmation">Sei sicuro di voler cancellare il tuo script init?</string>
<string name="cmd_backup_title">backup [-i]</string>
Expand Down Expand Up @@ -710,4 +710,5 @@
<string name="weather_bulk_too_many_locations">Troppe località nella richiesta</string>
<string name="weather_internal_error">Errore interno del servizio meteorologico</string>
<string name="weather_unknown_error">Errore sconosciuto del servizio meteorologico</string>
<string name="auto_launch_first_match">Auto-avvia prima corrispondenza con il comando LAUNCH</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,5 @@
<string name="weather_bulk_too_many_locations">Слишком много местоположений в запросе</string>
<string name="weather_internal_error">Внутренняя ошибка службы погоды</string>
<string name="weather_unknown_error">Неизвестная ошибка службы погоды</string>
<string name="auto_launch_first_match">Автозапуск первого совпадения с командой LAUNCH</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-sr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,5 @@
<string name="weather_bulk_too_many_locations">Превише локација у захтеву</string>
<string name="weather_internal_error">Унутрашња грешка временске службе</string>
<string name="weather_unknown_error">Непозната грешка временске службе</string>
<string name="auto_launch_first_match">Аутоматско покретање првог подударања са LAUNCH командом</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,5 @@
<string name="weather_bulk_too_many_locations">Занадто багато місцезнаходжень у запиті</string>
<string name="weather_internal_error">Внутрішня помилка служби погоди</string>
<string name="weather_unknown_error">Невідома помилка служби погоди</string>
<string name="auto_launch_first_match">Автозапуск першого збігу з командою LAUNCH</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<string name="show_current_folder_in_prompt">Show Current Folder in Prompt</string>
<string name="tap_anywhere_to_open_keyboard">Tap anywhere to open Keyboard</string>
<string name="hide_keyboard_on_command_enter">Hide Keyboard on Command Enter</string>
<string name="auto_launch_first_match">Auto-launch First Match with LAUNCH command</string>
<string name="auto_execute_command_when_secondary_suggestion_is_selected">Auto Execute command when Secondary Suggestion is Selected</string>
<string name="log_commands_in_the_init_script">Log commands in the \'init\' script</string>
<string name="change_double_tap_command">Change Double-Tap Command</string>
Expand Down