-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndroidExport.cs
546 lines (462 loc) · 21.5 KB
/
AndroidExport.cs
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
using Android.Content;
using Android.Database;
using DownloadTwo.Helper;
using Plugin.CurrentActivity;
using System;
using System.Collections.Generic;
using System.IO;
using Xamarin.Essentials;
[assembly: Xamarin.Forms.Dependency(typeof(DownloadTwo.Droid.AndroidExport))]
namespace DownloadTwo.Droid
{
public class AndroidExport : IExport
{
/// <summary>
/// After version 10 (API 29) cannot write directly to Downloads directory, so permissions not needed.
/// Before that, permission is needed.
/// </summary>
/// <returns></returns>
public bool IsPermissionNeeded()
{
Version version = DeviceInfo.Version;
if (version.Major < 10)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Write file out to app directory.
/// </summary>
/// <param name="contents">File content to write</param>
/// <param name="filename">Filename to export content into</param>
/// <param name="androidSetPending">Set is_pending flag to 0, if this is true and API 30 or above. Note: only needed for network creation download</param>
/// <param name="androidUri">Android URI of created file</param>
/// <returns>true = worked w/o error or false = failed with system error</returns>
public bool ExportFile(string contents, string filename, bool androidSetPending, out string androidUri)
{
Version version = DeviceInfo.Version;
// After version 10 (API 29) cannot write directly to Downloads directory
// and must do it via the Mediastore API due to security changes
if (version.Major < 10)
{
androidUri = null;
return WriteAPI28AndBelow(contents, filename);
}
else
{
bool pending = false;
if (version.Major > 10 && androidSetPending)
{
pending = true;
}
return WriteAPI29AndAbove(contents, filename, pending, out androidUri);
}
}
/// <summary>
/// Check to see if the file exists int the Download directory
/// </summary>
/// <param name="filename">Filename to check</param>
/// <param name="matched">Was filename found?</param>
/// <param name="androidUri">Android URI of found file</param>
/// <param name="androidFileId">Android file id of found file</param>
/// <returns>true = worked w/o error or false = failed with system error</returns>
public bool FileCheck(string filename, out bool matched, out string androidUri, out long androidFileId)
{
androidFileId = 0;
Version version = DeviceInfo.Version;
// After version 10 (API 29) cannot do anything directly to Downloads directory
// and must do it via the Mediastore API due to security changes
if (version.Major < 10)
{
return QueryAPI28AndBelow(filename, out matched, out androidUri);
}
else
{
return QueryAPI29AndAbove(filename, out matched, out androidUri, out androidFileId);
}
}
/// <summary>
/// This will try to delete a file
/// </summary>
/// <param name="filename">Filename to delete</param>
/// <param name="androidUri">URI to delete</param>
/// <param name="androidFileId">File id to delete</param>
/// <param name="deleteOK">Delete worked</param>
/// <returns>true = worked w/o error or false = failed with system error</returns>
public bool DeleteExportFile(string filename, string androidUri, long androidFileId, out bool deleteOK)
{
Version version = DeviceInfo.Version;
// After version 10 (API 29) cannot write directly to Downloads directory
// and must do it via the Mediastore API due to security changes
if (version.Major < 10)
{
return DeleteAPI28AndBelow(filename, out deleteOK);
}
else
{
return DeleteAPI29AndAbove(filename, androidUri, androidFileId, out deleteOK);
}
}
/// <summary>
/// This will try to update a file.
/// </summary>
/// <param name="androidUri">URI to delete</param>
/// <param name="updatedOK">Update worked</param>
/// <returns>true = worked w/o error or false = failed with system error</returns>
public bool UpdateExportFile(string androidUri, out bool updatedOK)
{
updatedOK = false;
Version version = DeviceInfo.Version;
// After version 10 (API 29) cannot write directly to Downloads directory.
// Update is only supported on version 11 (API 30).
if (version.Major > 10)
{
return UpdateAPI30AndAbove(androidUri, out updatedOK);
}
return true;
}
#region query file
private bool QueryAPI28AndBelow(string filename, out bool matched, out string androidUri)
{
matched = false;
androidUri = null;
#pragma warning disable CS0618 // Type or member is obsolete
string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
#pragma warning restore CS0618 // Type or member is obsolete
string filespec = Path.Combine(directory, filename);
try
{
FileInfo newFile = new FileInfo(filespec);
matched = newFile.Exists;
if (matched)
{
Android.Net.Uri uri1;
using (Java.IO.File file = new Java.IO.File(filespec))
{
uri1 = Android.Net.Uri.FromFile(file);
}
androidUri = uri1.ToString();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed file check. Passed in filename: " + filespec + ". Full filespec: " + filespec);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
return true;
}
private bool QueryAPI29AndAbove(string filename, out bool matched, out string androidUri, out long androidFileId)
{
matched = false;
androidUri = null;
androidFileId = 0;
/**
* A key concept when working with Android [ContentProvider]s is something called
* "projections". A projection is the list of columns to request from the provider,
* and can be thought of (quite accurately) as the "SELECT ..." clause of a SQL
* statement.
*
* It's not _required_ to provide a projection. In this case, one could pass `null`
* in place of `projection` in the call to [ContentResolver.query], but requesting
* more data than is required has a performance impact.
*
* For this sample, we only use a few columns of data, and so we'll request just a
* subset of columns.
*/
var projection = new List<string>()
{
Android.Provider.MediaStore.Downloads.InterfaceConsts.Id,
Android.Provider.MediaStore.Downloads.InterfaceConsts.DisplayName,
Android.Provider.MediaStore.Downloads.InterfaceConsts.DateAdded,
Android.Provider.MediaStore.Downloads.InterfaceConsts.Title,
Android.Provider.MediaStore.Downloads.InterfaceConsts.RelativePath,
Android.Provider.MediaStore.Downloads.InterfaceConsts.MimeType,
}.ToArray();
/**
* The `selection` is the "WHERE ..." clause of a SQL statement. It's also possible
* to omit this by passing `null` in its place, and then all rows will be returned.
* In this case we're using a selection based on the date the image was taken.
*
* Note that we've included a `?` in our selection. This stands in for a variable
* which will be provided by the next variable. Note: I modified this to not use
* the parameter, but just hard code the value - which is the text mime type.
*/
string selection = Android.Provider.MediaStore.Downloads.InterfaceConsts.MimeType + " = ?";
/**
* The `selectionArgs` is a list of values that will be filled in for each `?`
* in the `selection`.
*/
var selectionArgs = new List<string>()
{
"text/plain"
};
ICursor cursor;
////https://github.com/android/storage-samples/blob/main/MediaStore/app/src/main/java/com/android/samples/mediastore/MainActivityViewModel.kt
try
{
ContentResolver contentResolver = CrossCurrentActivity.Current.AppContext.ContentResolver;
cursor = contentResolver.Query(Android.Provider.MediaStore.Downloads.ExternalContentUri, projection, selection, selectionArgs.ToArray(), null);
if (cursor == null)
{
// If this does happen, then there's a problem somewhere
return false;
}
else if (cursor != null && cursor.Count > 0)
{
int idColumn = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Downloads.InterfaceConsts.Id);
int dispNameColumn = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Downloads.InterfaceConsts.DisplayName);
int addedColumn = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Downloads.InterfaceConsts.DateAdded);
int titleColumn = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Downloads.InterfaceConsts.Title);
int relativePathColumn = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Downloads.InterfaceConsts.RelativePath);
cursor.MoveToFirst();
System.Diagnostics.Debug.WriteLine("Cursor found " + cursor.Count + " rows");
do
{
long id = cursor.GetLong(idColumn);
string displayName = cursor.GetString(dispNameColumn);
long addedTimeInSeconds = cursor.GetLong(addedColumn);
//DateTime dateTime = new DateTime(TimeUnit.SECONDS.toMillis());
string title = cursor.GetString(titleColumn);
string relativePath = cursor.GetString(relativePathColumn);
if (displayName.Equals(filename))
{
Android.Net.Uri uri1 = Android.Provider.MediaStore.Downloads.ExternalContentUri.BuildUpon().AppendPath(id.ToString()).Build();
androidUri = uri1.ToString();
matched = true;
androidFileId = id;
var junk = contentResolver.OpenFileDescriptor(uri1, "r");
bool isUriDocument = Android.Provider.DocumentsContract.IsDocumentUri(CrossCurrentActivity.Current.AppContext, uri1);
if (isUriDocument)
{
Android.OS.Bundle metadata = Android.Provider.DocumentsContract.GetDocumentMetadata(contentResolver, uri1);
}
break;
}
}
while (cursor.MoveToNext());
cursor.Close();
cursor.Dispose();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed to get data back from content resolver. Filename: " + filename);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
return true;
}
#endregion query file
#region delete file
private bool DeleteAPI29AndAbove(string filename, string androidUri, long androidFileId, out bool deleteOK)
{
deleteOK = false;
/**
* The `selection` is the "WHERE ..." clause of a SQL statement. It's also possible
* to omit this by passing `null` in its place, and then all rows will be returned.
* In this case we're using a selection based on the date the image was taken.
*
* Note that we've included a `?` in our selection. This stands in for a variable
* which will be provided by the next variable.
*/
var selection = Android.Provider.MediaStore.Downloads.InterfaceConsts.Id + " = ?";
/**
* The `selectionArgs` is a list of values that will be filled in for each `?`
* in the `selection`.
*/
var selectionArgs = new List<string>()
{
androidFileId.ToString()
};
try
{
ContentResolver contentResolver = CrossCurrentActivity.Current.AppContext.ContentResolver;
int count = contentResolver.Delete(Android.Provider.MediaStore.Downloads.ExternalContentUri, selection, selectionArgs.ToArray());
if (count > 0)
{
deleteOK = true;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed to get data back from content resolver. Filename: " + filename);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
return true;
}
private bool DeleteAPI28AndBelow(string filename, out bool deleteWorked)
{
string directory;
deleteWorked = false;
#pragma warning disable CS0618 // Type or member is obsolete
directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
#pragma warning restore CS0618 // Type or member is obsolete
string filespec = Path.Combine(directory, filename);
FileInfo newFile = new FileInfo(filespec);
if (newFile.Exists)
{
try
{
newFile.Delete(); // ensures we create a new workbook
deleteWorked = true;
System.Diagnostics.Debug.WriteLine("existing file deleted: " + filespec);
System.Diagnostics.Debug.Flush();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed to delete existing filespec: " + filename);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
}
return true;
}
#endregion
#region write file
private bool WriteAPI28AndBelow(string contents, string filename)
{
string directory;
#pragma warning disable CS0618 // Type or member is obsolete
directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
#pragma warning restore CS0618 // Type or member is obsolete
string filespec = Path.Combine(directory, filename);
//FileInfo newFile = new FileInfo(filespec);
//if (newFile.Exists)
//{
// try
// {
// newFile.Delete(); // ensures we create a new workbook
//
// System.Diagnostics.Debug.WriteLine("existing file deleted: " + filespec);
// System.Diagnostics.Debug.Flush();
// }
// catch (Exception ex)
// {
// System.Diagnostics.Debug.WriteLine("Failed to delete existing filespec: " + filename);
// System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
// System.Diagnostics.Debug.Flush();
// return false;
// }
//}
bool worked = DeleteAPI28AndBelow(filename, out bool deleteOK);
if (!worked)
{
return false;
}
try
{
if (!deleteOK)
{
return false;
}
using (StreamWriter writer = new(filespec))
{
writer.WriteAsync(contents);
writer.Close();
writer.DisposeAsync();
}
System.Diagnostics.Debug.WriteLine("File write succeeded. filename: " + filespec);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed file write. Passed in filename: " + filespec + ". Full filespec: " + filespec);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
return true;
}
private bool WriteAPI29AndAbove(string contents, string filename, bool setPending, out string androidUri)
{
androidUri = null;
string fileNameWithoutExt = Path.ChangeExtension(filename, null);
int fileSize = (int)contents.Length;
ContentValues values = new ContentValues();
ContentResolver contentResolver = CrossCurrentActivity.Current.AppContext.ContentResolver;
values.Put(Android.Provider.MediaStore.IMediaColumns.Title, filename);
values.Put(Android.Provider.MediaStore.IMediaColumns.MimeType, "text/plain");
values.Put(Android.Provider.MediaStore.IMediaColumns.Size, fileSize);
values.Put(Android.Provider.MediaStore.Downloads.InterfaceConsts.DisplayName, fileNameWithoutExt);
// Note: This file creation doesn't need this, as this isn't downloading the file
// from the internet; however, it's here for an example purposes.
if (setPending)
{
values.Put(Android.Provider.MediaStore.IMediaColumns.IsPending, 1);
}
Android.Net.Uri newUri;
System.IO.Stream saveStream;
try
{
newUri = contentResolver.Insert(Android.Provider.MediaStore.Downloads.ExternalContentUri, values);
if (newUri == null)
{
System.Diagnostics.Debug.WriteLine("Failed to get URI back from content resolver.");
return false;
}
androidUri = newUri.ToString();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed to get data back from content resolver. Filename: " + filename);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
try
{
saveStream = contentResolver.OpenOutputStream(newUri);
using (StreamWriter writer = new(saveStream))
{
writer.WriteAsync(contents);
writer.Close();
writer.DisposeAsync();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed file write: " + filename);
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
return true;
}
#endregion
#region update file
private bool UpdateAPI30AndAbove(string androidUri, out bool updateOK)
{
updateOK = false;
ContentValues values = new ContentValues();
values.Put(Android.Provider.MediaStore.IMediaColumns.IsPending, 0);
try
{
ContentResolver contentResolver = CrossCurrentActivity.Current.AppContext.ContentResolver;
Android.Net.Uri updateUri = Android.Net.Uri.Parse(androidUri);
int count = contentResolver.Update(updateUri, values, null, null);
if (count > 0)
{
updateOK = true;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed to get data back from content resolver.");
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
System.Diagnostics.Debug.Flush();
return false;
}
return true;
}
#endregion
}
}