Skip to content

Commit

Permalink
feat: handle tel uri scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 27, 2024
1 parent f21f6ea commit 7223953
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 54 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<activity
android:name=".ui.activities.MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize">

<intent-filter>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/contacts/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bnyro.contacts

import android.app.Application
import com.bnyro.contacts.data.database.DatabaseHolder
import com.bnyro.contacts.domain.repositories.CallLogRepository
import com.bnyro.contacts.domain.repositories.DeviceContactsRepository
import com.bnyro.contacts.domain.repositories.DeviceSmsRepo
import com.bnyro.contacts.domain.repositories.LocalContactsRepository
Expand All @@ -19,6 +20,9 @@ class App : Application() {
val localContactsRepository by lazy {
LocalContactsRepository(this)
}
val callLogRepository by lazy {
CallLogRepository(this)
}

lateinit var smsRepo: SmsRepository

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.bnyro.contacts.util
package com.bnyro.contacts.domain.repositories

import android.Manifest
import android.content.Context
import android.provider.CallLog
import com.bnyro.contacts.domain.model.CallLogEntry
import com.bnyro.contacts.util.PermissionHelper
import com.bnyro.contacts.util.extension.intValue
import com.bnyro.contacts.util.extension.longValue
import com.bnyro.contacts.util.extension.stringValue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object CallLogHelper {
suspend fun getCallLog(context: Context): List<CallLogEntry> = withContext(Dispatchers.IO) {
if (!PermissionHelper.checkPermissions(context, arrayOf(Manifest.permission.READ_CALL_LOG))) return@withContext emptyList()
class CallLogRepository(private val context: Context) {
suspend fun getCallLog(): List<CallLogEntry> = withContext(Dispatchers.IO) {
if (!PermissionHelper.checkPermissions(
context,
arrayOf(Manifest.permission.READ_CALL_LOG)
)
) return@withContext emptyList()

val callLog = mutableListOf<CallLogEntry>()

Expand All @@ -34,11 +39,19 @@ object CallLogHelper {
return@withContext callLog
}

suspend fun deleteAll(context: Context, callLog: List<CallLogEntry>) = withContext(Dispatchers.IO) {
if (!PermissionHelper.checkPermissions(context, arrayOf(Manifest.permission.WRITE_CALL_LOG))) return@withContext
suspend fun deleteAll(callLog: List<CallLogEntry>) = withContext(Dispatchers.IO) {
if (!PermissionHelper.checkPermissions(
context,
arrayOf(Manifest.permission.WRITE_CALL_LOG)
)
) return@withContext

callLog.distinctBy { it.phoneNumber }.forEach { entry ->
context.contentResolver.delete(CallLog.Calls.CONTENT_URI, "NUMBER=${entry.phoneNumber}", null)
context.contentResolver.delete(
CallLog.Calls.CONTENT_URI,
"NUMBER=${entry.phoneNumber}",
null
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
import com.bnyro.contacts.presentation.screens.sms.model.SmsModel
Expand All @@ -34,7 +33,6 @@ fun HomeNavContainer(
onNavigate: (String) -> Unit,
smsModel: SmsModel,
contactsModel: ContactsModel,
callModel: CallModel,
themeModel: ThemeModel
) {
val navController = rememberNavController()
Expand Down Expand Up @@ -108,7 +106,6 @@ fun HomeNavContainer(
onNavigate = onNavigate,
smsModel = smsModel,
contactsModel = contactsModel,
callModel = callModel,
themeModel = themeModel
)
}
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/bnyro/contacts/navigation/HomeNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.bnyro.contacts.presentation.screens.calllog.CallLogsScreen
import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.ContactsPage
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
Expand All @@ -24,7 +23,6 @@ fun HomeNavHost(
modifier: Modifier = Modifier,
smsModel: SmsModel,
contactsModel: ContactsModel,
callModel: CallModel,
themeModel: ThemeModel
) {
val viewModelStoreOwner: ViewModelStoreOwner = LocalViewModelStoreOwner.current!!
Expand All @@ -38,10 +36,11 @@ fun HomeNavHost(
})
}
}
composable(HomeRoutes.Phone.route) {
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) {
CallLogsScreen(contactsModel, callModel, themeModel)
}
composable(
HomeRoutes.Phone.route,
deepLinks = HomeRoutes.Phone.deepLinks
) {
CallLogsScreen(contactsModel, themeModel)
}
composable(HomeRoutes.Messages.route) {
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
import com.bnyro.contacts.presentation.screens.sms.model.SmsModel

@Composable
fun NavContainer(
initialTab: HomeRoutes
initialTab: HomeRoutes,
) {
val navController = rememberNavController()
val smsModel: SmsModel = viewModel()
val contactsModel: ContactsModel = viewModel(factory = ContactsModel.Factory)
val callModel: CallModel = viewModel()
val themeModel: ThemeModel = viewModel()
AppNavHost(
navController,
Expand All @@ -26,7 +24,6 @@ fun NavContainer(
.fillMaxSize(),
smsModel = smsModel,
contactsModel = contactsModel,
callModel = callModel,
themeModel = themeModel
)
}
3 changes: 0 additions & 3 deletions app/src/main/java/com/bnyro/contacts/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.bnyro.contacts.presentation.screens.about.AboutScreen
import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.SettingsScreen
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
Expand All @@ -29,7 +28,6 @@ fun AppNavHost(
initialTab: HomeRoutes,
smsModel: SmsModel,
contactsModel: ContactsModel,
callModel: CallModel,
themeModel: ThemeModel
) {
val viewModelStoreOwner: ViewModelStoreOwner = LocalViewModelStoreOwner.current!!
Expand All @@ -53,7 +51,6 @@ fun AppNavHost(
},
smsModel = smsModel,
contactsModel = contactsModel,
callModel = callModel,
themeModel = themeModel
)
}
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/bnyro/contacts/navigation/NavRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.compose.material.icons.rounded.Message
import androidx.compose.material.icons.rounded.Person
import androidx.compose.material.icons.rounded.Phone
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.core.net.toUri
import androidx.navigation.navDeepLink
import com.bnyro.contacts.R

sealed class NavRoutes(
Expand All @@ -22,7 +24,18 @@ sealed class HomeRoutes(
@StringRes val stringRes: Int,
val icon: ImageVector
) {
object Phone : HomeRoutes("phone", R.string.dial, Icons.Rounded.Phone)
object Phone : HomeRoutes("phone", R.string.dial, Icons.Rounded.Phone) {
val phoneNumber = "phoneNumber"
val deepLink = "connectyou://dial/{$phoneNumber}"
val navAction = "com.bnyro.contacts.DIAL"
val deepLinks = listOf(navDeepLink {
uriPattern = deepLink
action = navAction
})

fun getDeepLink(number: String) = "connectyou://dial/$number".toUri()
}

object Contacts : HomeRoutes("contacts", R.string.contacts, Icons.Rounded.Person)
object Messages : HomeRoutes("messages", R.string.messages, Icons.Rounded.Message)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.contacts.R
import com.bnyro.contacts.domain.model.CallLogEntry
import com.bnyro.contacts.presentation.components.NothingHere
import com.bnyro.contacts.presentation.components.NumberInput
import com.bnyro.contacts.presentation.components.PhoneNumberDisplay
Expand All @@ -47,7 +47,6 @@ import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.editor.components.ContactIconPlaceholder
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
import com.bnyro.contacts.util.CallLogHelper
import com.bnyro.contacts.util.PermissionHelper
import com.bnyro.contacts.util.SmsUtil
import com.bnyro.contacts.util.extension.removeLastChar
Expand All @@ -56,20 +55,18 @@ import com.bnyro.contacts.util.extension.removeLastChar
@Composable
fun CallLogsScreen(
contactsModel: ContactsModel,
dialerModel: CallModel,
themeModel: ThemeModel
) {
val callModel: CallModel = viewModel()
val context = LocalContext.current

var showNumberPicker by remember {
mutableStateOf(false)
mutableStateOf(callModel.initialPhoneNumber != null)
}
var numberToCall by remember {
mutableStateOf(dialerModel.initialPhoneNumber.orEmpty())
}
var callLog by remember {
mutableStateOf(emptyList<CallLogEntry>())
mutableStateOf(callModel.initialPhoneNumber.orEmpty())
}

val subscriptions = remember {
SmsUtil.getSubscriptions(context)
}
Expand All @@ -96,10 +93,7 @@ fun CallLogsScreen(
context.startActivity(intent)
}

LaunchedEffect(Unit) {
callLog = CallLogHelper.getCallLog(context)
}

val callLog = callModel.callLogs
Scaffold(
floatingActionButton = {
FloatingActionButton(
Expand Down Expand Up @@ -189,14 +183,14 @@ fun CallLogsScreen(
},
subscriptions = subscriptions,
onSubscriptionIndexChange = {
chosenSubInfo = subscriptions?.get(it)
chosenSubInfo = subscriptions.get(it)
}
)
}
}
}

LaunchedEffect(Unit) {
dialerModel.requestDefaultDialerApp(context)
callModel.requestDefaultDialerApp(context)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package com.bnyro.contacts.presentation.screens.calllog.model

import android.Manifest
import android.app.Application
import android.content.Context
import android.content.Intent
import android.telecom.TelecomManager
import androidx.lifecycle.ViewModel
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.bnyro.contacts.App
import com.bnyro.contacts.domain.model.CallLogEntry
import com.bnyro.contacts.navigation.HomeRoutes
import kotlinx.coroutines.launch

class CallModel : ViewModel() {
var initialPhoneNumber: String? = null
class CallModel(application: Application, savedStateHandle: SavedStateHandle) :
AndroidViewModel(application) {
val callLogRepository = (application as App).callLogRepository

val initialPhoneNumber = savedStateHandle.get<String>(HomeRoutes.Phone.phoneNumber)

var callLogs by mutableStateOf<List<CallLogEntry>>(emptyList(), policy = neverEqualPolicy())
private set

init {
viewModelScope.launch {
callLogs = callLogRepository.getCallLog()
}
}

fun requestDefaultDialerApp(context: Context) {
val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
import com.bnyro.contacts.presentation.screens.sms.model.SmsModel
Expand All @@ -24,7 +23,6 @@ abstract class BaseActivity : FragmentActivity() {
ContactsModel.Factory
}
val smsModel by viewModels<SmsModel>()
val callModel by viewModels<CallModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Loading

0 comments on commit 7223953

Please sign in to comment.