Skip to content

Commit bd9828a

Browse files
tarunamavtrifonov
andauthored
feat(utils): work openFile in simulator (#8495)
* feat: open file in iOS simulator * feat: added isRealDevice util * feat: have in mind isRealDevice in utils.openFile refactor to avoid some circular dependencies Co-authored-by: Vasil Trifonov <v.trifonov@gmail.com>
1 parent 50eb372 commit bd9828a

10 files changed

Lines changed: 87 additions & 43 deletions

File tree

api-reports/NativeScript.api.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ export class Span extends ViewBase {
19471947

19481948
public textDecoration: TextDecoration;
19491949
//@endprivate
1950-
}
1950+
}
19511951

19521952
// @public
19531953
export class StackLayout extends LayoutBase {
@@ -2428,8 +2428,7 @@ export interface TapGestureEventData extends GestureEventData {
24282428
getPointerCount(): number;
24292429
getX(): number;
24302430
getY(): number;
2431-
2432-
}
2431+
}
24332432

24342433
// @public
24352434
export interface Template {
@@ -2623,6 +2622,7 @@ export const Utils: {
26232622
getModuleName: typeof getModuleName;
26242623
openFile: typeof openFile;
26252624
openUrl: typeof openUrl;
2625+
isRealDevice: typeof isRealDevice;
26262626
layout: typeof layout;
26272627
android: typeof ad;
26282628
ios: typeof ios;
@@ -3116,9 +3116,10 @@ export class XmlParser {
31163116
// nativescript-core/index.d.ts:114:5 - (ae-forgotten-export) The symbol "getModuleName" needs to be exported by the entry point index.d.ts
31173117
// nativescript-core/index.d.ts:115:5 - (ae-forgotten-export) The symbol "openFile" needs to be exported by the entry point index.d.ts
31183118
// nativescript-core/index.d.ts:116:5 - (ae-forgotten-export) The symbol "openUrl" needs to be exported by the entry point index.d.ts
3119-
// nativescript-core/index.d.ts:117:5 - (ae-forgotten-export) The symbol "layout" needs to be exported by the entry point index.d.ts
3120-
// nativescript-core/index.d.ts:118:5 - (ae-forgotten-export) The symbol "ad" needs to be exported by the entry point index.d.ts
3121-
// nativescript-core/index.d.ts:119:5 - (ae-forgotten-export) The symbol "ios" needs to be exported by the entry point index.d.ts
3119+
// nativescript-core/index.d.ts:117:5 - (ae-forgotten-export) The symbol "isRealDevice" needs to be exported by the entry point index.d.ts
3120+
// nativescript-core/index.d.ts:118:5 - (ae-forgotten-export) The symbol "layout" needs to be exported by the entry point index.d.ts
3121+
// nativescript-core/index.d.ts:119:5 - (ae-forgotten-export) The symbol "ad" needs to be exported by the entry point index.d.ts
3122+
// nativescript-core/index.d.ts:120:5 - (ae-forgotten-export) The symbol "ios" needs to be exported by the entry point index.d.ts
31223123
// nativescript-core/ui/core/view-base/view-base.d.ts:179:26 - (ae-forgotten-export) The symbol "Property" needs to be exported by the entry point index.d.ts
31233124
// nativescript-core/ui/core/view-base/view-base.d.ts:179:26 - (ae-forgotten-export) The symbol "CssProperty" needs to be exported by the entry point index.d.ts
31243125
// nativescript-core/ui/core/view-base/view-base.d.ts:179:26 - (ae-forgotten-export) The symbol "CssAnimationProperty" needs to be exported by the entry point index.d.ts

nativescript-core/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export declare const Trace: {
100100
isEnabled: typeof isEnabled;
101101
};
102102
export * from "./ui";
103-
import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, releaseNativeObject, getModuleName, openFile, openUrl, layout, ad as androidUtils, ios as iosUtils } from "./utils/utils";
103+
import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, releaseNativeObject, getModuleName, openFile, openUrl, isRealDevice, layout, ad as androidUtils, ios as iosUtils } from "./utils/utils";
104104
export declare const Utils: {
105105
GC: typeof GC;
106106
isFontIconURI: typeof isFontIconURI;
@@ -114,6 +114,7 @@ export declare const Utils: {
114114
getModuleName: typeof getModuleName;
115115
openFile: typeof openFile;
116116
openUrl: typeof openUrl;
117+
isRealDevice: typeof isRealDevice;
117118
layout: typeof layout;
118119
android: typeof androidUtils;
119120
ios: typeof iosUtils;

nativescript-core/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ import {
9898
GC, isFontIconURI, isDataURI, isFileOrResourcePath,
9999
executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, releaseNativeObject,
100100
getModuleName,
101-
openFile, openUrl,
101+
openFile, openUrl, isRealDevice,
102102
layout, ad as androidUtils, ios as iosUtils
103103
} from "./utils/utils";
104104

@@ -107,7 +107,7 @@ export const Utils = {
107107
executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, releaseNativeObject,
108108

109109
getModuleName,
110-
openFile, openUrl,
110+
openFile, openUrl, isRealDevice,
111111

112112
layout, android: androidUtils, ios: iosUtils
113113
};

nativescript-core/ui/gestures/gestures.d.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
touch
4444
}
45-
45+
4646
/**
4747
* Defines an enum with supported gesture states.
4848
*/
@@ -64,7 +64,7 @@
6464
*/
6565
ended
6666
}
67-
67+
6868
/**
6969
* Defines an enum for swipe gesture direction.
7070
*/
@@ -86,7 +86,7 @@
8686
*/
8787
down
8888
}
89-
89+
9090
/**
9191
* Defines a touch action
9292
*/
@@ -95,23 +95,23 @@
9595
* Down action.
9696
*/
9797
export const down: string;
98-
98+
9999
/**
100100
* Up action.
101101
*/
102102
export const up: string;
103-
103+
104104
/**
105105
* Move action.
106106
*/
107107
export const move: string;
108-
108+
109109
/**
110110
* Cancel action.
111111
*/
112112
export const cancel: string;
113113
}
114-
114+
115115
/**
116116
* Provides gesture event data.
117117
*/
@@ -137,7 +137,7 @@
137137
/**
138138
* Provides gesture event data.
139139
*/
140-
export interface TapGestureEventData extends GestureEventData {
140+
export interface TapGestureEventData extends GestureEventData {
141141
/**
142142
* Gets the number of pointers in the event.
143143
*/
@@ -152,27 +152,27 @@
152152
getY(): number;
153153

154154
}
155-
155+
156156
/**
157157
* Provides gesture event data.
158158
*/
159159
export interface TouchGestureEventData extends TapGestureEventData {
160160
/**
161161
* Gets action of the touch. Possible values: 'up', 'move', 'down', 'cancel'
162162
*/
163-
action: "up" | "move" | "down" | "cancel";
163+
action: "up" | "move" | "down" | "cancel";
164164
/**
165165
* Gets the pointers that triggered the event.
166166
* Note: In Android there is aways only one active pointer.
167167
*/
168168
getActivePointers(): Array<Pointer>;
169-
169+
170170
/**
171171
* Gets all pointers.
172172
*/
173173
getAllPointers(): Array<Pointer>;
174174
}
175-
175+
176176
/**
177177
* Pointer is an object representing a finger (or other object) that is touching the screen.
178178
*/
@@ -181,12 +181,12 @@
181181
* The id of the pointer.
182182
*/
183183
android: any;
184-
184+
185185
/**
186186
* The UITouch object associated to the touch
187187
*/
188188
ios: any;
189-
189+
190190
/**
191191
* Gets the X coordinate of the pointer inside the view that triggered the event.
192192
*/
@@ -221,46 +221,46 @@
221221
*/
222222
getYDIP(): number
223223
}
224-
224+
225225
/**
226226
* Provides gesture event data.
227227
*/
228228
export interface GestureEventDataWithState extends GestureEventData {
229229
state: number;
230230
}
231-
231+
232232
/**
233233
* Provides gesture event data for pinch gesture.
234234
*/
235235
export interface PinchGestureEventData extends GestureEventDataWithState {
236236
scale: number;
237-
237+
238238
getFocusX(): number;
239239
getFocusY(): number;
240240
}
241-
241+
242242
/**
243243
* Provides gesture event data for swipe gesture.
244244
*/
245245
export interface SwipeGestureEventData extends GestureEventData {
246246
direction: SwipeDirection;
247247
}
248-
248+
249249
/**
250250
* Provides gesture event data for pan gesture.
251251
*/
252252
export interface PanGestureEventData extends GestureEventDataWithState {
253253
deltaX: number;
254254
deltaY: number;
255255
}
256-
256+
257257
/**
258258
* Provides gesture event data for rotation gesture.
259259
*/
260260
export interface RotationGestureEventData extends GestureEventDataWithState {
261261
rotation: number;
262262
}
263-
263+
264264
/**
265265
* Provides options for the GesturesObserver.
266266
*/
@@ -272,39 +272,39 @@
272272
* @param context - default this argument for the callbacks.
273273
*/
274274
constructor(target: View, callback: (args: GestureEventData) => void, context: any);
275-
275+
276276
/**
277277
* Registers a gesture observer to a view and gesture.
278278
* @param type - Type of the gesture.
279279
*/
280280
observe(type: GestureTypes);
281-
281+
282282
/**
283283
* Disconnects the gesture observer.
284284
*/
285285
disconnect();
286-
286+
287287
/**
288288
* Gesture type attached to the observer.
289289
*/
290290
type: GestureTypes;
291-
291+
292292
/**
293293
* A function that will be executed when a gesture is received.
294294
*/
295295
callback: (args: GestureEventData) => void;
296-
296+
297297
/**
298298
* A context which will be used as `this` in callback execution.
299299
*/
300300
context: any;
301-
301+
302302
/**
303303
* An internal Android specific method used to pass the motion event to the correct gesture observer.
304304
*/
305305
androidOnTouchEvent: (motionEvent: any /* android.view.MotionEvent */) => void;
306306
}
307-
307+
308308
/**
309309
* A short-hand function that is used to create a gesture observer for a view and gesture.
310310
* @param target - View which will be watched for originating a specific gesture.
@@ -313,17 +313,16 @@
313313
* @param context - this argument for the callback.
314314
*/
315315
export function observe(target: View, type: GestureTypes, callback: (args: GestureEventData) => void, context?: any): GesturesObserver;
316-
316+
317317
/**
318318
* Returns a string representation of a gesture type.
319319
* @param type - Type of the gesture.
320320
* @param separator(optional) - Text separator between gesture type strings.
321321
*/
322322
export function toString(type: GestureTypes, separator?: string): string;
323-
323+
324324
/**
325325
* Returns a gesture type enum value from a string (case insensitive).
326326
* @param type - A string representation of a gesture type (e.g. Tap).
327327
*/
328328
export function fromString(type: string): GestureTypes;
329-

nativescript-core/utils/native-helper.android.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,10 @@ export module ad {
153153
return result;
154154
}
155155
}
156+
157+
export function isRealDevice(): boolean {
158+
const fingerprint = android.os.Build.FINGERPRINT;
159+
160+
return fingerprint != null && (fingerprint.indexOf("vbox") > -1 || fingerprint.indexOf("generic") > -1);
161+
}
156162
}

nativescript-core/utils/native-helper.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export module ad {
8383
*/
8484
export function getPaletteColor(name: string, context: any /* android.content.Context */): number;
8585
}
86+
87+
/**
88+
* Checks whether the application is running on real device and not on emulator.
89+
*/
90+
export function isRealDevice(): boolean;
8691
}
8792
/**
8893
* Module with ios specific utilities.
@@ -156,7 +161,7 @@ export module ios {
156161
export function getVisibleViewController(rootViewController: any/* UIViewController*/): any/* UIViewController*/;
157162

158163
/**
159-
*
164+
*
160165
* @param transform Applies a rotation transform over X,Y and Z axis
161166
* @param x Rotation over X axis in degrees
162167
* @param y Rotation over Y axis in degrees
@@ -165,4 +170,9 @@ export module ios {
165170
export function applyRotateTransform(transform: any /* CATransform3D*/, x: number, y: number, z: number): any /* CATransform3D*/;
166171

167172
export class UIDocumentInteractionControllerDelegateImpl { }
173+
174+
/**
175+
* Checks whether the application is running on real device and not on simulator.
176+
*/
177+
export function isRealDevice(): boolean;
168178
}

nativescript-core/utils/native-helper.ios.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
write as traceWrite
55
} from "../trace";
66

7+
declare var UIImagePickerControllerSourceType: any;
8+
79
const radToDeg = Math.PI / 180;
810

911
function isOrientationLandscape(orientation: number) {
@@ -14,7 +16,7 @@ function isOrientationLandscape(orientation: number) {
1416
function openFileAtRootModule(filePath: string): boolean {
1517
try {
1618
const appPath = ios.getCurrentAppPath();
17-
const path = filePath.replace("~", appPath);
19+
let path = ios.isRealDevice() ? filePath.replace("~", appPath) : filePath;
1820

1921
const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path));
2022
controller.delegate = new ios.UIDocumentInteractionControllerDelegateImpl();
@@ -153,4 +155,16 @@ export module ios {
153155
return this.getViewController().view.frame;
154156
}
155157
}
158+
159+
export function isRealDevice() {
160+
try {
161+
// https://stackoverflow.com/a/5093092/4936697
162+
const sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
163+
const mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
164+
165+
return mediaTypes;
166+
} catch (e) {
167+
return true;
168+
}
169+
}
156170
}

nativescript-core/utils/utils.android.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,7 @@ Please ensure you have your manifest correctly configured with the FileProvider.
182182
return false;
183183
}
184184
}
185+
186+
export function isRealDevice(): boolean {
187+
return ad.isRealDevice();
188+
}

0 commit comments

Comments
 (0)