Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -78,6 +78,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 @@ -152,6 +153,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 @@ -356,6 +358,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 @@ -602,4 +602,5 @@
<string name="imported_entity_name">Importado: %1$s</string>
<string name="select_theme_to_delete">Selecciona un tema para eliminar</string>
<string name="removed_theme_successfully">El tema %1$s se eliminó correctamente.</string>
<string name="launch_first_match">Auto-ejecutar primera coincidencia con comando LAUNCH</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@
<string name="ai_model_updated">Modello dell\'IA aggiornato!</string>
<string name="cannot_bt_restrictions">Non è possibile cambiare lo stato del Bluetooth in questa versione d Android a causa di restrizioni del sistema.</string>
<string name="failed_to_update_script">Aggiornamento dello script fallito!</string>
<string name="select_option_for_editing">Seleziona un'opzione per modificare %1$s</string>
<string name="select_option_for_editing">Seleziona un\'opzione per modificare %1$s</string>
<string name="launcher_s_editor">Launcher\'s editor</string>
<string name="external_editor">Editor di testo esterno</string>
<string name="no_external_editor">Nessun editor di testo disponibile</string>
Expand All @@ -602,4 +602,5 @@
<string name="imported_entity_name">%1$s importato</string>
<string name="select_theme_to_delete">Seleziona un tema da cancellare</string>
<string name="removed_theme_successfully">Tema %1$s rimosso con successo.</string>
<string name="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 @@ -602,4 +602,5 @@
<string name="imported_entity_name">Импортировано: %1$s</string>
<string name="select_theme_to_delete">Выберите тему для удаления</string>
<string name="removed_theme_successfully">Тема %1$s успешно удалена.</string>
<string name="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 @@ -602,4 +602,5 @@
<string name="imported_entity_name">Увезено %1$s</string>
<string name="select_theme_to_delete">Изаберите тему за брисање</string>
<string name="removed_theme_successfully">Тема %1$s је успешно уклоњена.</string>
<string name="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 @@ -602,4 +602,5 @@
<string name="imported_entity_name">Імпортовано: %1$s</string>
<string name="select_theme_to_delete">Виберіть тему для видалення</string>
<string name="removed_theme_successfully">Тему %1$s успішно видалено.</string>
<string name="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