-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGallery.uno
More file actions
182 lines (154 loc) · 5.24 KB
/
Gallery.uno
File metadata and controls
182 lines (154 loc) · 5.24 KB
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
using Uno;
using Uno.Collections;
using Fuse.Scripting;
using Fuse.Reactive;
using Fuse;
using Uno.Compiler.ExportTargetInterop;
using Uno.Threading;
public class Gallery : NativeModule {
public Gallery () {
// Add Load function to load image as a texture
AddMember(new NativePromise<Fuse.Camera.PictureResult, Fuse.Scripting.Object>("getPicture", GetPicture, Converter));
}
static Future<Fuse.Camera.PictureResult> GetPicture(object[] args)
{
var path = Uno.IO.Path.Combine(Uno.IO.Directory.GetUserDirectory(Uno.IO.UserDirectory.Data), "temp.jpg");
return GalleryImpl.GetPicture(path);
}
static Fuse.Scripting.Object Converter(Context context, Fuse.Camera.PictureResult result)
{
var func = (Fuse.Scripting.Function)context.GlobalObject["File"];
var file = (Fuse.Scripting.Object)func.Construct();
file["path"] = result.Path;
file["name"] = Uno.IO.Path.GetFileName(result.Path);
return file;
}
}
[ForeignInclude(Language.Java,
"android.app.Activity",
"android.content.Intent",
"android.net.Uri",
"android.os.Bundle",
"android.provider.MediaStore",
"java.io.InputStream",
"java.io.FileOutputStream",
"java.io.File")]
[ForeignInclude(Language.ObjC, "TakePictureTask.h")]
public class GalleryImpl
{
static int BAD_ID = 1234;
static bool InProgress {
get; set;
}
static Promise<Fuse.Camera.PictureResult> FuturePath {
get; set;
}
static string Path;
/*
static string Path {
get { return _Path; }
set { _Path = value; }
}
*/
static extern(Android) Java.Object _intentListener;
public static Future<Fuse.Camera.PictureResult> GetPicture (string path) {
if (InProgress) {
return null;
}
InProgress = true;
if defined(Android) {
if (_intentListener == null)
_intentListener = Init();
}
Path = path;
GetPictureImpl();
FuturePath = new Promise<Fuse.Camera.PictureResult>();
return FuturePath;
}
[Foreign(Language.Java)]
static extern(Android) Java.Object Init()
@{
com.fuse.Activity.ResultListener l = new com.fuse.Activity.ResultListener() {
@Override public boolean onResult(int requestCode, int resultCode, android.content.Intent data) {
return @{OnRecieved(int,int,Java.Object):Call(requestCode, resultCode, data)};
}
};
com.fuse.Activity.subscribeToResults(l);
return l;
@}
[Foreign(Language.Java)]
static extern(Android) bool OnRecieved(int requestCode, int resultCode, Java.Object data)
@{
debug_log("Got resultCode " + resultCode);
debug_log("(Okay is: " + Activity.RESULT_OK);
if (requestCode == @{BAD_ID}) {
if (resultCode == Activity.RESULT_OK) {
Intent i = (Intent)data;
Activity a = com.fuse.Activity.getRootActivity();
// File outFile = new File(@{Path});
// http://stackoverflow.com/questions/10854211/android-store-inputstream-in-file
try {
FileOutputStream output = new FileOutputStream(@{Path:Get()});
InputStream input = a.getContentResolver().openInputStream(i.getData());
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
output.close();
input.close();
debug_log("And it's ours!, and done");
@{Picked():Call()};
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
@{Cancelled():Call()};
}
}
else {
@{Cancelled():Call()};
}
}
return (requestCode == @{BAD_ID});
@}
static extern(!Mobile) void GetPictureImpl () {
throw new Fuse.Scripting.Error("Unsupported platform");
}
[Foreign(Language.Java)]
static extern(Android) void GetPictureImpl ()
@{
Activity a = com.fuse.Activity.getRootActivity();
// Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
a.startActivityForResult(intent, @{BAD_ID});
// http://stackoverflow.com/questions/5309190/android-pick-images-from-gallery
@}
[Require("Entity","GalleryImpl.Cancelled()")]
[Require("Entity","GalleryImpl.Picked()")]
[Foreign(Language.ObjC)]
static extern(iOS) void GetPictureImpl ()
@{
TakePictureTask *task = [[TakePictureTask alloc] init];
UIViewController *uivc = [UIApplication sharedApplication].keyWindow.rootViewController;
[task setUivc:uivc];
[task setPath:@{Path:Get()}];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
picker.delegate = task;
[uivc
presentViewController:picker
animated:YES
completion:nil];
@}
public static void Cancelled () {
InProgress = false;
FuturePath.Reject(new Exception("User cancelled the gallery select"));
}
public static void Picked () {
InProgress = false;
FuturePath.Resolve(new Fuse.Camera.PictureResult(Path, 0));
}
}