-
-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show more info on QS tile long press (#5008)
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/src/main/java/io/homeassistant/companion/android/qs/TilePreferenceActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.homeassistant.companion.android.qs | ||
|
||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.core.os.BundleCompat | ||
import androidx.lifecycle.lifecycleScope | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import io.homeassistant.companion.android.BaseActivity | ||
import io.homeassistant.companion.android.common.data.servers.ServerManager | ||
import io.homeassistant.companion.android.database.qs.TileDao | ||
import io.homeassistant.companion.android.database.qs.isSetup | ||
import io.homeassistant.companion.android.launch.LaunchActivity | ||
import io.homeassistant.companion.android.settings.SettingsActivity | ||
import io.homeassistant.companion.android.settings.qs.ManageTilesViewModel | ||
import io.homeassistant.companion.android.webview.WebViewActivity | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
@AndroidEntryPoint | ||
class TilePreferenceActivity : BaseActivity() { | ||
|
||
companion object { | ||
private const val TAG = "TilePrefActivity" | ||
} | ||
|
||
@Inject | ||
lateinit var serverManager: ServerManager | ||
|
||
@Inject | ||
lateinit var tileDao: TileDao | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
var tileId = "-1" | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
intent.extras?.let { extras -> | ||
BundleCompat.getParcelable(extras, Intent.EXTRA_COMPONENT_NAME, ComponentName::class.java)?.let { component -> | ||
try { | ||
val tileClass = Class.forName(component.className) | ||
val tileMap = ManageTilesViewModel.idToTileService | ||
tileMap.filter { it.value == tileClass }.entries.firstOrNull()?.key?.let { | ||
Log.d(TAG, "Tile ID for long press action: $it") | ||
tileId = it | ||
} | ||
} catch (e: Exception) { | ||
Log.e(TAG, "Couldn't get tile ID for component $component", e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
lifecycleScope.launch { | ||
val tileData = tileDao.get(tileId) | ||
|
||
val intent = if (!serverManager.isRegistered()) { | ||
Intent(this@TilePreferenceActivity, LaunchActivity::class.java) | ||
} else if (tileData?.isSetup == true) { | ||
WebViewActivity.newInstance( | ||
this@TilePreferenceActivity, | ||
path = "entityId:${tileData.entityId}", | ||
serverId = tileData.serverId | ||
) | ||
} else { | ||
SettingsActivity.newInstance(this@TilePreferenceActivity).apply { | ||
putExtra("fragment", "tiles/$tileId") | ||
} | ||
} | ||
|
||
withContext(Dispatchers.Main) { | ||
startActivity(intent) | ||
finish() | ||
@Suppress("DEPRECATION") | ||
overridePendingTransition(0, 0) // Disable activity start/stop animation | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters