Favorites?
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
package com.example.ichwillheim
|
package com.example.ichwillheim
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
@@ -25,6 +28,7 @@ import androidx.compose.ui.tooling.preview.Preview
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import com.example.ichwillheim.ui.theme.IchWillHeimTheme
|
import com.example.ichwillheim.ui.theme.IchWillHeimTheme
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@@ -43,14 +47,21 @@ class MainActivity : ComponentActivity() {
|
|||||||
@Composable
|
@Composable
|
||||||
fun LauncherScreen(modifier: Modifier = Modifier) {
|
fun LauncherScreen(modifier: Modifier = Modifier) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
val favorites = listOf(
|
||||||
|
FavoriteContact(
|
||||||
|
name = "Sandro",
|
||||||
|
phone = "+1-555-0100"
|
||||||
|
),
|
||||||
|
FavoriteContact(
|
||||||
|
name = "Paulo",
|
||||||
|
phone = "+1-555-0111"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
val apps = listOf(
|
val apps = listOf(
|
||||||
AppEntry(
|
|
||||||
label = "Phone",
|
|
||||||
packageName = "com.android.dialer"
|
|
||||||
),
|
|
||||||
AppEntry(
|
AppEntry(
|
||||||
label = "Messages",
|
label = "Messages",
|
||||||
packageName = "com.google.android.apps.messaging"
|
action = { openMessages(context) }
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -61,14 +72,25 @@ fun LauncherScreen(modifier: Modifier = Modifier) {
|
|||||||
verticalArrangement = Arrangement.Center,
|
verticalArrangement = Arrangement.Center,
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(text = "Favorites", style = MaterialTheme.typography.headlineSmall)
|
||||||
text = "Hallo Renate!",
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
style = MaterialTheme.typography.headlineSmall
|
favorites.forEach { favorite ->
|
||||||
)
|
Button(
|
||||||
Spacer(modifier = Modifier.height(24.dp))
|
onClick = { openDialerNumber(context, favorite.phone) },
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.height(96.dp)
|
||||||
|
) {
|
||||||
|
Text(text = favorite.name, fontSize = 24.sp)
|
||||||
|
}
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
}
|
||||||
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
Text(text = "Apps", style = MaterialTheme.typography.headlineSmall)
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
apps.forEach { app ->
|
apps.forEach { app ->
|
||||||
Button(
|
Button(
|
||||||
onClick = { launchApp(context, app) },
|
onClick = { app.action() },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.height(96.dp)
|
.height(96.dp)
|
||||||
@@ -91,18 +113,33 @@ fun LauncherPreview() {
|
|||||||
@Immutable
|
@Immutable
|
||||||
data class AppEntry(
|
data class AppEntry(
|
||||||
val label: String,
|
val label: String,
|
||||||
val packageName: String
|
val action: () -> Unit
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun launchApp(context: android.content.Context, app: AppEntry) {
|
@Immutable
|
||||||
val intent = context.packageManager.getLaunchIntentForPackage(app.packageName)
|
data class FavoriteContact(
|
||||||
if (intent != null) {
|
val name: String,
|
||||||
|
val phone: String
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun openDialerNumber(context: Context, phone: String) {
|
||||||
|
val intent = Intent(Intent.ACTION_DIAL).apply {
|
||||||
|
data = "tel:${Uri.encode(phone)}".toUri()
|
||||||
|
}
|
||||||
|
startIntentOrToast(context, intent, "Phone app not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openMessages(context: Context) {
|
||||||
|
val intent = Intent(Intent.ACTION_SENDTO).apply {
|
||||||
|
data = "smsto:".toUri()
|
||||||
|
}
|
||||||
|
startIntentOrToast(context, intent, "Messages app not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startIntentOrToast(context: Context, intent: Intent, error: String) {
|
||||||
|
try {
|
||||||
context.startActivity(intent)
|
context.startActivity(intent)
|
||||||
} else {
|
} catch (_: Exception) {
|
||||||
Toast.makeText(
|
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
|
||||||
context,
|
|
||||||
"App not found: ${app.label}",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user