Skip to content

Commit eed5c27

Browse files
committed
Create switch preference to enable launcher mode
1 parent 13ccef4 commit eed5c27

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

app/src/main/kotlin/io/homeassistant/companion/android/settings/SettingsFragment.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import kotlinx.coroutines.sync.Mutex
5858
import kotlinx.coroutines.sync.withLock
5959
import timber.log.Timber
6060

61-
6261
class SettingsFragment(
6362
private val presenter: SettingsPresenter,
6463
private val langProvider: LanguagesProvider
@@ -359,7 +358,15 @@ class SettingsFragment(
359358
}
360359
}
361360

361+
findPreference<SwitchPreference>("enable_ha_launcher")?.let { switchPreference ->
362+
switchPreference.setOnPreferenceClickListener {
363+
findPreference<Preference>("set_launcher_app")?.isVisible = switchPreference.isChecked
364+
true
365+
}
366+
}
367+
362368
findPreference<Preference>("set_launcher_app")?.let {
369+
it.isVisible = findPreference<SwitchPreference>("enable_ha_launcher")?.isChecked ?: false
363370
it.summary = getString(commonR.string.default_launcher_prompt_def, getDefaultLauncherInfo())
364371
it.setOnPreferenceClickListener {
365372
val intent = Intent(Settings.ACTION_HOME_SETTINGS)

app/src/main/kotlin/io/homeassistant/companion/android/settings/SettingsPresenterImpl.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class SettingsPresenterImpl @Inject constructor(
6464
"io.homeassistant.companion.android.assist.VoiceCommandIntentActivity"
6565
)
6666

67+
private val launcherAliasComponent = ComponentName(
68+
BuildConfig.APPLICATION_ID,
69+
"io.homeassistant.companion.android.launch.LauncherAlias"
70+
)
71+
6772
private var suggestionFlow = MutableStateFlow<SettingsHomeSuggestion?>(null)
6873

6974
override fun getBoolean(key: String, defValue: Boolean): Boolean = runBlocking {
@@ -78,6 +83,7 @@ class SettingsPresenterImpl @Inject constructor(
7883
val componentSetting = view.getPackageManager()?.getComponentEnabledSetting(voiceCommandAppComponent)
7984
componentSetting != null && componentSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED
8085
}
86+
"enable_ha_launcher" -> prefsRepository.isLauncherCapabilityEnabled()
8187
else -> throw IllegalArgumentException("No boolean found by this key: $key")
8288
}
8389
}
@@ -97,6 +103,10 @@ class SettingsPresenterImpl @Inject constructor(
97103
if (value) PackageManager.COMPONENT_ENABLED_STATE_DEFAULT else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
98104
PackageManager.DONT_KILL_APP
99105
)
106+
"enable_ha_launcher" -> {
107+
prefsRepository.setLauncherCapabilityEnabled(value)
108+
enableLauncherMode(value)
109+
}
100110
else -> throw IllegalArgumentException("No boolean found by this key: $key")
101111
}
102112
}
@@ -295,4 +305,12 @@ class SettingsPresenterImpl @Inject constructor(
295305
suggestionFlow.emit(null)
296306
}
297307
}
308+
309+
private fun enableLauncherMode(enable: Boolean) {
310+
view.getPackageManager()?.setComponentEnabledSetting(
311+
launcherAliasComponent,
312+
if (enable) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
313+
PackageManager.DONT_KILL_APP
314+
)
315+
}
298316
}

app/src/main/res/xml/preferences.xml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
</PreferenceCategory>
3838
<PreferenceCategory
3939
android:title="@string/other_settings">
40-
<Preference
41-
android:key="set_launcher_app"
42-
android:title="@string/default_launcher_prompt"
43-
android:summary="@string/default_launcher_prompt_def"
44-
android:icon="@drawable/ic_home_variant_outline" />
4540
<SwitchPreference
4641
android:key="fullscreen"
4742
android:icon="@drawable/ic_fullscreen"
@@ -190,6 +185,20 @@
190185
android:title="@string/manage_widgets"
191186
android:summary="@string/manage_widgets_summary" />
192187
</PreferenceCategory>
188+
<PreferenceCategory
189+
android:title="@string/launcher">
190+
<SwitchPreference
191+
android:key="enable_ha_launcher"
192+
android:icon="@drawable/ic_android_debug_bridge"
193+
app:isPreferenceVisible="true"
194+
android:title="@string/launcher_option_title"
195+
android:summary="@string/launcher_option_summary" />
196+
<Preference
197+
android:key="set_launcher_app"
198+
android:title="@string/default_launcher_prompt"
199+
android:summary="@string/default_launcher_prompt_def"
200+
android:icon="@drawable/ic_home_variant_outline" />
201+
</PreferenceCategory>
193202
<PreferenceCategory
194203
android:title="@string/need_help">
195204
<Preference

common/src/main/kotlin/io/homeassistant/companion/android/common/data/prefs/PrefsRepository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ interface PrefsRepository {
9595

9696
suspend fun addImprovPermissionDisplayedCount()
9797

98+
suspend fun isLauncherCapabilityEnabled(): Boolean
99+
100+
suspend fun setLauncherCapabilityEnabled(enabled: Boolean)
101+
98102
/** Clean up any app-level preferences that might reference servers */
99103
suspend fun removeServer(serverId: Int)
100104
}

common/src/main/kotlin/io/homeassistant/companion/android/common/data/prefs/PrefsRepositoryImpl.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class PrefsRepositoryImpl @Inject constructor(
3838
private const val PREF_AUTO_FAVORITES = "auto_favorites"
3939
private const val PREF_LOCATION_HISTORY_DISABLED = "location_history"
4040
private const val PREF_IMPROV_PERMISSION_DISPLAYED = "improv_permission_displayed"
41+
private const val PREF_LAUNCHER_CAPABILITY_ENABLED = "launcher_capability_enabled"
4142
}
4243

4344
init {
@@ -260,6 +261,14 @@ class PrefsRepositoryImpl @Inject constructor(
260261
localStorage.putInt(PREF_IMPROV_PERMISSION_DISPLAYED, getImprovPermissionDisplayedCount() + 1)
261262
}
262263

264+
override suspend fun isLauncherCapabilityEnabled(): Boolean {
265+
return localStorage.getBoolean(PREF_LAUNCHER_CAPABILITY_ENABLED)
266+
}
267+
268+
override suspend fun setLauncherCapabilityEnabled(enabled: Boolean) {
269+
localStorage.putBoolean(PREF_LAUNCHER_CAPABILITY_ENABLED, enabled)
270+
}
271+
263272
override suspend fun removeServer(serverId: Int) {
264273
val controlsAuthEntities = getControlsAuthEntities().filter { it.split(".")[0].toIntOrNull() != serverId }
265274
setControlsAuthEntities(controlsAuthEntities)

common/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,9 @@
13801380
<string name="thermostat_tile">Thermostat tile</string>
13811381
<string name="climate_heating">Heating</string>
13821382
<string name="climate_cooling">Cooling</string>
1383+
<string name="launcher">Launcher</string>
1384+
<string name="launcher_option_title">Enable Home Assistant launcher mode</string>
1385+
<string name="launcher_option_summary">Add Home Assistant to the list of available device launchers that can replace your device\'s home screen</string>
13831386
<string name="default_launcher_prompt">Change launcher app</string>
13841387
<string name="default_launcher_prompt_def">Open settings to choose a default launcher app. Currently set to %s</string>
13851388
<string name="unknown_launcher_label">unknown app</string>

0 commit comments

Comments
 (0)