diff --git a/PKHeX.Android/PKHeX.Android.csproj b/PKHeX.Android/PKHeX.Android.csproj index 4a9be874..10b5e6f8 100644 --- a/PKHeX.Android/PKHeX.Android.csproj +++ b/PKHeX.Android/PKHeX.Android.csproj @@ -16,7 +16,7 @@ Resources Assets false - v10.0 + v11.0 true Xamarin.Android.Net.AndroidClientHandler diff --git a/PKHeX.Android/Properties/AndroidManifest.xml b/PKHeX.Android/Properties/AndroidManifest.xml index 8ccbe3c0..6e19d071 100644 --- a/PKHeX.Android/Properties/AndroidManifest.xml +++ b/PKHeX.Android/Properties/AndroidManifest.xml @@ -1,8 +1,9 @@  - - + + + \ No newline at end of file diff --git a/PKHeX.Android/Properties/AssemblyInfo.cs b/PKHeX.Android/Properties/AssemblyInfo.cs index 9c12db36..ab9c3f5a 100644 --- a/PKHeX.Android/Properties/AssemblyInfo.cs +++ b/PKHeX.Android/Properties/AssemblyInfo.cs @@ -30,4 +30,6 @@ // Add some common permissions, these can be removed if not needed [assembly: UsesPermission(Android.Manifest.Permission.Internet)] +[assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)] [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)] +[assembly: UsesPermission(Android.Manifest.Permission.ManageExternalStorage)] diff --git a/PKHeX.Mobile/Logic/FileUtil.cs b/PKHeX.Mobile/Logic/FileUtil.cs index 8faa7cb1..31f17b11 100644 --- a/PKHeX.Mobile/Logic/FileUtil.cs +++ b/PKHeX.Mobile/Logic/FileUtil.cs @@ -12,12 +12,14 @@ namespace PKHeX.Mobile.Logic // todo: rename this class to not clash with PKHeX.Core public static class FileUtil { + private static string outputFolder = "/storage/emulated/0/PkHex/"; public static async Task PickFile() { var fileData = await FilePicker.PickAsync(PickOptions.Default).ConfigureAwait(false); if (fileData == null) return null; // user canceled file picking Debug.WriteLine($"File name chosen: {fileData.FileName}"); + Debug.WriteLine($"File path chosen: {fileData.FullPath}"); return fileData; } @@ -55,6 +57,26 @@ public static async Task TryGetSaveFile() } } + public static SaveFile TryGetSaveFile(string filePath) + { + try + { + var data = File.ReadAllBytes(filePath); + var len = data.Length; + bool isPossibleSAV = SaveUtil.IsSizeValid(len); + if (!isPossibleSAV) + return null; + var sav = SaveUtil.GetVariantSAV(data); + sav?.Metadata.SetExtraInfo(filePath); + return sav; + } + catch + { + //Ignore errors as this is meant to be a background scanning function + return null; + } + } + public static async Task ExportSAV(SaveFile sav) { if (!sav.State.Exportable) @@ -63,13 +85,26 @@ public static async Task ExportSAV(SaveFile sav) return false; } + //Create directory structure + if (!Directory.Exists(outputFolder)) + { + Directory.CreateDirectory(outputFolder); + } + String myDate = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss"); + if (!Directory.Exists(outputFolder + myDate + "/")) + { + Directory.CreateDirectory(outputFolder + myDate + "/"); + } + var data = sav.Write(); - var path = sav.Metadata.FilePath; + var path = outputFolder + myDate + "/" + Path.GetFileName(sav.Metadata.FilePath); + sav?.Metadata.SetExtraInfo(path); + Debug.WriteLine($"File path moved: {sav.Metadata.FilePath}"); try { if (sav.State.Exportable) await SaveBackup(sav).ConfigureAwait(false); - + File.Create(path).Close(); await File.WriteAllBytesAsync(path, data).ConfigureAwait(false); await UserDialogs.Instance.AlertAsync($"Exported save file to: {path}").ConfigureAwait(false); return true; @@ -78,7 +113,8 @@ public static async Task ExportSAV(SaveFile sav) catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { - await UserDialogs.Instance.AlertAsync($"Failed: {ex}").ConfigureAwait(false); + await UserDialogs.Instance.AlertAsync($"Failed to access \"" + outputFolder + "\" please grant All File Access Special Permision").ConfigureAwait(false); + //await UserDialogs.Instance.AlertAsync($"Failed: {ex}").ConfigureAwait(false); return false; } } diff --git a/PKHeX.Mobile/PKHeX.Mobile.csproj b/PKHeX.Mobile/PKHeX.Mobile.csproj index 908e2e23..2c2ff60c 100644 --- a/PKHeX.Mobile/PKHeX.Mobile.csproj +++ b/PKHeX.Mobile/PKHeX.Mobile.csproj @@ -4,6 +4,7 @@ netstandard2.1 true latest + icon.ico diff --git a/PKHeX.Mobile/Views/Loader.xaml.cs b/PKHeX.Mobile/Views/Loader.xaml.cs index d60a663d..e04ff170 100644 --- a/PKHeX.Mobile/Views/Loader.xaml.cs +++ b/PKHeX.Mobile/Views/Loader.xaml.cs @@ -8,15 +8,47 @@ using PKHeX.ViewModels; using Xamarin.Forms; using Xamarin.Forms.Xaml; +using System.IO; namespace PKHeX.Mobile.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class Loader : ContentPage { + private static string outputFolder = "/storage/emulated/0/PkHex/"; public Loader() { InitializeComponent(); + initialLoad(); + } + + private async void initialLoad() + { + try + { + if (CV_Saves.SelectedItem == null) + { + if (Directory.Exists(outputFolder)) + { + foreach (string filePath in Directory.EnumerateFiles(outputFolder, "*", SearchOption.AllDirectories)) + { + var sav = Logic.FileUtil.TryGetSaveFile(filePath); + if (sav != null) + { + var match = VM.Saves.FirstOrDefault(z => z.LocatedAt(sav.Metadata.FilePath)); + if (match != null) + CV_Saves.SelectedItem = match; + else + await LoadNewSaveFile(sav).ConfigureAwait(false); + } + } + } + } + } catch + { + //first time load, ignore error + return; + } } private async void TgrOpenTapped(object sender, EventArgs e) diff --git a/PKHeX.Mobile/icon.ico b/PKHeX.Mobile/icon.ico new file mode 100644 index 00000000..f20c6c06 Binary files /dev/null and b/PKHeX.Mobile/icon.ico differ