Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[screen-capture] Add is available async for availability check #12121

Merged
merged 3 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/pages/versions/unversioned/sdk/screen-capture.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ import * as ScreenCapture from 'expo-screen-capture';

## Methods

### `isAvailableAsync()`

Returns whether the Screen Capture API is available on the current device.

#### Returns

Async `boolean`, indicating whether the Screen Capture API is available on the current device. Currently this resolves `true` on iOS and Android only.

### `usePreventScreenCapture(key)`

A React hook to prevent screen capturing for as long as the owner component is mounted.
Expand Down
1 change: 1 addition & 0 deletions packages/expo-screen-capture/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### 🎉 New features

- Updated Android build configuration to target Android 11 (added support for Android SDK 30). ([#11647](https://github.com/expo/expo/pull/11647) by [@bbarthec](https://github.com/bbarthec))
- Added `isAvailableAsync` method. ([#12121](https://github.com/expo/expo/pull/9675) by [@bycedric](https://github.com/bycedric))

### 🐛 Bug fixes

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/expo-screen-capture/build/ExpoScreenCapture.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/expo-screen-capture/build/ScreenCapture.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/expo-screen-capture/build/ScreenCapture.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-screen-capture/build/ScreenCapture.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions packages/expo-screen-capture/src/ExpoScreenCapture.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,4 @@ export default {
get name(): string {
return 'ExpoScreenCapture';
},
async preventScreenCaptureAsync(tag = 'default'): Promise<null> {
return null;
},
async allowScreenCaptureAsync(tag = 'default'): Promise<null> {
return null;
},
async usePreventScreenCapture(tag = 'default'): Promise<null> {
return null;
},
};
19 changes: 18 additions & 1 deletion packages/expo-screen-capture/src/ScreenCapture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter, Subscription } from '@unimodules/core';
import { EventEmitter, Subscription, UnavailabilityError } from '@unimodules/core';
import { useEffect } from 'react';

import ExpoScreenCapture from './ExpoScreenCapture';
Expand All @@ -8,6 +8,15 @@ const emitter = new EventEmitter(ExpoScreenCapture);

const onScreenshotEventName = 'onScreenshot';

/**
* Returns whether the Screen Capture API is available on the current device.
*
* @returns Async `boolean`, indicating whether the Screen Capture API is available on the current device. Currently this resolves `true` on iOS and Android only.
*/
export async function isAvailableAsync(): Promise<boolean> {
return !!ExpoScreenCapture.preventScreenCapture && !!ExpoScreenCapture.allowScreenCapture;
}

/**
* Prevents screenshots and screen recordings. If you are
* already preventing screen capture, this method does nothing.
Expand All @@ -28,6 +37,10 @@ const onScreenshotEventName = 'onScreenshot';
* ```
*/
export async function preventScreenCaptureAsync(key: string = 'default'): Promise<void> {
if (!ExpoScreenCapture.preventScreenCapture) {
throw new UnavailabilityError('ScreenCapture', 'preventScreenCaptureAsync');
}

if (!activeTags.has(key)) {
activeTags.add(key);
await ExpoScreenCapture.preventScreenCapture();
Expand All @@ -50,6 +63,10 @@ export async function preventScreenCaptureAsync(key: string = 'default'): Promis
* ```
*/
export async function allowScreenCaptureAsync(key: string = 'default'): Promise<void> {
if (!ExpoScreenCapture.preventScreenCapture) {
throw new UnavailabilityError('ScreenCapture', 'allowScreenCaptureAsync');
}

activeTags.delete(key);
if (activeTags.size === 0) {
await ExpoScreenCapture.allowScreenCapture();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import ExpoScreenCapture from '../ExpoScreenCapture';
import * as ScreenCapture from '../ScreenCapture';

describe('ScreenCapture methods are defined', () => {
it('isAvailableAsync is defined', async () => {
expect(ScreenCapture.isAvailableAsync).toBeDefined();
});

it('preventScreenCapture is defined', async () => {
expect(ScreenCapture.preventScreenCaptureAsync).toBeDefined();
});
Expand All @@ -28,6 +32,10 @@ describe('Test key functionality', () => {
await cleanUpTags();
});

it('resolves true for isAvailableAsync on native platforms', async () => {
await expect(ScreenCapture.isAvailableAsync()).resolves.toBeTruthy();
});

it('Will not call the native method if default key already active', async () => {
await ScreenCapture.preventScreenCaptureAsync();
await ScreenCapture.preventScreenCaptureAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as ScreenCapture from '../ScreenCapture';

describe('ScreenCapture methods are defined', () => {
it('isAvailableAsync is defined', async () => {
expect(ScreenCapture.isAvailableAsync).toBeDefined();
});

it('preventScreenCapture is defined', async () => {
expect(ScreenCapture.preventScreenCaptureAsync).toBeDefined();
});

it('allowScreenCapture is defined', async () => {
expect(ScreenCapture.allowScreenCaptureAsync).toBeDefined();
});

it('usePreventScreenCapture hook is defined', async () => {
expect(ScreenCapture.usePreventScreenCapture).toBeDefined();
});

it('addScreenshotListener is defined', async () => {
expect(ScreenCapture.addScreenshotListener).toBeDefined();
});

it('removeScreenshotListener is defined', async () => {
expect(ScreenCapture.removeScreenshotListener).toBeDefined();
});
});

describe('Test key functionality', () => {
it('resolves false for isAvailableAsync on web platform', async () => {
await expect(ScreenCapture.isAvailableAsync()).resolves.toBeFalsy();
});

it('throws for preventScreenCapture on web platform', async () => {
await expect(ScreenCapture.preventScreenCaptureAsync).rejects.toThrowError('not available');
});

it('throws for allowScreenCapture on web platform', async () => {
await expect(ScreenCapture.allowScreenCaptureAsync).rejects.toThrowError('not available');
});
});