This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 505
/
Platform.android.cs
491 lines (385 loc) · 17.3 KB
/
Platform.android.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
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Hardware;
using Android.Hardware.Camera2;
using Android.Locations;
using Android.Net;
using Android.Net.Wifi;
using Android.OS;
using Android.Views;
using AndroidIntent = Android.Content.Intent;
using AndroidUri = Android.Net.Uri;
namespace Xamarin.Essentials
{
public static partial class Platform
{
static ActivityLifecycleContextListener lifecycleListener;
public static Context AppContext => Application.Context;
public static Activity CurrentActivity => lifecycleListener?.Activity;
public static event EventHandler<ActivityStateChangedEventArgs> ActivityStateChanged;
internal const string EssentialsConnectivityChanged = "com.xamarin.essentials.ESSENTIALS_CONNECTIVITY_CHANGED";
internal const int requestCodeFilePicker = 11001;
internal const int requestCodeMediaPicker = 11002;
internal const int requestCodeMediaCapture = 11003;
internal const int requestCodePickContact = 11004;
internal const int requestCodeStart = 12000;
static int requestCode = requestCodeStart;
internal static int NextRequestCode()
{
if (++requestCode >= 12999)
requestCode = requestCodeStart;
return requestCode;
}
internal static void OnActivityStateChanged(Activity activity, ActivityState ev)
=> ActivityStateChanged?.Invoke(null, new ActivityStateChangedEventArgs(activity, ev));
public static async Task<Activity> WaitForActivityAsync(CancellationToken cancelToken = default)
{
if (CurrentActivity != null)
return CurrentActivity;
var tcs = new TaskCompletionSource<Activity>();
var handler = new EventHandler<ActivityStateChangedEventArgs>((sender, args) =>
{
if (args.State == ActivityState.Created || args.State == ActivityState.Resumed)
tcs.TrySetResult(args.Activity);
});
try
{
using (cancelToken.Register(() => tcs.TrySetCanceled()))
{
ActivityStateChanged += handler;
return await tcs.Task.ConfigureAwait(false);
}
}
finally
{
ActivityStateChanged -= handler;
}
}
internal static Activity GetCurrentActivity(bool throwOnNull)
{
var activity = lifecycleListener?.Activity;
if (throwOnNull && activity == null)
throw new NullReferenceException("The current Activity can not be detected. Ensure that you have called Init in your Activity or Application class.");
return activity;
}
public static void Init(Application application)
{
lifecycleListener = new ActivityLifecycleContextListener();
application.RegisterActivityLifecycleCallbacks(lifecycleListener);
}
public static void Init(Activity activity, Bundle bundle)
{
Init(activity.Application);
lifecycleListener.Activity = activity;
}
public static void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) =>
Permissions.OnRequestPermissionsResult(requestCode, permissions, grantResults);
public static void OnNewIntent(AndroidIntent intent)
=> CheckAppActions(intent);
public static void OnResume(Activity activity = null)
{
if (activity != null)
CheckAppActions(activity.Intent);
}
static void CheckAppActions(AndroidIntent intent)
{
if (intent?.Action == Intent.ActionAppAction && !intent.GetBooleanExtra(AppActions.extraAppActionHandled, false))
{
// prevent launch intent getting handled on activity resume
intent.PutExtra(AppActions.extraAppActionHandled, true);
var appAction = intent.ToAppAction();
if (!string.IsNullOrEmpty(appAction?.Id))
AppActions.InvokeOnAppAction(Platform.CurrentActivity, appAction);
}
}
internal static AndroidIntent RegisterBroadcastReceiver(BroadcastReceiver receiver, IntentFilter filter, bool exported)
{
if (HasApiLevel(34))
{
var flags = exported ? ReceiverFlags.Exported : ReceiverFlags.NotExported;
// Explicit cast of flags because of: https://github.com/xamarin/xamarin-android/issues/7503
return AppContext.RegisterReceiver(receiver, filter, (ActivityFlags)flags);
}
return AppContext.RegisterReceiver(receiver, filter);
}
internal static bool HasSystemFeature(string systemFeature)
{
var packageManager = AppContext.PackageManager;
foreach (var feature in packageManager.GetSystemAvailableFeatures())
{
if (feature?.Name?.Equals(systemFeature, StringComparison.OrdinalIgnoreCase) ?? false)
return true;
}
return false;
}
internal static bool IsIntentSupported(AndroidIntent intent) =>
intent.ResolveActivity(AppContext.PackageManager) != null;
internal static bool IsIntentSupported(AndroidIntent intent, string expectedPackageName) =>
intent.ResolveActivity(AppContext.PackageManager) is ComponentName c && c.PackageName == expectedPackageName;
internal static AndroidUri GetShareableFileUri(FileBase file)
{
Java.IO.File sharedFile;
if (FileProvider.IsFileInPublicLocation(file.FullPath))
{
// we are sharing a file in a "shared/public" location
sharedFile = new Java.IO.File(file.FullPath);
}
else
{
var root = FileProvider.GetTemporaryRootDirectory();
var tmpFile = FileSystem.GetEssentialsTemporaryFile(root, file.FileName);
System.IO.File.Copy(file.FullPath, tmpFile.CanonicalPath);
sharedFile = tmpFile;
}
// create the uri, if N use file provider
if (HasApiLevelN)
{
return FileProvider.GetUriForFile(sharedFile);
}
// use the shared file path created
return AndroidUri.FromFile(sharedFile);
}
internal static bool HasApiLevelKitKat => HasApiLevel(BuildVersionCodes.Kitkat);
internal static bool HasApiLevelN => HasApiLevel(24);
internal static bool HasApiLevelNMr1 => HasApiLevel(25);
internal static bool HasApiLevelO => HasApiLevel(26);
internal static bool HasApiLevelQ => HasApiLevel(29);
static int? sdkInt;
internal static int SdkInt
=> sdkInt ??= (int)Build.VERSION.SdkInt;
internal static bool HasApiLevel(BuildVersionCodes versionCode) =>
SdkInt >= (int)versionCode;
internal static bool HasApiLevel(int apiLevel) =>
SdkInt >= apiLevel;
internal static CameraManager CameraManager =>
AppContext.GetSystemService(Context.CameraService) as CameraManager;
internal static ConnectivityManager ConnectivityManager =>
AppContext.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
#pragma warning disable CS0618 // Type or member is obsolete
internal static Vibrator Vibrator =>
AppContext.GetSystemService(Context.VibratorService) as Vibrator;
#pragma warning restore CS0618 // Type or member is obsolete
internal static WifiManager WifiManager =>
AppContext.GetSystemService(Context.WifiService) as WifiManager;
internal static SensorManager SensorManager =>
AppContext.GetSystemService(Context.SensorService) as SensorManager;
internal static ClipboardManager ClipboardManager =>
AppContext.GetSystemService(Context.ClipboardService) as ClipboardManager;
internal static LocationManager LocationManager =>
AppContext.GetSystemService(Context.LocationService) as LocationManager;
internal static PowerManager PowerManager =>
AppContext.GetSystemService(Context.PowerService) as PowerManager;
#if __ANDROID_25__
internal static ShortcutManager ShortcutManager =>
AppContext.GetSystemService(Context.ShortcutService) as ShortcutManager;
#endif
internal static IWindowManager WindowManager =>
AppContext.GetSystemService(Context.WindowService) as IWindowManager;
internal static ContentResolver ContentResolver =>
AppContext.ContentResolver;
internal static Java.Util.Locale GetLocale()
{
var resources = AppContext.Resources;
var config = resources.Configuration;
#if __ANDROID_24__
if (HasApiLevelN)
return config.Locales.Get(0);
#endif
#pragma warning disable CS0618 // Type or member is obsolete
return config.Locale;
#pragma warning restore CS0618 // Type or member is obsolete
}
internal static void SetLocale(Java.Util.Locale locale)
{
Java.Util.Locale.Default = locale;
var resources = AppContext.Resources;
var config = resources.Configuration;
#if __ANDROID_24__
if (HasApiLevelN)
config.SetLocale(locale);
else
#endif
#pragma warning disable CS0618 // Type or member is obsolete
config.Locale = locale;
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
resources.UpdateConfiguration(config, resources.DisplayMetrics);
#pragma warning restore CS0618 // Type or member is obsolete
}
public static class Intent
{
public const string ActionAppAction = "ACTION_XE_APP_ACTION";
}
}
public enum ActivityState
{
Created,
Resumed,
Paused,
Destroyed,
SaveInstanceState,
Started,
Stopped
}
public class ActivityStateChangedEventArgs : EventArgs
{
internal ActivityStateChangedEventArgs(Activity activity, ActivityState ev)
{
State = ev;
Activity = activity;
}
public ActivityState State { get; }
public Activity Activity { get; }
}
class ActivityLifecycleContextListener : Java.Lang.Object, Application.IActivityLifecycleCallbacks
{
WeakReference<Activity> currentActivity = new WeakReference<Activity>(null);
internal Context Context =>
Activity ?? Application.Context;
internal Activity Activity
{
get => currentActivity.TryGetTarget(out var a) ? a : null;
set => currentActivity.SetTarget(value);
}
void Application.IActivityLifecycleCallbacks.OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
Activity = activity;
Platform.OnActivityStateChanged(activity, ActivityState.Created);
}
void Application.IActivityLifecycleCallbacks.OnActivityDestroyed(Activity activity) =>
Platform.OnActivityStateChanged(activity, ActivityState.Destroyed);
void Application.IActivityLifecycleCallbacks.OnActivityPaused(Activity activity)
{
Activity = activity;
Platform.OnActivityStateChanged(activity, ActivityState.Paused);
}
void Application.IActivityLifecycleCallbacks.OnActivityResumed(Activity activity)
{
Activity = activity;
Platform.OnActivityStateChanged(activity, ActivityState.Resumed);
}
void Application.IActivityLifecycleCallbacks.OnActivitySaveInstanceState(Activity activity, Bundle outState) =>
Platform.OnActivityStateChanged(activity, ActivityState.SaveInstanceState);
void Application.IActivityLifecycleCallbacks.OnActivityStarted(Activity activity) =>
Platform.OnActivityStateChanged(activity, ActivityState.Started);
void Application.IActivityLifecycleCallbacks.OnActivityStopped(Activity activity) =>
Platform.OnActivityStateChanged(activity, ActivityState.Stopped);
}
[Activity(ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize)]
class IntermediateActivity : Activity
{
const string launchedExtra = "launched";
const string actualIntentExtra = "actual_intent";
const string guidExtra = "guid";
const string requestCodeExtra = "request_code";
static readonly ConcurrentDictionary<string, IntermediateTask> pendingTasks =
new ConcurrentDictionary<string, IntermediateTask>();
bool launched;
Intent actualIntent;
string guid;
int requestCode;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var extras = savedInstanceState ?? Intent.Extras;
// read the values
launched = extras.GetBoolean(launchedExtra, false);
#pragma warning disable CS0618 // Type or member is obsolete
actualIntent = extras.GetParcelable(actualIntentExtra) as Intent;
#pragma warning restore CS0618 // Type or member is obsolete
guid = extras.GetString(guidExtra);
requestCode = extras.GetInt(requestCodeExtra, -1);
if (GetIntermediateTask(guid) is IntermediateTask task)
{
task.OnCreate?.Invoke(actualIntent);
}
// if this is the first time, lauch the real activity
if (!launched)
StartActivityForResult(actualIntent, requestCode);
}
protected override void OnSaveInstanceState(Bundle outState)
{
// make sure we mark this activity as launched
outState.PutBoolean(launchedExtra, true);
// save the values
outState.PutParcelable(actualIntentExtra, actualIntent);
outState.PutString(guidExtra, guid);
outState.PutInt(requestCodeExtra, requestCode);
base.OnSaveInstanceState(outState);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
// we have a valid GUID, so handle the task
if (GetIntermediateTask(guid, true) is IntermediateTask task)
{
if (resultCode == Result.Canceled)
{
task.TaskCompletionSource.TrySetCanceled();
}
else
{
try
{
data ??= new AndroidIntent();
task.OnResult?.Invoke(data);
task.TaskCompletionSource.TrySetResult(data);
}
catch (Exception ex)
{
task.TaskCompletionSource.TrySetException(ex);
}
}
}
// close the intermediate activity
Finish();
}
public static Task<Intent> StartAsync(Intent intent, int requestCode, Action<Intent> onCreate = null, Action<Intent> onResult = null)
{
// make sure we have the activity
var activity = Platform.GetCurrentActivity(true);
// create a new task
var data = new IntermediateTask(onCreate, onResult);
pendingTasks[data.Id] = data;
// create the intermediate intent, and add the real intent to it
var intermediateIntent = new Intent(activity, typeof(IntermediateActivity));
intermediateIntent.PutExtra(actualIntentExtra, intent);
intermediateIntent.PutExtra(guidExtra, data.Id);
intermediateIntent.PutExtra(requestCodeExtra, requestCode);
// start the intermediate activity
activity.StartActivityForResult(intermediateIntent, requestCode);
return data.TaskCompletionSource.Task;
}
static IntermediateTask GetIntermediateTask(string guid, bool remove = false)
{
if (string.IsNullOrEmpty(guid))
return null;
if (remove)
{
pendingTasks.TryRemove(guid, out var removedTask);
return removedTask;
}
pendingTasks.TryGetValue(guid, out var task);
return task;
}
class IntermediateTask
{
public IntermediateTask(Action<Intent> onCreate, Action<AndroidIntent> onResult)
{
Id = Guid.NewGuid().ToString();
TaskCompletionSource = new TaskCompletionSource<Intent>();
OnCreate = onCreate;
OnResult = onResult;
}
public string Id { get; }
public TaskCompletionSource<Intent> TaskCompletionSource { get; }
public Action<Intent> OnCreate { get; }
public Action<AndroidIntent> OnResult { get; }
}
}
}