-
Notifications
You must be signed in to change notification settings - Fork 33
/
open-with.service.ts
166 lines (155 loc) · 7.07 KB
/
open-with.service.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
import { Injectable, NgZone } from "@angular/core";
import { Router } from "@angular/router";
import { MatDialog } from "@angular/material";
import { WebIntent, Intent } from "@ionic-native/web-intent/ngx";
import { RunningContextService } from "./running-context.service";
import { FileService } from "./file.service";
import { ToastService } from "./toast.service";
import { ResourcesService } from "./resources.service";
import { LoggingService } from "./logging.service";
import { ImageResizeService } from "./image-resize.service";
import { RouteStrings } from "./hash.service";
declare var universalLinks: any;
@Injectable()
export class OpenWithService {
constructor(private readonly resources: ResourcesService,
private readonly runningContextService: RunningContextService,
private readonly fileService: FileService,
private readonly toastService: ToastService,
private readonly matDialog: MatDialog,
private readonly router: Router,
private readonly loggingService: LoggingService,
private readonly webIntent: WebIntent,
private readonly ngZone: NgZone) { }
public initialize() {
if (!this.runningContextService.isCordova) {
return;
}
this.loggingService.info("[OpenWith] subscribing to universal link events");
universalLinks.subscribe("share", (event) => {
this.loggingService.info("[OpenWith] Opening a share: " + event.path);
if (this.matDialog.openDialogs.length > 0) {
this.matDialog.closeAll();
}
let shareId = event.path.replace("/share/", "");
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_SHARE, shareId]);
});
});
universalLinks.subscribe("poi", (event) => {
this.loggingService.info("[OpenWith] Opening a poi: " + event.path);
if (this.matDialog.openDialogs.length > 0) {
this.matDialog.closeAll();
}
let sourceAndId = event.path.replace("/poi/", "");
let source = sourceAndId.split("/")[0];
let id = sourceAndId.split("/")[1];
let language = event.params.language;
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_POI, source, id],
{ queryParams: { language } });
});
});
universalLinks.subscribe("url", (event) => {
this.loggingService.info("[OpenWith] Opening a file url: " + event.path);
if (this.matDialog.openDialogs.length > 0) {
this.matDialog.closeAll();
}
let url = event.path.replace("/url/", "");
let baseLayer = event.params.baselayer;
this.ngZone.run(() => {
this.router.navigate([RouteStrings.ROUTE_URL, url],
{ queryParams: { baseLayer } });
});
});
universalLinks.subscribe(null, (event) => {
this.loggingService.info("[OpenWith] Opening the null: " + event.path);
if (this.matDialog.openDialogs.length > 0) {
this.matDialog.closeAll();
}
this.ngZone.run(() => {
this.router.navigate(["/"]);
});
});
if ((window as any).externalUrl) {
this.handleUrl((window as any).externalUrl);
delete (window as any).externalUrl;
}
(window as any).handleExternalUrl = (url: string) => this.handleUrl(url);
this.webIntent.getIntent().then(intent => this.handleIntent(intent));
this.webIntent.onIntent().subscribe((intent) => {
this.handleIntent(intent);
});
}
private handleUrl(url: string) {
this.loggingService.info("[OpenWith] Opening a file shared with the app " + url);
setTimeout(async () => {
try {
let file = await this.fileService.getFileFromUrl(url);
this.fileService.addRoutesFromFile(file);
} catch (ex) {
this.toastService.error(ex, this.resources.unableToLoadFromFile);
}
}, 0);
}
private handleIntent(intent: Intent) {
this.ngZone.run(async () => {
try {
let data = (intent as any).data as string;
if (!data && !intent.clipItems) {
if (!intent.action.endsWith("MAIN")) {
this.loggingService.warning("[OpenWith] Could not extract data from intent: " + JSON.stringify(intent));
}
return;
}
if (intent.clipItems && intent.clipItems.length > 0) {
data = intent.clipItems[0].uri;
}
if (data.startsWith("http") || data.startsWith("geo")) {
this.handleExternalUrl(data);
} else {
this.loggingService.info("[OpenWith] Opening an intent with data: " + data + " type: " + intent.type);
if (intent.type && intent.type.startsWith("image/")) {
// this is hacking, but there's no good way to get the real file name when an image is shared...
intent.type = ImageResizeService.JPEG;
}
let file = await this.fileService.getFileFromUrl(data, intent.type);
this.loggingService.info("[OpenWith] Translated the data to a file: " + file.name + " " + file.type);
await this.fileService.addRoutesFromFile(file);
}
} catch (ex) {
this.toastService.error(ex, this.resources.unableToLoadFromFile);
}
});
}
private handleExternalUrl(uri: string) {
if (uri.toLocaleLowerCase().indexOf("israelhiking.osm.org.il") !== -1) {
// handled by deep links plugin
return;
}
this.loggingService.info("[OpenWith] Opening a url: " + uri);
if (uri.indexOf("maps?q=") !== -1) {
let coordsRegExp = /q=(\d+\.\d+),(\d+\.\d+)&z=/;
let coords = coordsRegExp.exec(decodeURIComponent(uri));
this.moveToCoordinates(coords);
return;
}
if (uri.indexOf("maps/place") !== -1) {
let coordsRegExp = /\@(\d+\.\d+),(\d+\.\d+),/;
let coords = coordsRegExp.exec(uri);
this.moveToCoordinates(coords);
return;
}
if (uri.indexOf("geo:") !== -1) {
let coordsRegExp = /:(-?\d+\.\d+),(-?\d+\.\d+)/;
let coords = coordsRegExp.exec(uri);
this.moveToCoordinates(coords);
return;
}
this.router.navigate([RouteStrings.ROUTE_URL, uri]);
}
private moveToCoordinates(coords: string[]) {
this.router.navigate([RouteStrings.ROUTE_POI, "Coordinates", `${(+coords[1]).toFixed(4)}_${(+coords[2]).toFixed(4)}`],
{ queryParams: { language: this.resources.getCurrentLanguageCodeSimplified() } });
}
}