Exemplo n.º 1
0
        private void InitDrives()
        {
            Action <DriveDetailed, int> setAvailableAndIndex = (DriveDetailed d, int i) =>
            {
                const int extraSpace = 512 * 1024 * 1024;
                d.AvailableForTest = d.BytesFree > FileSize + extraSpace;
                d.DisplayIndex     = d.BytesFree > FileSize + extraSpace ? (i < 9 ? (i + 1).ToString()[0] : '.') : ' ';
            };

            try
            {
                if (Device.RuntimePlatform == Device.Android)
                {
                    androidDrives = DependencyService.Get <IAndroidDrives>()?.GetDrives();

                    var  i        = 0;
                    long prevSize = -1;

                    foreach (var ad in androidDrives)
                    {
                        setAvailableAndIndex(ad, i);
                        i++;
                        if (ad.BytesFree == prevSize)
                        {
                            ad.ShowDiveIsSameMessage = true;
                        }
                        prevSize = ad.BytesFree;
                    }
                }
                else
                {
                    var drives = new List <DriveDetailed>();

                    var i = 0;

                    foreach (var d in RamDiskUtil.GetEligibleDrives())
                    {
                        var dd = new DriveDetailed();

                        dd.Name = d.Name;

                        long size = -1;

                        try
                        {
                            size = d.TotalFreeSpace; // requesting disk size might throw access exceptions
                        }
                        catch { }


                        dd.BytesFree = size;

                        setAvailableAndIndex(dd, i);
                        i++;

                        drives.Add(dd);

                        this.drives = drives;
                    }
                }
            }
            catch (Exception ex)
            {
                ViewModelContainer.ErrorViewModel.DoShow(ex);
            }
        }
        private void InitDrives()
        {
            Action <DriveDetailed, int> setEnoughSpaceAndIndex = (DriveDetailed d, int i) =>
            {
                const int extraSpace = 512 * 1024 * 1024;
                d.EnoughSpace  = d.BytesFree > FileSize + extraSpace;
                d.DisplayIndex = d.BytesFree > FileSize + extraSpace ? (i < 9 ? (i + 1).ToString()[0] : '.') : ' ';

                if (!d.EnoughSpace)
                {
                    AtLeastOneDriveWithNotEnoughSpace = true;
                }
            };

            try
            {
                availbleDrivesCount = 0;
                AtLeastOneDriveWithNotEnoughSpace = false;
                var i = 0;
                firstAvailableDrive = null;

                if (Device.RuntimePlatform == Device.Android || Device.RuntimePlatform == Device.iOS)
                {
                    platformDrives = DependencyService.Get <IPlatformDrives>()?.GetDrives();

                    string prevDrive = "";

                    const string d1 = "/data/user";
                    const string d2 = "/storage/emulated/0";

                    foreach (var ad in platformDrives)
                    {
                        setEnoughSpaceAndIndex(ad, i);

                        if (ad.AvailableForTest)
                        {
                            availbleDrivesCount++;
                            if (firstAvailableDrive == null)
                            {
                                firstAvailableDrive = ad.Name;
                            }
                        }

                        i++;

                        if ((prevDrive.Contains(d1) && ad.Name.Contains(d2)) || (prevDrive.Contains(d2) && ad.Name.Contains(d1)))
                        {
                            ad.ShowDiveIsSameMessage = true;
                        }

                        prevDrive = ad.Name;
                    }
                }
                else
                {
                    var drives = new List <DriveDetailed>();

                    foreach (var d in RamDiskUtil.GetEligibleDrives())
                    {
                        var dd = new DriveDetailed();

                        dd.Name = d.Name;

                        long size = -1;
                        long free = -1;

                        try
                        {
                            free = d.TotalFreeSpace; // requesting disk size might throw access exceptions
                            size = d.TotalSize;
                        }
                        catch { dd.Accessible = false; }


                        dd.BytesFree  = free;
                        dd.TotalBytes = size;

                        setEnoughSpaceAndIndex(dd, i);

                        if (dd.AvailableForTest)
                        {
                            availbleDrivesCount++;
                            if (firstAvailableDrive == null)
                            {
                                firstAvailableDrive = dd.Name;
                            }
                        }

                        i++;

                        drives.Add(dd);

                        this.posixDrives = drives;
                    }
                }
            }
            catch (Exception ex)
            {
                ViewModelContainer.ErrorViewModel.DoShow(ViewModelContainer.L11n.InitDrivesError, ex);
            }
        }