Skip to content

Commit 4f571d9

Browse files
authored
Merge pull request #7 from ltrudu/master
Added Datawedge Profile auto import.
2 parents fce7faa + 9a94cf1 commit 4f571d9

5 files changed

Lines changed: 108 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace datawedge_MAUI_SampleApp.Platforms.Android
8+
{
9+
public class FilePermissionHelper
10+
{
11+
public static Boolean setFilePermissions(String filePath, Boolean readable, Boolean writable, Boolean executable)
12+
{
13+
Java.IO.File file = new Java.IO.File(filePath);
14+
Boolean success = true;
15+
if (file.Exists())
16+
{
17+
if (!file.SetReadable(readable, false)) success = false;
18+
if (!file.SetWritable(writable, false)) success = false;
19+
if (!file.SetExecutable(executable, false)) success = false;
20+
}
21+
else success = false;
22+
return success;
23+
}
24+
}
25+
}

datawedge-MAUI-SampleApp/Platforms/Android/MainActivity.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Java.Lang;
99
using System.Diagnostics.Metrics;
1010
using System.Security.AccessControl;
11+
using System.Security.Principal;
1112
using System.Xml.Linq;
1213

1314
namespace datawedge_MAUI_SampleApp;
@@ -18,7 +19,8 @@ public class MainActivity : MauiAppCompatActivity
1819
{
1920
//protected override void OnCreate(Bundle savedInstanceState) {
2021
protected override void OnPostCreate(Bundle savedInstanceState)
21-
{
22+
{
23+
ImportProfile("dwprofile_com.ndzl.dwmaui");
2224
base.OnPostCreate(savedInstanceState);
2325
RegisterReceivers();
2426
WeakReferenceMessenger.Default.Send(DisplayDotNetVersion());
@@ -99,4 +101,80 @@ void RegisterReceivers()
99101

100102
Intent regres = AndroidX.Core.Content.ContextCompat.RegisterReceiver(this, new DWIntentReceiver(), filter, AndroidX.Core.Content.ContextCompat.ReceiverExported);
101103
}
104+
105+
private void ImportProfile(string profileFilenameWithoutDbExtension)
106+
{
107+
// Define directories and file names
108+
// /enterprise/device/settings/datawedge/autoimport
109+
string autoImportDir = "/enterprise/device/settings/datawedge/autoimport/";
110+
string temporaryFileName = profileFilenameWithoutDbExtension + ".tmp";
111+
string finalFileName = profileFilenameWithoutDbExtension + ".db";
112+
113+
Stream inputStream = null;
114+
FileStream fileOutputStream = null;
115+
FileInfo outputFile = null;
116+
FileInfo finalFile = null;
117+
118+
try
119+
{
120+
// Access the asset stream from the application assets
121+
inputStream = FileSystem.Current.OpenAppPackageFileAsync(finalFileName).Result;
122+
123+
// Create a directory for the output if it doesn't exist
124+
DirectoryInfo outputDirectory = new DirectoryInfo(autoImportDir);
125+
if (!outputDirectory.Exists)
126+
{
127+
outputDirectory.Create();
128+
}
129+
130+
// Create temporary and final file objects
131+
outputFile = new FileInfo(Path.Combine(outputDirectory.FullName, temporaryFileName));
132+
finalFile = new FileInfo(Path.Combine(outputDirectory.FullName, finalFileName));
133+
134+
// Create a FileStream for the temporary output file
135+
fileOutputStream = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write);
136+
137+
// Transfer bytes from the input stream to the output stream
138+
byte[] buffer = new byte[1024];
139+
int length;
140+
int tot = 0;
141+
while ((length = inputStream.Read(buffer, 0, buffer.Length)) > 0)
142+
{
143+
fileOutputStream.Write(buffer, 0, length);
144+
tot += length;
145+
}
146+
Console.WriteLine($"{tot} bytes copied");
147+
148+
// Flush and close the output stream
149+
fileOutputStream.Flush();
150+
}
151+
catch (Java.Lang.Exception e)
152+
{
153+
Console.WriteLine(e.ToString());
154+
}
155+
finally
156+
{
157+
// Ensure the stream is properly closed
158+
fileOutputStream?.Close();
159+
160+
// Set permissions and rename the file if applicable
161+
if (outputFile != null)
162+
{
163+
// Rename the temporary file to the final file
164+
if (finalFile != null)
165+
{
166+
if (outputFile.Exists)
167+
{
168+
if(finalFile.Exists)
169+
{
170+
finalFile.Delete();
171+
}
172+
173+
FilePermissionHelper.setFilePermissions(outputFile.FullName, true, true, true);
174+
outputFile.MoveTo(finalFile.FullName);
175+
}
176+
}
177+
}
178+
}
179+
}
102180
}
Binary file not shown.

datawedge-MAUI-SampleApp/datawedge-MAUI-SampleApp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
6060
</ItemGroup>
6161

62+
<ItemGroup>
63+
<AndroidResource Include="Resources\Raw\dwprofile_com.ndzl.dwmaui.db" />
64+
</ItemGroup>
65+
6266
<ItemGroup>
6367
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
6468
</ItemGroup>

dwprofile_Profile0 (default).db

-232 KB
Binary file not shown.

0 commit comments

Comments
 (0)