Skip to content

Add option to set HA as launcher #5348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 26, 2025
Merged
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
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@
</intent-filter>
</activity>

<activity-alias
android:name=".launch.LauncherAlias"
android:targetActivity=".launch.LaunchActivity"
android:enabled="false"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>

<activity android:name=".launch.link.LinkActivity"
android:exported="true"
android:launchMode="singleTask">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ class SettingsFragment(
return@setOnPreferenceClickListener true
}
}

setupLauncherPrefs()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -538,6 +540,39 @@ class SettingsFragment(
?: false
}

private fun getDefaultLauncherInfo(): String {
val intent = Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME)

getPackageManager()?.let { packageManager ->
packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)?.let {
val packageName = it.activityInfo.packageName
return packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, 0)).toString()
}
}

return getString(commonR.string.unknown_launcher_label)
}

private fun setupLauncherPrefs() {
val launcherSwitchPref = findPreference<SwitchPreference>("enable_ha_launcher")
val launcherPref = findPreference<Preference>("set_launcher_app")

launcherSwitchPref?.setOnPreferenceClickListener {
launcherPref?.isVisible = launcherSwitchPref.isChecked
true
}

launcherPref?.apply {
isVisible = launcherSwitchPref?.isChecked ?: false
summary = getString(commonR.string.default_launcher_prompt_def, getDefaultLauncherInfo())
setOnPreferenceClickListener {
startActivity(Intent(Settings.ACTION_HOME_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
true
}
}
}

override fun onAddServerResult(success: Boolean, serverId: Int?) {
view?.let {
snackbar = Snackbar.make(
Expand Down Expand Up @@ -569,6 +604,7 @@ class SettingsFragment(
super.onResume()
activity?.title = getString(commonR.string.companion_app)
context?.let { presenter.updateSuggestions(it) }
findPreference<Preference>("set_launcher_app")?.summary = getString(commonR.string.default_launcher_prompt_def, getDefaultLauncherInfo())
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class SettingsPresenterImpl @Inject constructor(
"io.homeassistant.companion.android.assist.VoiceCommandIntentActivity",
)

private val launcherAliasComponent = ComponentName(
BuildConfig.APPLICATION_ID,
"io.homeassistant.companion.android.launch.LauncherAlias",
)

private var suggestionFlow = MutableStateFlow<SettingsHomeSuggestion?>(null)

override fun getBoolean(key: String, defValue: Boolean): Boolean = runBlocking {
Expand All @@ -78,6 +83,10 @@ class SettingsPresenterImpl @Inject constructor(
val componentSetting = view.getPackageManager()?.getComponentEnabledSetting(voiceCommandAppComponent)
componentSetting != null && componentSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED
}
"enable_ha_launcher" -> {
val componentSetting = view.getPackageManager()?.getComponentEnabledSetting(launcherAliasComponent)
componentSetting != null && componentSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED
}
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand All @@ -97,6 +106,7 @@ class SettingsPresenterImpl @Inject constructor(
if (value) PackageManager.COMPONENT_ENABLED_STATE_DEFAULT else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP,
)
"enable_ha_launcher" -> enableLauncherMode(value)
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand Down Expand Up @@ -295,4 +305,12 @@ class SettingsPresenterImpl @Inject constructor(
suggestionFlow.emit(null)
}
}

private fun enableLauncherMode(enable: Boolean) {
view.getPackageManager()?.setComponentEnabledSetting(
launcherAliasComponent,
if (enable) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
if (enable) PackageManager.DONT_KILL_APP else 0,
)
}
}
14 changes: 14 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@
android:title="@string/manage_widgets"
android:summary="@string/manage_widgets_summary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/launcher">
<SwitchPreference
android:key="enable_ha_launcher"
android:icon="@drawable/ic_android_debug_bridge"
app:isPreferenceVisible="true"
android:title="@string/launcher_option_title"
android:summary="@string/launcher_option_summary" />
<Preference
android:key="set_launcher_app"
android:title="@string/default_launcher_prompt"
android:summary="@string/default_launcher_prompt_def"
android:icon="@drawable/ic_home_variant_outline" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/need_help">
<Preference
Expand Down
6 changes: 6 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1384,4 +1384,10 @@
<string name="thermostat_tile">Thermostat tile</string>
<string name="climate_heating">Heating</string>
<string name="climate_cooling">Cooling</string>
<string name="launcher">Device home screen</string>
<string name="launcher_option_title">Allow as home app</string>
<string name="launcher_option_summary">Include Home Assistant in the list of available home apps/launchers that can replace your device\'s home screen</string>
<string name="default_launcher_prompt">Change home app</string>
<string name="default_launcher_prompt_def">Open settings to choose your default home app. Currently set to %s.</string>
<string name="unknown_launcher_label">unknown app</string>
</resources>