-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathindex.android.ts
212 lines (187 loc) · 7.15 KB
/
index.android.ts
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
import { Application, Device, File, Utils, ImageSource } from '@nativescript/core';
import type { ShareImageOptions } from '.';
let numberOfImagesCreated = 0;
const FileProviderPackageName = useAndroidX() ? global.androidx.core.content : (<any>android).support.v4.content;
function getIntent(type) {
const intent = new android.content.Intent(android.content.Intent.ACTION_SEND);
intent.setType(type);
return intent;
}
function share(intent, subject) {
subject = subject || 'How would you like to share this?';
const shareIntent = android.content.Intent.createChooser(intent, subject);
shareIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
(<android.content.Context>Utils.android.getApplicationContext()).startActivity(shareIntent);
}
function useAndroidX() {
return global.androidx && global.androidx.appcompat;
}
const imageProperties: {
[format: string]: () => {
mimeType: string;
compressFormat: android.graphics.Bitmap.CompressFormat;
extension: string;
};
} = {
png: () => ({
mimeType: 'image/png',
compressFormat: android.graphics.Bitmap.CompressFormat.PNG,
extension: 'png',
}),
jpg: () => ({
mimeType: 'image/jpeg',
compressFormat: android.graphics.Bitmap.CompressFormat.JPEG,
extension: 'jpg',
}),
};
export function shareImage(image: ImageSource, subjectOrOptions?: string | ShareImageOptions, caption?: string) {
let subject: string;
let fileFormat = 'jpg';
if (typeof subjectOrOptions === 'string') {
subject = subjectOrOptions;
} else if (subjectOrOptions && typeof subjectOrOptions === 'object') {
subject = subjectOrOptions.subject;
caption = subjectOrOptions.caption;
fileFormat = subjectOrOptions.fileFormat || fileFormat;
if (!Object.hasOwnProperty.call(fileFormat)) {
fileFormat = 'jpg';
}
}
numberOfImagesCreated++;
const properties = imageProperties[fileFormat]();
const intent = getIntent(properties.mimeType);
const stream = new java.io.ByteArrayOutputStream();
const bitmap: android.graphics.Bitmap = image.android;
bitmap.compress(properties.compressFormat, 100, stream);
const imageFileName = `socialsharing${numberOfImagesCreated}.${properties.extension}`;
const newFile = new java.io.File((<android.content.Context>Utils.android.getApplicationContext()).getExternalFilesDir(null), imageFileName);
const fos = new java.io.FileOutputStream(newFile);
fos.write(stream.toByteArray());
fos.flush();
fos.close();
let shareableFileUri;
const sdkVersionInt = parseInt(Device.sdkVersion);
if (sdkVersionInt >= 21) {
shareableFileUri = FileProviderPackageName.FileProvider.getUriForFile(<android.content.Context>Utils.android.getApplicationContext(), Application.android.nativeApp.getPackageName() + '.provider', newFile);
} else {
shareableFileUri = android.net.Uri.fromFile(newFile);
}
intent.putExtra(android.content.Intent.EXTRA_STREAM, shareableFileUri);
if (typeof caption === 'string') {
intent.putExtra(android.content.Intent.EXTRA_TEXT, caption);
}
share(intent, subject);
}
export function shareText(text, subject) {
const intent = getIntent('text/plain');
intent.putExtra(android.content.Intent.EXTRA_TEXT, text);
share(intent, subject);
}
export function sharePdf(pdf: File, subject?: string, caption?: string) {
const intent = getIntent('application/pdf');
const fileName = pdf.name;
const newFile = new java.io.File((<android.content.Context>Utils.android.getApplicationContext()).getExternalFilesDir(null), fileName);
const bytes = pdf.readSync();
const fos = new java.io.FileOutputStream(newFile);
fos.write(bytes);
fos.flush();
fos.close();
let shareableFileUri;
const sdkVersionInt = parseInt(Device.sdkVersion);
if (sdkVersionInt >= 21) {
shareableFileUri = FileProviderPackageName.FileProvider.getUriForFile(<android.content.Context>Utils.android.getApplicationContext(), Application.android.nativeApp.getPackageName() + '.provider', newFile);
} else {
shareableFileUri = android.net.Uri.fromFile(newFile);
}
intent.putExtra(android.content.Intent.EXTRA_STREAM, shareableFileUri);
if (typeof caption === 'string') {
intent.putExtra(android.content.Intent.EXTRA_TEXT, caption);
}
share(intent, subject);
}
export function shareUrl(url, text, subject) {
const intent = getIntent('text/plain');
intent.putExtra(android.content.Intent.EXTRA_TEXT, url);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, text);
share(intent, subject);
}
export function shareViaTwitter(text?: string, url?: string): Promise<void> {
return new Promise((resolve, reject) => {
const activity = Application.android.foregroundActivity || Application.android.startActivity;
try {
// Check if the Twitter app is installed on the phone.
if (!activity) {
reject('No activity found.');
} else {
activity.getPackageManager().getPackageInfo('com.twitter.android', 0);
const intent = new android.content.Intent(android.content.Intent.ACTION_SEND);
intent.setClassName('com.twitter.android', 'com.twitter.android.composer.ComposerActivity');
intent.setType('text/plain');
let value = `${text || ''}`;
if (url) {
value = `${value} ${url}`;
}
intent.putExtra(android.content.Intent.EXTRA_TEXT, java.net.URLEncoder.encode(value, 'UTF-8'));
activity.startActivity(intent);
resolve();
}
} catch (ex) {
// App not found fallback to browser ?
if (!activity) {
reject('No activity found.');
} else {
try {
const browserIntent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(`https://twitter.com/intent/tweet?text=${text || ''}&url=${url || ''}`));
activity.startActivity(browserIntent);
resolve();
} catch (e) {
reject(e);
}
}
}
});
}
export function shareViaFacebook(text?: string, url?: string): Promise<void> {
return new Promise((resolve, reject) => {
try {
const activity = Application.android.foregroundActivity || Application.android.startActivity;
if (!activity) {
reject('No activity found.');
} else {
if (typeof (<any>com).facebook === 'undefined' || typeof (<any>com).facebook.CallbackManager === 'undefined' || typeof (<any>com).facebook.share === 'undefined') {
console.error('Please follow usage instructions to add facebook sdk.');
reject();
return;
}
const manager = (<any>com).facebook.CallbackManager.Factory.create();
Application.android.off('activityResult');
Application.android.on('activityResult', (args) => {
manager.onActivityResult(args.requestCode, args.resultCode, args.intent);
});
const callback = new (<any>com).facebook.FacebookCallback({
onSuccess(value) {
manager.unregisterCallback(callback);
resolve();
},
onError(error) {
manager.unregisterCallback(callback);
reject(error.getMessage());
},
onCancel() {
manager.unregisterCallback(callback);
reject('User cancelled');
},
});
const dialog = new (<any>com).facebook.share.widget.ShareDialog(activity);
dialog.registerCallback(manager, callback);
const content = new (<any>com).facebook.share.model.ShareLinkContent.Builder();
if (url) {
content.setContentUrl(android.net.Uri.parse(url));
}
dialog.show(content.build());
}
} catch (e) {
reject(e);
}
});
}