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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
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.my.MyActivity"
android:exported="true">
<intent-filter android:autoVerify="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,24 @@ class SettingsFragment(
return@setOnPreferenceClickListener true
}
}

findPreference<SwitchPreference>("enable_ha_launcher")?.let { switchPreference ->
switchPreference.setOnPreferenceClickListener {
findPreference<Preference>("set_launcher_app")?.isVisible = switchPreference.isChecked
true
}
}

findPreference<Preference>("set_launcher_app")?.let {
it.isVisible = findPreference<SwitchPreference>("enable_ha_launcher")?.isChecked ?: false
it.summary = getString(commonR.string.default_launcher_prompt_def, getDefaultLauncherInfo())
it.setOnPreferenceClickListener {
val intent = Intent(Settings.ACTION_HOME_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
true
}
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -560,6 +578,20 @@ class SettingsFragment(

override fun getPackageManager(): PackageManager? = context?.packageManager

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)
}

override fun onPause() {
super.onPause()
snackbar?.dismiss()
Expand All @@ -569,6 +601,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,7 @@ class SettingsPresenterImpl @Inject constructor(
val componentSetting = view.getPackageManager()?.getComponentEnabledSetting(voiceCommandAppComponent)
componentSetting != null && componentSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED
}
"enable_ha_launcher" -> prefsRepository.isLauncherCapabilityEnabled()
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand All @@ -97,6 +103,10 @@ class SettingsPresenterImpl @Inject constructor(
if (value) PackageManager.COMPONENT_ENABLED_STATE_DEFAULT else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
"enable_ha_launcher" -> {
prefsRepository.setLauncherCapabilityEnabled(value)
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,
PackageManager.DONT_KILL_APP
)
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ interface PrefsRepository {

suspend fun addImprovPermissionDisplayedCount()

suspend fun isLauncherCapabilityEnabled(): Boolean

suspend fun setLauncherCapabilityEnabled(enabled: Boolean)

/** Clean up any app-level preferences that might reference servers */
suspend fun removeServer(serverId: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PrefsRepositoryImpl @Inject constructor(
private const val PREF_AUTO_FAVORITES = "auto_favorites"
private const val PREF_LOCATION_HISTORY_DISABLED = "location_history"
private const val PREF_IMPROV_PERMISSION_DISPLAYED = "improv_permission_displayed"
private const val PREF_LAUNCHER_CAPABILITY_ENABLED = "launcher_capability_enabled"
}

init {
Expand Down Expand Up @@ -260,6 +261,14 @@ class PrefsRepositoryImpl @Inject constructor(
localStorage.putInt(PREF_IMPROV_PERMISSION_DISPLAYED, getImprovPermissionDisplayedCount() + 1)
}

override suspend fun isLauncherCapabilityEnabled(): Boolean {
return localStorage.getBoolean(PREF_LAUNCHER_CAPABILITY_ENABLED)
}

override suspend fun setLauncherCapabilityEnabled(enabled: Boolean) {
localStorage.putBoolean(PREF_LAUNCHER_CAPABILITY_ENABLED, enabled)
}

override suspend fun removeServer(serverId: Int) {
val controlsAuthEntities = getControlsAuthEntities().filter { it.split(".")[0].toIntOrNull() != serverId }
setControlsAuthEntities(controlsAuthEntities)
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 @@ -1382,4 +1382,10 @@
<string name="thermostat_tile">Thermostat tile</string>
<string name="climate_heating">Heating</string>
<string name="climate_cooling">Cooling</string>
<string name="launcher">Launcher</string>
<string name="launcher_option_title">Enable Home Assistant launcher mode</string>
<string name="launcher_option_summary">Add Home Assistant to the list of available device launchers that can replace your device\'s home screen</string>
<string name="default_launcher_prompt">Change launcher app</string>
<string name="default_launcher_prompt_def">Open settings to choose a default launcher app. Currently set to %s</string>
<string name="unknown_launcher_label">unknown app</string>
</resources>