Skip to content

Commit 0b9ce12

Browse files
committed
SMS message exporting, slightly better perf on win
1 parent a45cd2d commit 0b9ce12

File tree

9 files changed

+74
-10
lines changed

9 files changed

+74
-10
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
*.env
33
*-tmp
44
*.apk
5-
hooks.sh
5+
hooks.sh
6+
.vscode
7+
.idea

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Linux Android Backup is a tiny shell script & Flutter app that makes securely ba
1111
- Apps (.apk files of installed apps)
1212
- Internal storage (pictures, downloads, videos, Signal backups if enabled, etc)
1313
- Contacts (exported in vCard format)
14+
- SMS Messages (exported in CSV format; currently cannot be restored to a new device, viewable by opening the backup archive)
1415

1516
These 3 things are the majority of what most people would want to keep safe, but everybody has different expectations and requirements, so suggestions are welcome.
1617

backup-windows.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Write-Output "Installing dependencies and setting up environment..."
88
wsl sudo apt update
99
wsl sudo apt install p7zip-full curl bc dos2unix pv kdialog -y
1010
Write-Output "Converting files - this may take several minutes..."
11-
wsl bash -c "sudo find ./ -type f -print0 | sudo xargs -0 dos2unix --"
11+
wsl bash -c "sudo find ./ -name '*.sh' -type f -print0 | sudo xargs -0 dos2unix --"
1212
Clear-Host
1313
Write-Output "Ready to run the backup script."
1414
pause

companion_app/android/app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
66
<uses-permission android:maxSdkVersion="29" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
77
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
8+
<uses-permission android:name="android.permission.READ_SMS" />
9+
810
<application
911
android:requestLegacyExternalStorage="true"
1012
android:label="Linux Android Backup Companion"

companion_app/lib/main.dart

+44-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_contacts/flutter_contacts.dart';
33
import 'package:permission_handler/permission_handler.dart';
4+
import 'package:flutter_sms_inbox/flutter_sms_inbox.dart';
5+
import 'package:csv/csv.dart';
46
import "dart:io";
57

68
import 'package:device_info_plus/device_info_plus.dart';
@@ -40,6 +42,8 @@ class _HomeState extends State<Home> {
4042
bool showBackupProgress = false;
4143
int contactsAmountDatabase = 0;
4244
int contactsExported = 0;
45+
int smsMessageCount = 0;
46+
int smsMessagesExported = 0;
4347
// for restores
4448
bool showRestoreProgress = false;
4549
int contactsAmountFilesystem = 0;
@@ -48,7 +52,11 @@ class _HomeState extends State<Home> {
4852
Future<void> backup(BuildContext context) async {
4953
// Requests contacts & internal storage permissions
5054
if (await FlutterContacts.requestPermission() &&
51-
await Permission.storage.request().isGranted) {
55+
await Permission.storage.request().isGranted &&
56+
await Permission.sms.request().isGranted) {
57+
// create an instance of the SmsQuery class
58+
SmsQuery sms = SmsQuery();
59+
5260
// On Android 11 and later, request additional permissions.
5361
if ((await DeviceInfoPlugin().androidInfo).version.sdkInt! > 29 &&
5462
!await Permission.manageExternalStorage.request().isGranted) {
@@ -88,12 +96,40 @@ class _HomeState extends State<Home> {
8896
});
8997
}
9098

99+
// Export SMS messages.
100+
List<SmsMessage> messages = await sms.getAllSms;
101+
102+
setState(() {
103+
smsMessageCount = messages.length;
104+
});
105+
106+
// Process messages so they can be saved to a CSV file.
107+
List<List<String>> processedMessages = [];
108+
processedMessages.add(["ID", "Address", "Body", "Date"]);
109+
for (var i = 0; i < messages.length; i++) {
110+
List<String> message = [
111+
messages[i].id.toString(),
112+
messages[i].address.toString(),
113+
messages[i].body.toString(),
114+
messages[i].date.toString(),
115+
];
116+
processedMessages.add(message);
117+
setState(() {
118+
smsMessagesExported = i + 1;
119+
});
120+
}
121+
String csv = const ListToCsvConverter().convert(processedMessages);
122+
123+
final File sms_file_export = File(
124+
"/storage/emulated/0/linux-android-backup-temp/SMS_Messages.csv");
125+
sms_file_export.writeAsString(csv);
126+
91127
// Show a dialog if the export is complete
92128
showInfoDialog(context, "Data Exported",
93129
"Please continue the backup process on your computer.");
94130
} else {
95131
showInfoDialog(context, "Error",
96-
"Storage and/or contacts permissions have not been granted.");
132+
"Storage, SMS or contacts permissions have not been granted.");
97133
}
98134
}
99135

@@ -191,7 +227,11 @@ class _HomeState extends State<Home> {
191227
contactsExported.toString() +
192228
" contact(s) out of " +
193229
contactsAmountDatabase.toString() +
194-
".")),
230+
". Found " +
231+
smsMessageCount.toString() +
232+
" SMS messages to process, of which " +
233+
smsMessagesExported.toString() +
234+
" have been exported.")),
195235
const Divider(
196236
color: Color.fromARGB(31, 44, 44, 44),
197237
height: 25,
@@ -200,7 +240,7 @@ class _HomeState extends State<Home> {
200240
endIndent: 5,
201241
),
202242
const Text(
203-
"Upon restoring a backup, press the button below to automatically import all contacts.",
243+
"Upon restoring a backup, press the button below to automatically import all contacts. SMS message importing isn't currently available, but you can view your messages by opening your backup archive.",
204244
),
205245
ElevatedButton(
206246
onPressed: () {

companion_app/pubspec.lock

+14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ packages:
3636
url: "https://pub.dartlang.org"
3737
source: hosted
3838
version: "1.16.0"
39+
csv:
40+
dependency: "direct main"
41+
description:
42+
name: csv
43+
url: "https://pub.dartlang.org"
44+
source: hosted
45+
version: "5.0.1"
3946
device_info_plus:
4047
dependency: "direct main"
4148
description:
@@ -118,6 +125,13 @@ packages:
118125
url: "https://pub.dartlang.org"
119126
source: hosted
120127
version: "1.0.4"
128+
flutter_sms_inbox:
129+
dependency: "direct main"
130+
description:
131+
name: flutter_sms_inbox
132+
url: "https://pub.dartlang.org"
133+
source: hosted
134+
version: "1.0.2"
121135
flutter_test:
122136
dependency: "direct dev"
123137
description: flutter

companion_app/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ dependencies:
3737
flutter_contacts: ^1.1.5
3838
permission_handler: ^8.3.0
3939
device_info_plus: ^3.2.0
40+
flutter_sms_inbox: ^1.0.2
41+
csv: ^5.0.1
4042

4143
dev_dependencies:
4244
flutter_test:

functions/backup_func.sh

+6-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ function backup_func() {
5151
)
5252
done
5353

54-
# Export contacts
55-
cecho "Exporting contacts (as vCard)."
54+
# Export contacts and SMS messages
55+
cecho "Exporting contacts (as vCard) and SMS messages (as CSV)."
5656
mkdir ./backup-tmp/Contacts
57+
mkdir ./backup-tmp/SMS
5758
get_file /storage/emulated/0/linux-android-backup-temp . ./backup-tmp/Contacts
59+
mv ./backup-tmp/Contacts/SMS_Messages.csv ./backup-tmp/SMS
5860
cecho "Removing temporary files created by the companion app."
5961
adb shell rm -rf /storage/emulated/0/linux-android-backup-temp
60-
6162
# Export internal storage. We're not using adb pull due to reliability issues
6263
cecho "Exporting internal storage - this will take a while."
6364
mkdir ./backup-tmp/Storage
@@ -102,5 +103,7 @@ function backup_func() {
102103
fi
103104

104105
cecho "Backed up successfully."
106+
cecho "Note: SMS messages cannot be restored by Linux Android Backup at the moment. They are included in the backup archive for your own purposes."
107+
cecho "You can find them by opening the backup archive using 7-Zip and navigating to the 'SMS' directory. This data can be opened in LibreOffice or Microsoft Excel."
105108
rm -rf backup-tmp > /dev/null
106109
}

website/fonts/OFL.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2019 The Baloo 2 Project Authors (https://github.com/EkType/Baloo2)
1+
Copyright 2019 The Baloo 2 Project Authors (https://github.com/EkType/Baloo2)
22

33
This Font Software is licensed under the SIL Open Font License, Version 1.1.
44
This license is copied below, and is also available with a FAQ at:

0 commit comments

Comments
 (0)