feature: support db import on Android

This commit is contained in:
Tsanie Lily 2022-03-15 22:45:21 +08:00
parent 8ba6f4bf85
commit d3af69b31e
7 changed files with 113 additions and 38 deletions

View File

@ -24,8 +24,11 @@ namespace Billing
private static List<Account> accounts;
private static List<Category> categories;
public App()
private string initialUrl;
public App(string url = null)
{
initialUrl = url;
CurrentCulture = new PlatformCulture();
InitResources();
@ -36,6 +39,13 @@ namespace Billing
{
Helper.Debug($"personal folder: {StoreHelper.PersonalFolder}");
Helper.Debug($"cache folder: {StoreHelper.CacheFolder}");
if (initialUrl != null)
{
var url = initialUrl;
initialUrl = null;
_ = OpenUrl(url);
}
}
public static async Task InitializeData()
@ -83,17 +93,25 @@ namespace Billing
Resources = instance;
}
public static bool OpenUrl(Uri uri)
public static async Task<bool> OpenUrl(string url)
{
if (uri == null || !uri.IsFile)
if (string.IsNullOrEmpty(url))
{
return true;
return false;
}
var absolute = uri.AbsolutePath;
var url = System.Net.WebUtility.UrlDecode(absolute);
if (File.Exists(url))
{
Task.Run(async () =>
var status = await Permissions.CheckStatusAsync<Permissions.StorageRead>();
if (status != PermissionStatus.Disabled &&
status != PermissionStatus.Granted)
{
status = await Permissions.RequestAsync<Permissions.StorageRead>();
}
if (status != PermissionStatus.Granted)
{
return false;
}
_ = Task.Run(async () =>
{
var result = await StoreHelper.ReloadDatabase(url);
if (result)

View File

@ -37,11 +37,11 @@
<EnableLLVM>false</EnableLLVM>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<BundleAssemblies>false</BundleAssemblies>
<AndroidSupportedAbis>x86_64;x86</AndroidSupportedAbis>
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
<AndroidSupportedAbis>x86;x86_64;arm64-v8a</AndroidSupportedAbis>
<EmbedAssembliesIntoApk>false</EmbedAssembliesIntoApk>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugSymbols>false</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
@ -54,6 +54,10 @@
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<BundleAssemblies>false</BundleAssemblies>
<AndroidSupportedAbis>arm64-v8a</AndroidSupportedAbis>
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
<AndroidCreatePackagePerAbi>true</AndroidCreatePackagePerAbi>
<AndroidLinkTool>r8</AndroidLinkTool>
<MandroidI18n />
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Android" />
@ -130,44 +134,64 @@
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\layout\RadioGroup.xml">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\layout\RadioButton.xml">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\color\segmented_control_text.xml">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable\segmented_control_background.xml">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable\segmented_control_first_background.xml">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable\segmented_control_last_background.xml">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable\share.png">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable-mdpi\share.png">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable-xhdpi\share.png">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
<AndroidResource Include="Resources\drawable-xxhdpi\share.png">
<SubType></SubType>
<Generator></Generator>
<SubType>
</SubType>
<Generator>
</Generator>
</AndroidResource>
</ItemGroup>
<ItemGroup>
@ -583,10 +607,7 @@
<ItemGroup>
<AndroidResource Include="Resources\drawable\left.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\layout\" />
<Folder Include="Resources\color\" />
</ItemGroup>
<ItemGroup />
<Import Project="..\..\Billing.Shared\Billing.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
</Project>

View File

@ -1,7 +1,9 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Android.Net;
namespace Billing.Droid
{
@ -18,9 +20,22 @@ namespace Billing.Droid
{
base.OnCreate(savedInstanceState);
string url;
if (Intent.ActionView.Equals(Intent.Action) && Intent.Data is Uri uri)
{
url = uri.Path;
if (url != null && url.StartsWith("/root"))
{
url = url[5..];
}
}
else
{
url = null;
}
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
LoadApplication(new App(url));
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)

View File

@ -27,3 +27,4 @@ using Android.App;
// Add some common permissions, these can be removed if not needed
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
[assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)]

View File

@ -14,7 +14,7 @@ namespace Billing.Droid
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.2.0.155")]
public partial class Resource
{

View File

@ -1,5 +1,6 @@
using Android.App;
using Android.Content;
using Android.Net;
using Android.OS;
using AndroidX.AppCompat.App;
using System.Threading.Tasks;
@ -11,6 +12,12 @@ namespace Billing.Droid
NoHistory = true,
Theme = "@style/MainTheme.Splash",
Name = "org.tsanie.billing.SplashScreen")]
[IntentFilter(
new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "file",
DataMimeType = "*/*",
DataPathPattern = ".*\\\\.db3")]
public class SplashActivity : AppCompatActivity
{
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
@ -23,7 +30,16 @@ namespace Billing.Droid
protected override void OnResume()
{
base.OnResume();
Task.Run(() => StartActivity(new Intent(Application.Context, typeof(MainActivity))));
Task.Run(() =>
{
var intent = new Intent(Application.Context, typeof(MainActivity));
if (Intent?.Data is Uri uri)
{
intent.SetAction(Intent.ActionView);
intent.SetData(uri);
}
StartActivity(intent);
});
}
}
}

View File

@ -26,7 +26,11 @@ namespace Billing.iOS
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
return App.OpenUrl(url);
if (url?.IsFileUrl == true)
{
return App.OpenUrl(url.Path).Result;
}
return false;
}
}
}