/
AndSync.pl
executable file
·1420 lines (1204 loc) · 42.1 KB
/
AndSync.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
###############################################################################
#
# AndSync - Synchronize, Backup and Restore your Android device
#
# AndSync Copyright (C) 2012-2020 Winny Mathew Kurian (WiZarD)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
# ABOUT_END
#
# Prerequisites
# OS - Any OS
# Android SDK - ADB shell
# $ sudo apt install adb (on 20.04)
# Perl - Class::Struct File::Path File::Basename File::Fetch
# File::Copy Archive::Zip, (File::Fetch->Version)
# $ sudo perl -MCPAN -e shell
# Revisions
###############################################################################
# Date Version Author Comments
###############################################################################
# 28.08.2012 1.0 WiZarD Initial version
# 13.09.2012 1.1 WiZarD New features and bug fixes
# 18.09.2012 1.2 WiZarD Added new backup and restore method
# 23.09.2012 1.3 WiZarD Self updater, Prompt if backup exists
# 12.04.2014 1.4 WiZarD Fixed bugs, Enhancements (Not released)
# 18.10.2020 1.5 WiZarD Support Android Pie and above
###############################################################################
use strict;
use Class::Struct;
use File::Path;
use File::Basename;
use File::Fetch;
use File::Copy;
use Archive::Zip;
# use Version;
# 'Package' structure holds APK details parsed from ADB report
struct Package => {
name => '$',
path => '$',
version_code => '$',
version_name => '$',
data_path => '$',
system => '$',
};
# 'Device' structure holds device properties
struct Device => {
serial => '$',
manufacturer => '$',
product_model => '$',
android => '$',
rooted => '$',
};
# 'Settings' structure contains various settings using by this script
struct Settings => {
BackupSystemApps => '$',
BackupData => '$',
RestoreSystemApps => '$',
RestoreData => '$',
RemoveData => '$',
SyncMissing => '$',
DeleteSyncData => '$',
DeleteCompleted => '$',
InstallSD => '$',
UseNewADBCmd => '$',
ConfirmActions => '$',
OnlyBaseAPK => '$',
};
# Android System Namespaces used by different manufacturers
# Update this array as required
my @SYSTEM_NAMESPACE =
(
#"com.google",
"com.android", "com.sec",
"com.cyanogenmod",
"org.codeaurora", "com.qti", "com.qualcomm.qti",
"com.sonyericsson", "com.motorola", "com.htc", "com.samsung",
"com.lge", "com.huawei", "com.zte", "com.sony",
"com.asus"
);
# Enable for debug prints
my $DEBUG = 0;
my $DEBUG_PACKAGES = 0;
my $settings = Settings->new();
my @ADB_DEVICES;
my $ADB_DEVICES_COUNT;
my $DEVICE;
my $SERIAL;
# Path to RAW file 'AndSync.pl' on GitHub
my $UPDATER_GIT_FILE = "http://raw.github.com/wizwin/AndSync/master/AndSync.pl";
# Temporary path used by self updater
my $UPDATER_TMP_DIR = "./Updater.tmp";
# Log file name (not used now)
my $LOG_FILE = "./AndSync.log";
# Settings file
my $SETTINGS_FILE = "./Settings.ini";
# Sync data directory
my $SYNC_DIRECTORY = "./SyncData";
# Backup directory
my $BACKUP_DIRECTORY = "./Backup";
# Android versions
# ICS
my $VERSION_ICS = version->declare("4.0.0");
# Pie
my $VERSION_PIE = version->declare("9.0.0");
my $SYNC_DEVICE_MASTER = "";
my $SYNC_DEVICE_SLAVE = "";
my $SERIAL_DEVICE_MASTER = "";
my $SERIAL_DEVICE_SLAVE = "";
my $choice = " ";
# Print debug messages
sub printD
{
my $dbgMessage = shift;
if ($DEBUG == 1) {
print "DEBUG: " . $dbgMessage . "\n";
}
}
# Print out a banner for information
sub PrintBanner
{
print "\nAndSync v1.5 - Sync your Andorid devices";
print "\nCopyright (C) 2012-2020, Winny Mathew Kurian (WiZarD)\n";
}
sub CheckExec
{
my $check = `sh -c 'command -v $_[0]'`;
return $check;
}
# Print one time banner
PrintBanner();
# Check if we have pre-requisites installed
CheckExec('adb') or die "\nPre-requisite missing: adb\n";
# Make sure an instance of ADB server is running before we start
system("adb start-server");
# Refresh the device list
RefreshDeviceList();
# Read settings file
ParseSettings();
# We need 'SyncData' directory for storing files
if (not -d $SYNC_DIRECTORY)
{
mkdir $SYNC_DIRECTORY;
}
# We need 'Backup' directory for storing files
if (not -d $BACKUP_DIRECTORY)
{
mkdir $BACKUP_DIRECTORY;
}
# Clear log file
if (-e $LOG_FILE)
{
system ("echo \"\" > $LOG_FILE");
}
sub isNumber {
$_[0] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
}
# This is the main loop for the MAIN menu
while(1) {
DisplayDevices();
print "\n==================== MENU =====================\n\n";
print "0. Exit\n";
print "1. Sync application between devices\n";
print "2. Backup applications\n";
print "3. Restore applications\n";
print "4. Uninstall applications\n";
print "5. Restart ADB server\n";
print "6. Refresh device list\n";
print "7. Settings\n";
print "\n[0-7]> ";
chomp($choice = <STDIN>);
redo if not isNumber($choice);
if ($choice < 0 || $choice > 7) {
print "\nPlease enter a menu option (0-7)\n";
redo;
}
# Exit after clearing sync data if settings say so
if ($choice == 0) {
if ($settings->DeleteSyncData) {
rmtree("$SYNC_DIRECTORY");
}
exit;
}
elsif ($choice == 1) {
if ($ADB_DEVICES_COUNT <= 1) {
print "\nYou need to connect more than one device to perform sync\n";
redo;
}
$SYNC_DEVICE_MASTER = SelectDevice("\nSelect a device to sync from");
if ($SYNC_DEVICE_MASTER >= 0) {
$SYNC_DEVICE_SLAVE = SelectDevice("\nSelect a device to sync to");
if ($SYNC_DEVICE_SLAVE >= 0) {
SyncDevices(
$ADB_DEVICES[$SYNC_DEVICE_MASTER]->serial,
$ADB_DEVICES[$SYNC_DEVICE_SLAVE]->serial);
}
}
}
elsif ($choice == 2) {
if ($ADB_DEVICES_COUNT < 1) {
print "\nPlease connect an Android device.\n";
redo;
}
$DEVICE = SelectDevice("\nSelect a device to backup");
if ($DEVICE >= 0) {
BackupDevice($ADB_DEVICES[$DEVICE]->serial);
}
}
elsif ($choice == 3) {
if ($ADB_DEVICES_COUNT < 1) {
print "\nPlease connect an Android device.\n";
redo;
}
$DEVICE = SelectDevice("\nSelect a device to restore");
if ($DEVICE >= 0) {
RestoreDevice($ADB_DEVICES[$DEVICE]->serial);
}
}
elsif ($choice == 4) {
if ($ADB_DEVICES_COUNT < 1) {
print "\nPlease connect an Android device.\n";
redo;
}
$DEVICE = SelectDevice("\nSelect a device to uninstall applications");
if ($DEVICE >= 0) {
UninstallApps($ADB_DEVICES[$DEVICE]->serial);
}
}
elsif ($choice == 5) {
print "\n";
system("adb kill-server");
system("adb start-server");
}
elsif ($choice == 6) {
undef @ADB_DEVICES;
RefreshDeviceList();
}
elsif ($choice == 7) {
SettingsMain();
}
}
# This subroutine is used to save the settings file
sub WriteSettings
{
open (hSettingsFile, ">$SETTINGS_FILE");
print hSettingsFile "BackupSystemApps=" . $settings->BackupSystemApps . "\n";
print hSettingsFile "BackupData=" . $settings->BackupData . "\n";
print hSettingsFile "RestoreSystemApps=" . $settings->RestoreSystemApps . "\n";
print hSettingsFile "RestoreData=" . $settings->RestoreData . "\n";
print hSettingsFile "RemoveData=" . $settings->RemoveData . "\n";
print hSettingsFile "SyncMissing=" . $settings->SyncMissing . "\n";
print hSettingsFile "DeleteSyncData=" . $settings->DeleteSyncData . "\n";
print hSettingsFile "DeleteCompleted=" . $settings->DeleteCompleted . "\n";
print hSettingsFile "InstallSD=" . $settings->InstallSD . "\n";
print hSettingsFile "UseNewADBCmd=" . $settings->UseNewADBCmd . "\n";
print hSettingsFile "OnlyBaseAPK=" . $settings->OnlyBaseAPK . "\n";
print hSettingsFile "ConfirmActions=" . $settings->ConfirmActions . "\n";
close (hSettingsFile);
}
# This is the main loop for SETTINGS menu
sub SettingsMain
{
while(1) {
printf "\n================== SETTINGS ===================\n\n";
printf " 0. Back\n";
printf " 1. Backup system applications [%4s ]\n",
$settings->BackupSystemApps ? "Yes" : "No";
printf " 2. Backup data on rooted devices [%4s ]\n",
$settings->BackupData ? "Yes" : "No";
printf " 3. Restore system applications [%4s ]\n",
$settings->RestoreSystemApps ? "Yes" : "No";
printf " 4. Restore data on rooted devices [%4s ]\n",
$settings->RestoreData ? "Yes" : "No";
printf " 5. Remove data while un-installing [%4s ]\n",
$settings->RemoveData ? "Yes" : "No";
printf " 6. Sync missing applications [%4s ]\n",
$settings->SyncMissing ? "Yes" : "No";
printf " 7. Delete sync data on exit [%4s ]\n",
$settings->DeleteSyncData ? "Yes" : "No";
printf " 8. Delete package once installed [%4s ]\n",
$settings->DeleteCompleted ? "Yes" : "No";
printf " 9. Install apps on SD Card [%4s ]\n",
$settings->InstallSD ? "Yes" : "No";
printf "10. Use new backup-restore (above GB) [%4s ]\n",
$settings->UseNewADBCmd ? "Yes" : "No";
printf "11. Backup only Base APK (above Oreo) [%4s ]\n",
$settings->OnlyBaseAPK ? "Yes" : "No";
printf "12. Confirm all actions [%4s ]\n",
$settings->ConfirmActions ? "Yes" : "No";
printf "13. Update this script\n";
printf "14. About AndSync\n";
printf "\n[0-14]> ";
chomp($choice = <STDIN>);
redo if not isNumber($choice);
if ($choice < 0 || $choice > 14) {
print "\nPlease enter a menu option (0-14)\n";
redo;
}
if ($choice == 0) {
# Save settings and return
WriteSettings();
return;
}
elsif ($choice == 1) {
$settings->BackupSystemApps($settings->BackupSystemApps ? 0 : 1);
}
elsif ($choice == 2) {
$settings->BackupData($settings->BackupData ? 0 : 1);
}
elsif ($choice == 3) {
$settings->RestoreSystemApps($settings->RestoreSystemApps ? 0 : 1);
}
elsif ($choice == 4) {
$settings->RestoreData($settings->RestoreData ? 0 : 1);
}
elsif ($choice == 5) {
$settings->RemoveData($settings->RemoveData ? 0 : 1);
}
elsif ($choice == 6) {
$settings->SyncMissing($settings->SyncMissing ? 0 : 1);
}
elsif ($choice == 7) {
$settings->DeleteSyncData($settings->DeleteSyncData ? 0 : 1);
}
elsif ($choice == 8) {
$settings->DeleteCompleted($settings->DeleteCompleted ? 0 : 1);
}
elsif ($choice == 9) {
$settings->InstallSD($settings->InstallSD ? 0 : 1);
}
elsif ($choice == 10) {
$settings->UseNewADBCmd($settings->UseNewADBCmd ? 0 : 1);
}
elsif ($choice == 11) {
$settings->OnlyBaseAPK($settings->OnlyBaseAPK ? 0 : 1);
}
elsif ($choice == 12) {
$settings->ConfirmActions($settings->ConfirmActions ? 0 : 1);
}
elsif ($choice == 13) {
PerformSelfUpdate();
}
elsif ($choice == 14) {
# Open self and print out GNU Copyright notice
open SELF, __FILE__ or redo;
while (<SELF>) {
next if (/#!\//);
last if (/# ABOUT_END/);
print $_;
}
close(SELF);
print "\nIf you see a smiley [:)] your device is rooted!\n";
}
}
}
sub PerformSelfUpdate
{
$File::Fetch::WARN = 0;
my $Updater;
my $Path;
if (ConfirmPrompt("\nProceed with script update")) {
print "\nDownloading script from server...\n";
$Updater = File::Fetch->new(uri => $UPDATER_GIT_FILE);
if ($Updater) {
$Path = $Updater->fetch(to => $UPDATER_TMP_DIR);
if ($Path) {
copy("$UPDATER_TMP_DIR/AndSync.pl", "AndSync.pl");
rmtree($UPDATER_TMP_DIR);
if (ConfirmPrompt("\nUpdated. Restart script")) {
exec($^X, $0, @ARGV);
} else {
return;
}
}
}
# We will get here only only if the update fails
rmtree($UPDATER_TMP_DIR);
print "\nUpdate failed! Please make sure you are connected to internet.\n"
}
}
# Using ADB update the list of devices attached
sub RefreshDeviceList
{
my $ADB_DEVICES_CMD;
my @DEVICES;
my $PROP_MANUFACTURER;
my $PROP_PROD_MODEL;
my $PROP_ANDROID_VERSION;
my $IS_ROOTED;
# Get a list of devices, strip off extra details and split it
# into an array of serial numbers. No more Cygwin deps :p
$ADB_DEVICES_CMD = `adb devices`;
$ADB_DEVICES_CMD =~ s/List of devices attached//g;
$ADB_DEVICES_CMD =~ s/device//g;
$ADB_DEVICES_CMD =~ s/\R//g;
$ADB_DEVICES_CMD =~ s/ //g;
@DEVICES = split /\t/, $ADB_DEVICES_CMD;
# Get number of devices connected
$ADB_DEVICES_COUNT = scalar(@DEVICES);
foreach $SERIAL (@DEVICES) {
$SERIAL =~ s/\R//g;
# FIXME: We need to check device autherization here so that script won't abort
# for a device that is connected for the first time
$PROP_MANUFACTURER = `adb -s $SERIAL shell getprop ro.product.manufacturer`;
$PROP_PROD_MODEL = `adb -s $SERIAL shell getprop ro.product.model`;
$PROP_ANDROID_VERSION = `adb -s $SERIAL shell getprop ro.build.version.release`;
$IS_ROOTED = `adb -s $SERIAL shell ls /system/xbin/su 2>/dev/null`;
# Remove all newlines
$PROP_MANUFACTURER =~ s/\R//g;
$PROP_PROD_MODEL =~ s/\R//g;
$PROP_ANDROID_VERSION =~ s/\R//g;
$IS_ROOTED =~ s/\R//g;
# Check if we are rooted (Is there a better way?)
if ($IS_ROOTED =~ /\/system\/xbin\/su$/) {
$IS_ROOTED = 1;
} else {
$IS_ROOTED = 0;
}
$PROP_ANDROID_VERSION = version->parse($PROP_ANDROID_VERSION);
push @ADB_DEVICES, Device->new(
serial => $SERIAL,
manufacturer => $PROP_MANUFACTURER,
product_model => $PROP_PROD_MODEL,
android => $PROP_ANDROID_VERSION,
rooted => $IS_ROOTED);
}
}
# Smiley face means your device is rooted :)
sub DisplayDevices
{
my $nDevice = 0;
if ($ADB_DEVICES_COUNT > 0) {
print "\n============ Connected Devices [$ADB_DEVICES_COUNT] ============\n\n";
} else {
print "\nNo Android device connected!\n";
}
while ($nDevice < $ADB_DEVICES_COUNT) {
$nDevice++;
printf "%2d: %-14s [ %10s ] - %-16s%s\n", $nDevice,
$ADB_DEVICES[$nDevice-1]->manufacturer,
$ADB_DEVICES[$nDevice-1]->product_model,
$ADB_DEVICES[$nDevice-1]->serial,
$ADB_DEVICES[$nDevice-1]->rooted ? " [:)]" : "";
}
}
# Checks if the given app is a system app (Not very amazing, but no other way now)
#
# The package is marked as system if we find this is a system application
#
sub MarkSystemApp
{
my $package = shift;
my $packageName = $package->name;
my $packagePath = $package->path;
my $bSys = 0;
# Mark framework-res apk as a system app
if (($packageName =~ /^android/) || ($packagePath =~ /^\/system\/app|priv-app/)) {
$bSys = 1;
}
else {
foreach my $namespace (@SYSTEM_NAMESPACE) {
if ($packageName =~ /^$namespace.[\d\S]+/) {
$bSys = 1;
last;
}
}
}
$package->system($bSys);
return $bSys;
}
sub BackupDevice
{
$SERIAL = shift;
my $nPackages = 0;
my $nPackagesDone = 0;
my $VersionDev;
my $BackupSystemApps;
my @apk_files;
return if ($ADB_DEVICES_COUNT < 1);
# On newer devices use 'adb backup' if user has configured so else
# proceed with normal backup method.
$VersionDev = GetDeviceProperty($SERIAL, "android");
if ($settings->UseNewADBCmd && ($VersionDev > $VERSION_ICS)) {
if (ConfirmPrompt("\nProceed with backup")) {
$BackupSystemApps = $settings->BackupSystemApps ? "-system" : "-nosystem";
if (-e "$BACKUP_DIRECTORY/$SERIAL/$SERIAL.ab") {
if (not ConfirmPrompt("\nOverwrite existing backup")) {
return;
}
}
print "\nPlease confirm backup operation on your device [$SERIAL]...\n";
system("adb -s $SERIAL backup -f $BACKUP_DIRECTORY/$SERIAL/$SERIAL.ab -apk $BackupSystemApps -all");
if ($? < 0) {
print "\nBackup failed!\n";
} elsif ($? == 0) {
print "\nBackup complete!\n";
}
}
}
else {
RetriveDeviceReport($SERIAL);
my %phashMaster = ParseADBReport($SERIAL);
$nPackages = scalar(keys(%phashMaster));
print "\nFound $nPackages applications in $SERIAL\n";
if (ConfirmPrompt("\nProceed with backup")) {
@apk_files = glob "$BACKUP_DIRECTORY/$SERIAL/*.apk";
if (-e "$BACKUP_DIRECTORY/$SERIAL" && (scalar(@apk_files) > 1)) {
if (not ConfirmPrompt("\nOverwrite existing backups")) {
return;
}
}
$nPackagesDone = PullPackages(\%phashMaster, $SERIAL, $BACKUP_DIRECTORY);
}
print "\nTotal $nPackages packages. $nPackagesDone packages were backed up. " .
($nPackages - $nPackagesDone) . " packages were ignored.\n";
}
}
sub RestoreDevice
{
$SERIAL = shift;
my $DIR = $SERIAL;
my $nPackages = 0;
my $nPackagesDone = 0;
my $packageBaseName;
my $VersionDev;
my @apk_files;
my $package = Package->new();
return if ($ADB_DEVICES_COUNT < 1);
# On newer devices use 'adb restore' if user has configured so else
# proceed with normal restore method.
$VersionDev = GetDeviceProperty($SERIAL, "android");
if ($settings->UseNewADBCmd && ($VersionDev > $VERSION_ICS)) {
if (not -e "$BACKUP_DIRECTORY/$DIR/$DIR.ab") {
print "No data found to restore for $SERIAL...\n";
return;
}
if (ConfirmPrompt("\nProceed with restore")) {
print "\nPlease confirm restore operation on your device [$SERIAL]...\n";
system("adb -s $SERIAL restore $BACKUP_DIRECTORY/$DIR/$DIR.ab");
}
if ($? < 0) {
print "\nRestore failed!\n";
} elsif ($? == 0) {
print "\nRestore complete!\n";
}
return;
}
while (1) {
while (1) {
if (not -d "$BACKUP_DIRECTORY/$DIR/") {
print "\nNo backup data found for $DIR\n";
if (ConfirmPrompt("\nWould you like to restore from another device")) {
print "\nEnter serial number of the device: ";
chomp($DIR = <STDIN>);
return if (not $DIR);
redo;
} else {
return;
}
}
else {
last;
}
}
# This will get us file names with path
@apk_files = glob "$BACKUP_DIRECTORY/$DIR/*.apk";
$nPackages = scalar(@apk_files);
if ($nPackages < 1) {
print "\nThere are no packages to restore.\n";
redo;
} else {
last;
}
}
my $rooted = isDeviceRooted($SERIAL);
my $installSD = $settings->InstallSD ? "-s" : "";
print "\nFound $nPackages packages to restore\n";
if (ConfirmPrompt("\nProceed with restore")) {
foreach my $packageName (@apk_files) {
if (not $settings->RestoreSystemApps) {
$package->name($packageName);
$package->path(""); # FIXME
MarkSystemApp($package);
next if $package->system;
}
$packageBaseName = basename("$packageName");
$packageBaseName =~ s/\.[^.]+$//;
print "\nRestoring $packageBaseName\n";
system("adb -s $SERIAL install $installSD $packageName");
if ($? < 0) {
print "\n[Error] While installing package: $packageName. Continuing...\n";
next;
}
next if (($? >> 8) & 127);
if (($? == 0) && $rooted && $settings->RestoreData) {
print "\nRestoring data for $packageBaseName\n";
# Remove the extension from the name
$packageName =~ s/\.[^.]+$//;
# Extract the package data
print "\nExtracting data...\n";
my $zip = Archive::Zip->new("$packageName.zip");
$zip->extractTree("", "$BACKUP_DIRECTORY/$DIR/");
# FIXME: We have not preserved any metadata for restore,
# hence data path is hardcoded for now
system("adb -s $SERIAL push $packageName /data/data/$packageBaseName");
if ($? < 0) {
print "\n[Error] While installing data for: $packageName. Continuing...\n";
}
# Remove data directory
rmtree("$packageName");
}
$nPackagesDone++;
}
print "\nTotal $nPackages packages. $nPackagesDone packages were restored. " .
($nPackages - $nPackagesDone) . " packages were ignored.\n";
}
}
sub UninstallApps
{
$SERIAL = shift;
my $nPackages = 0;
my $nPackagesDone = 0;
return if ($ADB_DEVICES_COUNT < 1);
RetriveDeviceReport($SERIAL);
my %phashUninstall = ParseADBReport($SERIAL);
$nPackages = scalar(keys(%phashUninstall));
print "\nFound $nPackages applications in $SERIAL to uninstall\n";
if (ConfirmPrompt("\nProceed with Uninstall")) {
$nPackagesDone = UninstallPackages(\%phashUninstall, $SERIAL);
}
print "\nTotal $nPackages packages. $nPackagesDone packages were unistalled. " .
($nPackages - $nPackagesDone) . " packages were ignored.\n";
}
sub ConfirmPrompt
{
my $PROMPT = $_[0];
my $choice;
if (not $settings->ConfirmActions) {
return 1;
}
while (1) {
print "$PROMPT (Yes/No): ";
$choice = <STDIN>;
chomp ($choice);
if (not $choice =~ /^YN/i) {
return ($choice =~ /^Y/i) ? 1 : 0;
} else {
next;
}
}
}
sub SelectDevice
{
my $PROMPT = $_[0];
while (1) {
print "$PROMPT ('0' to Go Back): ";
$DEVICE = <STDIN>;
chomp ($DEVICE);
$DEVICE--;
if (($DEVICE >= $ADB_DEVICES_COUNT) || ($DEVICE < 0)) {
if ($DEVICE < 0) {
return $DEVICE;
} else {
next;
}
} else {
last;
}
}
return $DEVICE;
}
sub RetriveDeviceReport
{
$SERIAL = $_[0];
my $UPDATE = 1;
my $VersionDev;
if (-e "$SYNC_DIRECTORY/$SERIAL.txt") {
$UPDATE = not ConfirmPrompt("\nSync data found for $SERIAL. Use this?");
}
if ($UPDATE) {
print "\nRetriving device and application details from $SERIAL...\n";
$VersionDev = GetDeviceProperty($SERIAL, "android");
if ($VersionDev < $VERSION_PIE) {
system("adb -s $SERIAL bugreport > $SYNC_DIRECTORY/$SERIAL.txt");
} else {
# Newer bugreports are packaged as zip file and we need to
# unzip them to get the txt version of bug report
if (not -d "$SYNC_DIRECTORY/$SERIAL") {
mkdir "$SYNC_DIRECTORY/$SERIAL";
}
if (not -e "$SYNC_DIRECTORY/$SERIAL/$SERIAL.zip") {
system("adb -s $SERIAL bugreport $SYNC_DIRECTORY/$SERIAL/$SERIAL.zip");
}
system("unzip -q -o $SYNC_DIRECTORY/$SERIAL/$SERIAL.zip -d $SYNC_DIRECTORY/$SERIAL");
system("mv `find $SYNC_DIRECTORY/$SERIAL -name bugreport-*.txt` $SYNC_DIRECTORY/$SERIAL.txt");
}
}
}
# Perform device sync
sub SyncDevices
{
my $nPackagesPulled = 0;
my $nPackagesPushed = 0;
$SERIAL_DEVICE_MASTER = $_[0];
$SERIAL_DEVICE_SLAVE = $_[1];
return if ($ADB_DEVICES_COUNT <= 1);
print "\nYou have selected to sync $SERIAL_DEVICE_SLAVE with $SERIAL_DEVICE_MASTER\n";
RetriveDeviceReport($SERIAL_DEVICE_MASTER);
RetriveDeviceReport($SERIAL_DEVICE_SLAVE);
my %phashMaster = ParseADBReport($SERIAL_DEVICE_MASTER);
print "\nFound " . scalar(keys(%phashMaster)) . " applications in $SERIAL_DEVICE_MASTER\n";
my %phashSlave = ParseADBReport($SERIAL_DEVICE_SLAVE);
print "Found " . scalar(keys(%phashSlave)) . " applications in $SERIAL_DEVICE_SLAVE\n\n";
my %phashUpdate = ResolvePackageSync(\%phashMaster, \%phashSlave);
print "\nFound " . scalar(keys(%phashUpdate)) . " applications to be updated\n";
if (ConfirmPrompt("\nDo you wish to proceed with sync?")) {
$nPackagesPulled = PullPackages(\%phashUpdate, $SERIAL_DEVICE_MASTER, $SYNC_DIRECTORY);
print "\n";
$nPackagesPushed = PushPackages(\%phashUpdate, $SERIAL_DEVICE_MASTER,
$SERIAL_DEVICE_SLAVE, $SYNC_DIRECTORY);
}
print "\nSynced " . $nPackagesPushed . " packages\n";
}
# Install packages and push appliction data downloaded from a
# MASTER sync device to a SLAVE sync device
sub PushPackages
{
my $params = shift;
my %phashUpdate = %$params;
$SERIAL_DEVICE_MASTER = shift;
$SERIAL_DEVICE_SLAVE = shift;
my $DIRECTORY = shift;
my $nPackagesDone = 0;
my $dataPath;
my $rooted = isDeviceRooted($SERIAL_DEVICE_MASTER);
my $installSD = $settings->InstallSD ? "-s" : "";
while(my ($packageName, $packageUpdate) = each(%phashUpdate)) {
if (ConfirmPrompt("\nUpdate $packageName")) {
if (not $settings->RestoreSystemApps) {
next if $packageUpdate->system;
}
print "\nUpdating $packageName\n";
system("adb -s $SERIAL_DEVICE_SLAVE install -r $installSD $DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName.apk");
if ($? < 0) {
print "\n[Error] While installing package: $packageName. Continuing...\n";
next;
}
next if (($? >> 8) & 127);
# Now restore data if the device is rooted
if (($? == 0) && $rooted && $settings->RestoreData) {
print "\nUpdating data for $packageName\n";
# Extract the package data
print "\nExtracting data...\n";
my $zip = Archive::Zip->new("$DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName.zip");
$zip->extractTree("", "$DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName");
system("adb -s $SERIAL_DEVICE_SLAVE push $DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName $dataPath");
if ($? < 0) {
print "\n[Error] While installing data for: $packageName. Continuing...\n";
}
# Remove data directory
rmtree("$DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName");
if ($settings->DeleteCompleted) {
unlink("$DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName.zip");
}
}
# TODO: Check for return code and delete the package if successful
if ($settings->DeleteCompleted) {
unlink("$DIRECTORY/$SERIAL_DEVICE_MASTER/$packageName.apk");
}
$nPackagesDone++;
}
}
return $nPackagesDone;
}
# Retrive packages and data from a device
sub PullPackages
{
my $params = shift;
my %phashUpdate = %$params;
$SERIAL_DEVICE_MASTER = shift;
my $DIRECTORY = shift;
my $apkPath;
my $dataPath;
my $nPackages = 0;
my $rooted = isDeviceRooted($SERIAL_DEVICE_MASTER);
my $VersionDev;
my $cmdPull;
$VersionDev = GetDeviceProperty($SERIAL, "android");
# Pie and above keeps application and libraries in its own directories
# So create a directory per device serial
if ($VersionDev >= $VERSION_PIE) {
if (not -d "$DIRECTORY/$SERIAL_DEVICE_MASTER") {
mkdir "$DIRECTORY/$SERIAL_DEVICE_MASTER";
}
}
while(my ($packageName, $packageUpdate) = each(%phashUpdate)) {
$apkPath = $packageUpdate->path;
if (not $settings->BackupSystemApps) {
next if $packageUpdate->system;
}
if ($VersionDev >= $VERSION_PIE) {
if ($settings->OnlyBaseAPK) {
if ($packageUpdate->system) {
$apkPath = $apkPath . "/" . basename("$apkPath") . ".apk";
} else {
$apkPath = $apkPath . "/base.apk";
}
}
}
$cmdPull = "adb -s $SERIAL_DEVICE_MASTER pull $apkPath $DIRECTORY/$SERIAL_DEVICE_MASTER/";
print "\nRetriving package $apkPath\n";
if ($VersionDev < $VERSION_PIE) {
$cmdPull = $cmdPull . "$packageName.apk";
} else {
if ($settings->OnlyBaseAPK) {
$cmdPull = $cmdPull . "$packageName.apk";
} else {
$cmdPull = $cmdPull . "$packageName";
}
}
printD("ADB Pull Command: $cmdPull");
system($cmdPull);
if ($? < 0) {
print "\n[Error] While retriving package: $packageName. Continuing...\n";
next;
}
next if (($? >> 8) & 127);