forked from microsoft/Appsample-Photosharing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppShareTarget.cs
151 lines (130 loc) · 5.98 KB
/
AppShareTarget.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
//-----------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using PhotoSharingApp.Universal.Facades;
using PhotoSharingApp.Universal.Lifecycle;
using PhotoSharingApp.Universal.Models;
using PhotoSharingApp.Universal.Unity;
using PhotoSharingApp.Universal.ViewModels;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.DataTransfer;
using Windows.Globalization;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace PhotoSharingApp.Universal
{
/// <summary>
/// Share target extensions for the app.
/// </summary>
public partial class App : Application
{
private Frame GetOrCreateRootFrame()
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
// Set the default language
rootFrame.Language = ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
return rootFrame;
}
/// <summary>
/// Invoked when the application is activated through sharing association.
/// </summary>
/// <param name="args">Event data for the event.</param>
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
IAppEnvironment mainAppEnvironment = null;
IUploadFinishedHandler mainUploadFinishedHandler = null;
if (UnityBootstrapper.Container != null)
{
// We need to save the app environment from the current app.
mainAppEnvironment = ServiceLocator.Current.GetInstance<IAppEnvironment>();
mainUploadFinishedHandler = ServiceLocator.Current.GetInstance<IUploadFinishedHandler>();
}
else
{
// We only configure unity if the main app is not already launched.
UnityBootstrapper.Init();
UnityBootstrapper.ConfigureRegistries();
}
// Overwrite existing app environment instance.
UnityBootstrapper.Container.RegisterInstance(typeof(IAppEnvironment), new AppEnvironment());
// Do initializations, trying restoring current user into the
// provided app environment.
await AppInitialization.DoInitializations();
// Overwrite existing upload finished handler to make sure, the app
// does not navigate to the categories page after successful upload,
// but closes the share target window instead.
var uploadFinishedHandler =
new ShareTargetUploadFinishedHandler(() =>
{
// Restore original app environment.
if (mainAppEnvironment != null)
{
UnityBootstrapper.Container.RegisterInstance(typeof(IAppEnvironment), mainAppEnvironment);
}
// Restore original upload finished handler.
if (mainUploadFinishedHandler != null)
{
var uploadFinishedHandlerType = mainUploadFinishedHandler.GetType();
UnityBootstrapper.Container.RegisterType(typeof(IUploadFinishedHandler),
uploadFinishedHandlerType);
}
args.ShareOperation.ReportCompleted();
});
UnityBootstrapper.Container.RegisterInstance(typeof(IUploadFinishedHandler), uploadFinishedHandler);
// Access data that has been shared.
var data = args.ShareOperation.Data;
if (data.Contains(StandardDataFormats.StorageItems))
{
try
{
var items = await data.GetStorageItemsAsync();
// We expect the shared data to be a StorageFile.
var file = items.FirstOrDefault() as StorageFile;
if (file != null)
{
GetOrCreateRootFrame();
var navigationFacade = ServiceLocator.Current.GetInstance<INavigationFacade>();
navigationFacade.NavigateToCropView(file);
Window.Current.Activate();
}
}
catch (Exception)
{
args.ShareOperation.ReportCompleted();
}
}
}
}
}