-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to import contacts from Sim card (#178)
- Loading branch information
Showing
7 changed files
with
212 additions
and
35 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
115 changes: 115 additions & 0 deletions
115
app/src/main/java/com/bnyro/contacts/ui/components/dialogs/SimImportDialog.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,115 @@ | ||
package com.bnyro.contacts.ui.components.dialogs | ||
|
||
import android.widget.Toast | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.Checkbox | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateListOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
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.obj.ContactData | ||
import com.bnyro.contacts.ui.models.ContactsModel | ||
import com.bnyro.contacts.util.SimContactsHelper | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
@Composable | ||
fun SimImportDialog( | ||
onDismissRequest: () -> Unit | ||
) { | ||
val simContacts = remember { mutableStateListOf<ContactData>() } | ||
val selectedContacts = remember { mutableStateListOf<ContactData>() } | ||
var isLoading by remember { | ||
mutableStateOf(true) | ||
} | ||
val context = LocalContext.current | ||
val contactsModel: ContactsModel = viewModel() | ||
|
||
LaunchedEffect(Unit) { | ||
withContext(Dispatchers.IO) { | ||
try { | ||
val contacts = SimContactsHelper.getSimContacts(context) | ||
simContacts.addAll(contacts) | ||
selectedContacts.addAll(contacts) | ||
} catch (e: Exception) { | ||
Toast.makeText(context, e.localizedMessage, Toast.LENGTH_LONG).show() | ||
} | ||
isLoading = false | ||
} | ||
} | ||
|
||
AlertDialog( | ||
title = { Text(stringResource(R.string.import_sim)) }, | ||
onDismissRequest = onDismissRequest, | ||
dismissButton = { | ||
DialogButton(stringResource(R.string.cancel)) { | ||
onDismissRequest.invoke() | ||
} | ||
}, | ||
confirmButton = { | ||
DialogButton(text = stringResource(R.string.okay)) { | ||
selectedContacts.forEach { contact -> | ||
contactsModel.createContact(context, contact) | ||
} | ||
onDismissRequest.invoke() | ||
} | ||
}, | ||
text = { | ||
if (isLoading) { | ||
Box( | ||
modifier = Modifier | ||
.height(300.dp) | ||
.fillMaxWidth(), | ||
contentAlignment = Alignment.Center | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
} else { | ||
LazyColumn( | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
items(simContacts) { contact -> | ||
Row( | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Checkbox( | ||
checked = selectedContacts.contains(contact), | ||
onCheckedChange = { | ||
if (it) selectedContacts.add(contact) | ||
else selectedContacts.remove(contact) | ||
} | ||
) | ||
Spacer(modifier = Modifier.width(10.dp)) | ||
Column { | ||
Text(text = contact.displayName.orEmpty()) | ||
Spacer(modifier = Modifier.height(3.dp)) | ||
Text(text = contact.numbers.firstOrNull()?.value.orEmpty()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} |
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
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
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
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/bnyro/contacts/util/SimContactsHelper.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,38 @@ | ||
package com.bnyro.contacts.util | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import com.bnyro.contacts.ext.longValue | ||
import com.bnyro.contacts.ext.stringValue | ||
import com.bnyro.contacts.obj.ContactData | ||
import com.bnyro.contacts.obj.ValueWithType | ||
|
||
|
||
object SimContactsHelper { | ||
fun getSimContacts(context: Context): List<ContactData> { | ||
val simUri = Uri.parse("content://icc/adn") | ||
val cursorSim = context.contentResolver.query(simUri, null, null, null, null) ?: return emptyList() | ||
val contacts = mutableListOf<ContactData>() | ||
|
||
while (cursorSim.moveToNext()) { | ||
val name = cursorSim.stringValue("name") | ||
val phoneNumber = cursorSim.stringValue("number") | ||
?.replace("\\D","") | ||
?.replace("&", "") | ||
// skip empty sim contacts | ||
if (name.isNullOrBlank() && phoneNumber.isNullOrBlank()) continue | ||
|
||
val nameParts = ContactsHelper.splitFullName(name) | ||
val contact = ContactData( | ||
displayName = name, | ||
firstName = nameParts.first, | ||
surName = nameParts.second, | ||
alternativeName = "${nameParts.first} ${nameParts.second}", | ||
numbers = listOfNotNull(phoneNumber?.let { ValueWithType(it, 0) }) | ||
) | ||
contacts.add(contact) | ||
} | ||
cursorSim.close() | ||
return contacts | ||
} | ||
} |
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