@@ -2,14 +2,17 @@ package com.afkanerd.deku.DefaultSMS.ui
22
33import android.app.Activity.RESULT_OK
44import android.content.Context
5+ import android.content.ContextWrapper
56import android.content.Intent
7+ import android.net.Uri
68import android.provider.BlockedNumberContract
79import android.provider.ContactsContract
810import android.provider.Telephony
911import android.text.InputType
1012import android.util.Log
1113import android.widget.Toast
1214import androidx.activity.compose.BackHandler
15+ import androidx.activity.compose.rememberLauncherForActivityResult
1316import androidx.activity.result.contract.ActivityResultContracts
1417import androidx.appcompat.app.AppCompatActivity
1518import androidx.compose.animation.expandIn
@@ -132,7 +135,6 @@ import com.afkanerd.deku.DefaultSMS.Models.Contacts
132135import com.afkanerd.deku.DefaultSMS.Models.Conversations.Conversation
133136import com.afkanerd.deku.DefaultSMS.Models.Conversations.ThreadedConversations
134137import com.afkanerd.deku.DefaultSMS.Models.Conversations.ThreadedConversationsHandler
135- import com.afkanerd.deku.DefaultSMS.Models.ExportImportHandlers
136138import com.afkanerd.deku.DefaultSMS.Models.Notifications
137139import com.afkanerd.deku.DefaultSMS.Models.SIMHandler
138140import com.afkanerd.deku.DefaultSMS.Models.ThreadsCount
@@ -149,6 +151,9 @@ import kotlinx.coroutines.CoroutineScope
149151import kotlinx.coroutines.Dispatchers
150152import kotlinx.coroutines.coroutineScope
151153import kotlinx.coroutines.launch
154+ import java.io.BufferedReader
155+ import java.io.FileOutputStream
156+ import java.io.InputStreamReader
152157import kotlin.math.exp
153158
154159enum class InboxType (val value : Int ) {
@@ -414,11 +419,61 @@ fun ModalDrawerSheetLayout(
414419@Composable
415420private fun MainDropDownMenu (
416421 expanded : Boolean = false,
417- importCallback : (() -> Unit ) ? = null ,
422+ conversationViewModel : ConversationsViewModel = ConversationsViewModel () ,
418423 dismissCallback : ((Boolean ) -> Unit )? = null,
419424) {
420425 val context = LocalContext .current
421426
427+ val exportLauncher = rememberLauncherForActivityResult(
428+ ActivityResultContracts .CreateDocument (" application/json" )) { uri ->
429+ println (uri)
430+ uri?.let {
431+ CoroutineScope (Dispatchers .IO ).launch {
432+ with (context.contentResolver.openFileDescriptor(uri, " w" )) {
433+ this ?.fileDescriptor.let { fd ->
434+ val fileOutputStream = FileOutputStream (fd);
435+ fileOutputStream.write(conversationViewModel
436+ .getAllExport(context).encodeToByteArray());
437+ // Let the document provider know you're done by closing the stream.
438+ fileOutputStream.close();
439+ }
440+ this ?.close();
441+
442+ CoroutineScope (Dispatchers .Main ).launch {
443+ Toast .makeText(context,
444+ context.getString(R .string.conversations_exported_complete),
445+ Toast .LENGTH_LONG ).show();
446+ }
447+ }
448+ }
449+ }
450+ }
451+
452+ val importLauncher = rememberLauncherForActivityResult(
453+ ActivityResultContracts .GetContent ()) { uri ->
454+ println (uri)
455+ uri?.let {
456+ CoroutineScope (Dispatchers .IO ).launch {
457+ val stringBuilder = StringBuilder ()
458+ context.contentResolver.openInputStream(uri)?.use { inputStream ->
459+ BufferedReader (InputStreamReader (inputStream)).use { reader ->
460+ var line: String? = reader.readLine()
461+ while (line != null ) {
462+ stringBuilder.append(line)
463+ line = reader.readLine()
464+ }
465+ }
466+ }
467+ conversationViewModel.importDetails = stringBuilder.toString()
468+ CoroutineScope (Dispatchers .Main ).launch {
469+ Toast .makeText(context,
470+ context.getString(R .string.conversations_import_complete),
471+ Toast .LENGTH_LONG ).show();
472+ }
473+ }
474+ }
475+ }
476+
422477 Box (modifier = Modifier
423478 .fillMaxWidth()
424479 .wrapContentSize(Alignment .TopEnd )
@@ -470,7 +525,8 @@ private fun MainDropDownMenu(
470525 },
471526 onClick = {
472527 dismissCallback?.let { it(false ) }
473- ExportImportHandlers .exportInbox(context)
528+ val filename = " Deku_SMS_All_Backup" + System .currentTimeMillis() + " .json" ;
529+ exportLauncher.launch(filename)
474530 }
475531 )
476532
@@ -484,8 +540,7 @@ private fun MainDropDownMenu(
484540 },
485541 onClick = {
486542 dismissCallback?.let { it(false ) }
487- // ExportImportHandlers.importInbox(context)
488- importCallback?.invoke()
543+ importLauncher.launch(" application/json" )
489544 }
490545 )
491546
@@ -509,6 +564,8 @@ private fun MainDropDownMenu(
509564 }
510565}
511566
567+
568+
512569@OptIn(ExperimentalMaterial3Api ::class , ExperimentalMaterialApi ::class ,
513570 ExperimentalFoundationApi ::class
514571)
@@ -630,9 +687,6 @@ fun ThreadConversationLayout(
630687
631688 MainDropDownMenu (
632689 expanded= rememberMenuExpanded,
633- importCallback = {
634- ExportImportHandlers .importInbox(context)
635- }
636690 ) {
637691 rememberMenuExpanded = it
638692 }
0 commit comments