# Intercom & Recording Permissions

During IPC panel development, features like intercom, recording, and screenshot require explicit user permissions:

- Intercom: Microphone permission (`scope.record`)
- Recording/Screenshot: Storage/Photos permission (`scope.writePhotosAlbum`)

## Background

- When invoking permission-required features for the first time on iOS or Android, the system will show a permission popup where users can allow or deny access.
- ⚠️ If the user denies permission, the system will not show the popup again automatically, and existing APIs cannot trigger it. Calling permission-related APIs will fail, so developers must guide users to manually enable permissions in system settings.

## Recommended Solution

### 1. Intercom Feature

**Enable Intercom**

1. Check the microphone permission status (`ty.authorizeStatus`)
2. If it returns `fail` → permission has never been requested → call `ty.authorize` to request permission
3. If `ty.authorize` also returns `fail` → user previously denied → show a custom modal guiding the user to system settings
4. If success → permission granted → call intercom directly

**Example Code**

```javascript
import { showModal, openAppSystemSettingPage } from '@ray-js/ray';

// Custom modal to guide users to system settings
const showCustomDialog = () => {
  showModal({
    title: '',
    content: 'You currently do not have access to the microphone. Please enable it in Settings.',
    cancelText: 'Cancel',
    confirmText: 'Go to Settings',
    isShowGlobal: true,
    success: res => {
      if (res.confirm) {
        openAppSystemSettingPage({
          scope: 'App-Settings',
          success: () => {
            console.log('Navigation successful');
          },
        });
      }
    },
  });
};

const startTalkTest = () => {
  // 1. Check microphone permission
  ty.authorizeStatus({
    scope: 'scope.record',
    success: () => {
      // Permission granted, call intercom directly
    },
    fail: () => {
      // 2. Request microphone permission
      ty.authorize({
        scope: 'scope.record',
        success: () => {
          // Permission granted, call intercom directly
        },
        fail: () => {
          // 3. User previously denied, show custom modal
          showCustomDialog();
        },
      });
    },
  });
};
```

### 2. Recording & Screenshot Feature

**Enable Recording or Screenshot**

1. Check storage/photos permission status (`ty.authorizeStatus`)
2. If it returns (`fail`) → permission has never been requested → call (`ty.authorize`)
3. If (`ty.authorize`) returns (`fail`) → user previously denied → guide the user to manually enable it in system settings
4. If it returns (`success`) → permission granted → recording or screenshot can be used directly

> 💡 **Note:** After starting recording, wait at least 3 seconds before stopping; otherwise, errors may likely occur

**Example Code**

```javascript
import { showModal, openAppSystemSettingPage } from '@ray-js/ray';

// Custom modal to guide users to system settings
const showCustomDialog = () => {
  showModal({
    title: '',
    content: 'You currently do not have access to storage/photos. Please enable it in Settings.',
    cancelText: 'Cancel',
    confirmText: 'Go to Settings',
    isShowGlobal: true,
    success: res => {
      if (res.confirm) {
        openAppSystemSettingPage({
          scope: 'App-Settings',
          success: () => {
            console.log('Navigation successful');
          },
        });
      }
    },
  });
};

const startRecordTest = () => {
  // 1. Check storage/photos permission
  ty.authorizeStatus({
    scope: 'scope.writePhotosAlbum',
    success: () => {
      // Permission granted, use recording or screenshot features
      console.log('Permission granted');
    },
    fail: () => {
      // 2. Request storage/photos permission
      ty.authorize({
        scope: 'scope.writePhotosAlbum',
        success: () => {
          // Permission granted, use recording or screenshot features
          console.log('Permission granted');
        },
        fail: () => {
          // 3. User previously denied, show custom modal
          showCustomDialog();
        },
      });
    },
  });
};
```
